Q# short syntax

Q# is a programming language used by Microsoft to perform quantum computing. This language is similar in many respects to the C# language of the same manufacturer, but it has operations that allow you to work on quantum objects. Below is a shorthand syntax for this language.

//helloWorld in Q# language

namespace HelloQuantumWorld { //namespace 
    open Microsoft.Quantum.Intrinsic; // open is just import statment
    operation HelloWorld() : Unit { // functions, operations executed by software
        Message("Hello quantum world!");
    }
}

Primitive types:

Int – A 64-bit signed integer value like 16,21,56484
BigInt – A signed integer of arbitrary size used as stated for big numbers
Double – A 64-bit floating-point numer like 25.598, 3156.864512
Bool – true, false value
String – character string like „I like cakes”
Range – A sequence of integers written in the form of the expression start..step..stop. In this expression, we specify the initial value, how much this value is to change and what is the final condition. Just like in the classic for loop.
1..1..100 (can be written as 1..100), 1..-5.0, 17..12..75 and so on.

let someRange = 1..2..50

Result – represents a measurement result. The values can be Zero or One
Unit – A type with a single possible value
Qubit – A unique variable representing a quantum of information

 

Data structures:

Array – Data arrays working like in most known programming languages. Their initialization might look like this:

let zeroArray = new Int[8]; // create array [0,0,0,0,0,0,0,0]
let oneArray = [1, size = 2]; // create array [1,1]
let arrayOfNumbers = [3, 4, 5, 6]; // create array [3,4,5,6]

Tuple – They allow you to store many different values in one variable.

let tupleValue = (12.13, false, „OK”);
let (first, second, third) = tupleValue; //first = 12.13, second = false, third = „OK”

UDT – User Defined Types – Like Tuple, it allows you to create a variable containing various values with anonymous or named parameters.

newtype NamedPair = (Name : String, Value : Int);
let accuracy= NamedPair(„Accuracy”,  82);
let name = accuracy::Name; // Accuracy
let acValue = accuracy:: Value; // 82

Variables:

Q# has two types of variables, muttable and unmutable. An unmutable variable does not allow its value to be changed and is basically a constant. Muttable, on the other hand, can behave just like a normal variable. We assign the unmutable value to a variable with the let keyword. To assign a muttable value to a variable we use the mutabble keyword. To reassign a value to the mutable variable later, it is necessary to use the set function.

let a = [1, 2, 3];
mutable b = 123;
set b = 259;

Allocating qubits:

Qubits are unique data types that represent quanta of information. The state of a qubit can only be changed using quantum gates. Q# does not create new qubits, but only allocates and releases resources available to it. Allocating and releasing qubits is possible by using the use keyword.

use oneQubit = Qubit(); // allocating single qubit
use (qubit1, qubit2) = (Qubit(), Qubit());   // allocating two qubits
use registryQubit = Qubit[n]; // allocating array of n qubits

Use allocates qubits always in the |0> state and after using them the user should return them to this state. You can also allocate qubits in an unknown state using the borrow keyword. This is due to the fact that such qubits can be used for other operations.

borrow oneQubit = Qubit();
borrow (qubit1, qubit2) = (Qubit(), Qubit());
borrow registryQubit = Qubit[n];

Operators:

Q# uses standard operators to compare values
equality: ==
inequality: !=
less than: < greater than: >
less or equal than: <= more or equal than: >=
 

Logical operators:

Q# does not use the standard logical operators && || ! and uses keywords:
and: first and second
or: first or second
not: not first
 

Comments:

Comments are created using // at the time of writing this statement, only one-line comments can be created.

//Comments

 

Functions and Operations:

The language distinguishes between two types of calls, functions and operations. The way to build function is as follows:

function sumFunction(first : Int, second : Int ) : Int {
    let sum = first + second;
    return sum;
}

function nameOfFunction(parameterName : parameterType, …) : returnType {
//code
}

Operations are built and work similarly to functions, but they allow you to perform non-deterministic operations on quantum elements.

    operation GetRandomResult() : Result {
        use qubit = Qubit();
        H(qubit);
        return M(qubit);
    }

In addition, operations can be marked as adjoin or controlled operations:
Adj – it supports the Adjoint functor and the applied quantum transformation can be undone
Ctl – it supports the Controlled functor and its execution can be conditioned on the state of other qubits
 

Control:

Control functions are similar to other languages and should not be a problem for people familiar with them.
if else:

If first == 5 {
    Return 2;
}
Elif first == 2{
    Return 1;
}
Else{
    Return 0;
}

for loop:

mutable power = 2;
for i in 1 .. 10 {
    set power *= power;
}

enchanced for:

let array = [1, 2, 3, 4]; 
for i in array {
    Message(i);
}

while:

while someValue > 0 {
    set someValue += 1;
}

Repete until, like the while loop, allows you to repeat iterations until a certain condition is met. This expression was created mainly to work on quantum events and can only be used in operations.

use qubit = Qubit();
repeat {
    H(qubit);
    let result = M(qubit);
} until result == Zero;

within-apply
Only possible to execute on operation. Performs single transformations on qubits. It consists of the within and apply keywords. In the within block, the first transformation is performed, in the apply block, the second transformation is performed. After which the reverse of the first transformation is performed automatically.

within {
    ApplyToEachA(X, qubitArray);
} apply {
    Controlled X(qubitArray, auxQubit);
}

 

Fail:

fail "Result should be greater than 0"; 
// exception handling most commonly found as throw in other programming languages

 
A useful summary of all this knowledge can be found in the Q# Languge Quick Reference Guide available at:

https://github.com/microsoft/QuantumKatas/blob/main/quickref/qsharp-quick-reference.pdf

Leave a Comment

WordPress Video Lightbox Plugin