From 571c859e312cf28409f7b0fde847251246c0b142 Mon Sep 17 00:00:00 2001 From: Stan van der Linde Date: Mon, 16 Dec 2024 11:43:06 +0100 Subject: [PATCH] Apply suggestions --- .../exporter/quantify_scheduler_exporter.py | 2 +- opensquirrel/utils/bit_register_mapping.py | 24 +++++++++---------- test/utils/test_bit_register_mapping.py | 14 +++++------ 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/opensquirrel/passes/exporter/quantify_scheduler_exporter.py b/opensquirrel/passes/exporter/quantify_scheduler_exporter.py index 1051e912..98f66c75 100644 --- a/opensquirrel/passes/exporter/quantify_scheduler_exporter.py +++ b/opensquirrel/passes/exporter/quantify_scheduler_exporter.py @@ -84,7 +84,7 @@ def visit_controlled_gate(self, g: ControlledGate) -> None: def visit_measure(self, g: Measure) -> None: self.bit_string_mapping.add_measure(g) - acq_channel, acq_index = self.bit_string_mapping.get_last_added_acq() + acq_channel, acq_index = self.bit_string_mapping.get_last_added_acq_channel_and_index() self.schedule.add( quantify_scheduler_gates.Measure( self._get_qubit_string(g.qubit), diff --git a/opensquirrel/utils/bit_register_mapping.py b/opensquirrel/utils/bit_register_mapping.py index f4133f0e..20a6ed20 100644 --- a/opensquirrel/utils/bit_register_mapping.py +++ b/opensquirrel/utils/bit_register_mapping.py @@ -17,28 +17,26 @@ def __init__(self) -> None: self._mapping: defaultdict[int, list[int]] = defaultdict(list) self._bit_allocation: dict[int, int] = {} - def add_measure(self, g: Measure) -> None: - """Add a acquisition_channel and bitstring_index of `g` to the mapping. + def add_measure(self, measure: Measure) -> None: + """Add an acquisition channel and bitstring index of `measure` to the mapping. Args: - g: measurement to add the acquisition_channel and bitstring_index from. + measure: measurement to add the acquisition channel and bitstring index from. """ - acq_channel = g.qubit.index - bit_idx = g.bit.index + acq_channel = measure.qubit.index + bit_idx = measure.bit.index self._mapping[acq_channel].append(bit_idx) self._bit_allocation[bit_idx] = acq_channel - def get_last_added_acq(self) -> tuple[int, int]: - """Get the last added acquisition_channel corresponding acquisition_index. + def get_last_added_acq_channel_and_index(self) -> tuple[int, int]: + """Get the last added acquisition channel corresponding acquisition index. - Especially useful when exporting to ``quantify_scheduler``, since not only the - final acquisition_channel-acquisition_index pairs are needed, but also all - intermediate pairs. + Especially useful when exporting to ``quantify_scheduler``, since not only the final (acquisition channel, + acquisition index) pairs are needed, but also all intermediate pairs. Returns: - Tuple containing the last added acquisition_channel and corresponding - acquisition_index. + Tuple containing the last added acquisition channel and corresponding acquisition index. """ try: acq_channel = next(reversed(self._mapping)) @@ -49,7 +47,7 @@ def get_last_added_acq(self) -> tuple[int, int]: return acq_channel, acq_idx def to_export_format(self) -> dict[str, dict[str, int]]: - """Export to json format. + """Export to JSON serializable format. For each bit, only the last executed measurement remains in the mapping. diff --git a/test/utils/test_bit_register_mapping.py b/test/utils/test_bit_register_mapping.py index 6cb470f8..5854dfac 100644 --- a/test/utils/test_bit_register_mapping.py +++ b/test/utils/test_bit_register_mapping.py @@ -34,20 +34,20 @@ def test_add_measure( assert bit_string_mapping._bit_allocation == expected_bit_allocation -def test_get_last_added_acq(bit_string_mapping: BitStringMapping) -> None: +def test_get_last_added_acq_channel_and_index(bit_string_mapping: BitStringMapping) -> None: bit_string_mapping.add_measure(measure(0, 0)) - assert bit_string_mapping.get_last_added_acq() == (0, 0) + assert bit_string_mapping.get_last_added_acq_channel_and_index() == (0, 0) bit_string_mapping.add_measure(measure(0, 0)) - assert bit_string_mapping.get_last_added_acq() == (0, 1) + assert bit_string_mapping.get_last_added_acq_channel_and_index() == (0, 1) bit_string_mapping.add_measure(measure(1, 0)) - assert bit_string_mapping.get_last_added_acq() == (1, 0) + assert bit_string_mapping.get_last_added_acq_channel_and_index() == (1, 0) bit_string_mapping.add_measure(measure(2, 1)) - assert bit_string_mapping.get_last_added_acq() == (2, 0) + assert bit_string_mapping.get_last_added_acq_channel_and_index() == (2, 0) -def test_get_last_added_acq_error(bit_string_mapping: BitStringMapping) -> None: +def test_get_last_added_acq_channel_and_index_error(bit_string_mapping: BitStringMapping) -> None: with pytest.raises(ValueError, match="BitStringMapping is empty, so there is no acq to get"): - bit_string_mapping.get_last_added_acq() + bit_string_mapping.get_last_added_acq_channel_and_index() def test_to_export_format(bit_string_mapping: BitStringMapping) -> None: