DevelopersHiQ / Language
HiQ Script
Constants, inclusive for loops, repeat until, select, initialised declarations and parallel tasks that are checked so every interleaving gives the same result.
- .qcos2
- qcos-l2/0
- Imperative; end and next close blocks
- Lowers to HiQ Assembly
Script adds constants, constant expressions in indices and angles, inclusive for, repeat ... until, initialised declarations and parallel. Blocks close with words, not braces. Comments start with #.
Declarations
| Form | Meaning |
|---|---|
const N = 3 | A compile-time constant, usable in bounds, indices and angles |
qubit a, b, qubit q[N] | Qubits |
bit m, bit m[N], bool m | Bits |
uint8 c = 2, int32 z | Integers, with an optional initial value |
sub f(q; k: uint8) ... end | A routine |
Qubits and arrays take no initial value (lang.init).
Statements
| Form | Meaning |
|---|---|
h a, rz(pi / N) q[i], cx q[i], q[i + 1] | A gate |
measure a into m | Measure |
reset a | Reset |
alloc t | Allocate |
free t or free t reset | Release. clean is the default |
let x = y + 1 | Assignment |
for i = 0 to N - 1 step 1 ... next | A loop known at compile time, stop included. step is optional and may be negative |
while m limit 10 ... end | A real-time loop |
repeat ... until m limit 3 | Run the body, then repeat until the condition holds, at most the limit in total |
if m then ... elseif n then ... else ... end | A branch |
select x / case 1, 2: ... / default: ... / end | A switch |
compute ... do ... end | The form of Assembly's within |
call f(a; 1) | A call |
parallel / task one ... end / end | Tasks, see below |
Script expressions also take and, or, not, mod and <> for not-equal. Division is allowed only between constants, in constant expressions. A division of whole numbers that leaves no remainder is a whole number, so const HALF = N / 2 can bound a loop; 3 / 2 is 1.5 and can only be part of an angle.
parallel
Tasks are checked statically. They may not share a qubit (task.shares_qubit), and a variable one task writes may not be touched by another (task.shares_variable). Every interleaving then gives the same result, so tasks lower in sequence, and disjoint operations overlap in the reported depth. There is no concurrent execution on control hardware.
Idioms
program ghzconst N = 3qubit q[N]bit m[N]h q[0]for i = 0 to N - 2 cx q[i], q[i + 1]nextfor i = 0 to N - 1 measure q[i] into m[i]nextprogram feedbackqubit a, bbit m, nuint8 c = 1h ameasure a into mif m and c == 1 then x belseif m then z belse h bendmeasure b into nprogram until_onequbit abit mrepeat h a measure a into m reset auntil m limit 3program choosequbit abit muint8 c = 2sub flip(q; k: uint8) x qendselect c case 1, 2: call flip(a; 1) default: h aendmeasure a into mprogram side_by_sidequbit a, bbit muint8 c = 0parallel task one x a end task two h b let c = 1 endendmeasure a into mRefused, with the code
program pqubit a, bparallel task x h a end task y cx a, b endendprogram pbit mwhile mendExamples in this layer
- Fourier transformThe quantum Fourier transform with constants and loops.examples/l2/qft.qcos2
- Repetition codeEncode, inject an error, read the syndrome and correct it.examples/l2/repetition_code.qcos2
- Parallel preparationTasks on disjoint qubits, checked so the order cannot matter.examples/l2/parallel_preparation.qcos2
- GHZ stateGHZ on N qubits. Change N and nothing else.examples/l2/ghz.qcos2
- Bernstein-VaziraniThe hidden string 1011 read in one query, with a loop over N bits.examples/l2/bernstein_vazirani.qcos2
- Grover searchThree qubits, marked state 111, two iterations.examples/l2/grover.qcos2
- Repeat until successRun the body, then try again until a measurement reads 1, at most eight times.examples/l2/repeat_until_success.qcos2