|q⟩ Bad Qubits

beginner · Programming · Controlled Gates: CNOT, CZ, SWAP

Control and Target

Every controlled gate assigns a distinct job to each qubit: one qubit is the control and another is the target. The control qubit decides whether anything happens, while the target qubit is the one that is actually transformed.

The two roles

For the CNOT gate the rule is simple: if the control qubit is 1|1\rangle, apply XX (bit-flip) to the target; if the control is 0|0\rangle, leave the target alone. Written as a conditional:

CNOTc,t=c,tc,\text{CNOT}\,|\text{c},\,\text{t}\rangle = |\text{c},\,\text{t} \oplus \text{c}\rangle,

where \oplus is addition modulo 2. The control qubit c\text{c} is unchanged in every row of the truth table — it reads the signal without absorbing it.

The roles are labels, not qubit indices

Nothing in the physics requires the control to be the lower-numbered qubit. When you write c.cx(control, target) you specify both roles explicitly. Setting control = 1 and target = 0 is perfectly legal and completely different from c.cx(0, 1) — the two circuits produce different state vectors whenever qubit 1 and qubit 0 are not in the same state.

Generalising beyond CNOT

The same language applies to every controlled gate in this module. The CZ gate applies ZZ to the target when the control is 1|1\rangle; a controlled-phase gate CϕC_\phi applies a phase to the target. In each case the circuit description names two qubit indices and assigns one the control role and one the target role. Keeping those roles straight is the prerequisite for understanding entanglement, phase kickback, and every quantum algorithm that builds on them.

Try it

Start from 00|00\rangle but this time use qubit 1 as the control and qubit 0 as the target. Flip the control to 1|1\rangle, then apply the CNOT. If you correctly assign the roles you will reach 11|11\rangle; swapping the arguments by mistake will leave the state unchanged and the grader will fail.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.