Quantum ComputingQiskitIBM Quantum

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

← Back to research

The problem

You have NN 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 N/2N/2 tries, and the cost grows in direct proportion to NN.

Grover's 1996 algorithm finds the prize with about π4N\frac{\pi}{4}\sqrt{N} 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 nn qubits in an equal superposition of all N=2nN = 2^n items: each has the same amplitude 1/N1/\sqrt{N}, 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 2θ2\theta towards the marked item, where sin⁡θ=1/N\sin\theta = 1/\sqrt{N}. After kk rounds:

P(k)=sin⁡2 ⁣((2k+1) θ),kopt≈π4NP(k) = \sin^2\!\big((2k+1)\,\theta\big), \qquad k_{\text{opt}} \approx \frac{\pi}{4}\sqrt{N}

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 kk iterations. It rises, peaks, then falls again: Grover is a rotation, and carrying on rotates past the target.

012345678

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 sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) 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.

QubitsIterationsIdealExactGuessingTwo-qubit gates
21100%100%25%–
3295%95%13%–
4396%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).

QubitsIterationsIdealMeasuredGuessingTwo-qubit gates
21100%98%25%2
3295%74%13%39
4396%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.

Notes

  • The marked item for each size is an arbitrary fixed choice so runs can be compared.
  • Bitstrings follow Qiskit's convention: the rightmost bit is qubit 0.
  • The website never contacts IBM. It displays a results file that is regenerated by a manual GitHub Action (credentials stay in GitHub secrets).