Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Linde committed Dec 16, 2024
1 parent 47eb641 commit 571c859
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
24 changes: 11 additions & 13 deletions opensquirrel/utils/bit_register_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions test/utils/test_bit_register_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 571c859

Please sign in to comment.