- Home
- Library
- Computational Quantum Field Theory
- Differentiating and Optimizing Field Calculations
- Adjoint Methods for Field Evolution
Adjoint Methods for Field Evolution
How should a source profile change to move a scalar wave toward a desired final configuration? Repeating the evolution once for every source parameter is possible, but an adjoint computes the gradient of one objective with a single backward propagation. Its terminal conditions depend on the objective, and its numerical form depends on the forward time integrator.
We derive both the continuous-time adjoint of a spatially discretized nonlinear wave and the exact transpose of its velocity-Verlet steps. A finite calculation checks the result against reverse AD, directional perturbations and a separately refined time integration. It also shows why omitting a terminal momentum term and confusing the discrete and continuous derivatives are consequential errors.
Preparation: Time evolution explains the scalar lattice dynamics, and automatic differentiation explains reverse accumulation. Only real classical fields and a deterministic objective are used here.
A source-control problem with fixed spatial resolution
On a periodic lattice, let be the field, , spacing , and . The canonical momentum is , so the natural field inner product is . With the potential , define
The cube is componentwise. The plus source force follows from the Lorentzian source term , with . We optimize no parameters in this example; we calculate a gradient at one fixed source profile.
Take , , , , and
For a desired final field , use the objective
The first term measures the final mismatch, the second penalizes residual motion, and the third penalizes source size. These weights are fixed in the numerical units of Eq.(2). This is a finite-grid control objective, not a quantum expectation value or a measurement likelihood.
The force Jacobian is the symmetric matrix
Its dependence on the trajectory makes storing or recomputing forward fields necessary in the adjoint calculation.
The continuous-time adjoint and its endpoint conditions
For a source variation , linearizing Eq.(1) gives
Choose costates so that the derivative of their pairing with the perturbation eliminates the unknown field variations. The required equations are
Indeed, the state-dependent terms cancel in
This preserved-pairing construction is the basic adjoint principle; see Sanz-Serna, §3.1, Eqs.(19)–(23).
The terminal variation of Eq.(3) fixes
The costates in Eqs.(6)–(8) are defined using the -weighted pairing. With parameter-independent initial data, integration of Eq.(7) gives the ordinary coordinate gradient
Thus the final momentum penalty contributes through the terminal value of . Setting it to zero would differentiate a different terminal functional, even if the forward evolution and reported objective were unchanged.
If initial data also depend on a parameter, they contribute
to the objective variation. This term is absent here only because the initial data are fixed, not because adjoint evolution makes boundary terms vanish.
For a running objective , Eq.(6) acquires the terms and in its two right-hand sides, while Eq.(9) acquires . Terminal conditions still come from the terminal part of the objective. The same integration-by-parts argument determines these terms; one should not infer them by analogy with a different loss function.
Reverse the actual time steps
Let and . The implemented velocity-Verlet step is
The two kicks sample the time-dependent source at their stated endpoints. The derivative must preserve that choice.
Use coordinate covectors , including the factor . Their terminal values and the direct source gradient are
For one backward step, suppose the supplied covectors at are and . Reverse the second kick, then the drift, then the first kick:
For example, the second kick has differential . Pairing this with produces the first line and the first source contribution of Eq.(13). Applying the same procedure to the remaining two maps proves the entire recurrence.
Summing gives the derivative of the finite Verlet program, in exact arithmetic. This result does not require taking . It is also what reverse AD of the same program should return, up to rounding. The general relation between discrete adjoints and reverse accumulation is developed in Sanz-Serna, §§3.3–3.5, especially §3.5 and Lemma 12; Eq.(13) is derived directly for our particular composition.
Integrating Eq.(6) with an arbitrary time integrator is a different procedure. At finite it need not give Eq.(13). If optimization evaluates Eq.(3) using Eq.(11), consistency with that evaluated objective is tested against Eq.(13), while convergence to the continuous-time gradient is a separate test.
Three independent derivative checks
The reproducible wave_adjoint.py implements the forward map, a forward tangent and Eq.(13). The companion adjoint_checks.py also executes the forward map with the elementary reverse tape from automatic differentiation. Its inputs are the sixteen source coordinates; the analytic gradient is not supplied to the tape.
At , the hand-derived adjoint and reverse-AD gradient differ by at most . That tape has nodes, of which are operations, and parent edges.
For directional checks at , choose the unit source vector proportional to
The discrete objective is , and
A separately propagated forward tangent agrees with this directional derivative within . Centered source perturbations give the following absolute errors:
| Perturbation size | Directional finite-difference error |
|---|---|
The small-step loss of accuracy is retained. Omitting while leaving the objective unchanged produces a directional error and a maximum component error . Both are far larger than the finite-difference minimum. Separate variations of the initial and check Eq.(10), with absolute discrepancies and at perturbation size .
These checks answer different questions. Reverse AD checks the transpose recurrence against the executed arithmetic graph. A tangent checks forward/reverse pairing. Perturbing the scalar objective checks the gradient against actual changes of that objective. Their agreement is substantially more discriminating than reporting one small internal residual.
Time refinement still matters
An independent DOP853 integration solves the nonlinear semidiscrete wave in continuous time, stores a dense forward solution and integrates Eq.(6) backward. Tightening its tolerances from to changes the coordinate gradient by at most . This is a measured reference-refinement diagnostic, not a rigorous error certificate.
At the same fixed spatial lattice, the Verlet gradients approach that reference as follows:
| Steps | |||
|---|---|---|---|
Halving the time step reduces these errors by approximately four. The agreement between AD and Eq.(13) at a fixed time step does not remove this time-discretization error.
As a separate force and normalization control, set and . The exact semidiscrete Fourier frequencies are
The new forward code is compared with the already established exact Fourier propagator, using these explicit frequencies and the current mass . The maximum error across final field and momentum components decreases from at to at , again by the expected second-order trend.
Two different refinements at fixed and . Left: time-step dependence of the complete source gradient. Right: perturbation-size dependence for the already fixed program. The horizontal line shows the deliberate omission of the momentum endpoint term. No continuum spatial limit or quantum-field approximation error is inferred from either panel.
Checkpointing trades saved states for replay
The nonlinear matrix requires the forward fields at every reverse step. Saving all field–momentum pairs is simple, but its storage grows with the number of time steps. Our block scheme saves every th pair, then replays one block during the backward pass and discards its interior states afterward.
Replay must reproduce the original map: the same time indices, source evaluations, parameter values and arithmetic order. Using an approximate backward reconstruction of the state would be a different method and would need its own error analysis. General relations between the stored forward trace and reverse accumulation are discussed in Baydin et al., §3.2 (PDF).
For the -step primary trajectory, the retained implementation gives:
| Block length | Saved boundary pairs | Peak saved trajectory pairs | Replayed forward steps |
|---|---|---|---|
The peak counts include saved checkpoints and the replayed block. The starting checkpoint is shared; the replayed endpoint is a new pair even when an equal checkpoint already exists. They exclude the fixed number of live work arrays and covectors. Full trajectory storage needs pairs and no replay. Block length one is deliberately retained: it saves no memory in this implementation and still repeats the forward work.
All four replay gradients are identical to the full-storage gradient in this run. Approximately trajectory pairs are needed, explaining why intermediate block sizes help. The retained adjoint-results.json includes the memory convention, replay counts and every refinement above.
Summary
An adjoint propagates a terminal observable's sensitivity backward, with endpoint terms fixed by that observable. Reversing the actual Verlet maps gives the gradient of the actual finite-step objective. Continuous-time adjoints supply a separate limit and a separate accuracy test. The source example checks both derivatives, exposes a wrong terminal condition, and verifies deterministic checkpoint replay without claiming a continuum spatial result.
Exercises
1. Derive the second-order adjoint boundary terms
Set . Derive its second-order equation and endpoint conditions for Eq.(3). Integrate the perturbed second-order wave equation against and recover the source and initial-data contributions. State what changes for periodic spatial boundaries versus a problem with physical boundaries.
Solution
From Eq.(6), and . For the symmetric periodic force,
The perturbed wave equation is . Twice integrating by parts gives
The integral on the right vanishes. The terminal boundary expression is exactly the variation of the field and momentum terms of Eq.(3), divided by . Hence
Substituting and recovers Eqs.(9)–(10). For periodic space, the discrete Laplacian is symmetric and there is no spatial boundary term. On a finite interval, integration by parts also produces spatial endpoint terms; the adjoint boundary conditions must cancel or represent them consistently with the forward boundary conditions and any boundary part of the objective. One cannot transfer the periodic formula unchanged to an absorbing boundary problem.
2. Differentiate a time-discrete growth equation
Advance from a parameter-independent with explicit Euler steps of length , and define . Find its exact parameter derivative, including the case . Compare it with the continuous-time derivative at and with inserting into that continuous formula.
Solution
The finite program gives , so direct differentiation yields the polynomial expression
Because , this formula remains valid and gives zero when . Away from that point it may also be written
The exact continuous solution is , with derivative . Replacing that exact final state by gives , which lacks the finite-step factor wherever the quotient is defined. It is therefore generally not the gradient of the Euler objective. Both consistent derivatives approach the same limit as at fixed , but this convergence does not make them equal at a finite step.
References
- Atılım Güneş Baydin, Barak A. Pearlmutter, Alexey Andreyevich Radul and Jeffrey Mark Siskind, Automatic Differentiation in Machine Learning: a Survey, JMLR 18(153), 1–43 (2018), §3.2, reverse accumulation and the forward trace (PDF).
- J. M. Sanz-Serna, Symplectic Runge–Kutta schemes for adjoint equations, automatic differentiation, optimal control and more (2015), arXiv:1503.04021v2, §§3.1–3.2, Eqs.(19)–(23), and §§3.3–3.5, Theorem 11 and Lemma 12. The continuous pairing and relation to reverse accumulation motivate the construction; the specific Verlet transpose and its endpoint source contributions are derived here.
Scrollable formula. Use Left and Right arrow keys to reveal hidden content.
Scrollable table. Use Left and Right arrow keys to reveal hidden columns.
Scrollable figure. Use Left and Right arrow keys to reveal hidden content.