A Qiskit simulation proving, on a small scale, the viability of using Deutsch-Jozsa's algorithm as an "Activation Oracle" for neural-network sparsity routing (from the "QML Project Plan" concept).
In ReLU networks, many neurons evaluate to exactly 0 ("dead zones").
Classically, checking whether a whole block of neurons is dead requires
computing Wx + b for every neuron, for every input (2^n queries to be sure).
Deutsch-Jozsa answers the constant-or-balanced question about a binary
function with exactly 1 oracle query. We encode a ReLU block's
activation function f: {0,1}^n → {0,1} as a quantum oracle:
f(x) = 0→ block is dead for inputxf(x) = 1→ at least one neuron fires for inputx
Then:
- Constant (measures
00...0) → block always dead → SKIP the compute - Balanced (measures non-zero) → some inputs activate it → compute
| Inputs | Classical queries (worst) | Quantum queries (Deutsch-Jozsa) | Speedup |
|---|---|---|---|
| 2 | 4 | 1 | 4× |
| 3 | 8 | 1 | 8× |
| 4 | 16 | 1 | 16× |
| 5 | 32 | 1 | 32× |
| 6 | 64 | 1 | 64× |
All benchmark cases were verified correct against the classical truth table.
In the end-to-end sparsity-routing demo, an 8-block layer with 3 "dead" blocks was routed in 8 quantum queries vs 50 classical queries — 84% of queries saved, with all skip/compute decisions matching classical truth.
Under depolarizing noise (0→10%), balanced-block classification confidence degrades smoothly from 100% → 88% while staying correct. Error-correction codes (bit-flip, phase-flip, Shor) are implemented and demonstrated.
qml-simulation/
├── main.py # Full simulation runner (all 6 sections)
├── demo.py # 60-second quick demo
├── requirements.txt
├── venv/ # virtualenv (Python 3.14, Qiskit 2.5.2)
├── src/
│ ├── relu_oracle.py # ReLU → quantum oracle, DJ circuits, interpretation
│ ├── benchmark.py # classical vs quantum comparison suite
│ ├── error_correction.py # bit-flip / phase-flip / Shor codes + noise models
│ ├── sparsity_router.py # end-to-end layer routing simulation
│ └── visualization.py # circuit prints, heatmaps, scaling plots
├── tests/ # (add pytest tests here)
└── results/ # generated plots & circuit diagrams
cd qml-simulation
source venv/bin/activate # venv already has Qiskit 2.5.2 installed
python demo.py # 60-second demo
python main.py --full --noise # everything + scalability + noise + routing
python main.py --quick # oracles + DJ only (~3s)relu_oracle.py— encodes a weight/bias ReLU block as an oracle; builds DJ circuits; interprets measurement counts into SKIP/COMPUTE verdicts.error_correction.py— 3-qubit bit-flip, 3-qubit phase-flip, 9-qubit Shor codes with syndrome decoding and a real Aer depolarizing noise model.benchmark.py— query-complexity comparison; scalability and noise sweeps.sparsity_router.py— constructs a mock layer (mix of dead/alive blocks), routes it quantumly vs classically, and reports compute savings.visualization.py— saves PNGs: circuit diagrams, activation heatmaps, scalability, circuit depth, noise resilience.
- DJ's guarantee is constant-vs-balanced. Real ReLU blocks produce arbitrary Boolean functions, so the single-query classification only cleanly applies to exactly-constant or exactly-balanced blocks. For arbitrary blocks, measurement zero-rate is an imbalance estimator, and the router uses it conservatively (never skips unless near-certain).
- Simulated oracle pre-computes the activation table classically — the oracle construction itself is not free. The speedup claim here is about query complexity of the routing decision, not wall-clock on a simulator.
- Real hardware would need oracle compilation + QEC overhead costs.
Negative result. Deutsch-Jozsa is not viable for transformer-head sparsity routing, because the constant/balanced promise is generically violated by real attention/feed-forward functions:
- The promise holds only in three measure-zero cases out of all
Boolean functions on
nbits: constant-0, constant-1 (k = 0ork = 2^n), or exactly balanced (k = 2^(n-1)), wherek= number of inputs that make the block fire. - With
k"variations" violating the promise, detecting the imbalance costs the same either way (verified by Monte Carlo, exact to 1.000x):- classical sampling without replacement: expected (2^n + 1) / (k + 1) inputs
- quantum DJ zero-rate estimation: ~2.25 · 2^n / k shots (3-sigma) → the exponential advantage is entirely borrowed from the promise.
- Exactly-constant dead heads do exist after pruning, but they are trivially detectable classically — running DJ on them is circular.
- Promise-free quantum fallbacks (Grover witness search
O(sqrt(2^n/k)), quantum countingO(sqrt(2^n))for the fire countk) keep only a quadratic, not exponential, advantage — and still must pay oracle-encoding overhead that exceeds a classical GPU pass on real transformer activations (which are continuous, not Boolean — binarizing injects oracle error).
Net takeaway: this repo proves the mechanism at small scale (2–6 qubits, 4x–64x query reduction, all verdicts classically verified), and the boundary at which it stops applying. Use the included scripts to re-derive both halves of the result.
qiskit>=2.5, qiskit-aer>=0.17, numpy, matplotlib, pylatexenc