-
Notifications
You must be signed in to change notification settings - Fork 11
Fix state transfer not working for GRAPE and CRAB #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from qutip_qoc._optimizer import _global_local_optimization | ||
from qutip_qoc._time import _TimeInterval | ||
|
||
import qutip as qt | ||
|
||
try: | ||
from qutip_qoc._rl import _RL | ||
_rl_available = True | ||
|
@@ -28,6 +30,7 @@ def optimize_pulses( | |
optimizer_kwargs=None, | ||
minimizer_kwargs=None, | ||
integrator_kwargs=None, | ||
optimization_type=None, | ||
): | ||
""" | ||
Run GOAT, JOPT, GRAPE, CRAB or RL optimization. | ||
|
@@ -124,6 +127,9 @@ def optimize_pulses( | |
Options for the solver, see :obj:`MESolver.options` and | ||
`Integrator <./classes.html#classes-ode>`_ for a list of all options. | ||
|
||
optimization_type : str, optional | ||
Type of optimization: "state_transfer", "gate_synthesis", or None. | ||
|
||
Returns | ||
------- | ||
result : :class:`qutip_qoc.Result` | ||
|
@@ -187,10 +193,40 @@ def optimize_pulses( | |
"maxiter": algorithm_kwargs.get("max_iter", 1000), | ||
"gtol": algorithm_kwargs.get("min_grad", 0.0 if alg == "CRAB" else 1e-8), | ||
} | ||
# Iterate over objectives and convert initial and target states based on the optimization type | ||
for objective in objectives: | ||
if any(qt.issuper(H_i) for H_i in (objective.H if isinstance(objective.H, list) else [objective.H])): | ||
if optimization_type == "state_transfer": | ||
rochisha0 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make everything a bit easier to read and maintain, could we refactor a little bit so that this whole block of code becomes, for example for objective in objectives:
if objective._is_open():
objective._format_for_open(optimization_type) Without changing any of the logic; the logic seems good to me :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have refactored in a different way. Can you please check that, or explain this way more? |
||
if qt.isket(objective.initial): | ||
objective.initial = qt.operator_to_vector(qt.ket2dm(objective.initial)) | ||
elif qt.isoper(objective.initial): | ||
objective.initial = qt.operator_to_vector(objective.initial) | ||
if qt.isket(objective.target): | ||
objective.target = qt.operator_to_vector(qt.ket2dm(objective.target)) | ||
elif qt.isoper(objective.target): | ||
objective.target = qt.operator_to_vector(objective.target) | ||
elif optimization_type == "gate_synthesis": | ||
objective.initial = qt.to_super(objective.initial) | ||
objective.target = qt.to_super(objective.target) | ||
elif optimization_type is None: | ||
if qt.isoper(objective.initial) and qt.isoper(objective.target): | ||
if np.isclose(qt.tr(objective.initial), 1) and np.isclose(qt.tr(objective.target), 1): | ||
rochisha0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
objective.initial = qt.operator_to_vector(objective.initial) | ||
objective.target = qt.operator_to_vector(objective.target) | ||
else: | ||
objective.initial = qt.to_super(objective.initial) | ||
objective.target = qt.to_super(objective.target) | ||
if qt.isket(objective.initial): | ||
objective.initial = qt.operator_to_vector(qt.ket2dm(objective.initial)) | ||
|
||
# prepare qtrl optimizers | ||
qtrl_optimizers = [] | ||
if alg == "CRAB" or alg == "GRAPE": | ||
dyn_type = "GEN_MAT" | ||
for objective in objectives: | ||
if any(qt.isoper(H_i) for H_i in (objective.H if isinstance(objective.H, list) else [objective.H])): | ||
dyn_type = "UNIT" | ||
|
||
if alg == "GRAPE": # algorithm specific kwargs | ||
use_as_amps = True | ||
minimizer_kwargs.setdefault("method", "L-BFGS-B") # gradient | ||
|
@@ -247,7 +283,7 @@ def optimize_pulses( | |
"accuracy_factor": None, # deprecated | ||
"alg_params": alg_params, | ||
"optim_params": algorithm_kwargs.get("optim_params", None), | ||
"dyn_type": algorithm_kwargs.get("dyn_type", "GEN_MAT"), | ||
"dyn_type": algorithm_kwargs.get("dyn_type", dyn_type), | ||
"dyn_params": algorithm_kwargs.get("dyn_params", None), | ||
"prop_type": algorithm_kwargs.get( | ||
"prop_type", "DEF" | ||
|
Uh oh!
There was an error while loading. Please reload this page.