|q⟩ Bad Qubits

intermediate · Programming · Grover's Search Algorithm

Unstructured Search

Imagine a phone book with NN entries listed in random order. You want to find the one entry for "Alice." Classically the only strategy is to check entries one by one. On average you need N/2N/2 checks; in the worst case, N1N - 1. No classical algorithm — deterministic or randomised — can do better than Ω(N)\Omega(N) queries in the worst case when the list has no structure to exploit.

This is the unstructured search problem: given a black-box function

f:{0,1,,N1}{0,1}f: \{0, 1, \ldots, N-1\} \to \{0, 1\}

that returns 11 for exactly one "marked" input xx^* and 00 for all others, find xx^* using as few evaluations of ff as possible.

Why "unstructured"?

The word unstructured means ff reveals nothing beyond its output. There is no sorted order, no index, no hash table — each item looks the same until you query it. The function ff is therefore modelled as an oracle: a sealed box that answers f(x)f(x) when you hand it xx, but whose internals you cannot inspect.

Classical lower bound

Suppose you have queried kk distinct inputs and all returned 00. The marked item could be any of the remaining NkN - k unchecked inputs with equal probability, so no algorithm can guarantee success in fewer than NN queries in the worst case. A randomised search finds xx^* in O(N)O(N) expected queries. The lower bound is tight: a simple sequential scan achieves O(N)O(N).

The quantum advantage

In 1996 Lov Grover showed that a quantum computer can solve unstructured search in O(N)O(\sqrt{N}) oracle queries, achieving a quadratic speedup over any classical approach. For N=1012N = 10^{12} that means roughly 10610^6 queries instead of 101210^{12} — a million-fold practical difference.

The speedup is provably optimal: Bennett, Bernstein, Brassard, and Vazirani proved in 1997 that any quantum algorithm requires Ω(N)\Omega(\sqrt{N}) queries, so Grover's algorithm is asymptotically tight.

| Strategy | Query complexity | |---|---| | Classical worst-case | Θ(N)\Theta(N) | | Classical average-case (random) | Θ(N)\Theta(N) | | Grover's algorithm | Θ(N)\Theta(\sqrt{N}) |

The key ingredient is amplitude amplification: starting from a uniform superposition over all NN inputs, each Grover iteration steers probability toward xx^* using the oracle and a reflection operator called the diffuser. After π4N\approx \tfrac{\pi}{4}\sqrt{N} iterations the marked state has amplitude close to 11, so a single measurement finds it with high probability.

What comes next

The rest of this module unpacks each component:

  1. The search oracle encodes ff as a phase-kickback gate.
  2. The diffuser reflects amplitudes about their mean.
  3. Putting it together — the full Grover iteration and its geometric interpretation as a sequence of rotations in a two-dimensional plane.
  4. Multi-solution search — handling the case where more than one item is marked.

By the end you will implement Grover's algorithm for small instances directly in the circuit simulator and watch the probability concentrate on the marked item in real time.

Sign in on the full site to ask questions and join the discussion.