Controls and Derivatives

Gradient-based optimal control needs the derivative of the dynamical Generator with respect to whatever the optimization tunes. There are two kinds of derivatives, corresponding to the two ways of tuning a control:

The data structures of controls, amplitudes, generators, and parameter blocks are defined in QuantumPropagators, and explained on the page Time-Dependent Controls. The top half of the diagrams below match the diagrams from that page, with the lower half extending to the derivatives that QuantumControl adds.

Control Derivatives

generator amplitude control pulse number or operator contains results in, via method
H = Ĥ₀ + ϵ₁(t) Ĥ₁ + a₂(t) Ĥ₂ + a₃(t) Ĥ₃:: Generator evaluate Ĥ₀ + Σₗ cₗ Ĥₗ:: Operator a₂ = S(t) ϵ₂(t):: ShapedAmplitude a₃ = ϵ₃(t)²amplitude evaluate c₃ = ϵ₃(tₙ)²:: Float64 ϵ₁(t):: Function ϵ₂(t):: Function ϵ₃(t):: Function evaluate ϵ₃(tₙ):: Float64 discretize_on_midpoints discretize_on_midpoints discretize_on_midpoints p₁ :: Vector{Float64}123⋯N_T p₂ :: Vector{Float64}123⋯N_T p₃ :: Vector{Float64}123⋯N_T S(t):: LockedAmplitude 2ϵ₃(t)amplitude ∂H/∂ϵ₁ = Ĥ₁:: Matrix{ComplexF64} ∂H/∂ϵ₂ = S(t) Ĥ₂:: Generator ∂H/∂ϵ₃ = 2ϵ₃(t) Ĥ₃:: Generator evaluate 2ϵ₃(tₙ) Ĥ₃:: Operator get_control_deriv(H, ϵₗ)

A control derivative is the derivative $∂Ĥ(t)/∂ϵ_l(t)$ of the generator with respect to a control, obtained by get_control_deriv. For a linear control term, it is the control operator $Ĥ_l$. Otherwise, it depends on time, and possibly on the controls.

The function get_control_deriv(H, ϵ) returns a static operator, a Generator, or nothing. For an amplitude, it returns 1.0 if the amplitude is the control, 0.0 if it does not depend on it, a LockedAmplitude, or another amplitude. The function get_control_derivs(H, controls) returns a vector of these, one for each control.

Parameter Derivatives

generator amplitude control parameter block ArrayPartition number or operator contains or holds aliases results in, via method

For the built-in Generator, only the controls hold parameter blocks:

H = Ĥ₀ + a₁(t) Ĥ₁ + a₂(t) Ĥ₂:: Generator get_parameters 12345ArrayPartition a₁ = ϵ₁ + ϵ₂amplitude a₂ = ϵ₂ + ϵ₃amplitude ϵ₁ = A₁ g(t) cos ωt:: ParameterizedFunction ϵ₂ = A₂ g(t) cos ωt:: ParameterizedFunction ϵ₃ = c₀ + c₁ t:: ParameterizedFunction u :: ComponentVectorωA₁A₂ v :: Vector{Float64}c₀c₁ aliases u aliases v ∂ϵ₁/∂ω:: ParameterizedFunction ∂ϵ₂/∂ω:: ParameterizedFunction ∂ϵ₁/∂A₁:: ParameterizedFunction ∂ϵ₂/∂A₂:: ParameterizedFunction ∂ϵ₁/∂A₂ = ∂ϵ₂/∂A₁ = 0.0:: Float64 ∂ϵ₃/∂c₀ = 1.0:: Float64 ∂ϵ₃/∂c₁ = t ↦ t:: Function ∂a₁/∂ω = ∂ϵ₁/∂ω + ∂ϵ₂/∂ωamplitude ∂H/∂ω = (∂a₁/∂ω) Ĥ₁ + (∂ϵ₂/∂ω) Ĥ₂:: Generator get_parameter_deriv(H, u, 1) get_parameters 123ArrayPartition

A custom generator type can hold a block in its own fields, in addition to the blocks that it inherits from its controls:

