Skip to main content
Introducing The Quantum Infrastructure Cloud Learn more

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.

HiQ Assemblyqcos/0
program bellqubits a, bbits m0, m1​h acx a, bmeasure a -> m0measure b -> m1

Declarations

FormMeaning
qubits a, bNamed 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 zIntegers 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

FormMeaning
h a, rz(pi / 2) a, cx a, bA gate
measure a -> mMeasure a qubit into a bit
reset aReturn a qubit to zero
alloc tAllocate a qubit named t at this point
release t or release t cleanRelease t, stating it is already zero. This is the default
release t resetReset t, then release it
x = y + 1, m[0] = trueClassical 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:

HiQ Assemblyqcos/0
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:

HiQ Assemblyqcos/0
program feedbackqubits a, bbits m, nh ameasure a -> mif m {  x b}measure b -> n

A routine, with parameterised rotations from a loop:

HiQ Assemblyqcos/0
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] -> m

Repeat until success:

HiQ Assemblyqcos/0
program until_onequbits abits mh ameasure a -> mreset awhile !m budget 16 {  h a  measure a -> m  reset a}

Compute and uncompute:

HiQ Assemblyqcos/0
program compute_uncomputequbits a, bbits mwithin {  h a} do {  cz a, b}measure a -> m

Refused, with the code

Refused by the checkerloop.unbounded
program pbits mwhile m {}
Refused by the checkergate.unknown
program pqubits afoo a
Refused by the checkerlang.index_expression
program pqubits q[2]repeat i in 0..1 {  cx q[i], q[i + 1]}

Examples in this layer