You are viewing version 2 of this entry. View latest (v4)
Code
qcr:2604.95151.2

Grover's Search Algorithm in a 5-qubit space.

Implementation of Grover's search algorithm, the optimal quantum algorithm for unstructured search. Given an unsorted database of N items, it finds a marked item in O(sqrt(N)) queries, a provably optimal quadratic speedup over classical search. The algorithm repeatedly applies a Grover iterate consisting of an oracle (which marks target states with a phase flip via multi-controlled Z gates) and a diffuser (which reflects amplitudes about their mean). After approximately pi/4 * sqrt(N/M) iterations (M = number of targets), the targets have near-unit measurement probability. This implementation supports multiple simultaneous search targets, includes a full CLI for experimentation (configurable qubits, targets, shots), and displays an interactive matplotlib histogram distinguishing target hits (green) from non-targets (red). Default: searching for {0, 3, 9, 11} in a 5-qubit (N=32) space.
Optimization
Qubit
Circuit-based
Uploaded 2 months ago
300
Views
GitHub4
Citing this entry? Use this QCR ID
Uploaded by
HQ
Hello QCR

Overview

Assembly

1 available
QCRepository/qcr-circuit-examples
40
README.md

Grover's Search Algorithm

Grover's algorithm is arguably the most famous quantum algorithm after Shor's. It solves the unstructured search problem, finding a marked item in an unsorted database of N elements in O(√N) queries, a provably optimal quadratic speedup over any classical algorithm, which needs O(N) queries in the worst case.

Beyond database search, Grover's algorithm serves as a universal subroutine: any problem that can be framed as "find an input satisfying a given condition" can be accelerated quadratically. This includes constraint satisfaction, optimization, cryptographic key search, and more.

A note on "database": Grover doesn't search a literal stored database. The "database" is implicit in an oracle — a black-box function that, when queried, says whether a given input is a target. In real applications the oracle encodes some hard-to-invert relation (e.g. "does this candidate solve this SAT instance?"). In this example, the oracle is constructed from the known target set for demonstration purposes.

How it works

The algorithm repeatedly applies two operations — the Grover iterate — to amplify the probability of measuring the target state(s):

  1. Oracle — marks the target states by flipping their phase: |t⟩ → −|t⟩ for each target t, leaving all other states unchanged. Implemented here via X gates (to make each target look like |1…1⟩), a multi-controlled Z gate, then the X gates again.

  2. Diffuser (amplitude amplification) — reflects all amplitudes about their mean. Concretely: Hadamard all qubits, apply the oracle for the all-zeros state, Hadamard again. This "pushes" amplitude from non-target states toward target states.

After approximately (π/4)·√(N/M) iterations (where M is the number of target states), the targets have near-unit probability. Any more or fewer and the probability drops — the amplitude is literally rotating around a 2D subspace and overshooting reverses the amplification.

What the example does

This implementation supports multiple simultaneous search targets and includes a full CLI for experimentation. By default it searches for items {0, 3, 9, 11} in a 5-qubit (N = 32) space, running 1000 shots with 2 Grover iterations (the integer optimum for these parameters).

The output reports problem setup, the theoretical hit rate sin²((2k+1)θ) where θ = arcsin(√(M/N)), the measured hit rate, and a per-target frequency table so you can verify the distribution matches theory at a glance.

Circuit structure

At the top level, Grover is a fixed repeating pattern: initial Hadamards, then the Grover iterate (oracle + diffuser) repeated k times, then measurement.

Grover circuit

Expected results

With the default parameters, the 4 target states {0, 3, 9, 11} should dominate the measurement distribution — each appearing with roughly 24% probability, and the remaining ~5.5% spread thinly across the 28 non-target states:

Grover results histogram

The theoretical hit rate is sin²(5θ) ≈ 94.5% with θ = arcsin(√(1/8)), matching the measured rate to within sampling noise at 1000 shots.

Getting started

python -m venv .venv && source .venv/bin/activate
pip install -r requirements.lock
python grover.py
CLI options
python grover.py -n 4 -s 5 7 -S 2000 -c
Flag Description Default
-n Number of qubits 5
-s Target integers to search for 11 9 0 3
-S Number of simulation shots 1000
-p / --no-p Print circuit diagrams off
-c / --no-c Combine non-target bars into "Others" off
-f Histogram font size 10

Pre-exported circuit

assembly/openqasm3/grover_circuit.qasm contains the default 5-qubit, 2-iteration circuit transpiled into a standard basis gate set (u3, cx, measure) and exported as OpenQASM 3.0 — useful as a reference circuit for hardware experimentation or interop without rerunning Python.

Dependencies

  • Python 3.12
  • Qiskit (< 2.0)
  • Qiskit Aer
  • NumPy, Matplotlib

References

License

Apache 2.0 — see LICENSE.

Join the Discussion

Comments (0)

No comments yet. Be the first to share your thoughts!