A comprehensive implementation of the Bernstein-Vazirani algorithm using Qiskit, demonstrating:
- Superposition: Quantum states existing in multiple possibilities simultaneously
- Phase Kickback: Marking secret strings into quantum phase via CNOT gates
- Interference: Converting phase information back into measurable amplitudes
- Scalability: Automatically expanding from 3-bit to 8-bit to handle higher complexity
- Reality Gap: Measuring the difference between ideal (Statevector) and noisy (hardware) simulations
The Bernstein-Vazirani algorithm is a quantum algorithm that solves the following problem:
Problem Statement: Given a black-box function (oracle) f(x) = s Β· x mod 2, where:
sis a hidden n-bit string (the "secret")Β·denotes the bitwise inner product (dot product)- Find
susing as few queries tofas possible
Classical Complexity: Requires n queries (one query per bit)
Quantum Complexity: Requires 1 query using quantum superposition and interference β‘
All five steps are implemented in bernstein_vazirani/circuit.py:
Initialize n query qubits into Superposition using the Hadamard (H) Gate as a catalyst, transforming each qubit from Ground State (|0β©) into all possible states simultaneously.
# Hadamard catalyst: |0β© β (|0β© + |1β©)/β2
for i in range(n):
circuit.h(query_qubits[i])Prepare the Helper Qubit (ancilla) in the |ββ© state using X and H gates. This |ββ© state is the Phase Kickback engine that enables phase-marking.
# Helper Qubit preparation: |0β© β X β H β |ββ© = (|0β© - |1β©)/β2
circuit.x(ancilla_qubit[0])
circuit.h(ancilla_qubit[0])Apply CNOT gates according to Oracle Logic: the specific arrangement that encodes the hidden secret string. The Phase Kickback mechanism marks the secret into quantum phase.
# CNOT: Control qubit flips target if control is |1β©
# Implements the dot product f(x) = sΒ·x (mod 2)
for i in range(n):
if secret_string[i] == '1':
circuit.cx(query_qubits[i], ancilla_qubit[0])Apply Hadamard gates again to the query register. This Interference step converts the phase-encoded secret from quantum phase back into measurable amplitudes.
# Hadamard catalyst again: Extract phase information
for i in range(n):
circuit.h(query_qubits[i])Measure the query qubits to collapse the quantum state into the Ground State (|0β©) or |1β© classical answer. The secret string emerges with high probability.
# Measurement (M) Gate: Collapse to classical answer
circuit.measure(query_qubits, classical_bits)This project demonstrates the Reality Gap between two quantum compute models:
- Statevector Simulation: Theoretical, perfect execution of the quantum program
- Entirely noise-free; represents "ideal math"
- Achieved by
AerSimulator(method='statevector')
- Real quantum hardware errors (CNOT gate errors ~5%)
- Represents what actually happens on IBM Brisbane, IonQ, etc.
- Simulated by
AerSimulator(noise_model=NoiseModel())
The Hellinger Distance is a mathematical metric that quantifies how different two probability distributions are. It measures the Reality Gap.
from qiskit.quantum_info import hellinger_distance
gap = hellinger_distance(ideal_counts, noisy_counts)
# gap < 0.05 β π’ Excellent (< 5% degradation)
# gap < 0.10 β π‘ Good (5-10% degradation)
# gap < 0.20 β π Acceptable (10-20% degradation)
# gap β₯ 0.20 β π΄ Critical (> 20% degradation)Before running on expensive quantum hardware (Blue Path), the Shadow Oracle Validator checks if Hellinger Distance < 0.20. If not, error mitigation is recommended instead.
try:
ShadowOracleValidator.validate_execution(gap)
print("β
APPROVED: Circuit ready for QPU")
except ExecutionAbortedError:
print("β REJECTED: Apply error mitigation first")The algorithm's Scalability automatically expands from small examples to larger problems:
from bernstein_vazirani.circuit import test_scalability
results = test_scalability([3, 4, 8])
# 3-bit example: secret='101'
# 4-bit example: secret='1011'
# 8-bit example: secret='11010111'All examples execute in a single oracle query β‘
pip install -r requirements.txtpython main.pyOutput includes:
- β Ideal simulation results (Statevector)
- β Noisy simulation results (5% CNOT error)
- π Hellinger Distance (Reality Gap)
- π‘οΈ Shadow Oracle validation status
- π Scalability test results for 3-bit, 4-bit, 8-bit
python -m bernstein_vazirani.circuitfrom bernstein_vazirani.circuit import build_bv_circuit, run_statevector_simulation
# Create circuit for secret '101'
circuit, counts = run_statevector_simulation('101')
print(counts) # {'101': 1024} (100% probability)
# Test scalability
from bernstein_vazirani.circuit import test_scalability
results = test_scalability([3, 4, 8])Quantum-Katas/
βββ README.md # This file
βββ LEXICON.md # Quantum computing definitions
βββ requirements.txt # Python dependencies
βββ main.py # Main execution (Reality Gap analysis)
βββ Bernstein_Vazirani_Complete.py # Full integrated implementation
βββ .github/
β βββ copilot-instructions.md # Copilot configuration & definitions
βββ bernstein_vazirani/
β βββ __init__.py
β βββ circuit.py # BV circuit implementation
β βββ telemetry.py # Reality Gap telemetry & monitoring
β βββ test_scalability.py # Scale tests (3-bit to 8-bit)
βββ quanta/
βββ __init__.py
βββ oracle.py # Oracle implementations (Phase Kickback)
| File | Purpose |
|---|---|
| bernstein_vazirani/circuit.py | Full BV algorithm with all 5 steps, Statevector simulation, Scalability testing |
| quanta/oracle.py | Phase Kickback oracle construction using CNOT gates |
| main.py | Reality Gap analysis (ideal vs noisy), Hellinger Distance, Shadow Oracle |
| bernstein_vazirani/telemetry.py | Hellinger Distance tracking, statistics, health status |
| LEXICON.md | Complete reference for quantum computing definitions |
| .github/copilot-instructions.md | Copilot configuration & source of truth |
See LEXICON.md for complete quantum computing reference including:
- β Superposition
- β Hadamard (H) Gate
- β Phase Kickback
- β CNOT (CX) Gate
- β Interference
- β Oracle Logic
- β Helper Qubit
- β Measurement (M) Gate
- β Statevector Simulation
- β Reality Gap
- β Hellinger Distance
- β Ground State (|0β©)
- β Scalability
π REALITY GAP ANALYSIS FOR SECRET '101'
======================================================================
π’ IDEAL (Statevector Simulation - Noise-Free):
Result: 101
Top Probabilities: [('101', 1024)]
π‘ ACTUAL (Noisy Simulation - 5% CNOT Error Rate):
Result: 101
Top Probabilities: [('101', 987), ('100', 22), ('001', 15)]
π HELLINGER DISTANCE (Reality Gap):
Distance: 0.0341
Interpretation: 3.41% degradation from ideal
Health: π’ EXCELLENT (< 5% degradation)
π‘οΈ SHADOW ORACLE VALIDATION:
β
APPROVED: Circuit ready for Blue (QPU) Execution
======================================================================
======================================================================
SCALABILITY TEST: 3-BIT, 4-BIT, 8-BIT BERNSTEIN-VAZIRANI
======================================================================
SCALABILITY RESULTS:
----------------------------------------------------------------------
3-BIT | β SUCCESS | Secret=101 | Result=101
4-BIT | β SUCCESS | Secret=1011 | Result=1011
8-BIT | β SUCCESS | Secret=11010111 | Result=11010111
- Start here: Run
python main.pyto see the full algorithm - Understand circuits: Read bernstein_vazirani/circuit.py comments
- Explore oracles: Review quanta/oracle.py for Phase Kickback details
- Study definitions: Reference LEXICON.md for quantum concepts
- Test scalability: Modify
test_scalability()with your own bit sizes - Measure Reality Gap: See how algorithms degrade with noise in bernstein_vazirani/telemetry.py
- Bernstein & Vazirani (1997) - Original paper
- IBM Qiskit Documentation
- Quantum Computing Basics - Michael Nielsen & Isaac Chuang
- Architecture patterns for quantum algorithms
- All code uses Qiskit 1.0+ with Aer simulator
- Noise model represents 5% CNOT error (typical for NISQ devices)
- Circuit depths scale linearly with secret string length (O(n))
- Measurement takes all queries to classical bits simultaneously
### Run the starter circuit
```bash
python -m bernstein_vazirani.circuit
The script builds a 3-qubit Bernstein-Vazirani circuit for the hidden string s = 101 and prints both the circuit diagram and the measurement counts from a simulator run.
- Bernstein, E. & Vazirani, U. (1997). Quantum Complexity Theory. SIAM Journal on Computing, 26(5), 1411β1473.
- Qiskit documentation