We are given oracle access to a function which marks exactly one item: for a single unknown , and otherwise. Classically, finding requires queries where . How fast can a quantum computer find ?
- A quantum state can hold all candidates in superposition — but measuring the uniform superposition only finds with probability .
- The oracle can flip the phase of the marked item: .
- A phase is invisible to measurement. We need some way to convert phase information into amplitude information.
- Repeating something that "nudges" amplitude toward might compound.
IdeaPhase flip + inversion about the meanAC
What unitary operation converts a phase difference into an amplitude difference?
Start with the uniform superposition . After the oracle, the state is the same except the amplitude on is negative. The mean amplitude is now slightly below . If we reflect every amplitude about the mean, the negative amplitude on lands far above the mean — roughly — while every other amplitude shrinks slightly.
The reflection about the mean is the operator
and one Grover iteration is .
The state always remains in the 2-dimensional plane spanned by and , and each Grover iteration is a rotation in that plane by angle , where .
Both reflections — the oracle (a reflection about the hyperplane orthogonal to ) and (a reflection about ) — preserve the plane . The composition of two reflections is a rotation by twice the angle between the reflection axes, which is .
After iterations the state makes angle with the axis orthogonal to . Choosing brings the state within angle of , so measurement finds with probability .
Grover's algorithm is not "trying everything in parallel" — it is a slow rotation from toward , driven by two reflections. The arises because each iteration rotates by a fixed angle . Over-rotating past decreases the success probability — running the loop longer makes the answer worse.
Implementation
from qiskit import QuantumCircuit
from qiskit.circuit.library import GroverOperator, MCMTGate, ZGate
import math
n = 5 # qubits; N = 32
marked = "10110" # the unknown w (for the demo oracle)
oracle = QuantumCircuit(n)
for i, bit in enumerate(reversed(marked)):
if bit == "0":
oracle.x(i)
oracle.append(MCMTGate(ZGate(), n - 1, 1), range(n))
for i, bit in enumerate(reversed(marked)):
if bit == "0":
oracle.x(i)
grover_op = GroverOperator(oracle)
qc = QuantumCircuit(n)
qc.h(range(n)) # |s>
iterations = math.floor(math.pi / 4 * math.sqrt(2**n))
for _ in range(iterations):
qc.compose(grover_op, inplace=True) # G = D * O
qc.measure_all()Review
The reconstruction hinged on three moves: (1) realizing a phase flip alone is useless without a second operation, (2) asking what reflection geometry does to a single outlier amplitude, and (3) recognizing two reflections compose into a rotation — at which point the stopping rule stops being magic and becomes trigonometry.