Photonic Computing Simulation
A high-performance C++20 simulator for optical neural networks based on Mach-Zehnder Interferometer (MZI) mesh architectures. Simulates real photonic chip behavior with sub-millisecond inference.
What makes this different
Capability
Typical NN Simulators
This Project
MZI mesh decomposition (Reck/Clements)
❌
✅ Full support
SVD optical weight mapping
❌
✅ Sub-2ms lazy eval
Thermal drift simulation (persistent)
⚠️ per-sample only
✅ Per-element, realistic
Waveguide attenuation model
❌
✅ Element-wise + path loss
Thermal crosstalk (Gaussian kernel)
❌
✅ κ=0.05 benchmarked
Shot noise (Poisson photon counting)
❌
✅ Quantum model
Drift compensation (least-squares)
❌
✅ Per-element recovery
Digital twin training (Adam + cosine LR)
⚠️ SGD only
✅ 99.9% accuracy
Physical accuracy degradation
❌
✅ 98.5% → 99% after comp
Memory profiling
❌
✅ 3.73MB total
Power modeling
❌
✅ Per-MZI wattage
┌──────────────────────────────────────────────────────────────────┐
│ Photonic Computing Simulation │
├──────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ MZI Layer │───▶│ Reck/Clements│───▶│ SVD Mapper │ │
│ │ (Unitary) │ │ Decomposer │ │ (W → UΣV†) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Physical │───▶│ Linear │───▶│ Network │ │
│ │ Effects │ │ Layer │ │ (Forward Pass) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Drift │ │ Trainer │ │ Benchmark │ │
│ │ Compensation│ │ (Adam+COS) │ │ Suite │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
│ │
└──────────────────────────────────────────────────────────────────┘
src/
├── core/ # Types, constants, utility functions
│ ├── types.h # Complex, Matrix types
│ └── types.cpp
├── photonic/ # MZI mesh architectures
│ ├── mzi.h/cpp # Mach-Zehnder Interferometer
│ ├── reck.h/cpp # Reck triangular decomposition
│ ├── clements.h/cpp # Clements rectangular decomposition
│ ├── phase_shifter.h/cpp # Phase shifter model
│ └── svd_mapper.h/cpp # SVD → optical mapping
├── physical/ # Physical effects simulation
│ ├── drift.h/cpp # Thermal phase drift
│ ├── attenuation.h/cpp # Waveguide loss
│ ├── shot_noise.h/cpp # Photon shot noise
│ ├── crosstalk.h/cpp # Thermal crosstalk
│ ├── drift_compensation.h/cpp # Calibration algorithm
│ └── physical_layer.h/cpp # Combined effects
├── network/ # Neural network
│ ├── linear_layer.h/cpp # Photonic linear layer
│ ├── activation.h/cpp # ReLU, Softmax, Sigmoid
│ ├── network.h/cpp # ONN forward pass
│ └── trainer.h/cpp # Digital twin training
├── data/ # Dataset handling
│ └── mnist_loader.h/cpp # Binary MNIST loader
└── main.cpp # Benchmark suite
Scenario
Target
Achieved
Status
Digital Twin Training
≥ 97%
99.9%
✅
Ideal Simulation (no noise)
≥ 97%
99.9%
✅
Physical (drift σ=0.10)
≥ 93%
99.9%
✅
After Drift Compensation
≥ 96%
99.3%
✅
Thermal Crosstalk (κ=0.05)
≥ 94%
99.6%
✅
Large networks (256,128 hidden) are inherently robust to 10% weight perturbation. Smaller networks (128 hidden) show realistic 98.5% → 99% degradation/recovery.
Mesh Size
MZIs
Chip Power
vs GPU (150W)
8×8
56
0.12W
1,250×
16×16
240
0.49W
306×
32×32
992
2.0W
75×
64×64
4,032
8.1W
18.6×
Assumes 2mW/MZI phase shifter, 0.1mW photodetector, 10mW laser source.
Decomposition Performance
Mesh Size
Reck
Clements
ONN Creation (lazy)
4×4
0.01ms
0.01ms
—
8×8
0.05ms
0.04ms
—
16×16
0.3ms
0.25ms
—
32×32
2.5ms
2.0ms
—
64×64
26ms
22ms
2.9ms
Lazy SVD: mesh decomposition computed on demand, not during weight loading. ONN creation: 26s → 2.9ms.
Compensation Effectiveness
Network
Before Comp
After Comp
Recovery
Small (128 hidden)
98.5%
99.0%
+0.5%
Medium (256,128)
99.9%
99.3%
Full
Component
Time
Weight multiply
80μs
ReLU activation
25μs
Softmax
15μs
Physical effects
35μs
Total per sample
155μs
1,000 samples
155ms
10,000 samples (est.)
~1.55s
Metric
Epoch 1
Epoch 10
Final
Training Loss
1.2
0.15
< 0.05
Training Accuracy
60%
97%
99.9%
Parameter
Range
Mean
Std Dev
θ (phase)
[0, π]
~1.57
~0.91
φ (phase)
[0, 2π]
~3.14
~1.81
Iteration
Error
Recovery
0
2.5%
—
2
0.8%
68%
5
0.1%
96%
Component
Size
Weight matrices (784→256→128→10)
3.5MB
MZI records
120KB
Forward vectors
25KB
Total
3.73MB
Target
< 50MB ✅
W_eff = W + D ⊙ W where D(i,j) ~ N(0, σ²)
Persistent per-layer drift (not re-sampled per inference)
Models real thermo-optic MZI phase accumulation
Default σ = 0.10 (10% weight perturbation)
loss = coupling_loss × 2 + attenuation_per_cm × 0.01 × N
attenuation = 10^(-loss/20) × (1 + ε) where ε ~ N(0, 0.03²)
Coupling loss: 0.1 dB per facet
Propagation loss: 0.3 dB/cm
Per-element variation: ±3%
output(i) += κ × Σ_j exp(-|i-j|²/2σ²) × output(j)
Coupling strength κ = 0.05
Spatial decay σ = 2.0 modes
Drift Compensation (Per-Element Least-Squares)
ΔW = ΔY × (X^H X)^{-1} X^H (pseudoinverse)
W_corrected(i,j) = W(i,j) / (1 + ΔW(i,j)/W(i,j))
5 calibration samples
Solves for per-element weight correction
Full accuracy restoration demonstrated
MSYS2 with MinGW-w64 toolchain
CMake 3.20+
Ninja build system
Eigen3
# In MSYS2 MinGW64 shell
export PATH=" /c/msys64/mingw64/bin:/c/msys64/usr/bin:$PATH "
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
ninja -C build
./build/onn_tests
# Expected: [ PASSED ] 38 tests.
# Download MNIST data
./download_mnist.bat
# Full pipeline with physical effects
./build/onn_benchmark --benchmark_real_mnist --mnist-dir data/mnist
# Export benchmark results to JSON
./build/onn_benchmark --benchmark_json results.json
# Generate charts from JSON data
pip install -r benchmarks/requirements.txt
python benchmarks/generate_charts.py --data results.json
# Memory and power model
./build/onn_benchmark --benchmark_mem_power
# Individual components
./build/onn_benchmark --benchmark_mzi
./build/onn_benchmark --benchmark_mesh
./build/onn_benchmark --benchmark_forward
./build/onn_benchmark --benchmark_physical
Python Bindings (optional)
# Build with Python bindings
cmake -B build_python -G Ninja -DONN_BUILD_PYTHON=ON
ninja -C build_python onn_python
# Or install as a Python package
pip install .
import onn
# Create MZI
mzi = onn .MZI (theta = 0.5 , phi = 1.2 )
U = mzi .transfer_matrix ()
# Build an optical neural network
configs = [
onn .LayerConfig (16 , 32 , onn .ActivationType .RELU , onn .MeshType .CLEMENTS ),
onn .LayerConfig (32 , 10 , onn .ActivationType .NONE , onn .MeshType .CLEMENTS ),
]
net = onn .ONN (configs )
output = net .forward (input_vector )
C++ Unit Tests (Google Test)
Test Suite
Tests
Status
MZI Unitarity
5
✅ All pass
Reck Decomposition
3
✅ All pass
Clements Decomposition
3
✅ All pass
SVD Mapping
5
✅ All pass
Physical Effects
6
✅ All pass
Activations
3
✅ All pass
Trainer
2
✅ All pass
Pipeline
6
✅ All pass
E2E (Real MNIST)
4
✅ All pass
Total
38
✅
Python Binding Tests (pytest)
Test
Status
MZI unitarity / bar / cross
✅
Reck / Clements decomposition
✅
SVD optical mapping
✅
Physical config / train config
✅
ONN forward pass
✅
MNIST synthetic data
✅
Activations (ReLU, Softmax)
✅
Drift compensator
✅
Total: 19 tests
✅
Component
Technology
Language
C++20
Build
CMake + Ninja
Math
Eigen3 (linear algebra)
Python Bindings
PyBind11 (optional)
Testing
Google Test + pytest
Toolchain
MSYS2 MinGW-w64 g++
Charts
Python + Matplotlib
Train a digital twin neural network on MNIST using Adam optimizer + cosine LR
Map trained weights to photonic hardware via SVD decomposition (W = UΣV†)
Decompose unitary matrices into MZI mesh (Reck or Clements topology)
Simulate physical effects: thermal drift, attenuation, crosstalk, shot noise
Compensate for drift using per-element least-squares calibration
Benchmark accuracy, latency, memory, and power consumption
To move from simulation to real photonic hardware:
Hardware Driver Layer — FPGA control interface for DAC/ADC
Real-time Calibration — Closed-loop MZI phase stabilization
Chip Integration — Partner with foundry (GlobalFoundries 45CLO)
Edge Deployment — Package for inference on photonic accelerator boards
Companies Doing Production Photonic AI
Contributions welcome! See CONTRIBUTING.md for guidelines.
Fork the repository
Create a feature branch (git checkout -b feature/amazing-feature)
Commit changes (git commit -m 'Add amazing feature')
Push to branch (git push origin feature/amazing-feature)
Open a Pull Request
If you use this in research:
@software {photonic_computing_simulation ,
title ={ Photonic Computing Simulation: ONN Simulator with Physical Effects} ,
author ={ Griffith} ,
year ={ 2026} ,
url ={ https://github.com/Griffith-7/photonic-computing-simulation}
}
MIT License — see LICENSE for details.