Optimal control module

From Spinach Documentation Wiki
Revision as of 15:39, 21 July 2017 by Admin (talk | contribs) (→‎Stage 4: put together the control structure)
Jump to: navigation, search

This section contains instructions on setting up optimal control calculations in Spinach. The package contains an implementation of the gradient ascent pulse engineering (GRAPE) method, as well as its LBFGS-GRAPE and Newton-GRAPE enhancements. Multiple examples are available in examples/optimal_control directory of the standard distribution.

Stage 1: specify the spin system

The usual spin system specification should be done. For example, if we are interested in performing magnetisation transfer from one spin to another in a peptide chain in a liquid, the NMR calculation for that chain needs to be set up:

    % Magnetic field
    sys.magnet=9.4;

    % Spin system
    sys.isotopes={'15N','1H','13C','13C','13C','15N'};

    % Chemical shifts, ppm
    inter.zeeman.scalar={0.0, 0.0, 55.0, 30.0, 175.0, 0.0};

    % Scalar couplings, Hz (literature values)
    inter.coupling.scalar=cell(6);
    inter.coupling.scalar{1,3}=-11; 
    inter.coupling.scalar{2,3}=140;
    inter.coupling.scalar{3,4}=35;
    inter.coupling.scalar{3,5}=55;
    inter.coupling.scalar{3,6}=7;
    inter.coupling.scalar{5,6}=-15;

    % Basis set
    bas.formalism='sphten-liouv';
    bas.approximation='IK-0';
    bas.level=4;

    % Spinach housekeeping
    spin_system=create(sys,inter);
    spin_system=basis(spin_system,bas);

Stage 2: get initial and target states

You need to request your initial state and your desired target states from Spinach. The states must then be normalised. For example, if we are looking to move all magnetisation from Lz on spin 2 to Lz on spin 5, the following needs to be written:

    % Set up and normalise the initial state
    rho_init=state(spin_system,{'Lz'},{2});
    rho_init=rho_init/norm(full(rho_init),2);
    % Set up and normalise the target state
    rho_targ=state(spin_system,{'Lz'},{5});
    rho_targ=rho_targ/norm(full(rho_targ),2);

The first line of each block calls the usual state.m function, the the second line divides the resulting state vector by its 2-norm. If you have multiple source states that must be transferred to multiple destination states, put the corresponding vectors into cell arrays.

Stage 3: get drift and control operators

In an optimal control problem, the Hamiltonian is split into the part that the instrument cannot change (called drift Hamiltonian) and the operators whose coefficients the instrument can control (called control operators):

\(\mathbf{H}=\mathbf{H}_0+\sum\limits_{k=1}^K c_k(t)\mathbf{H}_k\)

where \(\mathbf{H}_0\) is the drift Hamiltonian, and \(\mathbf{H}_k\) are the control operators with time-dependent control coefficients \(c_k(t)\). In a typical NMR experiment, the drift Hamiltonian contains chemical shifts and J-couplings, and control operators are Lx and Ly spin operators. All of these should be requested from Spinach as usual: the Hamiltonian is obtained from hamiltonian.m (if relaxaton is present, you should also call relaxation.m), and spin operators are obtained by calling operator.m function. In our peptide system, we would like Lx and Ly operators on carbon, nitrogen and protons, therefore:

    % Drift Hamiltonian
    drift=hamiltonian(assume(spin_system,'nmr'));
    
    % Control operators
    LpH=operator(spin_system,'L+','1H');
    LpC=operator(spin_system,'L+','13C');
    LpF=operator(spin_system,'L+','19F');
    LxH=(LpH+LpH')/2; LyH=(LpH-LpH')/2i;
    LxC=(LpC+LpC')/2; LyC=(LpC-LpC')/2i;
    LxF=(LpF+LpF')/2; LyF=(LpF-LpF')/2i;

If you have an ensemble with a different drift Hamiltonian in each system, put those Hamiltonians into a cell array.

Stage 4: put together the control structure

Spinach requires the user to specify all optimal control related options in a single structure called control. The following fields are available:

