diff --git a/pyzx/basicrules.py b/pyzx/basicrules.py index 18eebc0e..093bb3eb 100644 --- a/pyzx/basicrules.py +++ b/pyzx/basicrules.py @@ -251,7 +251,11 @@ def remove_id(g: BaseGraph[VT,ET], v: VT) -> bool: if not check_remove_id(g, v): return False - v1, v2 = tuple(g.neighbors(v)) + neighbors = list(g.neighbors(v)) + if len(neighbors) == 2: + v1, v2 = neighbors[0], neighbors[0] + else: # self loop + v1, v2 = neighbors[0], neighbors[0] g.add_edge((v1,v2), edgetype=EdgeType.SIMPLE if g.edge_type(g.edge(v,v1)) == g.edge_type(g.edge(v,v2)) else EdgeType.HADAMARD) diff --git a/pyzx/graph/multigraph.py b/pyzx/graph/multigraph.py index 9d987f92..fc7f3861 100644 --- a/pyzx/graph/multigraph.py +++ b/pyzx/graph/multigraph.py @@ -334,8 +334,12 @@ def connected(self,v1,v2): def edge_type(self, e): if len(e) == 2: edges = list(self.edges(e[0],e[1])) - if len(edges) != 1: - raise ValueError('Cannot determine edge type') + if len(edges) > 1: + # if all edges are of the same type, return that type + if all(e[2] == edges[0][2] for e in edges): + return edges[0][2] + else: + raise ValueError('Cannot determine edge type') e = edges[0] return e[2] diff --git a/pyzx/rules.py b/pyzx/rules.py index 708a6248..09e2e0a7 100644 --- a/pyzx/rules.py +++ b/pyzx/rules.py @@ -791,9 +791,12 @@ def match_ids_parallel( v = candidates.pop() if phases[v] != 0 or not vertex_is_zx(types[v]) or g.is_ground(v) or g.vertex_degree(v) != 2: continue + if len(g.incident_edges(v)) != 2: continue neigh = g.neighbors(v) - if len(neigh) != 2: continue - v0, v1 = neigh + if len(neigh) == 2: + v0, v1 = neigh + else: + v0, v1 = list(neigh)[0], list(neigh)[0] if (g.is_ground(v0) and types[v1] == VertexType.BOUNDARY or g.is_ground(v1) and types[v0] == VertexType.BOUNDARY): # Do not put ground spiders on the boundary