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 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

Open In Colab

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


Download files

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

Source Distribution

torchoptics-0.4.0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distribution

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

torchoptics-0.4.0-py3-none-any.whl (55.0 kB view details)

Uploaded Python 3

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

Hashes for torchoptics-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7c640670bfb950b42646ec647b27b4a10c012e488f69250ccd7bd7fc7ae02d82
MD5 f1a9f7edb73ab8c9b9f073292afcd1c8
BLAKE2b-256 f617aa511782c69664b9e4257963f573884376e7e948f68aaaefd19231e52ef4

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchoptics-0.4.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-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

Hashes for torchoptics-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78d6bed2907d1f6c7e117584a5e1554c5e67086d2bd7b079770ecb1edbd24e35
MD5 9e228eca18b2dc4ff5902f71585d64b2
BLAKE2b-256 bb80d7b75eeb96d206ab81f1e1b43e1c7aee1287d2bc4a1f8beeed9f5d119b03

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchoptics-0.4.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