Optimal control parameter structure
Field name Mandatory/Optional Content
control.fidelity O A character string specifying the type of fidelity functional. Specify 'real' for the real part of the overlap between the final state and the target state, 'imag' for the imaginary part, and 'square' for the absolute square of the overlap. The default is 'real'.
control.operators M A cell array of matrices of control operators. If phase-amplitude optimisation or plotting is intended, these must be in the order {L1x,L1y,L2x,L2y,...}.
control.drifts M A cell array containing drift Hamiltonian matrices. If a single system is treated, there should be only one matrix; if there is an ensemble (with, for example, different J-couplings in each member), the cell array should contain multiple matrices.
control.rho_init M A cell array of initial states. For example, if state vector A should be moved into state vector X, and state vector B into state vector Y, your specification for the initial states should be {A,B}.
control.rho_targ M A cell array of target states. For example, if state vector A should be moved into state vector X, and state vector B into state vector Y, your specification for the target states should be {X,Y}.
control.pulse_dur M Control pulse duration in seconds.
control.pulse_nsteps M Number of time slices in the control pulse, an even integer.
control.pwr_levels M A vector of control power levels in the ensemble, in rad/s. These correspond to things like non-uniform B1 field: the amplifier outputs some nominal power, but it is felt differently in different parts of the sample.
control.offsets M dfg
control.offset_operator M dfg
control.basis M dfg
control.penalties M dfg
control.p_weights M dfg
control.u_bound M dfg
control.l_bound M dfg
control.plotting M dfg

Note that there are four independent ensembles: the first one for source-target pairs, the second one for drifts, the third one for control powers and the fourth one for offsets. These ensembles are kroneckered together. So, if you have two state-target pairs, ten different drift Hamiltonians, five power levels and five offsets, your ensemble would have 2x10x5x5=500 systems in it!

Initial guess=

The GRAPE optimal control relies on the piecewise constant approximation, where the total time allowed for the optimisation is split into nsteps time points. Although the Krotov optimal control does not rely on the piecewise constant approximation, time should also be split into (relatively small) time points, which numerically approximate continuous time.

An initial guess needs to be provided to any optimisation. In Spinach, the initial guess should be a matrix of size kxn, where k is the number of control operators, and n is the number of time points.

Syntax example for the above HCF spin system:

    % Power level
    ctrl_sys.power_level=2*pi*10e3;
    
    % Pulse duration (seconds)
    ctrl_sys.pulse_duration=0.100;
    
    % Number of time steps
    ctrl_sys.nsteps=50;
    
    % Calculate the time step
    ctrl_sys.time_step=ctrl_sys.pulse_duration/ctrl_sys.nsteps;
    
    % Take a random guess for the pulse
    guess=randn(numel(ctrl_sys.control_ops),ctrl_sys.nsteps)/10;

Note that it is useful to scale the initial guess, in this case by dividing by 10. The final stage in defining the control system is to collect and check the parameters into a single, well defined, structure:

    ctrl_sys=control_sys(spin_system,ctrl_sys);

Numerical optimisation

fminnewton.m contains an implementation of steepest ascent/descent, quasi‐Newton (SR1, BFGS and LBFGS), and Newton‐Raphson minimisation/maximisation algorithms. An example to maximise the above example is:

    % define the optimisation algorithm and maximum iterations
    optim = struct('method','grad_ascent','max_iterations',100);
    
    % run the optimisation calling the cost_function with a handle
    [oc_waveform,fidelity]=fminnewton(@cost_function,guess,optim);

The console output for this example shows iteratively better solutions to control the system from its initial state to its target state. In this case oc_waveform is the optimal waveform found after 100 iterates of the gradient ascent algorithm, with its resulting fidelity.

The gradient ascent algorithm does not have good convergence characteristics and it takes a large amount of iterates to get good solutions. Further examples, with better convergence, are outlined in Optimal control with GRAPE.

Notes

See also

Control functions

control_sys.m, grape.m, krotov.m, penalty.m

Optimisation functions

fminkrotov.m, fminnewton.m, fminsimplex.m, hessprep.m, hessreg.m, lbfgs.m, linesearch.m, objeval.m, optim_report.m, optim_tols.m, quasinewton.m

Other relevant functions

hess_reorder.m, dirdiff.m, step.m


Version 2.0, authors: Ilya Kuprov, David Goodwin