These code samples were written by Andres Paz and Mariia Mykhailova.
// Example 2-1: Random bit
operation RandomBit () : Unit {
// allocate one qubit
using (q = Qubit()) {
// put it into superposition of 0 and 1
H(q);
// measure the qubit and store the result
let bit = M(q);
// make sure the qubit is back to the 0 state
Reset(q);
Message($"{bit}");
}
}
%simulate RandomBit
// Example 2-2: Random byte
// open namespace which defines arithmetic operations
open Microsoft.Quantum.Arithmetic;
operation RandomByte () : Unit {
// allocate 8 qubits
using (qs = Qubit[8]) {
// put each qubit into superposition of 0 and 1
ApplyToEach(H, qs);
// measure the register and store the result
let randomByte = MeasureInteger(LittleEndian(qs));
// make sure the qubits are back to the 0 state
ResetAll(qs);
Message($"{randomByte}");
}
}
%simulate RandomByte
// Example 2-3: Root-of-NOT
open Microsoft.Quantum.Diagnostics;
operation SqrtNOT (q : Qubit) : Unit {
H(q);
S(q);
H(q);
}
operation RunSqrtNOT () : Unit {
// allocate a qubit
using (q = Qubit()) {
// apply SqrtNOT gate to the |0⟩ state
SqrtNOT(q);
Message("Qubit state after the first application of SqrtNOT");
DumpMachine();
// apply SqrtNOT gate again and verify that the qubit ends up in the |1⟩ state
SqrtNOT(q);
Message("Qubit state after the second application of SqrtNOT");
DumpMachine();
// make sure the qubit is back to the 0 state
Reset(q);
}
}
%simulate RunSqrtNOT