Skip to main content
Grover’s algorithm provides a quadratic speedup for searching an unstructured database of N entries for M marked solutions. While a classical search needs O(N) queries on average, Grover’s algorithm finds a solution in O(√(N/M)) iterations with high probability. This example searches a 4-bit state space representing a 2×2 binary grid, looking for assignments r=[a,b,c,d] where all adjacent cells differ — a constraint-satisfaction problem that has exactly two solutions.

Code

Step-by-step explanation

Gate definitions
  • x and h: Standard NOT and Hadamard gates.
  • cx: CNOT gate used inside the xor gate.
  • xor q0, q1, q2: Computes q2 ⊕= q0 ⊕ q1 using two CNOTs. This is the key building block for evaluating inequality constraints without measurement.
  • cccz: Three-controlled Z gate — flips the phase of |1111⟩ state. Uses ctrl(3) @ syntax.
  • ccccx: Four-controlled X gate — flips the ancilla only when all four scratch bits are 1. Uses ctrl(4) @ syntax.
The oracle The oracle checks four constraints on the search register r = [a, b, c, d]:
Each scratch bit in s is 1 if and only if its pair differs. The ccccx gate then flips the ancilla a only when all four scratch bits are 1 — meaning all four constraints are satisfied simultaneously. The second block of xor gates uncomputes s back to |0000⟩, leaving the ancilla as the only changed qubit. Phase kickback via the ancilla The ancilla a is initialized in |−⟩ = (|0⟩ − |1⟩)/√2 via x a; h a. When ccccx flips a, phase kickback applies a factor of −1 to the amplitude of the marked states in the superposition of r, turning an amplitude-flip into a phase-flip. The ancilla returns to |−⟩ and is unaffected by subsequent iterations. The diffuser (Grover diffusion operator) The diffuser amplifies the probability of marked states by reflecting all amplitudes about their mean:
The net effect is: each amplitude aᵢ → 2⟨a⟩ − aᵢ, where ⟨a⟩ is the mean amplitude. Marked states (with negative phase from the oracle) are amplified; all others are suppressed. Iteration count
Two Grover iterations are optimal for this instance. Applying more iterations would overshoot and reduce the success probability.

Expected output

After 2 iterations, the amplitudes concentrate on the two valid solutions. Measuring r gives: All other 14 states have near-zero probability. The scratch register s and ancilla a return to |0000⟩ and |−⟩ respectively, confirming the oracle correctly uncomputed its intermediate results.
Both solutions [1,0,0,1] and [0,1,1,0] represent valid 2-colorings of a 2×2 grid where no two adjacent cells share the same color. Visually, they are the two checkerboard patterns on a 2×2 board.

Quantum mechanics concepts

Phase oracle — Grover’s algorithm requires a phase oracle: a unitary that maps |x⟩ → −|x⟩ for marked states and leaves all others unchanged. This circuit constructs the phase oracle by combining a computational oracle (that flips an ancilla for marked states) with an ancilla in |−⟩ to convert amplitude flips into phase flips via phase kickback. Grover iteration — Each iteration consists of oracle application followed by the diffusion operator. The rotation angle in state space is arcsin(√(M/N)) ≈ 0.354 radians per iteration for this problem. After R ≈ π/(4·arcsin(√(M/N))) iterations, the quantum state aligns closely with the superposition of marked states. Amplitude amplification — Grover’s algorithm is a special case of amplitude amplification, a general technique for boosting the probability of a “good” outcome in any quantum computation. The diffusion operator reflects amplitudes around their mean, systematically transferring probability from unmarked to marked states. Uncomputation — The oracle must uncompute all scratch qubits (register s) before returning, ensuring they return to |0000⟩. If scratch qubits retained information about which state was evaluated, they would become entangled with the search register and destroy the interference needed for amplitude amplification.