Skip to content
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

[QHC-649] Move initialization of _qm into initial_setup #763

Open
wants to merge 82 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
2d8e25f
Fixed bus alias missing
jordivallsq Jul 9, 2024
5e06748
added spearing in the enums
jordivallsq Jul 9, 2024
30e9091
now get parameter works without QM connect
jordivallsq Jul 9, 2024
87255db
Fixed format issues
jordivallsq Jul 9, 2024
b81aadd
Fixed test
jordivallsq Jul 9, 2024
f3e0b86
fixed tests
jordivallsq Jul 9, 2024
ab09beb
added changelog
jordivallsq Jul 9, 2024
8e234b6
corrected tests to add correct gain location
jordivallsq Jul 9, 2024
1b529eb
test
jordivallsq Jul 9, 2024
6f93082
test
jordivallsq Jul 9, 2024
1ce72d5
reformatting
jordivallsq Jul 9, 2024
de911b0
changing bus
jordivallsq Jul 9, 2024
dfd65c0
deleted gain for now
jordivallsq Jul 9, 2024
439793d
added tests for time of flight and smearing
jordivallsq Jul 9, 2024
bc061d1
added code coverage
jordivallsq Jul 9, 2024
4d01379
polished
jordivallsq Jul 9, 2024
ce7bb58
corrected names
jordivallsq Jul 9, 2024
8c6e9bd
testing last coverage
jordivallsq Jul 10, 2024
66c0f5e
CORRECTED FORMAT
jordivallsq Jul 10, 2024
871822f
FIXED PYLINT DISABLE
jordivallsq Jul 10, 2024
fd406b5
MODIFIED TO-QUA-CONFIG INTO DICTQUACONFIG
jordivallsq Jul 10, 2024
2acc815
removed uused import
jordivallsq Jul 10, 2024
80a8337
Merge branch 'main' into qhc-588-bug-get_parameter-does-not-work-with…
jordivallsq Jul 10, 2024
95f8301
Add docstring arguments
GuillermoAbadLopez Jul 16, 2024
6142c07
Add test needed to pass
GuillermoAbadLopez Jul 16, 2024
0a5319e
Make connection not necessary for `set_parameter` in QM
GuillermoAbadLopez Jul 17, 2024
8dd05c2
Remove outdated test
GuillermoAbadLopez Jul 17, 2024
2205140
Add test to check that both local settings and _config are being changed
GuillermoAbadLopez Jul 18, 2024
2172523
Add more testing
GuillermoAbadLopez Jul 18, 2024
660e488
Solve §set_params§ unsynching `settings` and `_config` and adding mor…
GuillermoAbadLopez Jul 18, 2024
6f7df64
Remove test, where instruments have not been initialize
GuillermoAbadLopez Jul 18, 2024
2f7d068
Update quantum_machines_cluster.py
GuillermoAbadLopez Jul 18, 2024
7e4e45a
Typo in docstring
GuillermoAbadLopez Jul 24, 2024
916d5d7
Update quantum_machines_cluster.py
GuillermoAbadLopez Jul 24, 2024
a8209b4
Merge branch 'qhc-588-bug-get_parameter-does-not-work-with-qm-platfor…
GuillermoAbadLopez Jul 24, 2024
5b80879
Update quantum_machines_cluster.py
GuillermoAbadLopez Jul 24, 2024
8e0722c
Don't generate `_config` in the set
GuillermoAbadLopez Jul 24, 2024
af84411
Create test for `config_exist`
GuillermoAbadLopez Jul 25, 2024
4905b27
Improve test in QM_cluster
GuillermoAbadLopez Jul 25, 2024
8e90c39
Change dict comparison, for non-hashable
GuillermoAbadLopez Jul 25, 2024
dd34cc4
Update test_quantum_machines_cluster.py
GuillermoAbadLopez Jul 25, 2024
5846ca2
Update test_quantum_machines_cluster.py
GuillermoAbadLopez Jul 25, 2024
e5a87a4
change name to config_created flag
GuillermoAbadLopez Jul 25, 2024
017cd58
Assert that the `settings.to_qua_config()`` are a subset of the `appe…
GuillermoAbadLopez Jul 25, 2024
efeef92
Merge branch 'main' into qhc-588-bug-get_parameter-does-not-work-with…
GuillermoAbadLopez Jul 25, 2024
dfcbe96
Follow merge
GuillermoAbadLopez Jul 25, 2024
8a6b572
Put initialization into initial_setup
GuillermoAbadLopez Jul 25, 2024
2258974
Merge branch 'qhc-588-bug-get_parameter-does-not-work-with-qm-platfor…
GuillermoAbadLopez Jul 25, 2024
dc55c8d
Solve tests
GuillermoAbadLopez Jul 25, 2024
ae90f3c
Solve tests
GuillermoAbadLopez Jul 25, 2024
b02fa5d
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 25, 2024
783e2bd
Update test_platform.py
GuillermoAbadLopez Jul 25, 2024
86d6dea
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 25, 2024
38d5232
Solve problems in changelog
GuillermoAbadLopez Jul 26, 2024
1f6e44f
Improve docstring and pylint ignore
GuillermoAbadLopez Jul 26, 2024
a8e561e
Merge branch 'qhc-588-bug-get_parameter-does-not-work-with-qm-platfor…
GuillermoAbadLopez Jul 26, 2024
e68b9e3
Imprve docstring
GuillermoAbadLopez Jul 26, 2024
64f67de
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 26, 2024
ee6f045
increase coverage
GuillermoAbadLopez Jul 26, 2024
b5aae1f
increase coverage
GuillermoAbadLopez Jul 26, 2024
4c8c8d5
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 26, 2024
86027f6
Make that `_config` is created in the initialization of the class
GuillermoAbadLopez Jul 26, 2024
8794b4b
CodeQuality
GuillermoAbadLopez Jul 26, 2024
c70813e
Regex escape for test comparison
GuillermoAbadLopez Jul 26, 2024
c9e7a3d
Merge branch 'qhc-588-bug-get_parameter-does-not-work-with-qm-platfor…
GuillermoAbadLopez Jul 26, 2024
302c3fc
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 26, 2024
754f6ac
Improve testing, and possible error message
GuillermoAbadLopez Jul 26, 2024
f888f3e
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 26, 2024
2bffa4d
Update test_platform.py
GuillermoAbadLopez Jul 26, 2024
5260b32
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 26, 2024
57f958d
Improve get&set_parameter() readability
GuillermoAbadLopez Jul 26, 2024
d0fc539
Merge branch 'bug-set_parameter-without-connection' into qm_initializ…
GuillermoAbadLopez Jul 26, 2024
a71e723
Merge branch 'main' into qm_initialization_reorder
GuillermoAbadLopez Jul 30, 2024
75a1020
Merge branch 'main' into qm_initialization_reorder
GuillermoAbadLopez Jul 31, 2024
de7f152
Update test_quantum_machines_cluster.py
GuillermoAbadLopez Jul 31, 2024
6229786
Merge branch 'main' into qm_initialization_reorder
GuillermoAbadLopez Jul 31, 2024
2792c78
Update test_quantum_machines_cluster.py
GuillermoAbadLopez Jul 31, 2024
9ccef47
Delete initialization of `_config` in post_init
GuillermoAbadLopez Jul 31, 2024
45b0629
remove empty initial _config
GuillermoAbadLopez Jul 31, 2024
55af44d
Update quantum_machines_cluster.py
GuillermoAbadLopez Jul 31, 2024
1d765e3
Update quantum_machines_cluster.py
GuillermoAbadLopez Jul 31, 2024
c5fa829
Solve turn_off and turn_on
GuillermoAbadLopez Jul 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added calibration_db.json
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ def _get_elements_and_mixers_config(self) -> tuple:
_config: DictQuaConfig
_octave_config: QmOctaveConfig | None = None
_is_connected_to_qm: bool = False
_config_created: bool = False
_compiled_program_cache: dict[str, str] = {}

@property
Expand All @@ -269,26 +268,30 @@ def initial_setup(self):
host=self.settings.address, cluster_name=self.settings.cluster, octave=self._octave_config
)
self._config = self.settings.to_qua_config()
self._config_created = True

@Instrument.CheckDeviceInitialized
def turn_on(self):
"""Turns on the instrument."""
self._qm = self._qmm.open_qm(config=self._config, close_other_machines=True)
if not self._is_connected_to_qm:
self._qm = self._qmm.open_qm(config=self._config, close_other_machines=True)
self._compiled_program_cache = {}
self._is_connected_to_qm = True

if self.settings.run_octave_calibration:
self.run_octave_calibration()

@Instrument.CheckDeviceInitialized
def turn_on(self):
"""Turns on the instrument."""
if not self._is_connected_to_qm:
self.initial_setup()

@Instrument.CheckDeviceInitialized
def reset(self):
"""Resets instrument settings."""

@Instrument.CheckDeviceInitialized
def turn_off(self):
"""Turns off an instrument."""
"""Turns off an instrument. To turn it on again, run `initial_setup()`."""
# TODO: Weird, since now the `turn_on()`` doesn't do anything... Maybe it should be renamed disconnect?
# TODO: I think this is used for handling multiple users, and stuff, take a look into it...
if self._is_connected_to_qm:
self._qm.close()
self._is_connected_to_qm = False
Expand All @@ -302,7 +305,7 @@ def append_configuration(self, configuration: dict):
Raises:
ValueError: Raised if the `_config` dictionary does not exist.
"""
if not self._config_created:
if not self._is_connected_to_qm:
raise ValueError("The QM `config` dictionary does not exist. Please run `initial_setup()` first.")

merged_configuration = merge_dictionaries(dict(self._config), configuration)
Expand Down Expand Up @@ -352,41 +355,37 @@ def set_parameter_of_bus(self, bus: str, parameter: Parameter, value: float | st

# Now we will set the parameter in 3 places:
# 1) In the settings runtime dataclass (always).
# 2) If created: In the `_config`` dictionary.
# 3) If connected: In the instrument itself.
# 2) If connected: In the `_config`` dictionary and in the instrument itself.
if parameter == Parameter.LO_FREQUENCY:
lo_frequency = float(value)
settings_octave_rf_output["lo_frequency"] = lo_frequency
if self._config_created:
self._config["octaves"][octave_name]["RF_outputs"][out_port]["LO_frequency"] = lo_frequency
if self._is_connected_to_qm:
self._config["octaves"][octave_name]["RF_outputs"][out_port]["LO_frequency"] = lo_frequency
self._qm.octave.set_lo_frequency(element=bus, lo_frequency=lo_frequency)
if in_port is not None:
settings_octave_rf_input = next(
rf_input for rf_input in settings_octave["rf_inputs"] if rf_input["port"] == in_port
)
settings_octave_rf_input["lo_frequency"] = lo_frequency
if self._config_created:
if self._is_connected_to_qm:
self._config["octaves"][octave_name]["RF_inputs"][in_port]["LO_frequency"] = lo_frequency
return

if parameter == Parameter.GAIN:
gain_in_db = float(value)
settings_octave_rf_output["gain"] = gain_in_db
if self._config_created:
self._config["octaves"][octave_name]["RF_outputs"][out_port]["gain"] = gain_in_db
if self._is_connected_to_qm:
self._config["octaves"][octave_name]["RF_outputs"][out_port]["gain"] = gain_in_db
self._qm.octave.set_rf_output_gain(element=bus, gain_in_db=gain_in_db)
return

if parameter == Parameter.IF:
intermediate_frequency = float(value)
element["intermediate_frequency"] = intermediate_frequency
if self._config_created:
if self._is_connected_to_qm:
self._config["elements"][bus]["intermediate_frequency"] = intermediate_frequency
if f"mixer_{bus}" in self._config["mixers"]:
self._config["mixers"][f"mixer_{bus}"][0]["intermediate_frequency"] = intermediate_frequency
if self._is_connected_to_qm:
self._qm.set_intermediate_frequency(element=bus, freq=intermediate_frequency)
return
if parameter == Parameter.THRESHOLD_ROTATION:
Expand Down
40 changes: 23 additions & 17 deletions tests/instruments/quantum_machines/test_quantum_machines_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,36 @@ class TestQuantumMachinesCluster:
"""This class contains the unit tests for the ``QuantumMachinesCluster`` class."""

@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachinesManager")
@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachine")
@patch("qililab.instruments.Instrument.initial_setup")
@pytest.mark.parametrize("qmm_name", ["qmm", "qmm_with_octave"])
def test_initial_setup(
self, mock_instrument_init: MagicMock, mock_init: MagicMock, qmm_name, request
self, mock_qmm: MagicMock, mock_qm: MagicMock, mock_init: MagicMock, qmm_name, request
): # pylint: disable=unused-argument
"""Test QMM class initialization."""
qmm = request.getfixturevalue(qmm_name)

# Before initial_setup no _config should exist
assert qmm._config_created is False and "_config" not in dir(qmm)
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)

qmm.initial_setup()
mock_init.assert_called()

assert isinstance(qmm._qmm, MagicMock)
assert isinstance(qmm._qm, MagicMock)
assert isinstance(qmm._config, dict)
assert isinstance(qmm.config, dict)

# Assert that the _config has been created correctly (in synch):
assert qmm._config_created is True
assert qmm._is_connected_to_qm is True and "_config" in dir(qmm)

if qmm.settings.run_octave_calibration:
calls = [
call(element) for element in qmm._config["elements"] if "RF_inputs" in qmm._config["elements"][element]
]
qmm._qm.calibrate_element.assert_has_calls(calls)

# Assert that the _config is stll in synch with the settings:
assert qmm._config == qmm.settings.to_qua_config()

@pytest.mark.parametrize("qmm_name", ["qmm", "qmm_with_octave"])
Expand All @@ -156,19 +166,15 @@ def test_settings(self, qmm_name, request):
@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachinesManager")
@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachine")
@pytest.mark.parametrize("qmm_name", ["qmm", "qmm_with_octave"])
def test_turn_on(self, mock_qmm, mock_qm, qmm_name, request):
def test_turn_on_after_a_turn_off(self, mock_qmm, mock_qm, qmm_name, request):
"""Test turn_on method"""

qmm = request.getfixturevalue(qmm_name)
qmm.initial_setup()
qmm.turn_off()
qmm.turn_on()

assert isinstance(qmm._qm, MagicMock)
if qmm.settings.run_octave_calibration:
calls = [
call(element) for element in qmm._config["elements"] if "RF_inputs" in qmm._config["elements"][element]
]
qmm._qm.calibrate_element.assert_has_calls(calls)
assert qmm._is_connected_to_qm is True

# Assert that the settings are still in synch:
assert qmm._config == qmm.settings.to_qua_config()
Expand All @@ -185,7 +191,7 @@ def test_turn_off(self, mock_qmm, mock_qm, qmm_name, request):
qmm.turn_off()

# Assert that the settings are still in synch:
assert qmm._config == qmm.settings.to_qua_config()
assert qmm._is_connected_to_qm is False

assert isinstance(qmm._qm, MagicMock)
qmm._qm.close.assert_called_once()
Expand Down Expand Up @@ -279,7 +285,7 @@ def test_run(
assert isinstance(job, MagicMock)

# Assert that the settings are in synch:
assert qmm._config_created is False and "_config" not in dir(qmm)
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)

@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachinesManager")
@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachine")
Expand Down Expand Up @@ -308,7 +314,7 @@ def test_get_acquisitions(self, qmm: QuantumMachinesCluster):
assert "Q" in results

# Assert that the settings are in synch:
assert qmm._config_created is False and "_config" not in dir(qmm)
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)

@patch("qm.QuantumMachine")
def test_simulate(
Expand All @@ -322,7 +328,7 @@ def test_simulate(
assert isinstance(job, MagicMock)

# Assert that the settings are in synch:
assert qmm._config_created is False and "_config" not in dir(qmm)
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)

@pytest.mark.parametrize(
"bus, parameter, value",
Expand Down Expand Up @@ -535,7 +541,7 @@ def test_get_parameter_of_bus_method(
assert value == element.get("threshold_rotation", None)

# Assert that the settings are in synch:
assert qmm._config_created is False and "_config" not in dir(qmm)
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)

@pytest.mark.parametrize("parameter", [(Parameter.MAX_CURRENT), (Parameter.OUT0_ATT)])
@patch("qililab.instruments.quantum_machines.quantum_machines_cluster.QuantumMachinesManager")
Expand Down Expand Up @@ -578,6 +584,6 @@ def test_get_parameter_doesnt_create_a_config(
self, mock_qmm, mock_qm, bus: str, parameter: Parameter, qmm: QuantumMachinesCluster
):
"""Test the get_parameter_of_bus method doesn't create a `_config`."""
assert qmm._config_created is False
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)
qmm.get_parameter_of_bus(bus, parameter)
assert qmm._config_created is False
assert qmm._is_connected_to_qm is False and "_config" not in dir(qmm)
Loading