photonic-mzi
An electrical-circuit-level feasibility demonstration of photonic matrix multiply-accumulate — compile any real matrix into MZI meshes and execute the linear transform through optical propagation
The project asks one central question: can a photonic processor execute matrix
multiply-accumulate? It uses SVD, unitary transforms, MZI interference meshes,
and optical attenuators to build a runnable, device-by-device-verifiable chain for
y = Mx (or batched Y = MX) that can be checked against NumPy. It also includes
an animation that connects each line of code with propagation through each device.
input [x] -> [V^T unitary MZI mesh] -> [Sigma attenuators] -> [U unitary MZI mesh] -> detector [y]
What the project validates
An optical circuit does not execute electronic instructions one multiply at a time. Instead, the multiplications and sums for each output element are mapped to complex amplitude encoding, interference, per-channel scaling, and coherent readout. The project closes four parts of that chain:
- Compilation: any real matrix can be decomposed by SVD into two orthogonal transforms and non-negative scaling, then compiled into two MZI meshes and one VOA bank.
- Propagation: after encoding an input vector as coherent complex amplitudes, ideal propagation satisfies
E = (M @ x) / gain; the same relation holds for batched inputs. - Readout: coherent detection with a local oscillator recovers signed outputs that agree with
M @ xto floating-point precision. - Sensitivity: static phase-control offsets, per-sample phase jitter, insertion loss, VOA setting error, and equivalent readout noise can be injected and measured independently.
This establishes feasibility inside the mathematical mapping, software implementation, and simplified linear optical circuit model. It does not by itself establish the energy, latency, precision, scale, or manufacturability of a physical chip.
Animation
The left panel shows the executing code with the active line highlighted. The right panel shows what that line does inside the optical circuit. Color encodes phase (red at 0 degrees, cyan at 180 degrees), while bar height encodes amplitude.
python -m pip install "photonic-mzi[viz]"
python -m photonic_mzi
| Key | Action |
|---|---|
Space |
Pause / resume |
Left Right |
Step backward / forward |
, . |
Previous / next stage |
r |
Restart |
The animation has nine stages:
| Stage | Content |
|---|---|
| 0 Core question | Can a photonic processor execute y = Mx? |
| 1 SVD | Why M = U · Sigma · V^T maps a real matrix to transform-scale-transform |
| 2 Compile MZIs | Eliminate matrix elements and fill the MZI parameter table |
| 3 Inject light | Encode the input as complex amplitudes; a negative value is a pi phase shift |
| 4 V^T mesh | Propagate layer by layer and inspect interference and energy conservation |
| 5 Sigma attenuation | Apply the programmed attenuation corresponding to singular values |
| 6 U mesh | Apply the second orthogonal transform |
| 7 Detect output | Compare coherent readout against NumPy |
| 8 Non-idealities | Measure sensitivity to phase offsets, jitter, loss, VOA error, and readout noise |
Stage 2: matrix elimination and MZI parameter programming |
Stage 4: interference and energy conservation in one MZI |
Stage 1: M = U · Sigma · V^T |
Stage 8: NumPy, ideal photonic, and simplified non-ideal results |
Installation
python -m pip install "photonic-mzi[viz]"
For the NumPy-only compute kernel without the animation:
python -m pip install photonic-mzi
To develop from a source checkout, editable installs require pip 21.3 or later
because this is a pyproject.toml-only package using PEP 660:
python -m pip install --upgrade pip && pip install -e ".[dev]"
You can also run directly from the checkout:
PYTHONPATH=src python -m photonic_mzi
Usage
import numpy as np
from photonic_mzi import PhotonicMatrixProcessor
M = np.random.randn(8, 5) # rectangular matrices are supported
opu = PhotonicMatrixProcessor(M, seed=42)
x = np.random.randn(5)
y = opu.read_coherent(x) # agrees with M @ x to about 1e-15
Y = opu.read_coherent(np.random.randn(5, 256)) # batched inputs
E = opu.optical_field(x) # pre-detection field: (M @ x) / opu.gain when ideal
print(opu.report())
Add the simplified non-ideality model:
from photonic_mzi import NoiseModel
noise = NoiseModel(
fab_theta=0.02, # static phase-control offset; calibratable in the ideal model
drift_theta=0.005, # independent per-sample phase jitter
mzi_loss_db=0.2, # insertion loss per MZI
voa_rel_err=0.01,
detector_snr_db=40, # post-detection equivalent relative AWGN
)
opu = PhotonicMatrixProcessor(M, noise=noise, seed=7)
opu.read_coherent(x, ideal=False)
opu.calibrate()
opu.read_coherent(x, ideal=False)
The two readout methods represent different detection schemes:
| Method | Physical meaning |
|---|---|
read_coherent(x) |
Coherent or homodyne detection with a local oscillator; preserves the signed real component |
read_intensity(x) |
Square-law direct detection; returns calibrated gain²·|E|² and loses sign information |
Examples
python examples/01_hello_photonic.py
python examples/02_noise_and_calibration.py
python examples/03_neural_layer.py
The third example uses a synthetic, well-separated classification problem. Its accuracy under a selected noise model must not be generalized to other models, datasets, or time-correlated thermal drift.
Tests and benchmark
pytest -m "not slow"
python benchmarks/bench_decomposition.py
The fast suite contains 128 tests; the full suite contains 133 tests, including all-frame glyph checks and GIF export. Coverage includes degenerate and structured matrices, random dense and rectangular matrices, batching, energy conservation, noise semantics, detection boundaries, calibration boundaries, and input validation.
Known limitations
- The implementation uses a triangular Reck mesh, not a rectangular Clements mesh. Both use
N(N-1)/2MZIs, but Clements has lower optical depth and is usually more tolerant of uniform loss. fab_*models additive phase-control offsets, not beam-splitter fabrication errors that may restrict the reachable splitting ratio.drift_*is independent per-input phase jitter; it does not model temporal or spatial correlation or thermal crosstalk.detector_snr_dbis post-detection equivalent AWGN; there is no detailed shot-noise, local-oscillator, responsivity, TIA, or bandwidth model.mode_mzi_count()is a topology proxy, not end-to-end path tracing.- Forward propagation still loops over MZIs in Python; devices in one mesh layer could be vectorized.
- Input modulators, laser power, DAC/ADC, system energy and latency, wavelength dependence, polarization, nonlinearities, and device crosstalk are not modeled.
See Model and validation notes for the full scope.
References
- Reck et al., Experimental realization of any discrete unitary operator, PRL 73, 58 (1994)
- Clements et al., Optimal design for universal multiport interferometers, Optica 3, 1460 (2016)
- Shen et al., Deep learning with coherent nanophotonic circuits, Nature Photonics 11, 441 (2017)
License
Metadata
Release files for photonic-mzi 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| photonic_mzi-1.0.1.tar.gz | 45.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| photonic_mzi-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 81.4 kB
Release files / photonic_mzi-1.0.1.tar.gz
| Download URL | photonic_mzi-1.0.1.tar.gz |
|---|---|
| Size | 45.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eee316f51029437372d40d737226ed1754fc93cf8dde7e36e4a7ae0e956fa5c8
|
|
BLAKE2b-256 checksum How to use checksums |
1d9780c1a68d12503a22b74a41e4babf034d25191eb9c9326cd40f662e39710f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.
Transparency logRelease files / photonic_mzi-1.0.1-py3-none-any.whl
| Download URL | photonic_mzi-1.0.1-py3-none-any.whl |
|---|---|
| Size | 36.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2097c2f5802b7cd7e67b815aa1ca42f6e8b7e186ae77f03edf4ca4976a4537a4
|
|
BLAKE2b-256 checksum How to use checksums |
854ce019ae2de23471743c564d12b954827ad9f5643cdafed73e466f48f95710
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.
Transparency log