Skip to main content

Python library for differentiable Fourier optics simulations with PyTorch.

Project description

build Codecov Documentation Status PyPI version Python Version License

TorchOptics is an open-source Python library for simulating optical systems using Fourier optics, built on PyTorch. It provides GPU-accelerated, fully differentiable wave optics simulations, enabling end-to-end optimization of optical hardware jointly with machine learning models.

Learn more in our paper on arXiv.

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, phase/amplitude modulators, detectors, polarizers, and more.
  • 🖼️ Spatial Profiles: Hermite-Gaussian, Laguerre-Gaussian, Zernike modes, gratings, and others.
  • 🔆 Polarization and Coherence: Simulate polarized light and fields with arbitrary spatial coherence.

Installation

pip install torchoptics

Documentation

Full documentation is available at torchoptics.readthedocs.io.

Examples

Wave Propagation

Simulate free-space propagation of an octagonal aperture (full example):

import torch
import torchoptics
from torchoptics import Field
from torchoptics.profiles import octagon

device = "cuda" if torch.cuda.is_available() else "cpu"
torchoptics.set_default_spacing(10e-6)
torchoptics.set_default_wavelength(700e-9)

field = Field(octagon(shape=500, radius=150e-5)).to(device)

for z in torch.linspace(0, 2, 11):
    field.propagate_to_z(z).visualize(title=f"z = {z:.2f} m")

4f Imaging System

Simulate a 4f system with a high-pass spatial filter (full example):

import torch
import torchoptics
from torchoptics import Field, System
from torchoptics.elements import AmplitudeModulator, Lens
from torchoptics.profiles import checkerboard, circle

device = "cuda" if torch.cuda.is_available() else "cpu"
torchoptics.set_default_spacing(10e-6)
torchoptics.set_default_wavelength(700e-9)

shape = 1000
f = 200e-3

input_field = Field(checkerboard(shape, tile_length=400e-6, num_tiles=15)).to(device)

system = System(
    Lens(shape, f, z=1 * f),
    AmplitudeModulator(1 - circle(shape, radius=5e-4), z=2 * f),
    Lens(shape, f, z=3 * f),
).to(device)

for i in range(5):
    system.measure_at_z(input_field, z=i * f).visualize(title=f"z={i}f", vmax=1)

Inverse Design

Train a diffractive optical system to convert a Gaussian beam into a petal beam (full example):

import torch
import torchoptics
from torch.nn import Parameter
from torchoptics import Field, System
from torchoptics.elements import PhaseModulator
from torchoptics.profiles import gaussian, laguerre_gaussian

device = "cuda" if torch.cuda.is_available() else "cpu"
torchoptics.set_default_spacing(10e-6)
torchoptics.set_default_wavelength(700e-9)

shape = 250
waist_radius = 300e-6

input_field = Field(gaussian(shape, waist_radius=waist_radius), z=0).to(device)

petal_profile = laguerre_gaussian(shape, p=0, l=4, waist_radius=waist_radius)
petal_profile += laguerre_gaussian(shape, p=0, l=-4, waist_radius=waist_radius)
target_field = Field(petal_profile, z=0.8).normalize().to(device)

system = System(
    PhaseModulator(Parameter(torch.zeros(shape, shape)), z=0.2),
    PhaseModulator(Parameter(torch.zeros(shape, shape)), z=0.4),
    PhaseModulator(Parameter(torch.zeros(shape, shape)), z=0.6),
).to(device)

optimizer = torch.optim.Adam(system.parameters(), lr=0.05)
for iteration in range(100):
    optimizer.zero_grad()
    output_field = system.measure_at_z(input_field, 0.8)
    loss = 1 - output_field.inner(target_field).abs().square()
    loss.backward()
    optimizer.step()

For more examples, see the examples gallery.

Contributing

Contributions are welcome! See the 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

Distributed under the MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchoptics-1.0.0.tar.gz (11.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchoptics-1.0.0-py3-none-any.whl (56.9 kB view details)

Uploaded Python 3

File details

Details for the file torchoptics-1.0.0.tar.gz.

File metadata

  • Download URL: torchoptics-1.0.0.tar.gz
  • Upload date:
  • Size: 11.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torchoptics-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4b09f7409137f119639ca00565d05c8b97e3859c7884202a9ef43ad30567948d
MD5 4205622e118251ba6710d137af2b56f7
BLAKE2b-256 3f3e676e04ae8c9825e1183bcbdc2eb72ec52b6fe4a2212c4c5fed0328316350

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchoptics-1.0.0.tar.gz:

Publisher: publish.yml on MatthewFilipovich/torchoptics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchoptics-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: torchoptics-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 56.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torchoptics-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd5286d65853b04585ae56e7db2ec5bbc5de7ae4d7762db41f3c33d04892190f
MD5 be4e7f34accd98bdc23718ac521c1c0a
BLAKE2b-256 42d981f74ba8d8c79ffc092e3511d0cb4225f4ea46561e90613dcf7e8b1a144e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchoptics-1.0.0-py3-none-any.whl:

Publisher: publish.yml on MatthewFilipovich/torchoptics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page