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.
The problem
Section titled “The problem”There are candidates, numbered to . Exactly one of them, , is the answer. You have an oracle that recognises it: a function with and for every other . Find .
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 queries, and in the worst case. Grover’s algorithm takes about .
Two operations
Section titled “Two operations”As in every algorithm so far, start with qubits in the equal superposition of all candidates. Every amplitude is . Then repeat a pair of operations.
The oracle flips the sign of the marked amplitude and leaves the rest alone:
This is the phase kickback from Deutsch’s algorithm: the oracle for with its output wire in .
The diffusion replaces every amplitude by its mirror image about the mean of all the amplitudes:
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.
Things to try:
- With 16 items, apply the oracle once. What happened to the chance of finding the marked item?
- Apply the diffusion. Why did the marked amplitude grow, and what happened to the others?
- Keep going to the best number of rounds, and then go past it.
- 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 to the marked amplitude. Reaching an amplitude near 1 therefore takes on the order of rounds.
You prepare the equal superposition, apply the oracle once and measure immediately. What is the chance of finding the marked item?
The oracle only changes a sign, and probabilities are squared magnitudes. The oracle marks the answer in a way that cannot be seen directly. The diffusion is what turns the mark into a larger amplitude.
Why you must stop in time
Section titled “Why you must stop in time”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 , 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 , where
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 each round. After rounds the angle is , and the chance of measuring is
This is largest when the angle reaches . For large , , so the best number of rounds is
Keep rotating and the arrow swings past 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 solutions, the count becomes , and there are variants of the algorithm for when is unknown.
Roughly how many rounds does Grover’s algorithm need to find one marked item among a million?
. A classical search would need 500,000 queries on average.
Building it from gates
Section titled “Building it from gates”The case is special: , so a single round gives an angle of exactly 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 . So the diffusion is:
- on every qubit,
- flip the sign of every basis state except ,
- on every qubit again.
First build the middle step for two qubits.
Build a circuit that leaves alone and puts a minus sign on , and .
This puzzle needs JavaScript.
Hint
A 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.
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
on both wires, then the oracle. Then the diffusion: on both, the three gates from the last puzzle, and 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 controlled by every other qubit, and the round is repeated. The structure stays the same.
Nothing does better
Section titled “Nothing does better”It is natural to hope for a cleverer algorithm that finds the marked item in, say, queries. There is none. Bennett, Bernstein, Brassard and Vazirani proved that any quantum algorithm for unstructured search needs on the order of 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 candidates. Grover reduces that to about 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 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.
A problem has candidate solutions and no known structure. Does Grover’s algorithm solve it in a time polynomial in ?
The square root of an exponential is an exponential with half the exponent. For that is steps, far out of reach.
What it is and is not good for
Section titled “What it is and is not good for”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 . The same two reflections boost that probability to nearly 1 in about rounds, where plain repetition would need about . 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.
Remember it
Section titled “Remember it”How many oracle queries does Grover's algorithm need to find one marked item among ?
About . A classical search needs about on average.
What are the two operations in one round of Grover's algorithm?
The oracle flips the sign of the marked amplitude. The diffusion reflects every amplitude about the mean.
What is the success probability after rounds of Grover's algorithm?
, where .
What happens if you run more Grover rounds than the best number?
The success probability falls again. Each round is a rotation by a fixed angle, and it overshoots the marked state.
Could a cleverer quantum algorithm search unstructured items in far fewer than queries?
No. On the order of queries are provably necessary, so quantum computers do not brute-force NP-complete problems efficiently.
What is amplitude amplification?
Grover's method applied to any quantum procedure that succeeds with probability : it raises the success probability to nearly 1 in about rounds.