Skip to content
QFT.org

Search QFT.org

Enter a word or phrase.

    You can also browse the main navigation and chapter contents.

    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 qj(t)q_j(t) be the field, pj(t)=q˙j(t)p_j(t)=\dot q_j(t), spacing aa, and L=NaL=Na. The canonical momentum is Pj=apjP_j=ap_j, so the natural field inner product is ⟨u,v⟩a=a∑jujvj\langle u,v\rangle_a=a\sum_ju_jv_j. With the potential m2q2/2+λq4/24m^2q^2/2+\lambda q^4/24, define

    q˙=p,p˙=−R(q)+f(t)θ,R(q)=Kq+λ6q∘3,K=m2I−Δa,(Δaq)j=qj+1−2qj+qj−1a2.(1)\begin{aligned} \dot q&=p,\qquad \dot p=-\mathcal R(q)+f(t)\theta,\\ \mathcal R(q)&=Kq+\frac\lambda6q^{\circ3},\qquad K=m^2I-\Delta_a,\\ (\Delta_aq)_j&=\frac{q_{j+1}-2q_j+q_{j-1}}{a^2}. \end{aligned} \tag{1}

    The cube is componentwise. The plus source force follows from the Lorentzian source term +Jϕ+J\phi, with Jj(t)=f(t)θjJ_j(t)=f(t)\theta_j. We optimize no parameters in this example; we calculate a gradient at one fixed source profile.

    Take N=16N=16, a=0.5a=0.5, m=1m=1, λ=4\lambda=4, T=2T=2 and

    f(t)=sin⁡2(πt/T),qj(0)=0.1cos⁡(2πj/N),pj(0)=0.05sin⁡(2πj/N),θj=0.15cos⁡(2πj/N)+0.1sin⁡(4πj/N),yj=0.2cos⁡(2πj/N).(2)\begin{aligned} f(t)&=\sin^2(\pi t/T),\\ q_j(0)&=0.1\cos(2\pi j/N),\qquad p_j(0)=0.05\sin(2\pi j/N),\\ \theta_j&=0.15\cos(2\pi j/N)+0.1\sin(4\pi j/N),\\ y_j&=0.2\cos(2\pi j/N). \end{aligned} \tag{2}

    For a desired final field yy, use the objective

    O(θ)=a2∑j(qj(T)−yj)2+βa2∑jpj(T)2+ηa2∑jθj2,β=0.2,η=0.01.(3)\mathcal O(\theta) =\frac a2\sum_j(q_j(T)-y_j)^2 +\frac{\beta a}2\sum_jp_j(T)^2 +\frac{\eta a}2\sum_j\theta_j^2, \qquad \beta=0.2,\quad\eta=0.01. \tag{3}

    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

    H(q)=R′(q)=K+λ2diag⁡(qj2).(4)H(q)=\mathcal R'(q)=K+\frac\lambda2\operatorname{diag}(q_j^2). \tag{4}

    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 δθ\delta\theta, linearizing Eq.(1) gives

    δq˙=δp,δp˙=−H(q)δq+f(t)δθ.(5)\delta\dot q=\delta p, \qquad \delta\dot p=-H(q)\delta q+f(t)\delta\theta. \tag{5}

    Choose costates λq,λp\lambda_q,\lambda_p so that the derivative of their pairing with the perturbation eliminates the unknown field variations. The required equations are

    λ˙q=H(q)Tλp,λ˙p=−λq.(6)\dot\lambda_q=H(q)^{\mathsf T}\lambda_p, \qquad \dot\lambda_p=-\lambda_q. \tag{6}

    Indeed, the state-dependent terms cancel in

    ddt(λqTδq+λpTδp)=f(t)λpTδθ.(7)\frac{d}{dt}\left(\lambda_q^{\mathsf T}\delta q+ \lambda_p^{\mathsf T}\delta p\right) =f(t)\lambda_p^{\mathsf T}\delta\theta. \tag{7}

    This preserved-pairing construction is the basic adjoint principle; see Sanz-Serna, §3.1, Eqs.(19)–(23).

    The terminal variation of Eq.(3) fixes

    λq(T)=q(T)−y,λp(T)=βp(T).(8)\lambda_q(T)=q(T)-y, \qquad \lambda_p(T)=\beta p(T). \tag{8}

    The costates in Eqs.(6)–(8) are defined using the aa-weighted pairing. With parameter-independent initial data, integration of Eq.(7) gives the ordinary coordinate gradient

    ∇θO=a[ηθ+∫0Tf(t)λp(t) dt].(9)\nabla_\theta\mathcal O =a\left[\eta\theta+\int_0^T f(t)\lambda_p(t)\,dt\right]. \tag{9}

    Thus the final momentum penalty contributes through the terminal value of λp\lambda_p. 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

    a[λq(0)Tδq(0)+λp(0)Tδp(0)](10)a\left[\lambda_q(0)^{\mathsf T}\delta q(0) +\lambda_p(0)^{\mathsf T}\delta p(0)\right] \tag{10}

    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 a∫0Tℓ(q,p,θ,t) dta\int_0^T\ell(q,p,\theta,t)\,dt, Eq.(6) acquires the terms −∇qℓ-\nabla_q\ell and −∇pℓ-\nabla_p\ell in its two right-hand sides, while Eq.(9) acquires a∫∇θℓ dta\int\nabla_\theta\ell\,dt. 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 h=Δth=\Delta t and tn=nht_n=nh. The implemented velocity-Verlet step is

    pn+1/2=pn+h2[f(tn)θ−R(qn)],qn+1=qn+hpn+1/2,pn+1=pn+1/2+h2[f(tn+1)θ−R(qn+1)].(11)\begin{aligned} p_{n+1/2}&=p_n+\frac h2[f(t_n)\theta-\mathcal R(q_n)],\\ q_{n+1}&=q_n+h p_{n+1/2},\\ p_{n+1}&=p_{n+1/2}+\frac h2[f(t_{n+1})\theta-\mathcal R(q_{n+1})]. \end{aligned} \tag{11}

    The two kicks sample the time-dependent source at their stated endpoints. The derivative must preserve that choice.

    Use coordinate covectors qˉn,pˉn\bar q_n,\bar p_n, including the factor aa. Their terminal values and the direct source gradient are

    qˉM=a(qM−y),pˉM=βapM,gdirect=ηaθ,M=T/h.(12)\bar q_M=a(q_M-y),\qquad \bar p_M=\beta ap_M,\qquad g_{\mathrm{direct}}=\eta a\theta, \qquad M=T/h. \tag{12}

    For one backward step, suppose the supplied covectors at n+1n+1 are A=qˉn+1A=\bar q_{n+1} and B=pˉn+1B=\bar p_{n+1}. Reverse the second kick, then the drift, then the first kick:

    u=A−h2H(qn+1)TB,v=B+hu,qˉn=u−h2H(qn)Tv,pˉn=v,Δgn=h2[f(tn+1)B+f(tn)v].(13)\begin{aligned} u&=A-\frac h2H(q_{n+1})^{\mathsf T}B,\\ v&=B+hu,\\ \bar q_n&=u-\frac h2H(q_n)^{\mathsf T}v,\qquad \bar p_n=v,\\ \Delta g_n&=\frac h2\left[f(t_{n+1})B+f(t_n)v\right]. \end{aligned} \tag{13}

    For example, the second kick has differential δpn+1=δpn+1/2−(h/2)H(qn+1)δqn+1+(h/2)f(tn+1)δθ\delta p_{n+1}=\delta p_{n+1/2}-(h/2)H(q_{n+1})\delta q_{n+1}+(h/2)f(t_{n+1})\delta\theta. Pairing this with BB 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 gdirect+∑nΔgng_{\mathrm{direct}}+\sum_n\Delta g_n gives the derivative of the finite Verlet program, in exact arithmetic. This result does not require taking h→0h\to0. 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 hh 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 h=0.05h=0.05, the hand-derived adjoint and reverse-AD gradient differ by at most 7.63×10−177.63\times10^{-17}. That tape has 35,11535{,}115 nodes, of which 25,50925{,}509 are operations, and 43,38643{,}386 parent edges.

    For directional checks at h=0.025h=0.025, choose the unit source vector proportional to

    vj∝cos⁡(3j+0.2)+0.2sin⁡j,j=0,…,15.(14)v_j\propto\cos(3j+0.2)+0.2\sin j, \qquad j=0,\ldots,15. \tag{14}

    The discrete objective is 0.07198347413830.0719834741383, and

    vT∇θOh=−0.0270232868853.(15)v^{\mathsf T}\nabla_\theta\mathcal O_h=-0.0270232868853. \tag{15}

    A separately propagated forward tangent agrees with this directional derivative within 3.47×10−183.47\times10^{-18}. Centered source perturbations give the following absolute errors:

    Perturbation size ε\varepsilonDirectional finite-difference error
    10−110^{-1}5.49×10−85.49\times10^{-8}
    10−310^{-3}5.48×10−125.48\times10^{-12}
    10−410^{-4}1.27×10−131.27\times10^{-13}
    10−610^{-6}6.23×10−126.23\times10^{-12}
    10−810^{-8}1.19×10−91.19\times10^{-9}

    The small-step loss of accuracy is retained. Omitting pˉM=βapM\bar p_M=\beta ap_M while leaving the objective unchanged produces a directional error 3.42×10−43.42\times10^{-4} and a maximum component error 1.80×10−31.80\times10^{-3}. Both are far larger than the finite-difference minimum. Separate variations of the initial qq and pp check Eq.(10), with absolute discrepancies 1.60×10−121.60\times10^{-12} and 1.22×10−121.22\times10^{-12} at perturbation size 10−510^{-5}.

    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 (10−11,10−13)(10^{-11},10^{-13}) to (10−12,10−14)(10^{-12},10^{-14}) changes the coordinate gradient by at most 2.59×10−142.59\times10^{-14}. 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:

    Δt\Delta tSteps∥gh−gref∥∞\lVert g_h-g_{\mathrm{ref}}\rVert_\inftyOh−Oref\mathcal O_h-\mathcal O_{\mathrm{ref}}
    0.10.120201.2776×10−41.2776\times10^{-4}−6.8742×10−5-6.8742\times10^{-5}
    0.050.0540403.1800×10−53.1800\times10^{-5}−1.7077×10−5-1.7077\times10^{-5}
    0.0250.02580807.9413×10−67.9413\times10^{-6}−4.2626×10−6-4.2626\times10^{-6}
    0.01250.01251601601.9848×10−61.9848\times10^{-6}−1.0652×10−6-1.0652\times10^{-6}

    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 λ=0\lambda=0 and θ=0\theta=0. The exact semidiscrete Fourier frequencies are

    ωn2=m2+4a2sin⁡2(πn/N).(16)\omega_n^2=m^2+\frac4{a^2}\sin^2(\pi n/N). \tag{16}

    The new forward code is compared with the already established exact Fourier propagator, using these explicit frequencies and the current mass m=1m=1. The maximum error across final field and momentum components decreases from 3.23×10−43.23\times10^{-4} at h=0.1h=0.1 to 5.04×10−65.04\times10^{-6} at h=0.0125h=0.0125, again by the expected second-order trend.

    The discrete wave gradient approaches a continuous-time reference quadratically, while directional finite differences show a roundoff floor and the omitted terminal momentum gives a much larger error.

    Two different refinements at fixed N=16N=16 and a=0.5a=0.5. Left: time-step dependence of the complete source gradient. Right: perturbation-size dependence for the already fixed Δt=0.025\Delta t=0.025 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 H(qn)H(q_n) requires the forward fields at every reverse step. Saving all M+1M+1 field–momentum pairs is simple, but its storage grows with the number of time steps. Our block scheme saves every BBth 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 8080-step primary trajectory, the retained implementation gives:

    Block length BBSaved boundary pairsPeak saved trajectory pairsReplayed forward steps
    11818182828080
    44212125258080
    10109919198080
    20205525258080

    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 8181 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 M/B+BM/B+B 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 w=λpw=\lambda_p. Derive its second-order equation and endpoint conditions for Eq.(3). Integrate the perturbed second-order wave equation against ww 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), w˙=−λq\dot w=-\lambda_q and w¨=−H(q)Tw\ddot w=-H(q)^{\mathsf T}w. For the symmetric periodic force,

    w¨+H(q)w=0,w(T)=βp(T),w˙(T)=−(q(T)−y).\ddot w+H(q)w=0, \qquad w(T)=\beta p(T),\qquad \dot w(T)=-(q(T)-y).

    The perturbed wave equation is δq¨+H(q)δq=f(t)δθ\delta\ddot q+H(q)\delta q=f(t)\delta\theta. Twice integrating by parts gives

    ∫0TwTf(t)δθ dt=[wTδp−w˙Tδq]0T+∫0T[w¨+H(q)Tw]Tδq dt.\int_0^T w^{\mathsf T}f(t)\delta\theta\,dt =\left[w^{\mathsf T}\delta p-\dot w^{\mathsf T}\delta q\right]_0^T +\int_0^T[\ddot w+H(q)^{\mathsf T}w]^{\mathsf T}\delta q\,dt.

    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 aa. Hence

    δO=a[ηθ+∫0Tf(t)w(t) dt]Tδθ+a[w(0)Tδp(0)−w˙(0)Tδq(0)].\delta\mathcal O =a\left[\eta\theta+\int_0^Tf(t)w(t)\,dt\right]^{\mathsf T}\delta\theta +a\left[w(0)^{\mathsf T}\delta p(0)-\dot w(0)^{\mathsf T}\delta q(0)\right].

    Substituting w=λpw=\lambda_p and −w˙=λq-\dot w=\lambda_q 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 x˙=θx\dot x=\theta x from a parameter-independent x0x_0 with N≥1N\ge1 explicit Euler steps of length h>0h>0, and define ON=xN2/2O_N=x_N^2/2. Find its exact parameter derivative, including the case 1+hθ=01+h\theta=0. Compare it with the continuous-time derivative at T=NhT=Nh and with inserting xNx_N into that continuous formula.

    Solution

    The finite program gives xN=(1+hθ)Nx0x_N=(1+h\theta)^Nx_0, so direct differentiation yields the polynomial expression

    dONdθ=Nh(1+hθ)2N−1x02.\frac{dO_N}{d\theta} =Nh(1+h\theta)^{2N-1}x_0^2.

    Because 2N−1≥12N-1\ge1, this formula remains valid and gives zero when 1+hθ=01+h\theta=0. Away from that point it may also be written

    dONdθ=TxN21+hθ.\frac{dO_N}{d\theta}=\frac{T x_N^2}{1+h\theta}.

    The exact continuous solution is x(T)=eθTx0x(T)=e^{\theta T}x_0, with derivative d[x(T)2/2]/dθ=Te2θTx02d[x(T)^2/2]/d\theta=T e^{2\theta T}x_0^2. Replacing that exact final state by xNx_N gives TxN2Tx_N^2, which lacks the finite-step factor 1/(1+hθ)1/(1+h\theta) wherever the quotient is defined. It is therefore generally not the gradient of the Euler objective. Both consistent derivatives approach the same limit as h→0h\to0 at fixed TT, but this convergence does not make them equal at a finite step.

    References

    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.