ZKSF logo, a neon quantum brainZKSF
← All articles

Grover's Search Algorithm: Find a Needle in a Quantum Haystack

Last updated · 12 min read · ZKSF team

Open in Colab

Run this one yourself. The notebook builds the circuit and reads the certificate for the run below, with no account and nothing to install.

The short version

  • Quadratic, and provably optimal. Bennett, Bernstein, Brassard and Vazirani proved a matching lower bound, so no quantum algorithm does better on unstructured search
  • Two reflections per iteration. The oracle flips the sign of the marked state and the diffusion operator reflects all amplitudes about their mean
  • All 1,000 shots returned 11. At N = 4 a single iteration is exact, so the rotation lands precisely on the target with no scatter
  • Symmetric keys are where it genuinely bites. Grover halves the effective security of an n-bit key, which is why AES-256 is specified where AES-128 would otherwise suffice

Grover's algorithm finds a marked item among N unstructured possibilities using O(sqrt(N)) oracle queries, against the N/2 expected queries any classical method requires. This article derives the mechanism geometrically, gives the circuit, reports a certified run, and states precisely what the quadratic speedup is and is not worth.

The problem, and the optimality result

Unstructured search means the oracle offers no exploitable regularity. It recognises the correct answer but gives no hint about where it is. Classically, nothing better than checking candidates one at a time is possible, requiring N/2 queries on average and N in the worst case.

Grover's algorithm requires approximately (pi/4) sqrt(N). This is optimal: Bennett, Bernstein, Brassard and Vazirani proved a matching lower bound showing no quantum algorithm can search an unstructured space in fewer than order sqrt(N) queries.

The result predates Grover's algorithm, so the algorithm was known to be optimal essentially as soon as it appeared. Very few quantum algorithms have this status.

The mechanism, geometrically

The clearest derivation treats the algorithm as a rotation in a two-dimensional plane.

Let |w> denote the marked state and |s'> the uniform superposition over all *unmarked* states. These two vectors are orthogonal and span a plane containing the uniform superposition |s>, which begins at a small angle theta from |s'>, where sin(theta) = 1/sqrt(N).

Each Grover iteration applies two reflections. The oracle reflects about |s'>, flipping the sign of the |w> component. The diffusion operator reflects about |s>. The composition of two reflections is a rotation by twice the angle between their axes, so each iteration rotates the state toward |w> by exactly 2 theta.

Starting at angle theta and needing to reach pi/2, the required iteration count is (pi/2 - theta)/(2 theta), approximately (pi/4) sqrt(N) for large N. The derivation is exact and requires no approximation beyond sin(theta) approximately theta.

This picture predicts a phenomenon worth observing directly. The rotation does not stop at |w>. Continuing past the optimal count rotates *past* the target and the success probability falls, oscillating rather than saturating.

Grover's algorithm can be run too long, which is the clearest available demonstration that quantum algorithms are interference phenomena rather than parallel search. A parallel search would not get worse with more effort.

The circuit

The template searches the four states of two qubits for the target 11. Hadamards create the uniform superposition.

The oracle is a controlled-Z, which flips the phase of |11> alone. The diffusion operator is implemented as Hadamards and X gates around a second controlled-Z, which realises the reflection about the mean. For N = 4, theta = pi/6 and a single iteration rotates exactly to the target, so the algorithm succeeds with certainty.

OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
h q[1];
cz q[0],q[1];          // oracle: mark |11>
h q[0];
h q[1];
x q[0];
x q[1];
cz q[0],q[1];          // diffusion
x q[0];
x q[1];
h q[0];
h q[1];
measure q -> c;
Grover search circuit diagram for two qubits: Hadamards, a controlled-Z oracle, and a diffusion block, then measurement
Grover search circuit diagram for two qubits: Hadamards, a controlled-Z oracle, and a diffusion block, then measurement

The two Hadamards on the left create the superposition. The emerald controlled-Z is the oracle. The block that follows, Hadamards and X gates around a second controlled-Z, is the diffusion operator that converts the oracle's phase flip into probability.

A certified run

The circuit was submitted at 1,000 shots. Every gate is Clifford, so the router selected the exact stabilizer engine.

counts:     {"11": 1000}
error_info: {"method": "stabilizer (Gottesman-Knill)",
             "truncation_error": 0.0,
             "shot_noise_only": true, "shots": 1000}

All 1,000 shots returned 11. At N = 4 a single iteration is exact, so there is no scatter. The rotation lands precisely on the target. Any deviation from a single peak would indicate an implementation error rather than statistical noise, which makes this a useful correctness test. The run carries a public ZCC-v0.1 certificate.

What the quadratic speedup is actually worth

The practical value of Grover is routinely overstated, and the honest analysis is less favourable than the asymptotic statement suggests.

A quadratic speedup is consumed by constant factors. Quantum gates operate at kilohertz to megahertz rates on current hardware against gigahertz classical clocks, a gap of three to six orders of magnitude before parallelism is considered.

Classical search parallelises perfectly across cores and machines; Grover does not, since the iterations are inherently sequential and running k independent instances buys only a factor of sqrt(k). Fault-tolerant execution adds further overhead, as each logical operation decomposes into many physical ones plus error-correction rounds.

Published analyses generally conclude that Grover-type speedups pay off only for search spaces large enough to amortise these factors, and that for many realistic problem sizes a classical machine finishes first. This is not a reason to dismiss the algorithm; it is a reason to be precise about it.

Doubling key length is a manageable response, and the contrast with Shor's exponential threat to RSA is exactly the difference between quadratic and exponential. The migration is covered in the post-quantum cryptography primer.

Amplitude amplification, the general form

Grover is a special case of amplitude amplification, which applies to any procedure that succeeds with probability p and boosts it to near certainty in O(1/sqrt(p)) repetitions, against O(1/p) classically. This generalisation is the form in which the technique appears inside other algorithms, including quantum counting, collision finding and several optimisation routines, and it is more useful than the search framing.

Running it

Select Grover search in the console and run it. Change the oracle to mark a different target, for instance 01 rather than 11, and confirm the algorithm follows. Then extend to three qubits, where a single iteration is no longer exact, and observe that success probability peaks and then declines with additional iterations.

For the one-query counterpart that uses the same interference mechanism without iteration, see Bernstein-Vazirani. For the broader comparison with Shor's exponential speedup, see Grover and Shor, simulated.

Run your own 100-qubit circuit, with an error bar.

Share this articleLink copied