|q⟩ Bad Qubits

intermediate · Programming · Multi-Qubit Registers & State Simulation

Multi-Controlled Gates

A controlled gate applies a single-qubit operation only when a designated control qubit is 1|1\rangle. Extending this idea to two or more control qubits produces multi-controlled gates — gates that fire only when every control qubit is 1|1\rangle. They are the quantum analogue of multi-input AND logic, and they are indispensable for constructing oracles, arithmetic circuits, and quantum error correction.

The Toffoli gate (CCX)

The most important multi-controlled gate is the Toffoli gate, also written CCX or doubly-controlled-NOT. It acts on three qubits: two controls c1c_1, c2c_2 and one target tt.

CCXc1,c2,t={c1,c2,t1if c1=c2=1,c1,c2,totherwise.\mathrm{CCX}\,|c_1, c_2, t\rangle = \begin{cases} |c_1, c_2, t \oplus 1\rangle & \text{if } c_1 = c_2 = 1, \\ |c_1, c_2, t\rangle & \text{otherwise.} \end{cases}

In words: flip the target bit if and only if both controls are 1|1\rangle. The 8×88 \times 8 unitary matrix of CCX is the identity on all computational basis states except the swap 110111|110\rangle \leftrightarrow |111\rangle (using the convention that qubit 0 is most significant). CCX is its own inverse: CCX2=I\mathrm{CCX}^2 = I.

Controlled gates as conditional unitary maps

Any single-qubit gate UU can be lifted to a controlled version C(U)C(U) on two qubits:

C(U)=00I  +  11U.C(U) = |0\rangle\langle 0| \otimes I \;+\; |1\rangle\langle 1| \otimes U.

Extending to nn control qubits gives Cn(U)C^n(U), which applies UU to the target only when all nn controls are in state 1|1\rangle. The simulator provides the most common instances directly:

Higher-degree multi-controlled gates can be synthesised from these primitives using ancilla qubits, a technique covered in the next lesson.

Doubly-controlled Z (CCZ)

A related gate, CCZ, flips the phase of 111|111\rangle by 1-1 while leaving all other basis states unchanged:

CCZc1,c2,t={c1,c2,tif c1=c2=t=1,c1,c2,totherwise.\mathrm{CCZ}\,|c_1, c_2, t\rangle = \begin{cases} -|c_1, c_2, t\rangle & \text{if } c_1 = c_2 = t = 1, \\ |c_1, c_2, t\rangle & \text{otherwise.} \end{cases}

CCZ and CCX are related by a Hadamard on the target qubit:

CCX=(IIH)  CCZ  (IIH).\mathrm{CCX} = (I \otimes I \otimes H)\;\mathrm{CCZ}\;(I \otimes I \otimes H).

This conjugation identity is the multi-qubit generalisation of HZH=XHZH = X.

Try it

Build the Toffoli gate as a standalone 3-qubit circuit. The grader checks the full unitary matrix, so a do-nothing circuit will fail.

Run your code to see the quantum state.

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