Binary controlled qubit gates can be performed by adding c-
in front of the qubit gate and the use of an extra operand b[xxx] before the qubit operand, where xxx is a bit number or a range of bit numbers from the binary register.
All qubit gates, except the prep_z
, prep_y
, prep_x
and all measure
commands, can be controlled by the result of an earlier measurement. In the simplest form, the single qubit gate is executed only when the measurement result was 1. As an example, consider this simple cQASM program:
version 1.0
qubits 2
prep_z q[0:1]
H q[0]
measure_z q[0]
c-X b[0], q[1]
measure q[1]
q[0] | ||||||
---|---|---|---|---|---|---|
q[1] |
Line 5 prepares the qubits, followed by the creation of a superposition on qubit 0. This qubit is measured in line 7, which will result in a 0 or 1 (each with 50% probability) to be stored in the classical register at index 0. Line 9 shows the binary-controlled X gate.
Only when the measurement of the qubit at index 0, q[0]
on line 7 yielded 1 (and thus the bit at index 0, b[0]
is 1) will the X gate be executed. If b[0]
is 0, the X gate will not be applied.
Any qubit gate can be prepended with c-
to execute that gate only when the classical argument is one. Furthermore, it is possible to specify more than one classical bit, in which case the gate is only executed if and only if all specified classical bits are 1:
version 1.0
qubits 3
prep_z q[0:2]
X q[1]
measure_z q[0]
measure_z q[1]
c-Y b[0,1], q[2]
q[0] | ||||||
---|---|---|---|---|---|---|
q[1] | ||||||
q[2] |
In this example, the binary-controlled Y gate on line 9 will not apply (no Y rotation is carried out on q[2]
) because b[0]
will be 0.
Using a classical not
statement, bits can ale be inverted before applying a binary-controlled operation.