--- license: mit library_name: pytorch tags: - photonic-computing - verified-units - neural-cpu - mach-zehnder - optical-neural-network - physics --- # Neural Photonic — verified optical compute units Neural networks that **emulate** the operations of a **hybrid photonic computer**, built to the same *exhaustive (N/N) verification* discipline as [Quazim0t0/neural-aarch64-units](https://huggingface.co/Quazim0t0/neural-aarch64-units): every unit is a small neural net trained until it is **bit-identical to a golden reference over its entire finite input domain**. Light does the linear algebra by interference; a neural CPU does everything passive light cannot (gain, bias, nonlinearity, integer readout). ### v2 — hardware non-ideality, robustness & calibration Exhaustive (N/N) verification proves the **discrete, quantized logic** exactly and fabrication-independently. Real **analog** photonic hardware is a regime that cannot be exhaustively verified, so correctness there becomes a **bounded-error / yield** claim. `photonic/nonideal.py` + `robustness_demo.py` model it: ``` process fidelity (thermal drift): sigma=0.02 -> 0.997 (100% yield); sigma=0.10 -> 0.93 (0% yield) two-moons classifier: 99% on idealized HW -> 61% on real (noisy) HW noise-aware training: recovers to 98% on real (noisy) HW per-device calibration: fabrication fidelity 0.90 -> 1.0000; thermal floor 0.983 ``` Non-idealities modeled: coupler split error + static phase offsets (fabrication), thermal phase drift, detector responsivity + saturation, ADC gain/offset/ quantization/noise. **Calibration** tunes each device's control phases against its *fixed* fab errors, recovering the deterministic loss to ~unity; thermal drift and ADC noise are the irreducible floor. Run `python robustness_demo.py`. The interactive version is in the companion Space (`Quazim0t0/neural-photonic-hybrid`, "Hardware reality" section). *Exact where it can be proven (digital); error-bounded where it can't (analog); deterministic error is calibratable, random drift is the floor.* ### Simulate vs. emulate — what these models are - The **physics is simulated**: `photonic/mzi.py`, `linear.py`, and `prism.py` model how light actually behaves — complex fields (amplitude + phase), interference, Snell/Sellmeier dispersion. This produces the *golden reference*. - The **models are emulators**: `MZI2.pt` and `PD.pt` are **bit-exact functional replicas** of quantized photonic operations — they reproduce the reference's output exactly (verified N/N), they do not re-derive the physics. This is the same sense in which a CPU emulator reproduces a datapath: exactly the framing of the companion neural-aarch64 units. - The **whole assembled pipeline** running on a GPU *simulates* a photonic computer — it models one, it does not physically become one. This repository packages the **trained models (emulators)** and the **training + verification process**. The interactive 3D demo lives in the companion Space. --- ## The models | file | unit | maps | domain | verification | |------|------|------|--------|--------------| | `MZI2.pt` | **Neural Photonic MZI** | phase settings (4b θ, 4b φ) → quantized 2×2 interferometer matrix | 2⁸ = 256 | **256 / 256 bit-exact** | | `PD.pt` | **Neural Photodetector** | complex field (5b re, 5b im) → intensity byte (learned \|·\|²) | 2¹⁰ = 1024 | **1024 / 1024 bit-exact** | | `cpu_units/ADC8.pt` | **8-bit adder** *(from neural-aarch64-units, included for the hybrid)* | a, b, carry → sum, flags | 2¹⁷ | N/N (see source repo) | `MZI2.pt` and `PD.pt` are the two models trained here. `ADC8.pt` is reproduced from the author's [neural-aarch64-units](https://huggingface.co/Quazim0t0/neural-aarch64-units) so the end-to-end hybrid is runnable in one place; all credit for it is there. --- ## What the compute looks like Forward light-transport renders of optical energy moving through the mesh — produced by `step6_demo.py` (`photonic/visualize.py`), visualization only and firewalled from the compute path. ![Optical energy through the MZI mesh](core_energy.svg) *Energy of a single injected beam as it propagates through the interferometer mesh. Each column is a mesh layer (light flows left→right), each row a waveguide mode; brightness is optical intensity. One input mode fans out across many — this spreading **is** interference performing a matrix multiply, the thing ray optics cannot represent.* ![Optical core of the hybrid net](hybrid_core_energy.svg) *The same forward-transport view for the optical core inside the trained hybrid network — the light path that feeds the neural photodetector and, in turn, the neural-CPU readout.* --- ## Why this exists — the idea Photonic computing performs a matrix multiply with **light**: a mesh of Mach-Zehnder interferometers (MZIs) makes beams **interfere**, and the interference *is* the arithmetic. The catch is that the computation lives in the **phase and amplitude of a coherent field** — something geometric ray optics cannot represent. So the core of this project is an honest, complex-valued (wave-optics) model of an MZI mesh, then a path to make it **verifiable** the way a digital datapath is. The bridge from "continuous physics" to "verifiable unit" is **quantization**: discretize the phase/amplitude controls to a finite grid, and the input domain becomes finite and enumerable — the precondition for exhaustive N/N verification. --- ## How the units were built (the 6-step process) Each step ships as a runnable script with its own acceptance tests. | step | file | what it establishes | result | |------|------|---------------------|--------| | 1 | `step1_demo.py`, `photonic/mzi.py` | MZI mesh: matrix-vector multiply by **coherent interference** (unitary, energy-conserving, differentiable) | 4/4 | | 2 | `step2_demo.py`, `photonic/linear.py` | **Arbitrary** linear map via SVD mesh `U Σ Vᴴ`; passive optics realize contractions (‖M‖₂ ≤ 1) | 4/4 | | 3 | `step3_demo.py`, `photonic/quantize.py` | **Quantize** phases/amps → finite domain; phase quantization preserves unitarity exactly | 5/5 | | 4 | `step4_train_verify.py`, `photonic/unit.py` | Train + **exhaustively verify** `MZI2.pt` | **256/256** | | 5 | `step5_demo.py`, `photonic/hybrid.py`, `photonic/cpu_bridge.py` | Hybrid net: optical core + **real ADC8** electronic half; 99% on two-moons | 3/3 | | 6 | `step6_demo.py`, `photonic/visualize.py` | Forward-transport visualization, **firewalled** from compute | 3/3 | The photodetector unit was added afterwards: | file | what it does | result | |------|--------------|--------| | `train_detector.py`, `photonic/detector.py` | Train + exhaustively verify `PD.pt` (a learned \|·\|²) | **1024/1024** | `bench.py` measures speed (see below). `photonic/prism.py` is the forward spectral ray-trace used for the dispersion visual. ### Reproduce ```bash pip install torch numpy python step1_demo.py # ... through step6_demo.py python step4_train_verify.py # retrains + verifies MZI2.pt (256/256) python train_detector.py # retrains + verifies PD.pt (1024/1024) python bench.py # speed: sim vs frozen-deploy vs projection ``` --- ## Using the models ```python import torch from photonic.unit import NeuralPhotonicMZI, bits_of from photonic.detector import NeuralPhotodetector mzi = NeuralPhotonicMZI(); mzi.load_state_dict(torch.load("MZI2.pt")["state_dict"]); mzi.eval() pd = NeuralPhotodetector(); pd.load_state_dict(torch.load("PD.pt")["state_dict"]); pd.eval() # MZI2: phase indices (theta=3, phi=10) -> quantized 2x2 interferometer matrix block = mzi.forward_int(bits_of(3, 10).unsqueeze(0)) # int8 components # PD: detect a complex field vector -> intensity bytes (learned |.|^2) field = torch.tensor([0.7+0.2j, 0.1-0.3j], dtype=torch.complex64) bytes_out = pd.detect(field) ``` The hybrid pipeline (`MZI2 → PD → ADC8`) is assembled in `photonic/hybrid.py` and demonstrated end-to-end in `step5_demo.py`. --- ## Speed — what's real `bench.py`, measured on CUDA: | N | ① SIM (rebuild) | ② DEPLOY (frozen) | ③ DIGITAL | |---|---|---|---| | 64 | 2283 ms | **0.102 ms** | 0.101 ms | - **This is a simulation on a GPU** — simulating interference is *more* work than the matmul it produces, so the sim can never be faster than digital. The "sim" column is that overhead (3,400×–22,000× depending on N). - Once trained, **freeze the mesh to its matrix** and it runs at full GPU matmul speed — identical to digital (column ②). This is the shippable path. - **Projected hardware** (a model, *not* measured): a 64×64 MZI mesh does all N² MACs in one optical pass — ~179 ps latency, ~205 T-MAC/s, energy scaling with O(N) modulators rather than O(N²) MACs. The real speedup exists only on silicon photonics. --- ## Honesty (the recurring theme) This project is deliberately built so a nice-looking result can never stand in for a correct one: - **Interference is the compute** — modeled with complex fields (phase + amplitude), which ray optics cannot represent. - **Passive light only attenuates** — any gain > 1 comes from the electronic half (the neural CPU), never from passive optics. Enforced in Steps 3/5. - **The metric measures the invariant** — unitarity caught a bug in Step 1; the N/N curve caught training drift in Step 4; the firewall keeps Step 6's render from ever feeding back into a result. - **Analog ≠ exact** — the optical core is analog; N/N verification is only possible *after* quantization. `MZI2` and `PD` are verified on their quantized, finite domains, at finite resolution (4-bit phases, 5-bit field components). --- ## Files ``` MZI2.pt, PD.pt trained + verified models cpu_units/ADC8.pt adder from neural-aarch64-units (for the hybrid) photonic/ mzi, linear, quantize, unit, detector, hybrid, cpu_bridge, visualize, prism step1..6_demo.py the build, with acceptance tests step4_train_verify.py train + verify MZI2 (256/256) train_detector.py train + verify PD (1024/1024) bench.py speed benchmark core_energy.svg forward-transport render: energy per mode/layer hybrid_core_energy.svg same, for the hybrid net's optical core ``` The two SVGs are produced by `step6_demo.py` (`photonic/visualize.py`) — a **forward light-transport** render of optical energy flowing through the mesh, layer by layer. They are visualization only, firewalled from the compute path. **Companion Space:** `Quazim0t0/neural-photonic-hybrid` — live 3D visualization of light → detector → CPU → output. **Related:** [neural-aarch64-units](https://huggingface.co/Quazim0t0/neural-aarch64-units), [neural-raytracing](https://huggingface.co/Quazim0t0/neural-raytracing). --- ## Citation ```bibtex @misc{byrne2026neuralphotonic, title = {Neural Photonic: Verified Optical Compute Units}, author = {Byrne, Dean (Quazim0t0)}, year = {2026}, howpublished = {\url{https://huggingface.co/Quazim0t0/neural-photonic}}, note = {Neural networks that emulate photonic-computer operations, exhaustively (N/N) verified} } ``` **Dean Byrne (Quazim0t0)** · 2026