Python library for differentiable Fourier optics simulations with PyTorch.
Project description
TorchOptics is a differentiable wave optics simulation library built on PyTorch.
Key Features
- 🌊 Differentiable Wave Optics — Model, analyze, and optimize optical systems using Fourier optics.
- 🔥 Built on PyTorch — GPU acceleration, batch processing, and automatic differentiation.
- 🛠️ End-to-End Optimization — Joint optimization of optical hardware and machine learning models.
- 🔬 Optical Elements — Lenses, modulators, detectors, polarizers, and more.
- 🖼️ Spatial Profiles — Hermite-Gaussian, Laguerre-Gaussian, Zernike modes, and others.
- 🔆 Polarization and Coherence — Simulate polarized light and fields with arbitrary spatial coherence.
Learn more about TorchOptics in our research paper on arXiv.
Installation
TorchOptics is available on PyPI and can be installed with:
pip install torchoptics
Documentation
Read the full documentation at torchoptics.readthedocs.io.
Usage
This example shows how to simulate a 4f imaging system using TorchOptics, computing and visualizing the field at each focal plane along the optical axis:
import torch
import torchoptics
from torchoptics import Field, System
from torchoptics.elements import Lens
from torchoptics.profiles import checkerboard
# Set simulation properties
shape = 1000 # Number of grid points in each dimension
spacing = 10e-6 # Spacing between grid points (m)
wavelength = 700e-9 # Field wavelength (m)
focal_length = 200e-3 # Lens focal length (m)
tile_length = 400e-6 # Checkerboard tile length (m)
num_tiles = 15 # Number of tiles in each dimension
# Determine device
device = "cuda" if torch.cuda.is_available() else "cpu"
# Configure default properties
torchoptics.set_default_spacing(spacing)
torchoptics.set_default_wavelength(wavelength)
# Initialize input field with checkerboard pattern
field_data = checkerboard(shape, tile_length, num_tiles)
input_field = Field(field_data).to(device)
# Define 4f optical system with two lenses
system = System(
Lens(shape, focal_length, z=1 * focal_length),
Lens(shape, focal_length, z=3 * focal_length),
).to(device)
# Measure field at focal planes along the z-axis
measurements = [
system.measure_at_z(input_field, z=i * focal_length)
for i in range(5)
]
# Visualize the measured intensity distributions
for i, measurement in enumerate(measurements):
measurement.visualize(title=f"z={i}f", vmax=1)
Intensity distributions at different focal planes in the 4f system.
Propagation of the intensity distribution.
For more examples and detailed usage, please refer to the documentation.
Contributing
We welcome contributions! See our Contributing Guide for details.
Citing TorchOptics
If you use TorchOptics in your research, please cite our paper:
@misc{filipovich2024torchoptics,
title={TorchOptics: An open-source Python library for differentiable Fourier optics simulations},
author={Matthew J. Filipovich and A. I. Lvovsky},
year={2024},
eprint={2411.18591},
archivePrefix={arXiv},
primaryClass={physics.optics},
url={https://arxiv.org/abs/2411.18591},
}
License
TorchOptics is distributed under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
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 torchoptics-0.4.0.tar.gz.
File metadata
- Download URL: torchoptics-0.4.0.tar.gz
- Upload date:
- Size: 37.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c640670bfb950b42646ec647b27b4a10c012e488f69250ccd7bd7fc7ae02d82
|
|
| MD5 |
f1a9f7edb73ab8c9b9f073292afcd1c8
|
|
| BLAKE2b-256 |
f617aa511782c69664b9e4257963f573884376e7e948f68aaaefd19231e52ef4
|
Provenance
The following attestation bundles were made for torchoptics-0.4.0.tar.gz:
Publisher:
publish.yml on MatthewFilipovich/torchoptics
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchoptics-0.4.0.tar.gz -
Subject digest:
7c640670bfb950b42646ec647b27b4a10c012e488f69250ccd7bd7fc7ae02d82 - Sigstore transparency entry: 217809221
- Sigstore integration time:
-
Permalink:
MatthewFilipovich/torchoptics@5ff03adb965865f9a9e13813b4ddaffaed8ce743 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/MatthewFilipovich
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5ff03adb965865f9a9e13813b4ddaffaed8ce743 -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchoptics-0.4.0-py3-none-any.whl.
File metadata
- Download URL: torchoptics-0.4.0-py3-none-any.whl
- Upload date:
- Size: 55.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78d6bed2907d1f6c7e117584a5e1554c5e67086d2bd7b079770ecb1edbd24e35
|
|
| MD5 |
9e228eca18b2dc4ff5902f71585d64b2
|
|
| BLAKE2b-256 |
bb80d7b75eeb96d206ab81f1e1b43e1c7aee1287d2bc4a1f8beeed9f5d119b03
|
Provenance
The following attestation bundles were made for torchoptics-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on MatthewFilipovich/torchoptics
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchoptics-0.4.0-py3-none-any.whl -
Subject digest:
78d6bed2907d1f6c7e117584a5e1554c5e67086d2bd7b079770ecb1edbd24e35 - Sigstore transparency entry: 217809225
- Sigstore integration time:
-
Permalink:
MatthewFilipovich/torchoptics@5ff03adb965865f9a9e13813b4ddaffaed8ce743 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/MatthewFilipovich
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5ff03adb965865f9a9e13813b4ddaffaed8ce743 -
Trigger Event:
release
-
Statement type: