Grover's algorithm
Searching an unsorted list in about √N steps instead of N/2: built in Qiskit, checked against the theory, and then pushed towards real, noisy hardware.
By Paolo Minhas
The problem
You have boxes and exactly one contains a prize. You can open a box to check, but you have no clue which. On average you open half of them: about tries, and the cost grows in direct proportion to .
Grover's 1996 algorithm finds the prize with about checks. For a million boxes that is roughly 800 instead of 500,000. That is a real speed-up, but it is only a quadratic one. It is not the exponential speed-up that quantum computers are sometimes said to give for everything; for unstructured search, it can be proven that nothing does better.
How it works
Start with qubits in an equal superposition of all items: each has the same amplitude , so measuring now is just a random guess. Then repeat two moves.
- The oracle flips the sign of the marked item's amplitude and touches nothing else. It is a black box that recognises the answer without saying where it is.
- The diffuser reflects every amplitude about the average. Because the marked item now sits below the average, it is thrown up above it, and a little more each time.
Geometrically, each round rotates the state by an angle towards the marked item, where . After rounds:
Because it is a rotation, it overshoots if you keep going. Try it below: step through the sign flip and the reflection, and watch the probability curve.
Interactive
Search 16 items for one
Bars are amplitudes, one per item. Click a bar to choose which item is marked (the one the oracle recognises). The dashed line is the mean amplitude.
Equal superposition: every item has the same amplitude.
Chance of measuring the marked item now: 6.3% (guessing: 6.3%). Best number of iterations for 16 items: 3.
Stage 0 of 14; after 0 full iterations.
Why you must stop at the right moment
Success probability after iterations. It rises, peaks, then falls again: Grover is a rotation, and carrying on rotates past the target.
Orange dot: where the stepper is. White ring: the optimum, 3 iterations. Horizontal axis: iterations.
The Qiskit version
The playground above shuffles a list of numbers. The real thing is a quantum circuit: Hadamard gates make the superposition, a multi-controlled Z gate (surrounded by X gates that select the target bit pattern) is the oracle, and Hadamards plus another multi-controlled Z make the diffuser. I built these by hand rather than using a library routine, then wrote tests that check the circuit's output probabilities against for every iteration count up to six qubits. A circuit that fails to match the formula is a bug, so the formula is the test.
Results
Three ways of running the same circuits. The ideal numbers are exact. The noisy simulation adds the error rates from a snapshot of a real IBM device. Real hardware is a physical machine.
Ideal
Exact statevector results at the optimal number of iterations.
| Qubits | Iterations | Ideal | Exact | Guessing | Two-qubit gates |
|---|---|---|---|---|---|
| 2 | 1 | 100% | 100% | 25% | – |
| 3 | 2 | 95% | 95% | 13% | – |
| 4 | 3 | 96% | 96% | 6.3% | – |
Noisy simulation
Simulated, not measured on a real chip: the noise model comes from a calibration snapshot of IBM's fake_sherbrooke device (4000 shots).
| Qubits | Iterations | Ideal | Measured | Guessing | Two-qubit gates |
|---|---|---|---|---|---|
| 2 | 1 | 100% | 98% | 25% | 2 |
| 3 | 2 | 95% | 74% | 13% | 39 |
| 4 | 3 | 96% | 26% | 6.3% | 179 |
Real hardware
No hardware run has been added yet. The numbers above are simulations, and I am not going to present them as anything else. The run is a button in the repository (it needs my IBM credentials, which live in GitHub secrets); once it has been done, this section fills in automatically.
What the noise says
Grover needs the circuit to get deeper as the list grows, and every two-qubit gate adds error. In the noisy simulation the success probability falls from near-certain at two qubits to well below the ideal at four, while the circuit's two-qubit gate count climbs steeply (the table shows it). The quadratic speed-up only helps once the hardware is good enough to run the longer circuits faithfully, which today's devices are not, for problems of any useful size. That is the honest conclusion: this is a demonstration of the algorithm and of its limits, not a practical search engine.