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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file photonic_mzi-1.0.1.tar.gz.
File metadata
- Download URL: photonic_mzi-1.0.1.tar.gz
- Upload date:
- Size: 45.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eee316f51029437372d40d737226ed1754fc93cf8dde7e36e4a7ae0e956fa5c8
|
|
| MD5 |
e93c4663352f9ef542751437e8dff722
|
|
| BLAKE2b-256 |
1d9780c1a68d12503a22b74a41e4babf034d25191eb9c9326cd40f662e39710f
|
Provenance
The following attestation bundles were made for photonic_mzi-1.0.1.tar.gz:
Publisher:
release.yml on yaoniming3k/photonic-mzi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
photonic_mzi-1.0.1.tar.gz -
Subject digest:
eee316f51029437372d40d737226ed1754fc93cf8dde7e36e4a7ae0e956fa5c8 - Sigstore transparency entry: 2579316196
- Sigstore integration time:
-
Permalink:
yaoniming3k/photonic-mzi@f5cdbb920ec8b5666cb5ac67d044596b58a3f3b7 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/yaoniming3k
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f5cdbb920ec8b5666cb5ac67d044596b58a3f3b7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file photonic_mzi-1.0.1-py3-none-any.whl.
File metadata
- Download URL: photonic_mzi-1.0.1-py3-none-any.whl
- Upload date:
- Size: 36.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2097c2f5802b7cd7e67b815aa1ca42f6e8b7e186ae77f03edf4ca4976a4537a4
|
|
| MD5 |
3cb601b21e98b61b51d9b3a35a7ad013
|
|
| BLAKE2b-256 |
854ce019ae2de23471743c564d12b954827ad9f5643cdafed73e466f48f95710
|
Provenance
The following attestation bundles were made for photonic_mzi-1.0.1-py3-none-any.whl:
Publisher:
release.yml on yaoniming3k/photonic-mzi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
photonic_mzi-1.0.1-py3-none-any.whl -
Subject digest:
2097c2f5802b7cd7e67b815aa1ca42f6e8b7e186ae77f03edf4ca4976a4537a4 - Sigstore transparency entry: 2579316198
- Sigstore integration time:
-
Permalink:
yaoniming3k/photonic-mzi@f5cdbb920ec8b5666cb5ac67d044596b58a3f3b7 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/yaoniming3k
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f5cdbb920ec8b5666cb5ac67d044596b58a3f3b7 -
Trigger Event:
release
-
Statement type: