Skip to content

Commit

Permalink
use timelist instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick committed Apr 11, 2024
1 parent 61c9e69 commit db4d8f8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
5 changes: 1 addition & 4 deletions doc/guide/guide-control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ It requires some extra arguments to prepare the optimization.
# boundaries for the parameters
p_bounds = q_bounds = [(-1, 1), (-1, 1)]
# time interval for the evolution
t_interval = qoc.TimeInterval(evo_time=1.)
Eventually, the optimization for a desired `fid_err_targ` can be started by calling the ``optimize`` function.
Expand All @@ -268,7 +265,7 @@ Eventually, the optimization for a desired `fid_err_targ` can be started by call
"p": {"guess": p_guess, "bounds": p_bounds},
"q": {"guess": q_guess, "bounds": q_bounds}
},
time_interval=t_interval,
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.1,
Expand Down
15 changes: 9 additions & 6 deletions src/qutip_qoc/pulse_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import qutip_qtrl.pulseoptim as cpo

from qutip_qoc._optimizer import _global_local_optimization
from qutip_qoc.time import TimeInterval

__all__ = ["optimize_pulses"]


def optimize_pulses(
objectives,
control_parameters,
time_interval,
tlist,
algorithm_kwargs=None,
optimizer_kwargs=None,
minimizer_kwargs=None,
Expand All @@ -29,7 +30,7 @@ def optimize_pulses(
control_parameters : dict
Dictionary of options for the control pulse optimization.
The keys of this dict must be a unique string identifier for each control Hamiltonian / function.
For the GOAT and JOPT algorithms, the dict may optionally also contain the key "__time__" to treat the pulse duration as optimization parameter.
For the GOAT and JOPT algorithms, the dict may optionally also contain the key "__time__".
For each control function it must specify:
control_id : dict
Expand All @@ -43,7 +44,7 @@ def optimize_pulses(
__time__ : dict, optional
Only supported by GOAT and JOPT.
Dictionary of options for the time interval optimization.
If given the pulse duration is treated as optimization parameter.
It must specify both:
- guess: ndarray, shape (n,)
Expand All @@ -56,9 +57,8 @@ def optimize_pulses(
GRAPE and CRAB bounds are only one pair of ``(min, max)`` limiting the amplitude of all tslots equally.
time_interval : :class:`qutip_qoc.TimeInterval`
Pulse duration time interval.
GRAPE and CRAB require n_tslots attribute for discretization.
tlist: List.
Time over which system evolves.
algorithm_kwargs : dict, optional
Dictionary of options for the optimization algorithm.
Expand Down Expand Up @@ -124,6 +124,9 @@ def optimize_pulses(
if integrator_kwargs is None:
integrator_kwargs = {}

# create time interval
time_interval = TimeInterval(tslots=tlist)

time_options = control_parameters.pop("__time__", {})
alg = algorithm_kwargs.get("alg", "GRAPE") # works with most input types

Expand Down
13 changes: 6 additions & 7 deletions tests/test_analytical_pulses.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

from qutip_qoc.pulse_optim import optimize_pulses
from qutip_qoc.objective import Objective
from qutip_qoc.time import TimeInterval

Case = collections.namedtuple(
"Case",
[
"objectives",
"control_parameters",
"time_interval",
"tlist",
"algorithm_kwargs",
"optimizer_kwargs",
],
Expand Down Expand Up @@ -65,7 +64,7 @@ def grad_sin(t, p, idx):
"p": {"guess": p_guess, "bounds": p_bounds},
"q": {"guess": q_guess, "bounds": q_bounds},
},
time_interval=TimeInterval(evo_time=1.0),
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.01,
Expand All @@ -84,7 +83,7 @@ def grad_sin(t, p, idx):
"p": {"guess": p_guess, "bounds": p_bounds},
"q": {"guess": q_guess, "bounds": q_bounds},
},
time_interval=TimeInterval(evo_time=1.0),
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.01,
Expand All @@ -104,7 +103,7 @@ def grad_sin(t, p, idx):
"bounds": (0, 10),
},
},
time_interval=TimeInterval(evo_time=1.0),
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.01,
Expand All @@ -131,7 +130,7 @@ def grad_sin(t, p, idx):
"p": {"guess": p_guess, "bounds": p_bounds},
"q": {"guess": q_guess, "bounds": q_bounds},
},
time_interval=TimeInterval(evo_time=1.0),
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.1, # relaxed objective
Expand Down Expand Up @@ -209,7 +208,7 @@ def test_optimize_pulses(tst):
result = optimize_pulses(
tst.objectives,
tst.control_parameters,
tst.time_interval,
tst.tlist,
tst.algorithm_kwargs,
tst.optimizer_kwargs,
)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

from qutip_qoc.pulse_optim import optimize_pulses
from qutip_qoc.objective import Objective
from qutip_qoc.time import TimeInterval

Case = collections.namedtuple(
"Case",
[
"objectives",
"control_parameters",
"time_interval",
"tlist",
"algorithm_kwargs",
"optimizer_kwargs",
],
Expand Down Expand Up @@ -67,7 +66,7 @@ def grad_sin(t, p, idx):
"p": {"guess": p_guess, "bounds": p_bounds},
"q": {"guess": q_guess, "bounds": q_bounds},
},
time_interval=TimeInterval(evo_time=5.0),
tlist=np.linspace(0, 1, 100),
algorithm_kwargs={"alg": "GOAT", "fid_type": "PSU"},
optimizer_kwargs={
"seed": 0,
Expand Down Expand Up @@ -184,7 +183,7 @@ def test_optimize_pulses(tst):
result = optimize_pulses(
tst.objectives,
tst.control_parameters,
tst.time_interval,
tst.tlist,
tst.algorithm_kwargs,
tst.optimizer_kwargs,
)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[
"objectives",
"control_parameters",
"time_interval",
"tlist",
"algorithm_kwargs",
"optimizer_kwargs",
],
Expand Down Expand Up @@ -68,7 +68,7 @@ def grad_sin(t, p, idx):
"q": {"guess": q_guess, "bounds": q_bounds},
"r": {"guess": r_guess, "bounds": r_bounds},
},
time_interval=TimeInterval(evo_time=10),
tlist=np.linspace(0, 10, 100),
algorithm_kwargs={
"alg": "GOAT",
"fid_err_targ": 0.01,
Expand Down Expand Up @@ -120,7 +120,7 @@ def sin_z_jax(t, r, **kwargs):
# ------------------- discrete CRAB / GRAPE control ------------------------

n_tslots, evo_time = 100, 10
disc_interval = TimeInterval(n_tslots=n_tslots, evo_time=evo_time)
disc_interval = np.linspace(0, evo_time, n_tslots)

p_disc = q_disc = r_disc = np.zeros(n_tslots)
p_bound = q_bound = r_bound = (-1, 1)
Expand All @@ -137,7 +137,7 @@ def sin_z_jax(t, r, **kwargs):
"q": {"guess": q_disc, "bounds": q_bound},
"r": {"guess": r_disc, "bounds": r_bound},
},
time_interval=disc_interval,
tlist=disc_interval,
algorithm_kwargs={"alg": "GRAPE", "fid_err_targ": 0.01},
)

Expand All @@ -148,7 +148,7 @@ def sin_z_jax(t, r, **kwargs):
"q": {"guess": q_disc, "bounds": q_bound},
"r": {"guess": r_disc, "bounds": r_bound},
},
time_interval=disc_interval,
tlist=disc_interval,
algorithm_kwargs={"alg": "CRAB", "fid_err_targ": 0.01, "fix_frequency": False},
)

Expand All @@ -171,7 +171,7 @@ def test_optimize_pulses(tst):
result = optimize_pulses(
tst.objectives,
tst.control_parameters,
tst.time_interval,
tst.tlist,
tst.algorithm_kwargs,
tst.optimizer_kwargs,
)
Expand Down

0 comments on commit db4d8f8

Please sign in to comment.