Calculation of control fields for state-to-state transfer of a 2 qubit system using CRAB algorithm

Jonathan Zoller (jonathan.zoller@uni-ulm.de)

Example to demonstrate using the control library to determine control pulses using the ctrlpulseoptim.optimize_pulse_unitary function. The CRAB algorithm is used to optimize pulse shapes to minimize the fidelity error, which is equivalent maximising the fidelity to an optimal value of 1.

The system in this example are two qubits, where the interaction can be controlled. The target is to perform a pure state transfer from a down-down state to an up-up state.

The user can experiment with the timeslicing, by means of changing the number of timeslots and/or total time for the evolution. Different initial (starting) pulse types can be tried as well as boundaries on the control and a smooth ramping of the pulse when switching the control on and off (at the beginning and close to the end). The initial and final pulses are displayed in a plot

An in depth discussion of using methods of this type can be found in [1,2]


In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import datetime

In [2]:
from qutip import Qobj, identity, sigmax, sigmaz, tensor
import random
import qutip.logging as logging
logger = logging.get_logger()
#Set this to None or logging.WARN for 'quiet' execution
log_level = logging.INFO
#QuTiP control modules
import qutip.control.pulseoptim as cpo

example_name = '2qubitInteract'


You are in the installation directory. Change directories before running QuTiP.

Defining the physics

The dynamics of the system are governed by the combined Hamiltonian: H(t) = H_d + sum(u1(t)Hc1 + u2(t)Hc2 + ....) That is the time-dependent Hamiltonian has a constant part (called here the drift) and time vary parts, which are the control Hamiltonians scaled by some functions u_j(t) known as control amplitudes In this example we describe an Ising like Hamiltonian, encompassing random coefficients in the drift part and controlling the interaction of the qubits:

$ \hat{H} = \sum_{i=1}^2 \alpha_i \sigma_x^i + \beta_i \sigma_z^i + u(t) \cdot \sigma_z \otimes \sigma_z $

