|q⟩ Bad Qubits

intermediate · Programming · Grover's Search Algorithm

Multiple Marked Items

So far you have seen Grover's algorithm with exactly one marked item. The algorithm generalises to MM marked items, and understanding that generalisation reveals why Grover is fast and how fewer iterations are needed when more solutions exist.

Geometry with MM solutions

Grover's algorithm is best understood geometrically. Let N=2nN = 2^n be the total number of basis states and MM the number of marked items. Define two unit vectors:

W=1Mxmarkedx,W=1NMxmarkedx.|W\rangle = \frac{1}{\sqrt{M}}\sum_{x \in \text{marked}} |x\rangle, \qquad |W^\perp\rangle = \frac{1}{\sqrt{N-M}}\sum_{x \notin \text{marked}} |x\rangle.

The initial uniform superposition s=Hn0n|s\rangle = H^{\otimes n}|0\rangle^{\otimes n} lies in the plane spanned by W|W\rangle and W|W^\perp\rangle:

s=MNW+NMNW.|s\rangle = \sqrt{\frac{M}{N}}\,|W\rangle + \sqrt{\frac{N-M}{N}}\,|W^\perp\rangle.

The angle θ\theta between s|s\rangle and W|W^\perp\rangle satisfies sinθ=M/N\sin\theta = \sqrt{M/N}.

Each Grover iteration is a rotation by 2θ2\theta toward W|W\rangle. After kk iterations the total probability of measuring any marked item is

Pmarked(k)=sin2 ⁣((2k+1)θ).P_{\text{marked}}(k) = \sin^2\!\bigl((2k+1)\theta\bigr).

The optimal number of iterations

Setting Pmarked=1P_{\text{marked}} = 1 requires (2k+1)θ=π/2(2k+1)\theta = \pi/2, giving

k=π/2θ2θ=π4θ12.k^* = \frac{\pi/2 - \theta}{2\theta} = \frac{\pi}{4\theta} - \frac{1}{2}.

For small θ\theta this is approximately π/(4θ)π4N/M\pi/(4\theta) \approx \tfrac{\pi}{4}\sqrt{N/M}.

A special case: exact certainty

For N=8N = 8 (three qubits) and M=2M = 2, we get sinθ=2/8=1/2\sin\theta = \sqrt{2/8} = 1/2, so θ=π/6\theta = \pi/6. The optimal number of iterations is

k=π/2π/62π/6=π/3π/3=1.k^* = \frac{\pi/2 - \pi/6}{2 \cdot \pi/6} = \frac{\pi/3}{{\pi/3}} = 1.

Exactly one iteration, and the success probability is sin2(3π/6)=sin2(π/2)=1\sin^2(3 \cdot \pi/6) = \sin^2(\pi/2) = 1. This is not an approximation: the geometry works out perfectly for M=2M = 2 in an 8-element space.

The multi-target oracle

When the oracle must mark multiple states {x1,x2,,xM}\{x_1, x_2, \ldots, x_M\}, you simply compose the individual phase-flip unitaries:

Uf=UxMUx2Ux1U_f = U_{x_M} \cdots U_{x_2} \cdot U_{x_1}

where UxiU_{x_i} phase-flips only xi|x_i\rangle. The diffuser stays unchanged — it reflects about the uniform superposition s|s\rangle regardless of how many items the oracle marks.

Each single-target oracle is built with the same pattern you already know: apply XX gates to every qubit that should be 0|0\rangle in the target bitstring (converting the target to all-ones), then apply an nn-qubit controlled-ZZ, then undo the XX gates. The nn-qubit controlled-ZZ is realised without ancilla qubits by the identity

Cn1Z=Hn1Cn1XHn1,\text{C}^{n-1}Z = H_{n-1} \cdot \text{C}^{n-1}X \cdot H_{n-1},

i.e., sandwich a Toffoli (or its multi-controlled generalisation) between Hadamards on the last qubit. For three qubits this is H(2),CCX(0,1,2),H(2)H(2),\, \text{CCX}(0,1,2),\, H(2).

Try it

The exercise targets N=8N = 8 states with M=2M = 2 marked items: 000|000\rangle and 111|111\rangle. Complete the two oracle phase-flips; the diffuser is already provided. The grader checks the output distribution, which should place equal probability on 000|000\rangle and 111|111\rangle and zero on all other states.

Run your code to see the quantum state.

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