import matplotlib.pyplot as plt
import numpy as np
import qutip as qt
# the library for quantum control
import qutip_qtrl.pulseoptim as cpo
# total duration
T = 2 * np.pi
# number of time steps
times = np.linspace(0, T, 500)
U_0 = qt.operators.identity(4)
U_target = qt.core.gates.cnot()
U_0
U_target
# Drift Hamiltonian
g = 0
H_drift = g * (
qt.tensor(qt.sigmax(), qt.sigmax()) + qt.tensor(qt.sigmay(), qt.sigmay())
)
H_ctrl = [
qt.tensor(qt.sigmax(), qt.identity(2)),
qt.tensor(qt.sigmay(), qt.identity(2)),
qt.tensor(qt.sigmaz(), qt.identity(2)),
qt.tensor(qt.identity(2), qt.sigmax()),
qt.tensor(qt.identity(2), qt.sigmay()),
qt.tensor(qt.identity(2), qt.sigmaz()),
qt.tensor(qt.sigmax(), qt.sigmax()),
qt.tensor(qt.sigmay(), qt.sigmay()),
qt.tensor(qt.sigmaz(), qt.sigmaz()),
]
H_labels = [
r"$u_{1x}$",
r"$u_{1y}$",
r"$u_{1z}$",
r"$u_{2x}$",
r"$u_{2y}$",
r"$u_{2z}$",
r"$u_{xx}$",
r"$u_{yy}$",
r"$u_{zz}$",
]
result = cpo.optimize_pulse_unitary(
H_drift,
H_ctrl,
U_0,
U_target,
num_tslots=500,
evo_time=(2 * np.pi),
# this attribute is crucial for convergence!!
amp_lbound=-(2 * np.pi * 0.05),
amp_ubound=(2 * np.pi * 0.05),
fid_err_targ=1e-9,
max_iter=500,
max_wall_time=60,
alg="GRAPE",
optim_method="FMIN_L_BFGS_B",
method_params={
"disp": True,
"maxiter": 1000,
},
)
for attr in dir(result):
if not attr.startswith("_"):
print(f"{attr}: {getattr(result, attr)}")
print(np.shape(result.final_amps))
evo_full_final: Quantum object: dims=[[4], [4]], shape=(4, 4), type='oper', dtype=Dense, isherm=False Qobj data = [[ 7.07094644e-01-7.07118918e-01j -1.34351315e-05-6.08446090e-06j 5.88535157e-06-1.56595261e-05j 1.83032176e-05+2.14336237e-05j] [-6.08399866e-06-1.34348487e-05j 7.07124607e-01-7.07088954e-01j -1.65245383e-05-9.72452683e-06j 2.92746416e-06-5.80571018e-06j] [ 2.14345945e-05+1.83019117e-05j -5.80485894e-06+2.92723527e-06j -9.39382489e-08+3.90211561e-05j 7.07092280e-01-7.07121280e-01j] [-1.56596880e-05+5.88656976e-06j -9.72398285e-06-1.65247979e-05j 7.07115591e-01-7.07097970e-01j 3.90209310e-05-9.47875041e-08j]] evo_full_initial: Quantum object: dims=[[4], [4]], shape=(4, 4), type='oper', dtype=Dense, isherm=False Qobj data = [[ 0.98173536+0.05924513j 0.16332706-0.01645484j 0.03724668+0.01986515j -0.00134415+0.06289239j] [-0.16459767-0.00420594j 0.97865632-0.10884193j -0.00114895-0.03575666j 0.04163544+0.01617958j] [-0.03739331+0.03158625j -0.01370629-0.02601753j 0.98463826+0.02759627j 0.16258359-0.00563938j] [ 0.00963345+0.05555943j -0.03707557+0.03463191j -0.16297401-0.01017965j 0.98337967+0.02338042j]] fid_err: 9.943291745528882e-10 fidelity: 0.0 final_amps: [[ 0.30176284 0.31400222 0.00491555 ... 0.11847676 -0.02537698 -0.00433336] [ 0.18194806 0.31397224 -0.10209941 ... 0.3125787 -0.31336719 -0.08384633] [ 0.29994771 0.31393499 0.25367308 ... 0.11973832 -0.1771439 -0.2577667 ] ... [ 0.31278672 -0.14007267 0.23401463 ... 0.23946145 -0.14651537 -0.17527497] [ 0.24354953 -0.30674013 -0.18957523 ... 0.31321749 0.19375728 -0.00842078] [ 0.31244543 -0.20868862 0.14328137 ... 0.31314574 -0.06486363 0.15084076]] goal_achieved: True grad_norm_final: 3.5034482490916195e-05 grad_norm_min_reached: False initial_amps: [[ 0.12306952 0.25076806 -0.09834743 ... -0.24205457 0.14158553 0.00340267] [-0.12005275 -0.14524315 -0.20621746 ... 0.10552862 -0.28538723 -0.07533278] [ 0.15098471 -0.24282636 0.1881761 ... -0.23660955 -0.01440436 -0.25843437] ... [ 0.19068024 0.19816978 0.19790563 ... -0.06414348 -0.09098458 -0.2105755 ] [-0.12742979 -0.13197281 -0.28265971 ... 0.2795536 0.25121951 -0.03997968] [ 0.06534749 0.10330945 0.04311909 ... 0.18123658 -0.00264333 0.12221349]] initial_fid_err: 0.5097268489689029 max_fid_func_exceeded: False max_iter_exceeded: False num_fid_func_calls: 32 num_iter: 28 optimizer: <qutip_qtrl.optimizer.OptimizerLBFGSB object at 0x7efda4ac9550> reset: <bound method OptimResult.reset of <qutip_qtrl.optimresult.OptimResult object at 0x7efd5dee2990>> stats: None termination_reason: Goal achieved time: [0. 0.01256637 0.02513274 0.03769911 0.05026548 0.06283185 0.07539823 0.0879646 0.10053097 0.11309734 0.12566371 0.13823008 0.15079645 0.16336282 0.17592919 0.18849556 0.20106193 0.21362831 0.22619468 0.23876105 0.25132742 0.26389379 0.27646016 0.28902653 0.3015929 0.31415927 0.32672564 0.33929201 0.35185838 0.36442476 0.37699113 0.3895575 0.40212387 0.41469024 0.42725661 0.43982298 0.45238935 0.46495572 0.47752209 0.49008846 0.50265484 0.51522121 0.52778758 0.54035395 0.55292032 0.56548669 0.57805306 0.59061943 0.6031858 0.61575217 0.62831854 0.64088492 0.65345129 0.66601766 0.67858403 0.6911504 0.70371677 0.71628314 0.72884951 0.74141588 0.75398225 0.76654862 0.779115 0.79168137 0.80424774 0.81681411 0.82938048 0.84194685 0.85451322 0.86707959 0.87964596 0.89221233 0.9047787 0.91734507 0.92991145 0.94247782 0.95504419 0.96761056 0.98017693 0.9927433 1.00530967 1.01787604 1.03044241 1.04300878 1.05557515 1.06814153 1.0807079 1.09327427 1.10584064 1.11840701 1.13097338 1.14353975 1.15610612 1.16867249 1.18123886 1.19380523 1.20637161 1.21893798 1.23150435 1.24407072 1.25663709 1.26920346 1.28176983 1.2943362 1.30690257 1.31946894 1.33203531 1.34460169 1.35716806 1.36973443 1.3823008 1.39486717 1.40743354 1.41999991 1.43256628 1.44513265 1.45769902 1.47026539 1.48283176 1.49539814 1.50796451 1.52053088 1.53309725 1.54566362 1.55822999 1.57079636 1.58336273 1.5959291 1.60849547 1.62106184 1.63362822 1.64619459 1.65876096 1.67132733 1.6838937 1.69646007 1.70902644 1.72159281 1.73415918 1.74672555 1.75929192 1.7718583 1.78442467 1.79699104 1.80955741 1.82212378 1.83469015 1.84725652 1.85982289 1.87238926 1.88495563 1.897522 1.91008838 1.92265475 1.93522112 1.94778749 1.96035386 1.97292023 1.9854866 1.99805297 2.01061934 2.02318571 2.03575208 2.04831845 2.06088483 2.0734512 2.08601757 2.09858394 2.11115031 2.12371668 2.13628305 2.14884942 2.16141579 2.17398216 2.18654853 2.19911491 2.21168128 2.22424765 2.23681402 2.24938039 2.26194676 2.27451313 2.2870795 2.29964587 2.31221224 2.32477861 2.33734499 2.34991136 2.36247773 2.3750441 2.38761047 2.40017684 2.41274321 2.42530958 2.43787595 2.45044232 2.46300869 2.47557507 2.48814144 2.50070781 2.51327418 2.52584055 2.53840692 2.55097329 2.56353966 2.57610603 2.5886724 2.60123877 2.61380515 2.62637152 2.63893789 2.65150426 2.66407063 2.676637 2.68920337 2.70176974 2.71433611 2.72690248 2.73946885 2.75203522 2.7646016 2.77716797 2.78973434 2.80230071 2.81486708 2.82743345 2.83999982 2.85256619 2.86513256 2.87769893 2.8902653 2.90283168 2.91539805 2.92796442 2.94053079 2.95309716 2.96566353 2.9782299 2.99079627 3.00336264 3.01592901 3.02849538 3.04106176 3.05362813 3.0661945 3.07876087 3.09132724 3.10389361 3.11645998 3.12902635 3.14159272 3.15415909 3.16672546 3.17929184 3.19185821 3.20442458 3.21699095 3.22955732 3.24212369 3.25469006 3.26725643 3.2798228 3.29238917 3.30495554 3.31752191 3.33008829 3.34265466 3.35522103 3.3677874 3.38035377 3.39292014 3.40548651 3.41805288 3.43061925 3.44318562 3.45575199 3.46831837 3.48088474 3.49345111 3.50601748 3.51858385 3.53115022 3.54371659 3.55628296 3.56884933 3.5814157 3.59398207 3.60654845 3.61911482 3.63168119 3.64424756 3.65681393 3.6693803 3.68194667 3.69451304 3.70707941 3.71964578 3.73221215 3.74477853 3.7573449 3.76991127 3.78247764 3.79504401 3.80761038 3.82017675 3.83274312 3.84530949 3.85787586 3.87044223 3.8830086 3.89557498 3.90814135 3.92070772 3.93327409 3.94584046 3.95840683 3.9709732 3.98353957 3.99610594 4.00867231 4.02123868 4.03380506 4.04637143 4.0589378 4.07150417 4.08407054 4.09663691 4.10920328 4.12176965 4.13433602 4.14690239 4.15946876 4.17203514 4.18460151 4.19716788 4.20973425 4.22230062 4.23486699 4.24743336 4.25999973 4.2725661 4.28513247 4.29769884 4.31026522 4.32283159 4.33539796 4.34796433 4.3605307 4.37309707 4.38566344 4.39822981 4.41079618 4.42336255 4.43592892 4.44849529 4.46106167 4.47362804 4.48619441 4.49876078 4.51132715 4.52389352 4.53645989 4.54902626 4.56159263 4.574159 4.58672537 4.59929175 4.61185812 4.62442449 4.63699086 4.64955723 4.6621236 4.67468997 4.68725634 4.69982271 4.71238908 4.72495545 4.73752183 4.7500882 4.76265457 4.77522094 4.78778731 4.80035368 4.81292005 4.82548642 4.83805279 4.85061916 4.86318553 4.87575191 4.88831828 4.90088465 4.91345102 4.92601739 4.93858376 4.95115013 4.9637165 4.97628287 4.98884924 5.00141561 5.01398198 5.02654836 5.03911473 5.0516811 5.06424747 5.07681384 5.08938021 5.10194658 5.11451295 5.12707932 5.13964569 5.15221206 5.16477844 5.17734481 5.18991118 5.20247755 5.21504392 5.22761029 5.24017666 5.25274303 5.2653094 5.27787577 5.29044214 5.30300852 5.31557489 5.32814126 5.34070763 5.353274 5.36584037 5.37840674 5.39097311 5.40353948 5.41610585 5.42867222 5.4412386 5.45380497 5.46637134 5.47893771 5.49150408 5.50407045 5.51663682 5.52920319 5.54176956 5.55433593 5.5669023 5.57946867 5.59203505 5.60460142 5.61716779 5.62973416 5.64230053 5.6548669 5.66743327 5.67999964 5.69256601 5.70513238 5.71769875 5.73026513 5.7428315 5.75539787 5.76796424 5.78053061 5.79309698 5.80566335 5.81822972 5.83079609 5.84336246 5.85592883 5.86849521 5.88106158 5.89362795 5.90619432 5.91876069 5.93132706 5.94389343 5.9564598 5.96902617 5.98159254 5.99415891 6.00672529 6.01929166 6.03185803 6.0444244 6.05699077 6.06955714 6.08212351 6.09468988 6.10725625 6.11982262 6.13238899 6.14495536 6.15752174 6.17008811 6.18265448 6.19522085 6.20778722 6.22035359 6.23291996 6.24548633 6.2580527 6.27061907 6.28318544] wall_time: 3.6964038119999714 wall_time_limit_exceeded: False (500, 9)
def plot_control_amplitudes(times, final_amps, labels):
num_controls = final_amps.shape[1]
y_max = 0.1 # Fixed y-axis scale
y_min = -0.1
for i in range(num_controls):
fig, ax = plt.subplots(figsize=(8, 3))
for j in range(num_controls):
# Highlight the current control
color = "black" if i == j else "gray"
alpha = 1.0 if i == j else 0.1
ax.plot(
times,
final_amps[:, j],
label=labels[j],
color=color,
alpha=alpha
)
ax.set_title(f"Control Fields Highlighting: {labels[i]}")
ax.set_xlabel("Time")
ax.set_ylabel(labels[i])
ax.set_ylim(y_min, y_max) # Set fixed y-axis limits
ax.grid(True)
ax.legend()
plt.tight_layout()
plt.show()
plot_control_amplitudes(times, result.final_amps / (2 * np.pi), H_labels)
U_target
U_f = result.evo_full_final
U_f.dims = [[2, 2], [2, 2]]
U_f
print(f"Fidelity: {qt.process_fidelity(U_f, U_target)}")
Fidelity: 0.9999999980113417
Quantum Process Tomography (QPT) is a technique used to characterize an unknown quantum operation by reconstructing its process matrix (also called the χ (chi) matrix). This matrix describes how an input quantum state is transformed by the operation.
Defines the basis operators { 𝐼 , 𝑋 , 𝑌 , 𝑍 } for the two-qubit system.
These operators form a complete basis to describe any quantum operation in the Pauli basis.
op_basis = [[qt.qeye(2), qt.sigmax(), qt.sigmay(), qt.sigmaz()]] * 2
op_label = [["i", "x", "y", "z"]] * 2
U_target is the ideal CNOT gate.
qt.to_super(U_target) converts it into superoperator form, which is necessary for QPT.
qt.qpt(U_i_s, op_basis) computes the χ matrix for the ideal gate.
fig = plt.figure(figsize=(12, 6))
U_i_s = qt.to_super(U_target)
chi = qt.qpt(U_i_s, op_basis)
fig = qt.qpt_plot_combined(chi, op_label, fig=fig, threshold=0.001)
op_basis = [[qt.qeye(2), qt.sigmax(), qt.sigmay(), qt.sigmaz()]] * 2
op_label = [["i", "x", "y", "z"]] * 2
fig = plt.figure(figsize=(12, 6))
U_f_s = qt.to_super(U_f)
chi = qt.qpt(U_f_s, op_basis)
fig = qt.qpt_plot_combined(chi, op_label, fig=fig, threshold=0.01)
qt.about()
QuTiP: Quantum Toolbox in Python ================================ Copyright (c) QuTiP team 2011 and later. Current admin team: Alexander Pitchford, Nathan Shammah, Shahnawaz Ahmed, Neill Lambert, Eric Giguère, Boxi Li, Simon Cross, Asier Galicia, Paul Menczel, and Patrick Hopf. Board members: Daniel Burgarth, Robert Johansson, Anton F. Kockum, Franco Nori and Will Zeng. Original developers: R. J. Johansson & P. D. Nation. Previous lead developers: Chris Granade & A. Grimsmo. Currently developed through wide collaboration. See https://github.com/qutip for details. QuTiP Version: 5.2.0.dev0+4e6ded9 Numpy Version: 2.2.5 Scipy Version: 1.15.2 Cython Version: 3.0.12 Matplotlib Version: 3.10.1 Python Version: 3.12.0 Number of CPUs: 4 BLAS Info: Generic INTEL MKL Ext: None Platform Info: Linux (x86_64) Installation path: /home/runner/miniconda3/envs/test-environment-v5/lib/python3.12/site-packages/qutip Installed QuTiP family packages ------------------------------- qutip-qtrl: 0.2.0.dev0+acb71a0 qutip-jax: 0.1.1.dev5 qutip-qip: 0.5.0.dev0+a6d68ca ================================================================================ Please cite QuTiP in your publication. ================================================================================ For your convenience a bibtex reference can be easily generated using `qutip.cite()`