Quantum
Module 7 · Algorithms · Lesson 4

Grover's search

Find a needle in a haystack of N items using O(√N) queries. The famous quadratic speedup. Watch the correct answer's amplitude grow iteration by iteration.

12 min read · Lesson 24 of 32

Grover’s search (1996) solves the most generic algorithmic problem imaginable: find one special item among NN options. Classical: O(N)O(N) queries in the worst case, O(N/2)O(N/2) on average. Quantum: O(N)O(\sqrt{N}). That’s a quadratic speedup — less dramatic than Shor’s exponential one, but applicable to a vastly larger class of problems.

Search is everywhere in computer science. SAT solvers, constraint satisfaction, cryptographic brute force, collision finding — they all reduce to search at the bottom. Grover applies to any of them.

The problem

You have a function f:{0,1,2,,N1}{0,1}f: \{0, 1, 2, \ldots, N-1\} \to \{0, 1\} that acts as an “is this the answer?” check. Exactly one input mm (the “marked item”) satisfies f(m)=1f(m) = 1. You don’t know which.

Classical query complexity: O(N)O(N).

Grover: O(N)O(\sqrt{N}).

The algorithm

Grover iteratively applies two steps — an oracle and a diffuser — that together rotate the state vector toward the marked item.

  1. Initialize nn qubits (where N=2nN = 2^n) in a uniform superposition ψ=1Nxx|\psi\rangle = \frac{1}{\sqrt{N}}\sum_x |x\rangle via Hadamards.
  2. Repeat πN/4\lfloor\pi\sqrt{N}/4\rfloor times:
    • Oracle: flip the phase of the marked item: mm|m\rangle \mapsto -|m\rangle.
    • Diffuser: reflect about the mean amplitude. This can be written as Hn(200I)HnH^{\otimes n} \cdot (2|0\rangle\langle 0| - I) \cdot H^{\otimes n}.
  3. Measure. With high probability, you get mm.

Watch it happen

Pick a marked item. Click “Next iteration” and watch the marked item’s amplitude bar grow while all the others shrink. After about π8/42\lfloor\pi\sqrt{8}/4\rfloor \approx 2 iterations for N=8N=8, the marked item has over 94%94\% probability. Any more iterations and the amplitude starts to decrease again — Grover actually overshoots if you iterate too many times, because each step rotates the state vector by a fixed angle. Run too many iterations and you rotate past the marked state.

This “sweet spot” behavior is unusual for classical algorithms but fundamental to Grover’s: the algorithm’s correctness depends on stopping at the right moment.

Why it works — geometric intuition

Grover’s algorithm has a beautiful geometric interpretation. Define two orthogonal states:

Any state that’s a real linear combination of these two lives on a 2D plane. The initial uniform superposition is ψ=1Nm+N1Nm|\psi\rangle = \frac{1}{\sqrt{N}}|m\rangle + \frac{\sqrt{N-1}}{\sqrt{N}}|m^\perp\rangle — mostly in the “unmarked” direction, with a small component along m|m\rangle.

Each Grover iteration is a rotation by angle 2θ2\theta in this plane, where sinθ=1/N\sin\theta = 1/\sqrt{N}. After kk iterations, the state has rotated by angle (2k+1)θ(2k+1)\theta from the m|m^\perp\rangle axis. You want the state to point along m|m\rangle, which happens when (2k+1)θ=π/2(2k+1)\theta = \pi/2, giving:

koptimal=π4N12π4Nk_{\text{optimal}} = \frac{\pi}{4}\sqrt{N} - \frac{1}{2} \approx \frac{\pi}{4}\sqrt{N}

The oracle is a reflection about the m|m^\perp\rangle axis (it only affects m|m\rangle). The diffuser is a reflection about ψ|\psi\rangle. The composition of two reflections is a rotation by twice the angle between them — which is exactly the Grover rotation.

The quadratic speedup is optimal

A theorem (Bennett-Bernstein-Brassard-Vazirani) says: no quantum algorithm can do better than O(N)O(\sqrt{N}) on this problem. The quadratic speedup is the best possible for unstructured search. If you want better, you need structure — and specific structure gives you specific algorithms (like Shor’s for integer factoring).

This is one of those rare “tight” theorems in quantum complexity. For most quantum speedups, we don’t know if they’re optimal. For Grover, we do.

What you get with a quadratic speedup

Quadratic doesn’t sound as sexy as exponential, but it’s still a big deal in practice. For a problem where classical takes 21002^{100} steps (out of reach), Grover takes 2502^{50} (still very large, but perhaps feasible on a future fault-tolerant quantum computer). Grover’s speedup is the reason:

Quick check
What speedup does Grover's algorithm provide for unstructured search?
Quick check
What happens if you run too many Grover iterations?
Quick check
Is Grover's quadratic speedup optimal?

What’s next

The next lesson introduces the Quantum Fourier Transform — the heart of Shor’s algorithm, and one of the most important primitives in all of quantum computing. Grover’s search uses simple Hadamards because the structure being searched is unstructured. The moment we have periodic structure, the QFT takes over.