Skip to content

Commit

Permalink
Fix multigraph color change
Browse files Browse the repository at this point in the history
  • Loading branch information
boldar99 committed Jun 22, 2024
1 parent b2bb047 commit 5bdf6c2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
3 changes: 1 addition & 2 deletions pyzx/editor_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def color_change(g: BaseGraph[VT,ET], matches: List[VT]) -> rules.RewriteOutputT
for v in matches:
g.set_type(v, toggle_vertex(g.type(v)))
for e in g.incident_edges(v):
et = g.edge_type(e)
g.set_edge_type(e, toggle_edge(et))
g.toggle_edge_type(e)
return ({}, [],[],False)


Expand Down
6 changes: 6 additions & 0 deletions pyzx/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,12 @@ def set_edge_type(self, e: ET, t: EdgeType.Type) -> None:
"""Sets the type of the given edge."""
raise NotImplementedError("Not implemented on backend " + type(self).backend)

def toggle_edge_type(self, e: ET) -> None:
"""Toggles the type of the edge between ``EdgeType.SIMPLE`` and ``EdgeType.HADAMARD``.
Does nothing if the edge is empty.
"""
raise NotImplementedError("Not implemented on backend " + type(self).backend)

def type(self, vertex: VT) -> VertexType.Type:
"""Returns the type of the given vertex:
VertexType.BOUNDARY if it is a boundary, VertexType.Z if it is a Z node,
Expand Down
3 changes: 3 additions & 0 deletions pyzx/graph/graph_ig.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ig = None

from .base import BaseGraph, VertexType, EdgeType
from ..utils import toggle_edge

class GraphIG(BaseGraph):
"""Implementation of :class:`~graph.base.BaseGraph` using ``python-igraph``
Expand Down Expand Up @@ -115,6 +116,8 @@ def edge_type(self, e):
def set_edge_type(self, e, t):
self.graph.es[e]['_t'] = t

def toggle_edge_type(self, e):
self.set_edge_type(e, toggle_edge(self.graph.es[e]['_t']))

def type(self, v):
t = self.graph.vs[v]['_t']
Expand Down
6 changes: 5 additions & 1 deletion pyzx/graph/graph_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .base import BaseGraph

from ..utils import VertexType, EdgeType, FractionLike, FloatInt, vertex_is_zx_like, vertex_is_z_like, set_z_box_label, get_z_box_label
from ..utils import VertexType, EdgeType, FractionLike, FloatInt, vertex_is_zx_like, vertex_is_z_like, set_z_box_label, get_z_box_label, toggle_edge

class GraphS(BaseGraph[int,Tuple[int,int]]):
"""Purely Pythonic implementation of :class:`~graph.base.BaseGraph`."""
Expand Down Expand Up @@ -275,6 +275,10 @@ def set_edge_type(self, e, t):
self.graph[v1][v2] = t
self.graph[v2][v1] = t

def toggle_edge_type(self, e):
if edge_type := self.edge_type(e):
self.set_edge_type(e, toggle_edge(edge_type))

def type(self, vertex):
return self.ty[vertex]
def types(self):
Expand Down
5 changes: 5 additions & 0 deletions pyzx/graph/multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ def set_edge_type(self, edge, t):
elif t == EdgeType.HADAMARD: e.add(h=1)
else: e.add(w_io=1)

def toggle_edge_type(self, edge):
v1,v2 = edge
e = self.graph[v1][v2]
e.h, e.s = e.s, e.h

def type(self, vertex):
return self.ty[vertex]
def types(self):
Expand Down
6 changes: 5 additions & 1 deletion pyzx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ class EdgeType:

def toggle_edge(ty: EdgeType.Type) -> EdgeType.Type:
"""Swap the regular and Hadamard edge types."""
return EdgeType.HADAMARD if ty == EdgeType.SIMPLE else EdgeType.SIMPLE
if ty == EdgeType.SIMPLE:
return EdgeType.HADAMARD
if ty == EdgeType.HADAMARD:
return EdgeType.SIMPLE
return ty

def phase_to_s(a: FractionLike, t:VertexType.Type=VertexType.Z) -> str:
if isinstance(a, Fraction) or isinstance(a, int):
Expand Down

0 comments on commit 5bdf6c2

Please sign in to comment.