G = ν b†b + (α/2) b†b†bb + ϵ₄(t)(b + b†):: TransmonGenerator get_parameters 12345ArrayPartition contains holds ϵ₄ = A exp(−(t−t₀)²/2σ²):: GaussianControl z :: ComponentVectorAσt₀ w :: ComponentVectorνα aliases w aliases z ∂ϵ₄/∂A:: ParameterizedFunction ∂ϵ₄/∂σ:: ParameterizedFunction ∂ϵ₄/∂t₀:: ParameterizedFunction ∂G/∂σ = (b + b†) ∂ϵ₄/∂σ:: Generator ∂G/∂ν = b†b:: Operator ∂G/∂α = b†b†bb/2:: Operator get_parameter_deriv(G, z, 2) get_parameters 123ArrayPartition

The derivatives with respect to the parameters are obtained via get_parameter_deriv(object, block, i), which returns the derivative with respect to the parameter (block, i). For a derivative generator, get_parameters returns an ArrayPartition whose only block is the block u (respectively z) of the original, which it aliases: the derivative adds no new block.

Identifying a Parameter

A control is identified by object identity, and get_control_deriv(generator, control) relies on that. A parameter is a Float64 inside a block. It has no identity of its own. A parameter is therefore identified by the pair (block, i), that is, the combination of a block and a linear index into it. Blocks are compared with === to determine their identity. This enables sharing without further rules: two controls that hold an identical block, by definition, depend on the same parameters.

Parameter Derivative Functions

There are two functions:

The object can be a control, an amplitude, or a generator, and its derivative is an object of the same kind:

objectDerivativeReturn value of get_parameter_deriv(object, block, i)
Control $ϵ_l(t)$$∂ϵ_l(t)/∂u$0.0 if the control does not depend on the parameter, otherwise a number, a function of t without parameters, or a control that holds block
Amplitude $a_l(t)$$∂a_l(t)/∂u$0.0, a number, or an amplitude
Generator $Ĥ(t)$$∂Ĥ(t)/∂u$nothing if the generator does not depend on the parameter, otherwise a static operator or a Generator

In get_parameter_derivs, the parameters are the parameters of any object, as returned by get_parameters: an ArrayPartition of all the blocks that the object holds or inherits. The result is a vector of the same length, whose element n is the derivative with respect to parameters[n]. The function calls get_parameter_deriv for every parameter (block, i) in the blocks parameters.x. A single block is also accepted as parameters.

Evaluating Derivatives

A derivative is evaluated with evaluate, with the same arguments as the object itself: evaluate(D, t) or evaluate(D, tlist, n), with an optional vals_dict. On a time grid, $∂Ĥ(t)/∂u_n$ and $Ĥ(t)$ are thus evaluated at the same midpoints. The vals_dict argument supplies values of controls, never values of parameters.

Since a derivative references the parameter blocks of the original object, it automatically uses updated parameter values when it is evaluated. For example, for the GaussianControl from How to define a parameterized control, with the derivative method defined in Implementing Parameter Derivatives below:

ϵ = GaussianControl(A=2.0, t0=5.0, sigma=0.5)
H = hamiltonian(σz, (σx, ϵ))

u = get_parameters(H)                # contains the single block ϵ.parameters
derivs = get_parameter_derivs(H, u)  # three generators, each with the single term (σx, ∂ϵ/∂uₙ)
∂H╱∂σ = derivs[3]                    # equivalent to get_parameter_deriv(H, ϵ.parameters, 3)

evaluate(∂H╱∂σ, 4.5)                 # Operator for 2.426 σx
ϵ.parameters.sigma = 0.8             # plug in a new value
evaluate(∂H╱∂σ, 4.5)                 # Operator for 0.803 σx: the existing object follows the new value

Writing to u instead, as an optimizer does with copyto!(u, x), has the same effect, since u aliases the block. Access by name (like sigma) goes through the block, here ϵ.parameters; see How to work with control parameters.

Derivatives of Generators

For a Generator, the derivative is assembled from the derivatives of its amplitudes,

\[\frac{∂Ĥ(t)}{∂u} = \sum_l Ĥ_l \frac{∂a_l(t)}{∂u}\,,\]

