Skip to content

Commit

Permalink
Merge pull request #251 from RazinShaikh/remove-id-loop
Browse files Browse the repository at this point in the history
Fixing remove id when removing id results in a self-loop
  • Loading branch information
jvdwetering authored Jul 7, 2024
2 parents c08de18 + 7eac585 commit bad6ae2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pyzx/basicrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions pyzx/graph/multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
7 changes: 5 additions & 2 deletions pyzx/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bad6ae2

Please sign in to comment.