Splitting and Parallelization
A gradient generator with $K$ derivatives (with respect to controls or control parameters) propagates a gradient vector of dimension $N(K + 1)$, for a Hilbert space of dimension $N$. Since the gradient states couple only to the state, never to each other, split_gradgen can split the gradient generator into smaller ones for groups of the derivatives, which are propagated independently, e.g., one per thread, and combine_gradvecs reassembles the gradient vector. This page explains why this works, for which propagators the result is identical to the propagation under the full gradient generator, how the speedup scales, and how to choose the number of groups. For an example, see How to propagate a gradient vector in parallel.
Contents
- Decoupling of the gradient states
- Exactness of the split propagation
- Cost model
- Choosing the number of groups
Decoupling of the gradient states
For a gradient generator $G$ with the dynamical generator $\hat{H}$ and the derivatives $\hat{μ}_k = \partial \hat{H} / \partial x_k$ ($k = 1, \dots, K$) with respect to the variables $x_k$ = G.wrt[G.wrt_indices[k]] (controls or control parameters), and a gradient vector $Ψ$ with the gradient states $|Ψ^\prime_k⟩$ and the state $|Ψ⟩$,
\[G Ψ = \begin{pmatrix} \hat{H} |Ψ^\prime_1⟩ + \hat{μ}_1 |Ψ⟩ \\ \vdots \\ \hat{H} |Ψ^\prime_K⟩ + \hat{μ}_K |Ψ⟩ \\ \hat{H} |Ψ⟩ \end{pmatrix}\,.\]
Each gradient state $|Ψ^\prime_k⟩$ is driven only by itself and by the state $|Ψ⟩$, and the state evolves under $\hat{H}$ alone. As shown in the reduction to a single control on the Background page, the pair $(|Ψ^\prime_k⟩, |Ψ⟩)$ thus evolves exactly as it would under the single-derivative gradient generator $G_k = \left(\begin{smallmatrix} \hat{H} & \hat{μ}_k \\ 0 & \hat{H} \end{smallmatrix}\right)$, in the exact time evolution, and in any numerical propagation whose operations do not depend on the gradient states (see Exactness of the split propagation). Hence, for any partition of the derivatives into groups $\mathcal{K}_1, \dots, \mathcal{K}_p$, the gradient generator $G_{\mathcal{K}_g}$ with the derivatives $\hat{μ}_k$ for $k \in \mathcal{K}_g$ propagates the gradient states $|Ψ^\prime_k⟩$, $k \in \mathcal{K}_g$, exactly as $G$ does, provided that each group starts from the same state $|Ψ⟩$. The groups can be propagated independently, each with its own copy of the state.
In the code, split_gradgen(G, n) splits the derivatives into $p = \min(n, K)$ contiguous groups, in the order of G.wrt_indices, whose sizes differ by at most one. The gradient generators Gs[g] of the groups share the dynamical generator G.H, the variables G.wrt, and the derivatives with G, so no derivatives are recomputed. The method split_gradgen(G, Ψ, n) also returns independent copies Ψs[g] of the gradient vector for each group. Since Gs[g].wrt_indices holds the indices of the derivatives of the group in G.wrt, Ψs[g].grad_states[j] is the derivative with respect to G.wrt[Gs[g].wrt_indices[j]].
Exactness of the split propagation
For the exact time evolution, the decoupling is a direct consequence of the block structure. For a numerical propagation, it holds whenever the operations that the propagator applies to a gradient vector do not depend on the gradient states. This is the case for
- the matrix exponential of
ExpProp, - the Chebychev propagator (
Cheby), if every group uses the same spectral range: with explicitE_minandE_max, or with the defaultspecrange_method = :autofor a Hilbert space dimension of at most 32, where QuantumPropagators obtains the spectral range of $\hat{H}$ from an exact diagonalization, and - an ODE solver whose time steps are chosen from the state alone, with the default
frozen_internalnorm(the frozen semantics).
For these, the split propagation is identical to the propagation under the full gradient generator, up to rounding errors.
A Krylov method like the Newton propagator (Newton) or the Krylov propagation of ExponentialUtilities builds its subspace from the entire gradient vector of a group, including its gradient states, and an ODE solver with flat_internalnorm (the flat semantics) chooses its time steps from it. The result for each group then depends on the gradient states in that group, and agrees with the propagation under $G$ only within the tolerance of the propagator. The same holds for Cheby in a Hilbert space of dimension larger than 32, where specrange_method = :auto estimates the spectral range with an Arnoldi iteration from a random starting vector. In particular, the state $|Ψ⟩$ differs slightly between the groups, and combine_gradvecs takes the state from the first group.
Cost model
Splitting is most useful for a gradient generator with respect to control parameters, e.g., for GOAT. A control problem typically has many more parameters than controls, so the gradient generator has many derivatives, and the propagation of the gradient states makes up most of the work.
For most propagators, the cost is dominated by applying the gradient generator to a gradient vector. With $K$ derivatives, this takes $K + 1$ applications of $\hat{H}$ and $K$ applications of a derivative $\hat{μ}_k$. Let $r$ be the cost of applying a derivative relative to the cost of applying $\hat{H}$. The cost of applying $G$ is then proportional to $K + 1 + rK$.
For a dynamical generator $\hat{H}(t) = \hat{H}_0 + \sum_{l=1}^{L} ϵ_l(t) \hat{H}_l$ with $L$ controls, the $k$'th derivative, with respect to a parameter $u$ of the control $ϵ_l$, is $\hat{μ}_k = (\partial ϵ_l / \partial u) \hat{H}_l$. Applying it costs one application of the control Hamiltonian $\hat{H}_l$, while applying $\hat{H}(t)$ applies all $L + 1$ terms (as for the Operator that a QuantumPropagators Generator evaluates to, which applies its terms one by one). For dense operators, this gives $r = 1/(L + 1)$, i.e., $r = 1/2$ for a single control. For sparse operators, $r$ is roughly the number of non-zero elements of $\hat{H}_l$ relative to the total number of non-zero elements of all terms of $\hat{H}(t)$. This is smaller than $1/(L + 1)$ for a control Hamiltonian that is sparser than the drift $\hat{H}_0$, and larger for a diagonal drift.
When $K$ derivatives are split into $p = \min(n, K)$ groups whose sizes differ by at most one, the largest group has $m = \lceil K/n \rceil$ derivatives. Propagating the $p$ groups in parallel on $n$ threads, with at most one thread per core, takes the time of the largest group. For a propagator whose cost is linear in the number of applications of the generator, and which applies the generator equally often in all groups (Cheby and ODE solvers with frozen_internalnorm exactly, Newton, ExponentialUtilities, and ODE solvers with flat_internalnorm approximately), the ideal speedup is
\[\begin{equation}\label{eq:split-speedup} S(K, n) = \frac{K + 1 + rK}{m + 1 + rm}\,, \qquad m = \lceil K/n \rceil\,. \end{equation}\]
For $n > 1$, the speedup is below the linear speedup $n$, since every group applies $\hat{H}$ to its own copy of the state. The total work grows from $K + 1 + rK$ to $K + p + rK$, in units of one application of $\hat{H}$, so in the model, propagating the groups one after the other is slower than propagating the full gradient vector, by a factor of $(K + p + rK)/(K + 1 + rK)$.
The speedup depends on $n$ only through $m$, and reaches its maximum $(K + 1 + rK)/(2 + r)$ for $n \ge K$, when every group has a single derivative. For $K = 20$ parameters of a single control ($r = 1/2$), with up to 32 threads:
The top panel shows the ideal speedup $S$ (blue) as a function of the number of threads $n$, and the linear speedup $n$ (dashed). The bottom panel shows the ratio $S/n$ in percent. Since all $n$ from 10 to 19 give $m = 2$, they have the same speedup $S = 7.75$, and all $n \ge 20$ give $m = 1$ and the maximum $S = 12.4$.
The ideal speedup of Eq. \eqref{eq:split-speedup} does not account for the following effects:
- The Arnoldi iteration of a Krylov method orthogonalizes each new vector against all previous ones, at a cost that scales with the length of the gradient vector, and the dimension of the Krylov subspace (and thus the number of applications of the generator) may differ between the groups.
- With the flat semantics, the adaptive time steps of an ODE solver may differ between the groups, so that the groups do not take the same time.
- Every group evaluates the dynamical generator at every time step, into its own
GradgenOperator. Together with other fixed costs of a time step, such as allocations, this is a large part of the work for small Hilbert spaces. - The threads share hardware, such as memory bandwidth and caches. How much this limits the speedup depends on the machine and on the problem. On the other hand, a group works on less memory than the full gradient generator and may use the caches better, so the measured speedup can also exceed the model.
Choosing the number of groups
Propagate one group per thread. With $n$ = Threads.nthreads() (at most the number of physical cores), the ideal speedup of Eq. \eqref{eq:split-speedup} is the largest possible, since it never decreases with $n$. Since the speedup depends on $n$ only through $m = \lceil K/n \rceil$, the same speedup is possible with only $\lceil K/m \rceil$ groups and threads, e.g., with 10 instead of 16 for $K = 20$. The remaining threads are then free for other work.
When propagating the groups on threads, turn off the multithreading of BLAS:
using LinearAlgebra
BLAS.set_num_threads(1)For dense operators, every application of $\hat{H}$ and of the derivatives calls BLAS, which by default runs on several threads of its own. While $n$ threads propagate their groups at the same time, these BLAS threads compete with them for the same cores. With a single BLAS thread, every Julia thread does its own linear algebra without starting further BLAS threads. A BLAS library that replaces Julia's default OpenBLAS may not honor BLAS.set_num_threads, so check its documentation for how to limit it to a single thread. For example, Apple Accelerate (via AppleAccelerate.jl) ignores BLAS.set_num_threads and needs AppleAccelerate.set_num_threads(1).
Splitting pays off for many derivatives, not for a few: by Eq. \eqref{eq:split-speedup}, the speedup for $K = 2$ is $(3 + 2r)/(2 + r)$, at most 5/3 for $r \le 1$, and always below 2. For small Hilbert spaces, the fixed cost of every time step can keep the speedup well below the model.
How close the speedup comes to the model depends on the machine and on the problem. Always benchmark on the actual machine, with the actual operators, states, and propagator, and compare the absolute runtimes with and without splitting. Understand the settings for each level of parallelization, and choose them deliberately: the number of Julia threads (julia -t or JULIA_NUM_THREADS), the number of groups, and the number of BLAS threads (BLAS.set_num_threads, or environment variables like OPENBLAS_NUM_THREADS, depending on the BLAS library). The benchmark folder of the repository has a benchmark with random operators as a starting point.