From 65ab5b7842d925cb258cbef1976a9e550d38a61b Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Thu, 31 Oct 2024 19:20:41 +0100 Subject: [PATCH 1/6] feat: Allow parameters update through update dictionaries --- src/qibolab/_core/parameters.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/qibolab/_core/parameters.py b/src/qibolab/_core/parameters.py index 1ad0957cd..80869acb2 100644 --- a/src/qibolab/_core/parameters.py +++ b/src/qibolab/_core/parameters.py @@ -169,6 +169,18 @@ def _dump_configs(obj: dict[ComponentId, Config]) -> dict[str, dict]: return {k: a.dump_python(v) for k, v in obj.items()} +def _setvalue(d: dict, path: str, val: Any): + steps = path.split(".") + current = d + for acc in steps[:-1]: + current = current[acc] + + current[steps[-1]] = val + + +Update = dict[str, Any] + + class Parameters(Model): """Serializable parameters.""" @@ -179,3 +191,11 @@ class Parameters(Model): PlainSerializer(_dump_configs), ] = Field(default_factory=dict) native_gates: NativeGates = Field(default_factory=NativeGates) + + def update(self, update: Update) -> "Parameters": + """Update parameters' values.""" + d = self.model_dump() + for path, val in update.items(): + _setvalue(d, path, val) + + return self.model_validate(d) From 98c4761cbdf3b839f08c4ec937c680f6274724b6 Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Thu, 31 Oct 2024 19:22:43 +0100 Subject: [PATCH 2/6] fix: Handle list access with string represented indices --- src/qibolab/_core/parameters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qibolab/_core/parameters.py b/src/qibolab/_core/parameters.py index 80869acb2..b28b8dcf5 100644 --- a/src/qibolab/_core/parameters.py +++ b/src/qibolab/_core/parameters.py @@ -172,8 +172,8 @@ def _dump_configs(obj: dict[ComponentId, Config]) -> dict[str, dict]: def _setvalue(d: dict, path: str, val: Any): steps = path.split(".") current = d - for acc in steps[:-1]: - current = current[acc] + for step in steps[:-1]: + current = current[step if isinstance(current, dict) else int(step)] current[steps[-1]] = val From 6542f2c08dd2a7233391656078a9a10028ec86c9 Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Wed, 6 Nov 2024 17:38:06 +0100 Subject: [PATCH 3/6] feat: Improve update ui, expose through the platform --- src/qibolab/_core/parameters.py | 2 +- src/qibolab/_core/platform/platform.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/qibolab/_core/parameters.py b/src/qibolab/_core/parameters.py index b28b8dcf5..640c879bd 100644 --- a/src/qibolab/_core/parameters.py +++ b/src/qibolab/_core/parameters.py @@ -192,7 +192,7 @@ class Parameters(Model): ] = Field(default_factory=dict) native_gates: NativeGates = Field(default_factory=NativeGates) - def update(self, update: Update) -> "Parameters": + def replace(self, update: Update) -> "Parameters": """Update parameters' values.""" d = self.model_dump() for path, val in update.items(): diff --git a/src/qibolab/_core/platform/platform.py b/src/qibolab/_core/platform/platform.py index 218fbdb0e..ff1bbe73d 100644 --- a/src/qibolab/_core/platform/platform.py +++ b/src/qibolab/_core/platform/platform.py @@ -12,7 +12,7 @@ from ..execution_parameters import ExecutionParameters from ..identifier import ChannelId, QubitId, QubitPairId, Result from ..instruments.abstract import Controller, Instrument, InstrumentId -from ..parameters import NativeGates, Parameters, Settings, update_configs +from ..parameters import NativeGates, Parameters, Settings, Update, update_configs from ..pulses import PulseId from ..qubits import Qubit from ..sequence import PulseSequence @@ -168,6 +168,10 @@ def config(self, name: str) -> Config: # pylint: disable=unsubscriptable-object return self.parameters.configs[name] + def update(self, update: Update): + """Update platform's parameters.""" + self.parameters = self.parameters.replace(update) + def connect(self): """Connect to all instruments.""" if not self.is_connected: From ebcd1a6b4f73b38e61320ac17481799cc741d534 Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Wed, 6 Nov 2024 18:10:19 +0100 Subject: [PATCH 4/6] test: Set up test for updating parameters --- tests/test_parameters.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 5cc956a9d..546188ff4 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -5,6 +5,8 @@ from qibolab._core.components.configs import Config from qibolab._core.native import Native, TwoQubitNatives from qibolab._core.parameters import ConfigKinds, Parameters, TwoQubitContainer +from qibolab._core.platform.load import create_platform +from qibolab._core.pulses.pulse import Pulse def test_two_qubit_container(): @@ -77,3 +79,12 @@ def test_within_parameters(self): reloaded = Parameters.model_validate(dump) assert reloaded == pars + + +def test_update(): + dummy = create_platform("dummy") + dummy.update({}) + + assert isinstance(dummy.parameters.native_gates.single_qubit[1].RX[0][1], Pulse) + assert dummy.parameters.native_gates.single_qubit[1].RX[0][1].amplitude > 0 + dummy.update({"native_gates.single_qubit.1.RX.0.1.amplitude": -0.5}) From a151964dbc249a254d915ace3d35e6930186428e Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Wed, 6 Nov 2024 18:11:58 +0100 Subject: [PATCH 5/6] Update src/qibolab/_core/parameters.py Co-authored-by: Stavros Efthymiou <35475381+stavros11@users.noreply.github.com> --- src/qibolab/_core/parameters.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/qibolab/_core/parameters.py b/src/qibolab/_core/parameters.py index 640c879bd..dba5aa3db 100644 --- a/src/qibolab/_core/parameters.py +++ b/src/qibolab/_core/parameters.py @@ -173,7 +173,10 @@ def _setvalue(d: dict, path: str, val: Any): steps = path.split(".") current = d for step in steps[:-1]: - current = current[step if isinstance(current, dict) else int(step)] + try: + current = current[int(step)] + except ValueError: + current = current[step] current[steps[-1]] = val From f2850cd276bee0afdac9d18eee517d1113d4188e Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Wed, 6 Nov 2024 18:33:48 +0100 Subject: [PATCH 6/6] test: Extend parameters' update test --- tests/test_parameters.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 546188ff4..5cc461c1f 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -86,5 +86,21 @@ def test_update(): dummy.update({}) assert isinstance(dummy.parameters.native_gates.single_qubit[1].RX[0][1], Pulse) - assert dummy.parameters.native_gates.single_qubit[1].RX[0][1].amplitude > 0 + assert dummy.natives.single_qubit[1].RX[0][1].amplitude > 0 dummy.update({"native_gates.single_qubit.1.RX.0.1.amplitude": -0.5}) + assert dummy.natives.single_qubit[1].RX[0][1].amplitude < 0 + + assert dummy.settings.nshots != 1234567890 + dummy.update({"settings.nshots": 1234567890}) + assert dummy.settings.nshots == 1234567890 + + dummy.update( + { + "settings.nshots": 42, + "native_gates.single_qubit.1.RX.0.1.amplitude": -0.123, + "native_gates.single_qubit.1.RX.0.1.duration": 456.7, + } + ) + assert dummy.settings.nshots == 42 + assert dummy.natives.single_qubit[1].RX[0][1].amplitude == -0.123 + assert dummy.natives.single_qubit[1].RX[0][1].duration == 456.7