Initial $\newcommand{\ket}[1]{\left|{#1}\right\rangle} \ket{\psi_0} = \text{U_0}$ and target state $\ket{\psi_t} = \text{U_targ}$ are chosen to be:

$ \ket{\psi_0} = \begin{pmatrix} 1 \\ 0 \\ 0 \\ 0 \end{pmatrix}$

$ \ket{\psi_t} = \begin{pmatrix} 0 \\ 0 \\ 0 \\ 1 \end{pmatrix}$


In [3]:
random.seed(20)
alpha = [random.random(),random.random()]
beta  = [random.random(),random.random()]

Sx = sigmax()
Sz = sigmaz()

H_d = (alpha[0]*tensor(Sx,identity(2)) + 
      alpha[1]*tensor(identity(2),Sx) +
      beta[0]*tensor(Sz,identity(2)) +
      beta[1]*tensor(identity(2),Sz))
H_c = [tensor(Sz,Sz)]
# Number of ctrls
n_ctrls = len(H_c)

U_0 = Qobj(np.array([[1], 
                   [0], 
                   [0], 
                   [0]]))

U_targ = Qobj(np.array([0, 0, 0, 1]))

Defining the time evolution parameters

To solve the evolution the control amplitudes are considered constant within piecewise timeslots, hence the evolution during the timeslot can be calculated using U(t_k) = expm(-iH(t_k)dt). Combining these for all the timeslots gives the approximation to the evolution from an initial state $\psi_0$ at t=0 to U(T) at the t=evo_time. The number of timeslots and evo_time have to be chosen such that the timeslot durations (dt) are small compared with the dynamics of the system.


In [4]:
# Number of time slots
n_ts = 100
# Time allowed for the evolution
evo_time = 18

Set the conditions which will cause the pulse optimisation to terminate

At each iteration the fidelity of the evolution is tested by comparaing the calculated evolution U(T) with the target U_targ. For unitary systems such as this one this is typically: f = normalise(overlap(U(T), U_targ)). The maximum fidelity (for a unitary system) calculated this way would be 1, and hence the error is calculated as fid_err = 1 - fidelity. As such the optimisation is considered completed when the fid_err falls below such a target value.

In some cases the optimisation either gets stuck in some local minima, or the fid_err_targ is just not achievable, therefore some limits are set to the time/effort allowed to find a solution.

The algorithm uses the CRAB algorithm to determine optimized coefficients that lead to a minimal fidelity error. The underlying optimization procedure is set to be the Nelder-Mead downhill simplex. Therefore, when all vertices shrink together, the algorithm will terminate.


In [5]:
# Fidelity error target
fid_err_targ = 1e-3
# Maximum iterations for the optisation algorithm
max_iter = 100
# Maximum (elapsed) time allowed in seconds
max_wall_time = 120

Set the initial pulse type

The control amplitudes must be set to some initial values. Typically these are just random values for each control in each timeslot. These do however result in erratic optimised pulses. For this example, a solution will be found for any initial pulse, and so it can be interesting to look at the other initial pulse alternatives.


In [6]:
# pulse type alternatives: RND|ZERO|LIN|SINE|SQUARE|SAW|TRIANGLE|
p_type = 'DEF'

Give an extension for output files


In [7]:
#Set to None to suppress output files
f_ext = "{}_n_ts{}_ptype{}.txt".format(example_name, n_ts, p_type)

Run the optimisation

In this step, the actual optimization is performed. At each iteration the Nelder-Mead algorithm calculates a new set of coefficients that improves the currently worst set among all set of coefficients. For details see [1,2] and a textbook about static search methods. The algorithm continues until one of the termination conditions defined above has been reached. If undesired results are achieved, rerun the algorithm and/or try to change the number of coefficients to be optimized for, as this is a very crucial parameter.


In [8]:
result = cpo.opt_pulse_crab_unitary(H_d, H_c, U_0, U_targ, n_ts, evo_time, 
                fid_err_targ=fid_err_targ, 
                max_iter=max_iter, max_wall_time=max_wall_time, 
                init_coeff_scaling=5.0, num_coeffs=5, 
                method_params={'xtol':1e-3},
                guess_pulse_type=None, guess_pulse_action='modulate',
                out_file_ext=f_ext,
                log_level=log_level, gen_stats=True)


INFO:qutip.control.pulseoptim:System configuration:
Drift Hamiltonian:
[[ 1.67112549+0.j  0.68625416+0.j  0.90563968+0.j  0.00000000+0.j]
 [ 0.68625416+0.j -0.13810698+0.j  0.00000000+0.j  0.90563968+0.j]
 [ 0.90563968+0.j  0.00000000+0.j  0.13810698+0.j  0.68625416+0.j]
 [ 0.00000000+0.j  0.90563968+0.j  0.68625416+0.j -1.67112549+0.j]]
Control 1 Hamiltonian:
[[ 1.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j -1.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j -1.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  1.+0.j]]
Initial operator:
[[ 1.+0.j]
 [ 0.+0.j]
 [ 0.+0.j]
 [ 0.+0.j]]
Target operator:
[[ 0.+0.j]
 [ 0.+0.j]
 [ 0.+0.j]
 [ 1.+0.j]]
INFO:qutip.control.pulseoptim:Initial amplitudes output to file: ctrl_amps_initial_2qubitInteract_n_ts100_ptypeDEF.txt
INFO:qutip.control.optimizer:Optimising pulse using 'fmin' method
INFO:qutip.control.pulseoptim:Final amplitudes output to file: ctrl_amps_final_2qubitInteract_n_ts100_ptypeDEF.txt

Report the results

Firstly the performace statistics are reported, which gives a breakdown of the processing times. In this example it can be seen that the majority of time is spent calculating the propagators, i.e. exponentiating the combined Hamiltonian.

The optimised U(T) is reported as the 'final evolution', which is essentially the string representation of the Qobj that holds the full time evolution at the point when the optimisation is terminated.

The key information is in the summary (given) last. Here the final fidelity is reported and the reasonn for termination of the algorithm.


In [9]:
result.stats.report()
print("Final evolution\n{}\n".format(result.evo_full_final))
print("********* Summary *****************")
print("Final fidelity error {}".format(result.fid_err))
print("Final gradient normal {}".format(result.grad_norm_final))
print("Terminated due to {}".format(result.termination_reason))
print("Number of iterations {}".format(result.num_iter))
print("Completed in {} HH:MM:SS.US".format(
        datetime.timedelta(seconds=result.wall_time)))


------------------------------------
---- Control optimisation stats ----
**** Timings (HH:MM:SS.US) ****
Total wall time elapsed during optimisation: 0:00:25.931032
Wall time computing Hamiltonians: 0:00:00.119777 (0.46%)
Wall time computing propagators: 0:00:25.906193 (99.90%)
Wall time computing forward propagation: 0:00:00.022750 (0.09%)
Wall time computing onward propagation: 0:00:00.019951 (0.08%)
Wall time computing gradient: 0:00:00 (0.00%)

**** Iterations and function calls ****
Number of iterations: 100
Number of fidelity function calls: 150
Number of times fidelity is computed: 151
Number of gradient function calls: 0
Number of times gradients are computed: 0
Number of times timeslot evolution is recomputed: 151

**** Control amplitudes ****
Number of control amplitude updates: 150
Mean number of updates per iteration: 1.5
Number of timeslot values changed: 14999
Mean number of timeslot changes per update: 99.9933333333
Number of amplitude values changed: 14999
Mean number of amplitude changes per update: 99.9933333333
------------------------------------
Final evolution
Quantum object: dims = [[4], [1]], shape = [4, 1], type = ket
Qobj data =
[[-0.06555152-0.06431858j]
 [-0.00911692+0.0561679j ]
 [-0.09964556-0.04052853j]
 [-0.25677868+0.95436947j]]

********* Summary *****************
Final fidelity error 0.0116901458711
Final gradient normal 0.0
Terminated due to Maximum number of iterations reached
Number of iterations 100
Completed in 0:00:25.931032 HH:MM:SS.US

Plot the initial and final amplitudes

Here the (random) starting pulse is plotted along with the pulse (control amplitudes) that was found to produce the target gate evolution to within the specified error.


In [10]:
t = result.time[:n_ts]

fig1 = plt.figure()
ax1 = fig1.add_subplot(2, 1, 1)
ax1.set_title("Initial Control amps")
ax1.set_xlabel("Time")
ax1.set_ylabel("Control amplitude")
ax1.plot(t, result.initial_amps[:, 0])

ax2 = fig1.add_subplot(2, 1, 2)
ax2.set_title("Optimised Control Amplitudes")
ax2.set_xlabel("Time")
ax2.set_ylabel("Control amplitude")
ax2.plot(t, result.final_amps[:, 0])

plt.show()


Versions


In [11]:
from qutip.ipynbtools import version_table

version_table()


Out[11]:
SoftwareVersion
QuTiP3.1.0
Numpy1.8.2
SciPy0.13.3
matplotlib1.3.1
Cython0.20.1post0
IPython1.2.1
Python2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2]
OSposix [linux2]
Tue May 05 17:20:11 2015 CEST

References

[1] Doria, P., Calarco, T. & Montangero, S.: Optimal Control Technique for Many-Body Quantum Dynamics. Phys. Rev. Lett. 106, 1–4 (2011).

[2] Caneva, T., Calarco, T. & Montangero, S.: Chopped random-basis quantum optimization. Phys. Rev. A - At. Mol. Opt. Phys. 84, (2011).


In [ ]: