Skip to content

1. 🗺️ Language Features and Syntax

Gabriele Messina edited this page Aug 19, 2024 · 2 revisions

Variable Declaration and Assignment

bool a = true;
int b = 10;
string c = "Hello";
qubit d = 1q;
quint e = 10q;

Variable Scope

bool b = false;
{
    int b = 100; //this int b overrides the upper bool b.  
}
b = true; //this b refers to the bool b.

Printing

int a = 10;
quint b = 10q;
print b;

Branching

bool b = true;
if(b){
    //Do something.
}

if(!b){
    //Do something.
}
else{
    //Do something else.
}

if(b){
    //Do something.
}
else if(true){
    //Do something else.
}

Looping

bool b = true;
while(b){
    //Do something while b == true.
    b = false;
}

do{
    //Do something until b == false.
    b = true;
}
while(!b)

Qubit declaration syntax

//alpha and beta values
qubit a = 0.7,0.3q;
//states we want the qubit to be able to take
qubit b = [0, 1]q;
//shorter syntax for a qubit that takes only the zero value
qubit c = 0q;
//special syntax for common superposition
qubit d = |+>;
qubit e = |->;

Quint declaration syntax

//quint with 2^4 qubits that represent the number 10.
quint a = 10q;
//quint representing the number 1
quint b = [0q, 0q, 1q]q; 
//quint with a superposition state where the most significant bit can be 0(70%) or 1(30%)
quint c = [0.7,0.3q, 0q, 1q]q;
//quint with a superposition state where the most significant bit can be 0(50%) or 1(50%) 
quint d = [[0, 1]q, 0q, 1q]q;
//quint with a superposition state that can be both 1(50%) or 3(50%)
quint e = [1, 3]q; 

Qustring declaration syntax

//quint representing the string "111010010"
qustring a = "111010010";
//quint representing the strings "111010010" and "111010011"
qustring a = "11101001*";

> Right now, qustring handles only a 2-character alphabet of '0' and '1'. This is needed for the circuit to run on the Qiskit simulator; otherwise, it will need too many qubits.

Quantum basic operations

qubit a = 1q;
qubit b = 1q;
not a;
pauliz a;
hadamard a;
quint c = a + b;

Quantum circuit measurement

//Explicit measure
qubit a = [1]q;
qubit b = [1]q;
quint x = a + b;
measure x;

//Implicit measure
qubit a = [true]q;
qubit b = [true]q;
int classical = a + b;

Functions

quint sum(quint a, quint b){
    return a+b;
}
quint z = 1q;
quint y = 3q;
quint c = sum(z, y);
print c;

Arrays

string[] foo = ["1","0","1","0","1","0","1","0"];
qubit[] fas = [0q, 1q, |+>];
print foo[1];
print fas;

Search element in Array Type

qustring a = "00111111";
if("01" in a){
    print "Element found.";
}
else{
    print "Element not found.";
}

Cycle through Array Elements

qubit b = 1q;
qubit c = 0q;
qubit[] a = [b, c, |+>];
foreach qubit q in a{
    print q;
}
//or
foreach qubit q, int index in a{
    print a[index];
}