Grover's Algorithm

Four vertically stacked drawers labelled 00, 01, 10 and 11. The bottom drawer is open and holds a purple star.
The labels $00$, $01$, $10$ and $11$ represent the four two-qubit basis states. Here the star marks $\ket{11}$, the state recognised by the oracle.

For me the clearest way to see why a quantum computer can beat a classical one is the smallest non-trivial case of Grover's search.

Imagine a star hidden in one of four drawers, with each drawer equally likely. An oracle is a black-box test that answers a single question: “Is this the marked drawer?” A classical search checks one drawer at a time. The first check finds the star with probability $1/4$. If the answer is “no”, you can still guess among the three remaining drawers. One check followed by a final guess therefore identifies the right drawer with probability $1/4 + (3/4)(1/3) = 1/2$, although that guess has not been verified. Grover's algorithm uses a quantum version of the oracle, followed by interference and a measurement, to identify the marked drawer with certainty after just one query.

With two qubits there are $N = 2^2 = 4$ basis states, $\ket{00}, \ket{01}, \ket{10}, \ket{11}$. One of them, , is marked: the oracle can recognise it and nothing else can. In this special case exactly one Grover iteration finds it with probability $1$. The marked state is drawn in purple and the mean amplitude in green.

1The algorithm, step by step

scroll ↓

2The diffuser as a matrix

Inversion about the mean is not a separate trick. It is exactly what the matrix $U_s$ does when it multiplies $\ket{\psi_1}$. Here it is built and applied row by row.

3Why it lands exactly

The oracle only touches $\ket{w}$ and the diffuser treats every unmarked state alike, so the state never leaves a flat plane: the one spanned by $\ket{w}$ and $\ket{s'}$, the even mix of the unmarked states. Everything above is a picture in that plane.

An engraved sundial on a turned pedestal, with a triangular gnomon and faint purple marks indicating successive angular positions.
The dial is an analogy for equal angular steps. Every Grover iteration turns the state vector through the same angle, $2\theta$.
The starting angle

In this plane $\ket{\psi_0} = \cos\theta\,\ket{s'} + \sin\theta\,\ket{w}$, where

$$\sin\theta = \frac{1}{\sqrt N}.$$

For $N = 4$, $\sin\theta = \frac12$, so the state starts at $\theta = 30^\circ$ above the $\ket{s'}$ axis.

The oracle is a mirror

Negating the $\ket{w}$ component reflects the state in the $\ket{s'}$ axis, from $+30^\circ$ to $-30^\circ$:

$$U_w = \begin{bmatrix}1&0\\0&-1\end{bmatrix}.$$

Its length is unchanged and so is every probability. Only the angle has moved.

The diffuser is a mirror too

$U_s = 2\ket{\psi_0}\!\bra{\psi_0} - I$ reflects in the line through $\ket{\psi_0}$:

$$U_s = \begin{bmatrix}\cos2\theta & \sin2\theta\\ \sin2\theta & -\cos2\theta\end{bmatrix}.$$

The state sat $60^\circ$ below that line, so it lands $60^\circ$ above it, at $90^\circ$: exactly on $\ket{w}$.

Two mirrors make a rotation $$G = U_s U_w = \begin{bmatrix}\cos2\theta & -\sin2\theta\\ \sin2\theta & \cos2\theta\end{bmatrix}.$$

Each iteration turns the state by $2\theta$, so after $k$ of them it is at $(2k+1)\theta$ and

$$P_k = \sin^2\!\big((2k+1)\theta\big).$$

For four items one turn of $60^\circ$ takes $30^\circ$ to $90^\circ$ on the nose.

More items, smaller steps

With $N = 64$ items, $\theta = \arcsin\frac18 \approx 7.2^\circ$. Each turn is only about $14.4^\circ$, so it takes six of them to get near the top: $13\theta \approx 93.3^\circ$ and $P_6 \approx 0.997$.

Keep scrolling and the state keeps turning. The number of turns needed is about $\frac{\pi}{4\theta} \approx \frac\pi4\sqrt N$, which is where the square root comes from.

4Larger N

A post-office cabinet with nine columns and five rows of pigeonholes. One envelope in the fourth row is marked with a purple wax seal.
Among $45$ possible locations, a classical scan reaches the marked one after $23$ checks on average. Five Grover iterations find it with about $99.4\%$ probability.

Four items is special because $3\theta$ hits $90^\circ$ exactly. For $N = 8$, $\theta \approx 20.7^\circ$, so one turn reaches $62^\circ$, two reach $103.6^\circ$ and three reach $145^\circ$. Two iterations is best, and a third makes things worse:

Figure 1: Amplitudes after $k$ iterations for $N = 8$. By $k = 2$ the unmarked amplitudes have gone slightly negative, so the next mean is negative and the third reflection pulls the marked amplitude back down.

In general the best number of iterations is $k \approx \frac\pi4\sqrt N$. That is a quadratic saving, not an exponential one, and no quantum algorithm can do better for unstructured search. It still grows into a large gap:

Table 1: Oracle queries to find one marked item among $N = 2^n$.
$n$$N$classical, $\approx N/2$Grover, $\approx\frac\pi4\sqrt N$
$2$$4$$2$$1$
$10$$1\,024$$512$$25$
$20$$1\,048\,576$$524\,288$$804$
$40$$\approx 1.10\times10^{12}$$\approx 5.50\times10^{11}$$823\,549$
A sweeping engraved celestial sky over a mountain ridge, filled with stars, fine astronomical arcs and one lavender star.