Difference between revisions of "Optimal control module"

From Spinach Documentation Wiki
Jump to: navigation, search
(small updates to the optimal control toolbox - still needs more updating)
(Updated to the current doc style and updated syntax + options)
Line 1: Line 1:
 
This section contains the description of the functions in the Optimal Control module of Spinach.
 
This section contains the description of the functions in the Optimal Control module of Spinach.
  
−
=Control operators=
+
==Creating an Optimal Control problem==
 +
Optimal control can be thought of as a numerical mathematical optimisation algorithm used to solve a solve a physics problem. To keep this obvious to the user, Spinach divides an optimal control problem into two structures:
 +
# The control problem
 +
#: - containing all physics defining the control system.
 +
# The optimisation problem
 +
#: - defining options, tolerances and an "objective function" to be solved with a root-finding-type numerical method.
 +
The two structures will be defined by the control system in <code>[[control_sys.m|ctrl_sys]]</code> and the optimisation options in <code>[[optim_tols.m|optim]]</code>.
 +
 
 +
===Control operators===
 
A bilinear control system should be split into a drift Hamiltonian (that which is beyond control of the experiment), and control Hamiltonians.
 
A bilinear control system should be split into a drift Hamiltonian (that which is beyond control of the experiment), and control Hamiltonians.
  
Line 8: Line 16:
 
</center>
 
</center>
  
−
where <math>\mathbf{H}_0</math> is the <tt>drift</tt> Hamiltonian, and <math>\mathbf{H}_k</math> are the control operators with time-dependent control amplitudes <math>c_k(t)</math>. The optimal control finds the optimal set of control amplitudes with respect to the overlap of an initial state and a target state.
+
where <math>\mathbf{H}_0</math> is the <code>drift</code> Hamiltonian, and <math>\mathbf{H}_k</math> are the control operators with time-dependent control amplitudes <math>c_k(t)</math>. The optimal control finds the optimal set of control amplitudes with respect to the overlap of an initial state and a target state.
  
−
An example of six control operators for an HCF [[Spin system specification|spin system]] defined with Spinach:
+
An example of six control operators for an HCF <code>[[Spin system specification|spin_system]]</code> defined with Spinach:
  
 
     % Drift Hamiltonian
 
     % Drift Hamiltonian
Line 23: Line 31:
 
     LxF=(LpF+LpF')/2; LyF=(LpF-LpF')/2i;
 
     LxF=(LpF+LpF')/2; LyF=(LpF-LpF')/2i;
  
−
The control operators should be defined as a cell array being a field of a structure <tt>optim</tt>
+
The control operators should be defined as a cell array being a field of a structure <code>ctrl_sys</code>
  
−
     optim.controls={LxH,LyH,LxC,LyC,LxF,LyF};
+
     ctrl_sys.control_ops={LxH,LyH,LxC,LyC,LxF,LyF};
  
−
The structure <tt>optim</tt> should have optimisation and control field names added, to be an input of <tt>[[fminnewton.m]]</tt>, then to be parsed by <tt>[[optim_tols.m]]</tt>.
+
===Initial and target states===
−
 
+
In the context of state-to-state transfer, an initial and target state must be defined. Example syntax for a three <code>[[Spin system specification|spin_system]]</code> stating with all magnetisation on the first spin with the target state of all magnetisation on the third spin:
−
=Initial and target states=
 
−
In the context of state-to-state transfer, an initial and target state must be defined. Example syntax for a three [[Spin system specification|spin system]] stating with all magnetisation on the first spin with the target state of all magnetisation on the third spin:
 
  
 
     % Initial normalised state
 
     % Initial normalised state
 
     rho=[[State.m|state]](spin_system,{'Lz'},{1});  
 
     rho=[[State.m|state]](spin_system,{'Lz'},{1});  
−
     optim.rho=rho/norm(rho);
+
     ctrl_sys.rho=rho/norm(rho);
 
      
 
      
 
     % Desired normalised target state
 
     % Desired normalised target state
 
     target=[[State.m|state]](spin_system,{'Lz'},{3});  
 
     target=[[State.m|state]](spin_system,{'Lz'},{3});  
−
     optim.target=target/norm(target);
+
     ctrl_sys.target=target/norm(target);
  
−
The metric to measure the overlap of the current state from the optimised waveform with the target state is the fidelity (or the cost):
+
===Initial guess===
−
 
 
−
<math>J=\left\langle\texttt{target}\middle|\texttt{rho}\right\rangle</math>
 
−
 
 
−
It is useful to define a function to minimise, rather than maximise (as above). For this reason, the optimal control module uses a functional metric of overlap as the infidelity, defined as
 
−
 
 
−
<math>1-J=1-\left\langle\texttt{target}\middle|\texttt{rho}\right\rangle</math>
 
−
 
 
−
which would find a minimum at <math>1-J=0</math>
 
