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: 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).
Simulate vs. emulate β what these models are
- The physics is simulated:
photonic/mzi.py,linear.py, andprism.pymodel how light actually behaves β complex fields (amplitude + phase), interference, Snell/Sellmeier dispersion. This produces the golden reference. - The models are emulators:
MZI2.ptandPD.ptare 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
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.
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.
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
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
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.
MZI2andPDare 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,
neural-raytracing.
Citation
@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