Skip to content

Grover's search

Shor’s algorithm exploits a hidden period. Grover’s algorithm is for problems with no structure to exploit, where the only thing you can do is try candidates. Its speedup is smaller, a square root and not an exponential, but it applies very widely, and it comes with a proof that no quantum algorithm can do better.

There are NN candidates, numbered 00 to N−1N - 1. Exactly one of them, ww, is the answer. You have an oracle that recognises it: a function with f(w)=1f(w) = 1 and f(x)=0f(x) = 0 for every other xx. Find ww.

Think of a problem where a proposed solution is easy to check but hard to find, such as a password that must match a stored hash, or a way of filling in a puzzle. The oracle is the checker. It knows the answer when it sees it and gives no hint about where to look.

Classically, with no further structure, you try candidates one at a time. On average that takes N/2N/2 queries, and NN in the worst case. Grover’s algorithm takes about π4N\tfrac{\pi}{4}\sqrt N.

As in every algorithm so far, start with nn qubits in the equal superposition of all N=2nN = 2^n candidates. Every amplitude is 1/N1/\sqrt N. Then repeat a pair of operations.

The oracle flips the sign of the marked amplitude and leaves the rest alone:

∣x⟩  ⟶  (−1)f(x)∣x⟩.\ket{x} \;\longrightarrow\; (-1)^{f(x)}\ket{x}.

This is the phase kickback from Deutsch’s algorithm: the oracle for ff with its output wire in ∣−⟩\ket{-}.

The diffusion replaces every amplitude axa_x by its mirror image about the mean μ\mu of all the amplitudes:

ax  ⟶  2μ−ax.a_x \;\longrightarrow\; 2\mu - a_x.

An amplitude above the mean ends up the same distance below it, and the other way round.

One oracle followed by one diffusion is a round. Step through a few rounds here.

SimulatorGrover's search, step by step
Amplitude of each item (the marked one is orange, the dashed line is the mean)
Chance of finding the marked item after each round

Things to try:

  1. With 16 items, apply the oracle once. What happened to the chance of finding the marked item?
  2. Apply the diffusion. Why did the marked amplitude grow, and what happened to the others?
  3. Keep going to the best number of rounds, and then go past it.
  4. Change the number of items to 64. How did the best number of rounds change when the number of items was multiplied by 4?

Here is what you should have seen. The oracle alone changes no probabilities, since a sign does not affect a squared magnitude. But it drags the mean down slightly, and leaves the marked amplitude far below the mean. The reflection then throws the marked amplitude far above it, while the others, which were just above the mean, drop a little. Each round adds roughly 2/N2/\sqrt N to the marked amplitude. Reaching an amplitude near 1 therefore takes on the order of N\sqrt N rounds.

Check

You prepare the equal superposition, apply the oracle once and measure immediately. What is the chance of finding the marked item?

The growth does not continue for ever. Past the best round the marked amplitude shrinks again, and the success probability rises and falls like a wave.

The reason is geometric. Throughout the algorithm the state is a combination of just two things: the marked state ∣w⟩\ket{w}, and the equal superposition of all the unmarked ones. So it lives in a plane, and it can be drawn as an arrow at some angle from the “unmarked” axis. At the start that angle is θ\theta, where

sin⁡θ=1N.\sin\theta = \frac{1}{\sqrt N}.

The oracle reflects the arrow in the unmarked axis. The diffusion reflects it in the direction of the starting state. Two reflections make a rotation, and here the rotation is by 2θ2\theta each round. After kk rounds the angle is (2k+1)θ(2k + 1)\theta, and the chance of measuring ww is

Pk=sin⁡2((2k+1)θ).P_k = \sin^2\bigl((2k + 1)\theta\bigr).

This is largest when the angle reaches 90∘90^\circ. For large NN, θ≈1/N\theta \approx 1/\sqrt N, so the best number of rounds is

k≈π4N.k \approx \frac{\pi}{4}\sqrt N.

