# E10 · Designing light with gradients

Design a phase mask by measuring how each parameter affects an image, then replace thousands of separate perturbations with one adjoint calculation.

<!-- course-motivation:start -->
## Why learn this?

**How do you improve a design with thousands of controls?**

You want a phase mask to produce a chosen intensity pattern, but each pixel is a design variable. Perturbing every pixel separately makes each optimization step expensive. You need the sensitivity of one objective to all those variables at once.

**What you gain:** Compute and verify a gradient with an adjoint calculation, use it to improve the pattern, and assess the resulting design without assuming that a lower error proves a global optimum.
<!-- course-motivation:end -->

<!-- course-preparation:start -->
## Before this lesson

Use these focused refreshers if an idea is unfamiliar; you do not need to complete each linked course.

- **Chain rule and gradients:** Apply the multivariable chain rule to one scalar objective. Used here: The gradient measures how each phase pixel changes the image mismatch. [Review: MIT OCW · Chain Rule, Gradient and Directional Derivatives](https://ocw.mit.edu/courses/18-02sc-multivariable-calculus-fall-2010/pages/2.-partial-derivatives/part-b-chain-rule-gradient-and-directional-derivatives/). Use Sessions 32 and 34 for the multivariable chain rule, Session 35 for the gradient, and Session 38 for directional derivatives.
- **Complex inner products:** Use a complex inner product and conjugate transpose. Used here: Moving a linear operator across that inner product produces the adjoint operation. [Review: MIT OCW · Lecture 26: Complex matrices; fast Fourier transform](https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-26-complex-matrices-fast-fourier-transform/). Focus on complex vectors, conjugate transpose, Hermitian matrices, and unitary matrices. The FFT algorithm at the end is optional here.
- **Fourier transforms and convolution:** Follow a complex field through a forward and inverse Fourier transform. Used here: The core design example uses a unitary Fourier propagation model. [Review: TU Delft · Fourier analysis](https://qiweb.tudelft.nl/aoi/fourieranalysis/fourieranalysis/). Use §4.1 for the transform pair, then §4.2.1–4.2.4 for linearity, scaling, shifts, and convolution. Consult §4.2.7 for two successive transforms.

**Python preparation:** Use complex arrays and compare an analytic derivative with a finite-difference perturbation.
- [NumPy · The absolute basics for beginners](https://numpy.org/doc/stable/user/absolute_beginners.html) — Use the sections on array shape, indexing and slicing, basic operations, and broadcasting. Practice changing one parameter and rerunning a cell.
- [NumPy · Discrete Fourier Transform](https://numpy.org/doc/stable/reference/routines.fft.html) — Read Implementation details and Normalization; look up fftfreq, fftshift, and ifftshift as they appear in the notebook.

**Developed here:** Derive, check, and use an adjoint phase gradient. Prior knowledge of adjoint optimization is not required.

[All prerequisites and learning resources](https://vandermode.github.io/optics-in-5-minutes-course/preparation/)
<!-- course-preparation:end -->

## Learning outcomes

- Derive a real phase gradient through a complex Fourier operator.
- Verify that gradient with central finite differences.
- Explain why adjoints are not generally inverse propagation and why gradients do not guarantee global optimality.

## Use the lesson

Watch the [E10 film](https://www.youtube.com/watch?v=x9Ys3GWC1Q0) (5:15), embedded near the top of the lesson.

Serve the complete teaching/ directory over HTTP. The lesson uses shared local assets and native JavaScript modules. Open this episode's folder in the browser. Its standalone notebook also works directly in Colab or Jupyter; it does not import repository code or require a GPU.

The notebook's core takes about 25–30 minutes to work through, with a separate optional extension. The first code cell installs numerical packages only when missing. Saved outputs, figures/, and figures.zip provide static figure access if a notebook frontend blocks rich output.

## Model and normalization

64 × 64 scalar phase-only pupil with a circular fixed amplitude and a unitary Fourier propagation operator. Targets have equal total power. No pixel coupling, material dispersion, noise, or fabrication constraints are included in the browser optimization. The optional Helmholtz example has a lossy boundary layer, explicitly not a PML.

Computed quantities are generated by model.js in the browser and the explicitly written NumPy/SciPy models in lab.py. Model-specific assertions and independent references are in the lab and the course checks. The browser and notebook use documented but not necessarily identical grid sizes. Synthetic randomness is seeded.

## Rebuild

From the repository root, run: python teaching/tools/build_course.py, then python teaching/tools/validate_notebooks.py. The authored page content and controls are in teaching/tools/curriculum.py; opening motivations are in teaching/tools/motivation.py; preparation is in teaching/tools/preparation.py; notebook source is this folder's percent-format lab.py. Building preserves saved outputs when code cells are unchanged. Run npm test --prefix teaching for numerical browser-model tests.

## References

- [Steven G. Johnson · Notes on adjoint methods](https://math.mit.edu/~stevenj/18.336/adjoint.pdf)
- [SciPy · Sparse factorization solve conventions](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.SuperLU.solve.html)
- [NumPy · Fourier transforms and normalization](https://numpy.org/doc/stable/reference/routines.fft.html)