−
 
 
−
=Initial guess=
 
 
The [[grape.m|GRAPE]] optimal control relies on the piecewise constant approximation, where the total time allowed for the optimisation is split into <tt>nsteps</tt> time points. Although the [[krotov.m|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.
 
The [[grape.m|GRAPE]] optimal control relies on the piecewise constant approximation, where the total time allowed for the optimisation is split into <tt>nsteps</tt> time points. Although the [[krotov.m|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.
  
Line 58: Line 54:
  
 
     % Power level
 
     % Power level
−
     optim.power_level=2*pi*10e3;
+
     ctrl_sys.power_level=2*pi*10e3;
 
      
 
      
 
     % Pulse duration (seconds)
 
     % Pulse duration (seconds)
−
     optim.pulse_duration=0.100;
+
     ctrl_sys.pulse_duration=0.100;
 
      
 
      
 
     % Number of time steps
 
     % Number of time steps
−
     optim.nsteps=50;
+
     ctrl_sys.nsteps=50;
 
      
 
      
 
     % Calculate the time step
 
     % Calculate the time step
−
     optim.time_step=pulse_duration/nsteps;
+
     ctrl_sys.time_step=ctrl_sys.pulse_duration/ctrl_sys.nsteps;
 
      
 
      
 
     % Take a random guess for the pulse
 
     % Take a random guess for the pulse
−
     guess=randn(numel(optim.controls),optim.nsteps)/10;
+
     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.
 
  
−
=Objective function=
+
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:
−
The fundamental basis of the optimal control module is to define a cost function (also named an objective function), which returns the cost, gradient, and Hessian.
 
  
−
     function [cost,grad,hess] = [[cost_function]](objective)
+
     ctrl_sys=[[control_sys.m|control_sys]](spin_system,ctrl_sys);
  
−
Spinach provides the GRAPE and Krotov methods of optimal control.
+
===Objective function===
 +
The fundamental basis of the optimal control module is to define a objective function (also named an cost function), which returns the fidelity cost and its gradient with respect to controls.
  
−
=Numerical optimisation=
+
    function [cost,cost_gradient] = [[cost_function]](waveform)
−
fminnewton.m contains an implementation of steepest descent, quasi‐Newton (SR1, BFGS and LBFGS), and Newton‐Raphson minimization algorithms. Works in much the same way as Matlab's built‐in [http://uk.mathworks.com/help/optim/ug/fmincon.html fmincon], but allows for much greater flexibility, such as passing the initial Hessian guess. An example syntax could be:
 
  
−
    options = struct('method','newton','terminal_fidelity',1-eps,'max_iterations',Inf,'tol_x',eps,'tol_gfx',eps);
+
The cost function handle should not be anonymous - use nested, local, or simple functions. An example of this objective function using [[grape.m|GRAPE]]:
−
    [[fminnewton]](@cost_function,guess,options);
 
  
−
 
+
     function [cost,cost_grad] = [[cost_function]](waveform)
−
The options structure is very similar to Matlab's built‐in [http://uk.mathworks.com/help/optim/ug/optimset.html optimset] and is created with Spinach's [[optim_tols.m]].
 
−
 
 
−
==Example==
 
−
The following is a simple code to optimise state transfer between to species in a chain of three spins (assuming [[Spin system specification|<tt>spin_system</tt>]] is already defined):
 
−
 
 
−
    % Drift Hamiltonian
 
−
    drift=[[Hamiltonian.m|hamiltonian]](assume(spin_system,'nmr'));
 
−
   
 
−
    LpH=[[Operator.m|operator]](spin_system,'L+','1H');
 
−
    LpC=[[Operator.m|operator]](spin_system,'L+','13C');
 
−
    LpF=[[Operator.m|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;
 
−
   
 
−
    rho=[[State.m|state]](spin_system,{'Lz'},{1});
 
−
    target=[[State.m|state]](spin_system,{'Lz'},{3});
 
−
   
 
−
    optim.controls={LxH,LyH,LxC,LyC,LxF,LyF};    % Control operators
 
−
    optim.rho=rho/norm(rho);                      % Initial normalised state
 
−
    optim.target=target/norm(target);            % Desired normalised target state
 
−
    optim.power_level=2*pi*10e3;                  % Power level (rad/s)
 
−
    optim.pulse_duration=0.100;                  % Pulse duration (seconds)
 
−
     optim.nsteps=50;                              % Number of time steps
 
−
    optim.time_step=pulse_duration/nsteps;        % Calculate the time step
 
−
   
 
−
    % Take a random guess for the pulse
 
−
    guess=randn(numel(optim.controls),optim.nsteps)/10;
 
−
   
 
−
    optim.method=newton;
 
−
    optim.terminal_fidelity=1-eps;
 
−
    optim.max_iterations=Inf;
 
−
    optim.tol_x=eps;
 
−
    optim.tol_gfx=eps;
 
−
   
 
−
    [[fminnewton]](@cost_function,guess,optim);
 
−
   
 
−
          function [cost,grad,hess] = [[cost_function]](objective)
 
−
             
 
−
              [data,cost,grad,hess]=[[grape]](spin_system,drift,optim.controls,waveform,...
 
−
                    optim.time_step,optim.nsteps,optim.rho,optim.target,optim.power_level);
 
 
                
 
                
−
              [penalty_cost,penalty_grad,penalty_hess]=...
+
          [~,fidelity,gradient]=[[grape]](spin_system,ctrl_sys,drift,waveform);
−
                    [[penalty]](waveform,'SNS',-ones(size(waveform)),ones(size(waveform)));
 
 
                
 
                
−
              cost=cost+penalty_cost;
+
          [pen_cost,pen_grad]=[[penalty]](waveform,'SNS',-ones(size(waveform)),ones(size(waveform)));
−
              grad=grad+penalty_grad;
 
−
              hess=hess+penalty_hess;
 
 
                
 
                
−
           end
+
           cost=fidelity+pen_cost;
−
   
+
          cost_grad=gradient+pen_grad;
−
 
+
         
−
Note: this example uses the optim structure to collect all options into fminnewton - it is equivalent to using matlab's struct function. The cost_function is supplied as a nested function in this example, although a local function or simple function could also be used.
+
    end
−
 
 
−
==Optimal control toolbox function==
 
−
 
 
−
=Control functions=
 
  
−
[[control_sys.m]]
+
===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:
  
−
[[grape.m]]
+
    options = struct('method','grad_ascent','max_iterations',100);
 +
    [oc_waveform,fidelity]=[[fminnewton]](@cost_function,guess,optim);
  
−
[[krotov.m]]
+
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 <code>oc_waveform</code> is the optimal waveform found after 100 iterates of the gradient ascent algorithm, with its resulting <code>fidelity</fidelity>.
  
−
[[penalty.m]]
+
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]].
  
−
=Optimisation functions=
+
==Notes==
  
−
[[fminkrotov.m]]
+
==See also==
  
−
[[fminnewton.m]]
+
'''Control functions'''
  
−
[[fminsimplex.m]]
+
[[control_sys.m]], [[grape.m]], [[krotov.m]], [[penalty.m]]
  
−
[[hessprep.m]]
+
'''Optimisation functions'''
  
−
[[hessreg.m]]
+
[[fminkrotov.m]], [[fminnewton.m]], [[fminsimplex.m]], [[hessprep.m]], [[hessreg.m]], [[lbfgs.m]], [[linesearch.m]], [[objeval.m]], [[optim_report.m]], [[optim_tols.m]],  [[quasinewton.m]]
  
−
[[lbfgs.m]]
+
'''Other relevant functions'''
  
−
[[linesearch.m]]
+
[[hess_reorder.m]], [[dirdiff.m]], [[step.m]]
  
−
[[optim_report.m]]
 
  
−
[[quasinewton.m]]
+
''Revision 3399, authors: [[Ilya Kuprov]], [[David Goodwin]]''

Revision as of 09:50, 3 September 2016

This section contains the description of the functions in the Optimal Control module of Spinach.

Creating an Optimal Control problem

Optimal control can be thought of as a numerical mathematical optimisation algorithm used to solve a solve a physics problem. To keep this obvious to the user, Spinach divides an optimal control problem into two structures:

  1. The control problem
    - containing all physics defining the control system.
  2. The optimisation problem
    - defining options, tolerances and an "objective function" to be solved with a root-finding-type numerical method.

The two structures will be defined by the control system in ctrl_sys and the optimisation options in optim.

Control operators

A bilinear control system should be split into a drift Hamiltonian (that which is beyond control of the experiment), and control Hamiltonians.

\(\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 amplitudes \(c_k(t)\). The optimal control finds the optimal set of control amplitudes with respect to the overlap of an initial state and a target state.

An example of six control operators for an HCF spin_system defined with Spinach:

    % 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;

The control operators should be defined as a cell array being a field of a structure ctrl_sys

    ctrl_sys.control_ops={LxH,LyH,LxC,LyC,LxF,LyF};

Initial and target states

In the context of state-to-state transfer, an initial and target state must be defined. Example syntax for a three spin_system stating with all magnetisation on the first spin with the target state of all magnetisation on the third spin:

    % Initial normalised state
    rho=state(spin_system,{'Lz'},{1}); 
    ctrl_sys.rho=rho/norm(rho);
    
    % Desired normalised target state
    target=state(spin_system,{'Lz'},{3}); 
    ctrl_sys.target=target/norm(target);

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);

Objective function

The fundamental basis of the optimal control module is to define a objective function (also named an cost function), which returns the fidelity cost and its gradient with respect to controls.

    function [cost,cost_gradient] = cost_function(waveform)

The cost function handle should not be anonymous - use nested, local, or simple functions. An example of this objective function using GRAPE:

    function [cost,cost_grad] = cost_function(waveform)
              
         [~,fidelity,gradient]=grape(spin_system,ctrl_sys,drift,waveform);
              
         [pen_cost,pen_grad]=penalty(waveform,'SNS',-ones(size(waveform)),ones(size(waveform)));
              
         cost=fidelity+pen_cost;
         cost_grad=gradient+pen_grad;
         
    end

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:

    options = struct('method','grad_ascent','max_iterations',100);
    [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</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


Revision 3399, authors: Ilya Kuprov, David Goodwin