Skip to content

The Famous Algorithms

Grover's search

Find one of N in about sqrt(N) steps, by rotation

You have boxes, one holds a prize, and your only tool is a checker that says yes or no for a box you name. Classically you open boxes until you get lucky - about tries on average. Grover's algorithm finds the prize in roughly steps. Not by opening boxes in parallel, and not by magic - by rotating a single state vector, a fixed angle at a time, until it points almost exactly at the answer. And if you keep rotating past that point, you rotate away from the answer. That failure mode is the most useful thing in this lesson.

Two beliefs to break

First: "Grover checks all boxes at once." No - after one query you are back at the measurement wall from the parallelism lesson. Second, and more dangerous: "more iterations push the success probability higher and higher." It does the opposite past a sharp optimum. Grover is a rotation, and a rotation you run too long overshoots.

Rotate the state, and try to overshoot

The widget starts every item at the same amplitude and marks one target. Two moves are available. The oracle flips the sign of the target's amplitude (nothing else changes). The diffusion step reflects every amplitude about their mean. Apply them in turn, or use one full iteration, and watch both the bars and the vector inset. Then deliberately keep going past the marked optimum and watch the target amplitude collapse back down.

Grover's search: flip the marked amplitude, invert about the mean, and watch the rotation carry the state toward the target - then past it.
Items N
Amplitude of every item. The target starts level with the rest.
no matchmatch
iterations 0match probability 12.5%optimal near 2 steps

Each full iteration rotates the state a fixed angle toward the target. Keep going and watch it climb - but do not overshoot.

The inset is the whole algorithm in one picture: the state is a single vector in a plane, and each full iteration turns it by the same angle toward the vertical match axis. The two myths die here - one vector, one fixed rotation per step, and a definite best place to stop.

Why two reflections make a rotation

Collapse the whole -dimensional state into the only two directions that matter: the marked answer , and the even blend of all the wrong answers . The equal superposition you start from sits almost entirely along , making a small angle with it, where

Now watch the two moves as geometry. flips the sign of the component, which is a reflection across the axis. then reflects the state across the equal-superposition direction. Two reflections, across lines that meet at angle , always compose to a rotation by - a fact of plane geometry, no quantum content at all. So one Grover iteration rotates the state by toward the answer:

Where to stop, and why more is worse

The match probability is largest when the state points straight up the axis, that is when . Solving for and using for large gives the optimal number of iterations:

That is the speedup: about queries where a classical search needs about . It is a quadratic speedup, not an exponential one - Grover on a million boxes takes about a thousand steps, not twenty. And the reason to stop matters as much as the count: run past and the angle sails past , so starts decreasing. The vector rotates through the target and out the other side. There is no "keep going to be safe" here; the number of iterations is a knob you tune, not a dial you crank.

Structured search is still faster

Grover assumes the search space has no exploitable structure - the checker is a black box. When structure exists, classical algorithms already win big: a sorted list is searchable in binary search's steps, far better than Grover's . Grover's quadratic gain is real but narrow: it is the payoff precisely when you have nothing but a yes/no oracle and brute force to fall back on.
import math
from qiskit import QuantumCircuit

n = 4                                  # N = 16 items
k = round(math.pi / 4 * math.sqrt(2**n))   # optimal iterations, about 3

qc = QuantumCircuit(n)
qc.h(range(n))                         # equal superposition over all N items

for _ in range(k):                     # exactly k iterations - do not overshoot
    apply_oracle(qc)                   # flip the sign of the marked item
    apply_diffusion(qc)                # invert all amplitudes about their mean

qc.measure_all()                       # near-certain to read the marked item
Grover's algorithm in Qiskit's prebuilt form

Lock it in

  • Grover is two reflections per step: the oracle flips the marked amplitude's sign, diffusion inverts every amplitude about the mean.
  • Two reflections across lines meeting at angle theta compose to a rotation by 2*theta, so each iteration turns the state a fixed step toward the answer.
  • Match probability is sin^2((2k+1)theta), maximized at about (pi/4)sqrt(N) iterations - a quadratic, not exponential, speedup.
  • Past the optimum the state rotates through the target and the success probability falls. More iterations are actively worse.

Check yourself

How big is Grover's speedup for unstructured search over N items?

What does the diffusion operator do to the amplitudes?

Recall: describe Grover as two reflections, state its speedup, and say what happens if you iterate too long.

Mechanism, magnitude, and the over-rotation trap. Try to state it, then check.

Match each Grover piece to what it does.

drop here

flips the phase of the marked item

drop here

inverts every amplitude about the mean

drop here

rotates the state by 2*theta toward the answer

drop here

the optimal number of iterations to stop at

Primary source

Quantum Country: How does the quantum search algorithm work?

Matuschak and Nielsen build Grover from the reflection-and-rotation picture with spaced-repetition prompts baked in, and they are careful to keep the quadratic speedup honest rather than hyped. It is the best place to make the geometry stick.[1]

Next: the quantum Fourier transform, the interference pattern that reads a hidden period out of a superposition - the engine inside Shor's algorithm.[2]

Sources

  1. 1.Andy Matuschak and Michael Nielsen, How does the quantum search algorithm work? (Quantum Country)
  2. 2.Nielsen and Chuang, Quantum Computation and Quantum Information, chapter 6 (quantum search)