Scalable, Noise-Aware Implementation
A production-grade, scalable implementation of Grover's quantum search algorithm with dynamic oracle synthesis, noise simulation, and amplitude amplification visualization.
> python main.py --qubits 4 --search 1101 --noise --save-plot outputs/histogram.pngGrover's algorithm searches an unsorted database of
- Dynamic oracle - specify any binary marked state; the circuit adapts
-
Scalable diffuser - generalized inversion-about-the-mean for
$n$ qubits -
Optimal iteration calculator - automatically computes
$R \approx \frac{\pi}{4}\sqrt{\frac{2^n}{M}}$ - Noise simulation - depolarizing gate errors + readout errors on a realistic backend
-
Multi-state search - search for
$M$ marked states simultaneously via--search "001,110" - Circuit visualization - text diagrams, probability histograms, scalability benchmarks
git clone https://github.com/mahmoud-a-shalaby/Grover-s-Algorithm.git
cd Grover-s-Algorithm
pip install -r requirements.txt# 4-qubit search for |1101> on ideal simulator
python main.py --qubits 4 --search 1101
# Same search with noise simulation
python main.py --qubits 4 --search 1101 --noise
# Save the histogram plot and circuit diagram (PNG via matplotlib)
python main.py --qubits 5 --search 10101 --save-plot outputs/histogram.png --save-circuit outputs/circuit.png
# Multi-state search: find |000> and |111> simultaneously
python main.py --qubits 3 --search "000,111"jupyter notebook notebooks/demonstration.ipynbpytest tests/ -vpython scripts/benchmark.py📦 Grover-s-Algorithm/
│
├── 📂 src/ # Core implementation
│ ├── 📄 oracle.py # Dynamic phase oracle (any n, any marked state)
│ ├── 📄 diffuser.py # Generalized diffusion operator (n qubits)
│ └── 📄 pipeline.py # Build circuit, compute iterations, run sims
│
├── 📂 tests/
│ └── 🧪 test_circuits.py # pytest: oracle, diffuser, full pipeline
│
├── 📂 scripts/
│ └── 📈 benchmark.py # Scalability analysis (depth, gates, time vs n)
│
├── 📂 notebooks/
│ └── 📓 demonstration.ipynb # Interactive walkthrough
│
├── 📂 .github/workflows/
│ └── ⚙️ pytest.yml # CI/CD: lint + test on every push
│
├── 🚀 main.py # CLI entry point
├── 📄 requirements.txt # Single-command install
├── 🛠️ pyproject.toml # Package metadata & build config
├── 🙈 .gitignore # venv, cache, outputs, IDE
└── 📖 README.md
The oracle marks the target state
Implementation: for each qubit where
The diffusion operator inverts amplitudes about their mean:
After each oracle-diffuser pair, the amplitude of
Let
For
Where
The code uses the exact formula because the approximation can be off by 1 for small systems. For example,
The algorithm iterates the state vector through a 2D subspace spanned by
Measurement Results (Ideal Simulator):
State Probability
-------- ------------
101 94.75% <- marked
100 0.81%
110 0.79%
001 0.74%
010 0.74% n=3, searching |101>
Iters=0 -> p=13.2% (expected 12.5%, uniform)
Iters=1 -> p=77.6% (expected 78.1%)
Iters=2 -> p=94.8% <- optimal (expected 94.5%)
Iters=3 -> p=33.0% (expected 33.0%, overshoot)
Iters=4 -> p=1.3% (expected 1.2%)| Metric | Ideal | Noisy (1% 1q, 1% 2q, 2% readout) |
|---|---|---|
| Marked state prob | 94.5% | 71.2% |
| Correct ranking | ✅ | ✅ |
| Circuit depth | 14 | 14 |
| Component | Status |
|---|---|
| Dynamic Oracle | ✅ |
| Scalable Diffuser | ✅ |
| Multi-State Search (M marked states) | ✅ |
| Optimal Iteration Calculator | ✅ |
| Ideal Simulation | ✅ |
| Noise Simulation (depolarizing + readout) | ✅ |
| CLI with argparse | ✅ |
| Probability Histograms | ✅ |
| Circuit Diagram Export (PNG + text) | ✅ |
| Unit Tests (pytest) | ✅ |
| CI/CD (GitHub Actions) | ✅ |
| Scalability Benchmark | ✅ |
| Jupyter Notebook | ✅ |
MIT