|q⟩ Bad Qubits

intermediate · Programming · Grover's Search Algorithm

Three-Qubit Grover

Scaling Grover's algorithm from 2 to 3 qubits takes us from N=4N = 4 to N=8N = 8 items and introduces the first non-trivial question: how many iterations do we need?

Optimal iteration count for eight items

The geometric analysis (see the Optimal Number of Iterations lesson) gives

t=π4NM,t^* = \left\lfloor \frac{\pi}{4}\sqrt{\frac{N}{M}} \right\rfloor,

where MM is the number of marked items. With N=8N = 8, M=1M = 1:

sinθ=18,t=π48=π22=2.22=2.\sin\theta = \sqrt{\frac{1}{8}}, \qquad t^* = \left\lfloor \frac{\pi}{4}\sqrt{8} \right\rfloor = \left\lfloor \frac{\pi\sqrt{2}}{2} \right\rfloor = \lfloor 2.22 \rfloor = 2.

Two iterations push the success probability to

Psuccess=sin2 ⁣((22+1)θ)=sin2(5θ)0.945.P_{\text{success}} = \sin^2\!\bigl((2\cdot 2 + 1)\theta\bigr) = \sin^2(5\theta) \approx 0.945.

That is close to — but not quite — certainty; N=8N = 8 does not produce an exact multiple of π/2\pi/2 the way N=4N = 4 did. A third iteration would over-rotate and lower the probability.

Oracle for a three-qubit marked state

Suppose we want to mark 101|101\rangle (qubit 0 is 1|1\rangle, qubit 1 is 0|0\rangle, qubit 2 is 1|1\rangle). The standard approach converts a phase-flip oracle for 111|111\rangle into one for any target pattern by flipping the zero-valued qubits with XX before and after the core gate:

O101=(IXI)  O111  (IXI).O_{|101\rangle} = (I \otimes X \otimes I)\; O_{|111\rangle}\; (I \otimes X \otimes I).

The O111O_{|111\rangle} gate itself is a three-qubit controlled-ZZ (CCZ). Because the circuit API provides CCX (Toffoli) rather than CCZ directly, we use the identity CZ=(IH)CX(IH)CZ = (I \otimes H)\,CX\,(I \otimes H), extended to three qubits:

CCZ(q0,q1,q2)=H(q2)  CCX(q0,q1,q2)  H(q2).\text{CCZ}(q_0,q_1,q_2) = H(q_2)\;\text{CCX}(q_0,q_1,q_2)\;H(q_2).

So the full oracle for 101|101\rangle is:

  1. X(q1)X(q_1) — flip the zero qubit.
  2. H(q2)H(q_2), CCX(0,1,2)\text{CCX}(0,1,2), H(q2)H(q_2) — controlled-controlled-ZZ.
  3. X(q1)X(q_1) — undo the flip.

The three-qubit diffuser

The diffuser reflects amplitudes about their mean. For nn qubits the pattern is always:

Hn  Xn  (zero-phaseflip)  Xn  Hn,H^{\otimes n}\; X^{\otimes n}\; \text{(zero-phaseflip)}\; X^{\otimes n}\; H^{\otimes n},

where the zero-phase-flip adds a 1-1 only to 000|00\ldots0\rangle. For three qubits the zero-phase-flip is again a CCZ gate (all controls on 1|1\rangle after the XX layer):

H(2)  CCX(0,1,2)  H(2).H(2)\;\text{CCX}(0,1,2)\;H(2).

Putting it together

One full Grover iteration is: oracle then diffuser. With two iterations the procedure is:

  1. Apply H3H^{\otimes 3} to create the uniform superposition s|s\rangle.
  2. Apply oracle + diffuser (iteration 1).
  3. Apply oracle + diffuser (iteration 2).
  4. Measure — outcome 101|101\rangle appears with probability 94.5%\approx 94.5\,\%.

Try it

The starter code provides the superposition and both diffuser blocks. Add the oracle for 101|101\rangle in each iteration. The grader checks the output distribution: 101|101\rangle (the 5th basis state in binary, index 5 counting from 0) must carry the dominant probability.

Run your code to see the quantum state.

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