TeamGraphix / graphix

measurement-based quantum computing (MBQC) compiler and simulator
https://graphix.readthedocs.io
Apache License 2.0
64 stars 21 forks source link

[Bug]: `remove_edge` ignores edge symmetry with `rustworkx` #206

Open thierry-martinez opened 3 months ago

thierry-martinez commented 3 months ago

The following test succeeds with networkx back-end but fails with rustworkx back-end. Edge keys in the table num_to_data are directed.

@pytest.mark.parametrize("use_rustworkx", [False, True])
def test_remove_edge(use_rustworkx: bool) -> None:
    g = GraphState(nodes=(0, 1), edges=[(0, 1)], use_rustworkx=use_rustworkx)
    g.remove_edge(1, 0)

The following trivial fix circumvents this bug, but there must be cleaner way to fix it (why all the graph is duplicated as views in Python data structures when rustworkx is used?).

@@ -145,7 +145,14 @@ class EdgeList:
                 continue
             self.add_edge(enum, edata, eidx)

-    def remove_edge(self, enum: tuple[int, int]):
+    def remove_edge(self, enum):
+        (u, v) = enum
+        try:
+            self.remove_edge_((u, v))
+        except ValueError:
+            self.remove_edge_((v, u))
+
+    def remove_edge_(self, enum: tuple[int, int]):
         if enum not in self.num_to_data:
             raise ValueError(f"Edge {enum} does not exist")
         self.edges.remove(enum)