Binary controlled gates
All single qubit gates 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 measure_z q c-X b, q measure q
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) being 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 on line 9 yielded 1 (and thus the bit at index 0,
b is 1) will the X gate be executed. If
b is 0, the X gate will not be applied. In the circuit visualisation a small '0' is attached to the left of the binary controlled X gate.
Any single qubit gate can be prepended with
c- to execute that gate only when the classical argument is 1. Furthermore, it is possible to specify more than one classical bit using SGMQ syntax. The gate is executed if and only if all specified classical bits are 1:
version 1.0 qubits 3 prep_z q[0:2] X q measure_z q measure_z q c-Y b[0,1], q
In this example, the binary-controlled Y gate on line 9 will not apply (no Y rotation is carried out on
b will be 0.
Classical logic not
Sometimes it is desirable to apply the conditional gate when a bit is 0. A bit in the measurement register can be flipped with the
version 1.0 qubits 2 prep_z q[0,1] H q measure_z q not b c-X b, q