DevelopersHiQ / Language
HiQ Assembly
One statement per line: named qubits, routines, real-time branches and switches, and loops that wait on a measurement only with a declared budget.
- .qcos
- qcos/0
- One statement per line, braces
- Lowers to qcos-ir/2
Structure
The first statement is program <name>. One statement sits on each line. Comments start with //. Whitespace other than a newline is free. Declarations sit at the top level.
program bellqubits a, bbits m0, m1h acx a, bmeasure a -> m0measure b -> m1Declarations
| Form | Meaning |
|---|---|
qubits a, b | Named qubits |
qubits q[4] | An array of qubits, used as q[0] to q[3] |
bits m or bit m, bits m[4] | Classical bits |
uint8 x, y, int32 z | Integers of width 1 to 64 |
routine f(q, r; k: uint8) { ... } | A routine: qubit parameters, then ;, then typed classical parameters |
An array size is a whole number from 1 to 65,536. In routine, a classical parameter takes uintN, intN, bit or bool.
Statements
| Form | Meaning |
|---|---|
h a, rz(pi / 2) a, cx a, b | A gate |
measure a -> m | Measure a qubit into a bit |
reset a | Return a qubit to zero |
alloc t | Allocate a qubit named t at this point |
release t or release t clean | Release t, stating it is already zero. This is the default |
release t reset | Reset t, then release it |
x = y + 1, m[0] = true | Classical assignment |
repeat i in 0..4 { ... } | A loop known at compile time, stop excluded |
repeat i in 4..0 step -1 { ... } | The same, counting down |
while m budget 10 { ... } | A real-time loop with a required iteration budget |
if m { ... } else if n { ... } else { ... } | A real-time branch |
match x { 0, 1 => { ... } _ => { ... } } | A real-time switch. _ is the default |
within { ... } do { ... } | Run the first block, the second, then the first inverted |
call f(a, b; 3) | Call a routine. Qubits, then ;, then classical arguments |
Loop bounds are whole numbers, or a variable of an enclosing loop that has been unrolled.
Angles and indices
An angle is a constant expression of numbers, pi, +, -, *, /, parentheses and loop variables. A loop whose variable appears in an angle or a bit index unrolls into copies of its body.
An index is a whole number or a loop variable: q[2] or q[i]. An index expression such as q[i + 1] is refused in Assembly with lang.index_expression. Script and Objects allow it.
Classical expressions
Operators, loosest first: |, ^, &, then == !=, then < <= > >=, then << >>, then + -, then *. Unary: ! and -. Atoms: whole numbers, true, false, a variable, m[0], and parentheses. There is no division in Assembly expressions. A condition is a bit or a comparison.
Dynamic qubits
alloc and release give a qubit a lifetime. The checker plans memory and reports peak_qubits beside declared_qubits, so an ancilla used in turn costs one qubit. Ancillas in sequence reuse the same physical slot:
program ancilla_reusequbits databits mrepeat i in 0..3 { alloc t h t cz t, data h t measure t -> m release t reset}A qubit cannot be used after measurement until it is reset (qubit.use_after_measure), and one released clean must already be zero (qubit.release_dirty).
Idioms
Feedback on a measurement:
program feedbackqubits a, bbits m, nh ameasure a -> mif m { x b}measure b -> nA routine, with parameterised rotations from a loop:
program ladderqubits q[3]bits mroutine pair(left, right) { h left cx left, right}call pair(q[0], q[1])repeat i in 0..3 { rz(pi * i / 4) q[i]}reset q[0]measure q[2] -> mRepeat until success:
program until_onequbits abits mh ameasure a -> mreset awhile !m budget 16 { h a measure a -> m reset a}Compute and uncompute:
program compute_uncomputequbits a, bbits mwithin { h a} do { cz a, b}measure a -> mRefused, with the code
program pbits mwhile m {}program pqubits afoo aprogram pqubits q[2]repeat i in 0..1 { cx q[i], q[i + 1]}Examples in this layer
- Bell pairTwo qubits whose measurements always agree.examples/l1/bell.qcos
- GHZ stateFour qubits that read 0000 or 1111.examples/l1/ghz.qcos
- TeleportationMove a state with two measurements and real-time corrections.examples/l1/teleport.qcos
- Superdense codingTwo classical bits carried by one qubit of an entangled pair.examples/l1/superdense.qcos
- Deutsch-JozsaConstant or balanced, in one query.examples/l1/deutsch_jozsa.qcos
- Bernstein-VaziraniRead a hidden string in one query.examples/l1/bernstein_vazirani.qcos
- Grover searchFind the marked state 11 in one iteration.examples/l1/grover.qcos
- Phase estimationRead the phase of an eigenstate into a register.examples/l1/phase_estimation.qcos
- Repeat until successTry again until a measurement reads 1, within a budget of 16.examples/l1/repeat_until_success.qcos
- Fourier transformThe quantum Fourier transform on three qubits, applied to 101.examples/l1/qft.qcos
- Parity with one ancillaEach check allocates, measures and releases one reused qubit.examples/l1/parity_ancilla.qcos