Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing remove id when removing id results in a self-loop #251

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading