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 N-item uniform superposition ∣s⟩. Split the Hilbert space into
two directions:
∣W⟩=N−M1unmarked∑∣x⟩,∣T⟩=M1marked∑∣x⟩,
so ∣s⟩=cosθ∣W⟩+sinθ∣T⟩ with
sinθ=NM.
Each Grover iteration is a rotation by 2θ in this plane. After t iterations the
state is cos((2t+1)θ)∣W⟩+sin((2t+1)θ)∣T⟩.
The success probability is sin2((2t+1)θ), which is maximised when
(2t+1)θ≈π/2, i.e.
t≈2θπ/2−θ≈4θπ.
For M≪N we have θ≈sinθ=M/N, giving
t∗=⌊4πMN⌋.
This is the origin of Grover's O(N) query complexity: the optimal iteration
count grows as N rather than N as in classical linear search.
Worked example: N=16, M=1
With four qubits (N=16) and one marked item:
θ=arcsin(161)=arcsin(0.25)≈0.2527 rad.
The exact maximizer of sin2((2t+1)θ) over integer t is the integer
nearest to 21(2θπ−1):
2θπ/2−θ=0.50541.3181=2.608,
so the two candidate integers are t=2 and t=3. Because t=3 places the angle
(2⋅3+1)θ≈1.769 closer to π/2 than t=2 does
((2⋅2+1)θ≈1.263), we get higher success probability at t=3
(≈96.1%) than at t=2 (≈90.8%).
The small-angle approximation formula reaches the same conclusion directly:
t∗=⌊4π16⌋=⌊4π⋅4⌋=⌊3.1415…⌋=3.
For M≪N the approximation sinθ≈θ is excellent, and the floor
ensures we never over-rotate past the target. The answer for N=16, M=1 is
t∗=3.
Try it
Implement the formula in JavaScript and return the floor value for N=16, M=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.