Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

QML Simulation — Quantum-Inspired ReLU Forward Pass

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).

The Idea

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 input x
  • f(x) = 1 → at least one neuron fires for input x

Then:

  • Constant (measures 00...0) → block always dead → SKIP the compute
  • Balanced (measures non-zero) → some inputs activate it → compute

Results (small-scale proof)

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.

Noise resilience

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.

Project Structure

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

Quick Start

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)

Modules

  • 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.

Honest Limitations (read before extending)

  • 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.

Research Conclusion — no transfer to transformer heads

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 n bits: constant-0, constant-1 (k = 0 or k = 2^n), or exactly balanced (k = 2^(n-1)), where k = 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 counting O(sqrt(2^n)) for the fire count k) 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.

Requirements

qiskit>=2.5, qiskit-aer>=0.17, numpy, matplotlib, pylatexenc

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages