From eb153e6630d3aea94fae4e2894deeb408fe414cc Mon Sep 17 00:00:00 2001 From: Boxi Li Date: Wed, 3 Nov 2021 12:40:55 +0100 Subject: [PATCH] Fix a bug: Model parameters provided to the processor class 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. --- src/qutip_qip/device/processor.py | 34 +++++++++++++++---------------- src/qutip_qip/device/spinchain.py | 2 -- tests/test_model.py | 4 ++++ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/qutip_qip/device/processor.py b/src/qutip_qip/device/processor.py index beb182452..69f104af2 100644 --- a/src/qutip_qip/device/processor.py +++ b/src/qutip_qip/device/processor.py @@ -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__( @@ -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 @@ -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"}]``. @@ -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." @@ -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 = {} diff --git a/src/qutip_qip/device/spinchain.py b/src/qutip_qip/device/spinchain.py index 901fe6d2e..bbfa8aa36 100644 --- a/src/qutip_qip/device/spinchain.py +++ b/src/qutip_qip/device/spinchain.py @@ -128,7 +128,6 @@ def __init__( num_qubits, correct_global_phase=correct_global_phase, model=model, - **params, ) @property @@ -193,7 +192,6 @@ def __init__( num_qubits, correct_global_phase=correct_global_phase, model=model, - **params, ) @property diff --git a/tests/test_model.py b/tests/test_model.py index dbec255eb..ca339dc7f 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -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]))