and the derivative of an amplitude follows from the chain rule over its controls,

\[\frac{∂a_l(t)}{∂u} = \sum_{l'} \frac{∂a_l(t)}{∂ϵ_{l'}(t)} \frac{∂ϵ_{l'}(t)}{∂u}\,,\]

where the first factor is get_control_deriv(ampl, control). For a non-linear amplitude, the derivative depends on the controls. For example, for ParameterizedAmplitude(ϵ; parameterization=SquareParameterization()),

\[a(t) = ϵ^2(t)\,, \qquad \frac{∂a(t)}{∂u_n} = 2ϵ(t)\frac{∂ϵ(t)}{∂u_n}\,,\]

so the derivative is an amplitude with two controls: $ϵ(t)$ and its derivative.

Thus, the derivative of a generator is the chain rule with the control derivatives,

\[\frac{∂Ĥ(t)}{∂u_n} = \sum_l \frac{∂Ĥ(t)}{∂ϵ_l(t)} \frac{∂ϵ_l(t)}{∂u_n}\,,\]

and both factors are available separately:

controls = get_controls(H)
μ = get_control_derivs(H, controls)                 # one generator or operator per control
J = [get_parameter_derivs(ϵ, u) for ϵ in controls]  # J[l][n] is ∂ϵₗ/∂uₙ, or 0.0

A consumer can use the derivatives from get_parameter_derivs(H, u) as opaque generators, which works for any generator, or use the factors. With $M$ parameters and $L$ controls, the factored form applies $L$ operators to a state and forms $M$ linear combinations, where the opaque derivatives apply up to $M L$ operators. The factored form is valid only if the generator does not hold blocks itself, which holds for the built-in Generator.

Since the derivative of a control holds at most the block of the original control (see the table above), the derivative of any object contains new controls (the derivatives of its controls), but no new blocks. A derivative generator inherits a subset of the blocks of the original, possibly in a different order. Its parameters are joined from those blocks like the parameters of any other object, so they are the same as those of the original only if it inherits every block in the same order. Nothing depends on that, since a parameter is identified by (block, i).

Trajectories and Control Problems

As for get_control_derivs, there are no methods for a Trajectory or a ControlProblem. The pattern is:

u = get_parameters(problem)
for traj in problem.trajectories
    derivs = get_parameter_derivs(traj.generator, u)
    # length(derivs) == length(u); `nothing` where traj.generator does not depend on u[n]
end

There is only one index space, the flat index of whatever vector is passed in. Nothing translates indices between a control, a generator, and the problem: the blocks of u are u.x, and each generator recognizes the blocks that it holds or inherits by identity. This requires get_parameters(problem) to contain every block exactly once, which combine_parameters ensures. A copy of the parameters is not recognized, so all derivatives with respect to a copy are nothing.

All of these functions are meant for setting up an optimization. The call u = get_parameters(problem) combines the blocks into one ArrayPartition without copying values. The call get_parameter_derivs(traj.generator, u) builds one derivative per parameter without creating any new block. During the optimization, an optimizer writes its iterate into the blocks with copyto!(u, x), and the propagation evaluates the derivatives that were built during the setup. The gradient is a vector indexed like u.

Implementing Parameter Derivatives

The author of a ParameterizedFunction implements the two-argument method get_parameter_deriv(control, i), which returns the derivative with respect to control.parameters[i]. For the Gaussian control from the how-to How to define a parameterized control, this could be

using ComponentArrays
using QuantumControl.Controls: ParameterizedFunction
import QuantumControl.Controls: get_parameter_deriv

struct GaussianControl <: ParameterizedFunction
    parameters::ComponentVector{Float64,Vector{Float64},Tuple{Axis{(A=1, t0=2, sigma=3)}}}
end

GaussianControl(; A=1.0, t0=0.0, sigma=1.0) = GaussianControl(ComponentVector(; A, t0, sigma))

function (control::GaussianControl)(t)
    u = control.parameters
    return u.A * exp(-(t - u.t0)^2 / (2 * u.sigma^2))
end

# The derivative ∂ϵ/∂uᵢ is a control that holds the same block
struct GaussianControlDeriv{P} <: ParameterizedFunction
    parameters::P  # the block of the original control, not a copy
    i::Int
end

function (deriv::GaussianControlDeriv)(t)
    u = deriv.parameters  # the values are read when the derivative is called
    g = exp(-(t - u.t0)^2 / (2 * u.sigma^2))
    if deriv.i == 1      # ∂ϵ/∂A
        return g
    elseif deriv.i == 2  # ∂ϵ/∂t₀
        return u.A * g * (t - u.t0) / u.sigma^2
    else                 # ∂ϵ/∂σ
        return u.A * g * (t - u.t0)^2 / u.sigma^3
    end
end

get_parameter_deriv(control::GaussianControl, i) = GaussianControlDeriv(control.parameters, i)

The derivative holds the block of the original control, not a copy, and reads the values from it when it is called. This is what makes the existing derivative follow new parameter values, as shown in Evaluating Derivatives.

The two-argument method returns a number for a constant derivative (including 0.0 for an element of a shared block that the control does not use), any function of t for a derivative that does not depend on the parameters (which is the case for every parameter that enters linearly), or a ParameterizedFunction that holds the block of the original control, like GaussianControlDeriv above. QuantumControl.Interfaces.check_control verifies these requirements, and compares the derivatives with finite differences. The two-argument method is for authors only. Consumers always call the three-argument method.

There is no fallback: a ParameterizedFunction without the two-argument method raises a MethodError, instead of silently having a zero derivative, and fails check_control unless it is called with for_gradient_optimization=false. How a type obtains its derivatives is up to the author. For example, a type that wraps a function f(t, u) can apply automatic differentiation to that function. Everything else is generic: amplitudes (including ShapedAmplitude, GuidedAmplitude, the amplitudes in QuantumControl.PulseParameterizations, and any custom amplitude that supports get_control_deriv) and the built-in Generator need no new methods. The CRABFunction, VariedFrequencyCRABFunction, and StaticParameter have parameter derivatives of any order.

Higher Derivatives

A derivative is a control, an amplitude, or a generator that inherits blocks of the original object. Hence, get_parameter_deriv applies to it again:

∂²H╱∂σ∂t₀ = get_parameter_deriv(∂H╱∂σ, ϵ.parameters, 2)

Whether a particular higher derivative exists depends on the objects involved, and supporting it is optional. For a control, the type that the two-argument method returns needs a two-argument method of its own (for GaussianControlDeriv in the example). If the first derivative is a number or a function without parameters, all further derivatives are 0.0, so a control that is linear in its parameters has higher derivatives without any additional code. For an amplitude that is not linear in its controls, the control derivative must in turn have a control derivative. A Generator needs nothing new. With for_hessian=true, check_control and check_generator also verify the second derivatives.

Static Parameters and Custom Generators

The built-in Generator holds no blocks. A tunable parameter of the drift or of a dissipator is therefore a control that is constant in time, a StaticParameter. For example, for a detuning $Δ$:

Δ = StaticParameter([0.3])
H = hamiltonian((σz, Δ), (σx, ϵ))
get_parameter_derivs(H, get_parameters(H))[1]  # ∂H/∂Δ: the matrix σz

A custom generator type can hold blocks in its own fields. Its get_parameters method combines these blocks with the parameters of its controls, e.g., get_parameters(G::TransmonGenerator) = combine_parameters(G.w, get_parameters(G.control)) for the TransmonGenerator in the diagram above, using combine_parameters (public, but not exported). See How to implement a generator with its own parameters for the complete implementation. For gradient-based optimization, such a type also needs its own three-argument get_parameter_deriv method, which recognizes the blocks that it holds by identity. For example, for a type that holds the block [Δ, Ω] and evaluates to Δ * σz + Ω * σx, that method returns the matrix σz for the first element of the block, the matrix σx for the second, and nothing for any other block. A custom generator type that contains parameterized controls needs the three-argument method as well, as it needs its own get_control_deriv method: the generic assembly covers only the built-in Generator. For the blocks of its controls, the method applies the chain rule. QuantumControl.Interfaces.check_generator with for_parameterization=true verifies the parameter derivatives of a generator.