|q⟩ Bad Qubits

intermediate · Programming · Grover's Search Algorithm

Optimal Number of Iterations

Running Grover's algorithm for exactly the right number of iterations is critical. Too few and the marked state is not yet fully amplified; too many and the state over-rotates past the target — the success probability falls again. This lesson derives and lets you compute the sweet spot.

Geometry of Grover iterations

Start with the NN-item uniform superposition s|s\rangle. Split the Hilbert space into two directions:

W=1NMunmarkedx,T=1Mmarkedx,|W\rangle = \frac{1}{\sqrt{N - M}} \sum_{\text{unmarked}} |x\rangle, \qquad |T\rangle = \frac{1}{\sqrt{M}} \sum_{\text{marked}} |x\rangle,

so s=cosθW+sinθT|s\rangle = \cos\theta\,|W\rangle + \sin\theta\,|T\rangle with

sinθ=MN.\sin\theta = \sqrt{\frac{M}{N}}.

Each Grover iteration is a rotation by 2θ2\theta in this plane. After tt iterations the state is cos((2t+1)θ)W+sin((2t+1)θ)T\cos\bigl((2t+1)\theta\bigr)|W\rangle + \sin\bigl((2t+1)\theta\bigr)|T\rangle. The success probability is sin2((2t+1)θ)\sin^2\bigl((2t+1)\theta\bigr), which is maximised when (2t+1)θπ/2(2t+1)\theta \approx \pi/2, i.e.

tπ/2θ2θπ4θ.t \approx \frac{\pi/2 - \theta}{2\theta} \approx \frac{\pi}{4\theta}.

For MNM \ll N we have θsinθ=M/N\theta \approx \sin\theta = \sqrt{M/N}, giving

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

This is the origin of Grover's O(N)O(\sqrt{N}) query complexity: the optimal iteration count grows as N\sqrt{N} rather than NN as in classical linear search.

Worked example: N=16N = 16, M=1M = 1

With four qubits (N=16N = 16) and one marked item:

θ=arcsin ⁣(116)=arcsin(0.25)0.2527 rad.\theta = \arcsin\!\left(\sqrt{\frac{1}{16}}\right) = \arcsin(0.25) \approx 0.2527 \text{ rad}.

The exact maximizer of sin2 ⁣((2t+1)θ)\sin^2\!\bigl((2t+1)\theta\bigr) over integer tt is the integer nearest to 12 ⁣(π2θ1)\tfrac{1}{2}\!\left(\tfrac{\pi}{2\theta} - 1\right):

π/2θ2θ=1.31810.5054=2.608,\frac{\pi/2 - \theta}{2\theta} = \frac{1.3181}{0.5054} = 2.608,

so the two candidate integers are t=2t = 2 and t=3t = 3. Because t=3t = 3 places the angle (23+1)θ1.769(2\cdot3+1)\theta \approx 1.769 closer to π/2\pi/2 than t=2t = 2 does ((22+1)θ1.263(2\cdot2+1)\theta \approx 1.263), we get higher success probability at t=3t = 3 (96.1%\approx 96.1\%) than at t=2t = 2 (90.8%\approx 90.8\%).

The small-angle approximation formula reaches the same conclusion directly:

t=π416=π44=3.1415=3.t^* = \left\lfloor \frac{\pi}{4}\sqrt{16} \right\rfloor = \left\lfloor \frac{\pi}{4} \cdot 4 \right\rfloor = \lfloor 3.1415\ldots \rfloor = 3.

For MNM \ll N the approximation sinθθ\sin\theta \approx \theta is excellent, and the floor ensures we never over-rotate past the target. The answer for N=16N = 16, M=1M = 1 is t=3t^* = 3.

Try it

Implement the formula in JavaScript and return the floor value for N=16N = 16, M=1M = 1. The grader checks that you return exactly 3 (the approximation result).

Run your code to see the quantum state.

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