  We develop a framework of "semi-automatic differentiation" that combines
existing gradient-based methods of quantum optimal control with automatic
differentiation. The approach allows to optimize practically any computable
functional and is implemented in two open source Julia packages, GRAPE.jl and
Krotov.jl, part of the QuantumControl.jl framework. Our method is based on
formally rewriting the optimization functional in terms of propagated states,
overlaps with target states, or quantum gates. An analytical application of the
chain rule then allows to separate the time propagation and the evaluation of
the functional when calculating the gradient. The former can be evaluated with
great efficiency via a modified GRAPE scheme. The latter is evaluated with
automatic differentiation, but with a profoundly reduced complexity compared to
the time propagation. Thus, our approach eliminates the prohibitive memory and
runtime overhead normally associated with automatic differentiation and
facilitates further advancement in quantum control by enabling the direct
optimization of non-analytic functionals for quantum information and quantum
metrology, especially in open quantum systems. We illustrate and benchmark the
use of semi-automatic differentiation for the optimization of perfectly
entangling quantum gates on superconducting qubits coupled via a shared
transmission line. This includes the first direct optimization of the
non-analytic gate concurrence.
