← All quantum articles

Reconstructing Grover's Algorithm

Deriving quantum search from first principles — why amplitude amplification works, and why √N is the natural stopping point.

Problem

We are given oracle access to a function f:{0,1}n{0,1}f : \{0,1\}^n \to \{0,1\} which marks exactly one item: f(w)=1f(w) = 1 for a single unknown ww, and f(x)=0f(x) = 0 otherwise. Classically, finding ww requires Θ(N)\Theta(N) queries where N=2nN = 2^n. How fast can a quantum computer find ww?

Initial Observations
  1. A quantum state can hold all NN candidates in superposition — but measuring the uniform superposition only finds ww with probability 1/N1/N.
  2. The oracle can flip the phase of the marked item: Ox=(1)f(x)xO|x\rangle = (-1)^{f(x)}|x\rangle.
  3. A phase is invisible to measurement. We need some way to convert phase information into amplitude information.
  4. Repeating something that "nudges" amplitude toward ww might compound.
IdeaPhase flip + inversion about the meanAC
Key Question

What unitary operation converts a phase difference into an amplitude difference?

Write It Out

Start with the uniform superposition s=1Nxx|s\rangle = \frac{1}{\sqrt{N}}\sum_x |x\rangle. After the oracle, the state is the same except the amplitude on w|w\rangle is negative. The mean amplitude is now slightly below 1N\frac{1}{\sqrt{N}}. If we reflect every amplitude about the mean, the negative amplitude on w|w\rangle lands far above the mean — roughly 3N\frac{3}{\sqrt{N}} — while every other amplitude shrinks slightly.

The reflection about the mean is the operator

D=2ssID = 2|s\rangle\langle s| - I

and one Grover iteration is G=DOG = D \cdot O.

Lemma.

The state always remains in the 2-dimensional plane spanned by w|w\rangle and s|s\rangle, and each Grover iteration is a rotation in that plane by angle 2θ2\theta, where sinθ=1N\sin\theta = \frac{1}{\sqrt{N}}.

Proof.

Both reflections — the oracle (a reflection about the hyperplane orthogonal to w|w\rangle) and DD (a reflection about s|s\rangle) — preserve the plane span{w,s}\mathrm{span}\{|w\rangle, |s\rangle\}. The composition of two reflections is a rotation by twice the angle between the reflection axes, which is 2θ2\theta.

Corollary.

After kk iterations the state makes angle (2k+1)θ(2k+1)\theta with the axis orthogonal to w|w\rangle. Choosing kπ4Nk \approx \frac{\pi}{4}\sqrt{N} brings the state within angle θ\theta of w|w\rangle, so measurement finds ww with probability 1O(1/N)1 - O(1/N).

Lesson

Grover's algorithm is not "trying everything in parallel" — it is a slow rotation from s|s\rangle toward w|w\rangle, driven by two reflections. The N\sqrt{N} arises because each iteration rotates by a fixed angle 2/N\approx 2/\sqrt{N}. Over-rotating past w|w\rangle decreases the success probability — running the loop longer makes the answer worse.

Implementation

from qiskit import QuantumCircuit
from qiskit.circuit.library import GroverOperator, MCMTGate, ZGate
import math
 
n = 5                       # qubits; N = 32
marked = "10110"            # the unknown w (for the demo oracle)
 
oracle = QuantumCircuit(n)
for i, bit in enumerate(reversed(marked)):
    if bit == "0":
        oracle.x(i)
oracle.append(MCMTGate(ZGate(), n - 1, 1), range(n))
for i, bit in enumerate(reversed(marked)):
    if bit == "0":
        oracle.x(i)
 
grover_op = GroverOperator(oracle)
 
qc = QuantumCircuit(n)
qc.h(range(n))                                   # |s>
iterations = math.floor(math.pi / 4 * math.sqrt(2**n))
for _ in range(iterations):
    qc.compose(grover_op, inplace=True)          # G = D * O
qc.measure_all()

Review

The reconstruction hinged on three moves: (1) realizing a phase flip alone is useless without a second operation, (2) asking what reflection geometry does to a single outlier amplitude, and (3) recognizing two reflections compose into a rotation — at which point the π4N\frac{\pi}{4}\sqrt{N} stopping rule stops being magic and becomes trigonometry.