Skip to content

Commit

Permalink
Fix NodeIndex.remove bug which misinterprets non-runnable patterns
Browse files Browse the repository at this point in the history
Nodes were not removed from `NodeIndex` dictionary which allow
non-runnable patterns to use wrong nodes by mistake.

Related to TeamGraphix#193 (runnability issues).
  • Loading branch information
thierry-martinez committed Sep 27, 2024
1 parent b58111a commit f57033e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions graphix/sim/base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def remove(self, node: int) -> None:
"""Remove the specified node label from the list and dictionary, and re-attributes qubit indices for the remaining nodes."""
index = self.__dict[node]
del self.__list[index]
del self.__dict[node]
for new_index, node in enumerate(self.__list[index:], start=index):
self.__dict[node] = new_index

Expand Down
7 changes: 7 additions & 0 deletions tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,13 @@ def test_arbitrary_inputs_tn(self, fx_rng: Generator, nqb: int, rand_circ: Circu
backend="tensornetwork", graph_prep="sequential", input_state=states, rng=fx_rng
)

def test_remove_qubit(self) -> None:
p = Pattern(input_nodes=[0, 1])
p.add(M(node=0))
p.add(C(node=0, clifford=clifford.X))
with pytest.raises(KeyError):
p.simulate_pattern()


def assert_equal_edge(edge: Sequence[int], ref: Sequence[int]) -> bool:
return any(all(ei == ri for ei, ri in zip(edge, other)) for other in (ref, reversed(ref)))

0 comments on commit f57033e

Please sign in to comment.