Keep rotating and the arrow swings past ∣w⟩\ket{w} and away again. A quantum search has to be stopped at the right moment, which means you need to know roughly how many solutions there are. If there are MM solutions, the count becomes π4N/M\tfrac{\pi}{4}\sqrt{N/M}, and there are variants of the algorithm for when MM is unknown.

Check

Roughly how many rounds does Grover’s algorithm need to find one marked item among a million?

The case N=4N = 4 is special: θ=30∘\theta = 30^\circ, so a single round gives an angle of exactly 90∘90^\circ and finds the marked item with certainty.

The diffusion looks complicated, but it is a familiar pattern. Reflecting about the mean is the same as flipping the sign of everything except the equal superposition. A row of Hadamards turns the equal superposition into ∣00…0⟩\ket{00\dots0}. So the diffusion is:

  1. HH on every qubit,
  2. flip the sign of every basis state except ∣00…0⟩\ket{00\dots0},
  3. HH on every qubit again.

First build the middle step for two qubits.

PuzzleFlip everything except |00⟩

Build a circuit that leaves ∣00⟩\ket{00} alone and puts a minus sign on ∣01⟩\ket{01}, ∣10⟩\ket{10} and ∣11⟩\ket{11}.

This puzzle needs JavaScript.

Hint

A ZZ on each wire gives signs +,−,−,++,-,-,+ on the four basis states. One more gate fixes the last one.

Now the whole algorithm. The oracle below marks one of the four two-bit strings. The target is hidden.

PuzzleSearch four items with one query

Use the Oracle once to find the string it marks. Make the equal superposition, apply the oracle, then apply the diffusion. The puzzle is solved when the state is the marked basis state. Press Measure ×100 to confirm that the answer comes out every time.

This puzzle needs JavaScript.

Hint

HH on both wires, then the oracle. Then the diffusion: HH on both, the three gates from the last puzzle, and HH on both again.

A classical search of four items needs a little over two queries on average, and three in the worst case. This used one.

For more qubits the middle step needs a gate that acts on all of them at once, a ZZ controlled by every other qubit, and the round is repeated. The structure stays the same.

It is natural to hope for a cleverer algorithm that finds the marked item in, say, log⁡N\log N queries. There is none. Bennett, Bernstein, Brassard and Vazirani proved that any quantum algorithm for unstructured search needs on the order of N\sqrt N queries. Grover’s algorithm is optimal.

This result shapes what we expect from quantum computers in general.

  • They do not solve hard search problems by brute force in reasonable time. Many famous hard problems, the NP-complete ones, can be phrased as a search over 2n2^n candidates. Grover reduces that to about 2n/22^{n/2} steps, which is still exponential. Quantum computers are not believed to solve these problems efficiently. An exponential speedup needs structure, as in Shor’s algorithm.
  • Symmetric cryptography survives with longer keys. Searching for a 128-bit key takes about 2642^{64} Grover rounds, which must run one after another. Doubling the key length restores the original margin, which is one reason 256-bit keys are recommended.
Check

A problem has 2n2^n candidate solutions and no known structure. Does Grover’s algorithm solve it in a time polynomial in nn?

Three cautions about using Grover’s search in practice.

It does not search a database. The oracle has to be a quantum circuit that can be run on a superposition of candidates. For data sitting in a classical memory, building such an oracle requires loading the data in a way that costs about as much as reading all of it. Grover’s algorithm is suited to searching for the solution of a problem whose answers can be computed and checked, not to looking things up.

A quadratic speedup is fragile. Each quantum step is far slower and more expensive than a classical one, especially with error correction, and the rounds cannot be run in parallel without losing part of the gain. Published analyses suggest that, on the kinds of fault-tolerant machines currently foreseen, a merely quadratic speedup pays off only for very large problems, if at all.

The idea generalises. Replace “the equal superposition” by the output of any quantum procedure that succeeds with a small probability pp. The same two reflections boost that probability to nearly 1 in about 1/p1/\sqrt p rounds, where plain repetition would need about 1/p1/p. This is amplitude amplification, and it appears as a component inside many other quantum algorithms.

The last lesson of the track turns to the task quantum computers were first proposed for: simulating quantum systems.

RecallAnswer from memory, then check
LessonFinish this lesson