Skip to content

Commit

Permalink
Fix a bug: Model parameters provided to the processor class
Browse files Browse the repository at this point in the history
Model parameters should be defined while initialization and should not be updated in Processor. This is because updating will not trigger the _compute_params method in the model, making the parameters outdated or incomplete.
  • Loading branch information
BoxiLi committed Nov 9, 2021
1 parent 5ce6c21 commit eb153e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
34 changes: 16 additions & 18 deletions src/qutip_qip/device/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class Processor(object):
If other parameters, such as `t1` is given as input,
it will overwrite those saved in :obj:`Processor.model.params`.
**params:
- t1 : float or list, optional
Characterize the amplitude damping for each qubit.
A list of size `num_qubits` or a float for all qubits.
- t2 : float or list, optional
Characterize the total dephasing for each qubit.
A list of size `num_qubits` or a float for all qubits.
t1 : float or list, optional
Characterize the amplitude damping for each qubit.
A list of size `num_qubits` or a float for all qubits.
t2 : float or list, optional
Characterize the total dephasing for each qubit.
A list of size `num_qubits` or a float for all qubits.
"""

def __init__(
Expand All @@ -75,18 +75,15 @@ def __init__(
spline_kind="step_func",
model=None,
N=None,
**params
t1=None,
t2=None,
):
num_qubits = num_qubits if num_qubits is not None else N
if model is None:
self.model = Model(num_qubits=num_qubits, dims=dims, **params)
self.model = Model(num_qubits=num_qubits, dims=dims, t1=t1, t2=t2)
else:
self.model = model
self.model.num_qubits = (
num_qubits if num_qubits is not None else self.model.num_qubits
)
self.model.dims = dims if dims is not None else self.model.dims
self.model.params.update(deepcopy(params))

self.pulses = []
# FIXME # Think about the handling of spline_kind.
self.spline_kind = spline_kind
Expand Down Expand Up @@ -751,7 +748,7 @@ def plot_pulses(
num_steps: int, optional
Number of time steps in the plot.
pulse_labels: list of dict, optional
A map between pulse labels and the labels shown in the y axis.
E.g. ``[{"sx": "sigmax"}]``.
Expand Down Expand Up @@ -796,8 +793,9 @@ def plot_pulses(

# choose labels
if pulse_labels is None:
if use_control_latex and \
not hasattr(self.model, "get_control_latex"):
if use_control_latex and not hasattr(
self.model, "get_control_latex"
):
warnings.warn(
"No method get_control_latex defined in the model. "
"Switch to using the labels defined in each pulse."
Expand Down Expand Up @@ -1228,7 +1226,7 @@ class Model:
"""

def __init__(self, num_qubits, dims=None, **params):
self.num_qubits = num_qubits
self.num_qubits = num_qubits if num_qubits is not None else N
self.dims = dims if dims is not None else num_qubits * [2]
self.params = deepcopy(params)
self._controls = {}
Expand Down
2 changes: 0 additions & 2 deletions src/qutip_qip/device/spinchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def __init__(
num_qubits,
correct_global_phase=correct_global_phase,
model=model,
**params,
)

@property
Expand Down Expand Up @@ -193,7 +192,6 @@ def __init__(
num_qubits,
correct_global_phase=correct_global_phase,
model=model,
**params,
)

@property
Expand Down
4 changes: 4 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ def test_define_model_in_processor():
assert noise_object in processor.noise
with pytest.raises(TypeError):
processor.add_noise("non-noise-object")

def test_change_parameters_in_processor():
processor = LinearSpinChain(0, sx=0.1)
assert(all(processor.params["sx"] == [0.1]))

0 comments on commit eb153e6

Please sign in to comment.