Grover search
April 1, 2025
Grover’s algorithm finds a marked item in an unstructured database of size using oracle queries, classically in the worst case.
It is an oracle-based algorithm: you assume access to a black-box function that can recognize solutions, and you use quantum interference to amplify their probability.
Problem setup (oracle model)
You are given an oracle for a Boolean function over inputs:
- for “good/marked” solutions
- otherwise
In the phase-oracle form, the oracle is a unitary that acts as:
So marked states get a phase flip (a minus sign), while all other basis states stay unchanged.
Oracle and diffusion
You implement an oracle that flips the phase of “good” states, then apply a diffusion operator that reflects amplitudes about the average.
The diffusion operator is often written as:
where is the uniform superposition:
The Grover iterate and iteration count
One Grover iteration (the “Grover operator”) is:
If there are marked items, the optimal number of Grover iterations is approximately:
Minimal example in Qiskit (2 qubits, mark )
Below is a tiny end-to-end circuit that searches among states and marks . For and , a single Grover iteration is enough.
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
def grover_oracle_mark_11(qc: QuantumCircuit):
# Phase flip on |11> using CZ
qc.cz(0, 1)
def diffusion_2q(qc: QuantumCircuit):
# D = H⊗H · (2|00><00| - I) · H⊗H
qc.h([0, 1])
qc.x([0, 1])
qc.h(1)
qc.cx(0, 1)
qc.h(1)
qc.x([0, 1])
qc.h([0, 1])
qc = QuantumCircuit(2)
# 1) Prepare |s>
qc.h([0, 1])
# 2) Oracle
grover_oracle_mark_11(qc)
# 3) Diffusion
diffusion_2q(qc)
sv = Statevector.from_instruction(qc)
print(sv.probabilities_dict())You should see most probability mass concentrated on '11'.
What Grover is really doing (geometric picture)
Grover iterations rotate the state in the 2D subspace spanned by:
- : the uniform superposition over marked states
- : the uniform superposition over unmarked states
Each iteration rotates the state closer to , increasing the chance of measuring a marked item.
Exercises
- Modify the oracle to mark instead of . (Hint: use
Xgates to map the target state to , apply the same phase flip, then undo theXgates.) - Try marking two states (so ) and observe how the “best” iteration count changes.
Next
Learn the general technique behind Grover: Amplitude amplification.
Then continue to Shor’s algorithm.