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.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/qasm-playground/llms.txt
Use this file to discover all available pages before exploring further.
Code
Step-by-step explanation
Gate definitionsxandh: Standard NOT and Hadamard gates.cx: CNOT gate used inside thexorgate.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. Usesctrl(3) @syntax.ccccx: Four-controlled X gate — flips the ancilla only when all four scratch bits are 1. Usesctrl(4) @syntax.
r = [a, b, c, d]:
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:
Expected output
After 2 iterations, the amplitudes concentrate on the two valid solutions. Measuringr gives:
| Outcome (r[3]r[2]r[1]r[0]) | Meaning | Probability |
|---|---|---|
1001 | [1,0,0,1] | ~50% |
0110 | [0,1,1,0] | ~50% |
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 (registers) 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.