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

Fix multigraph color change #239

Closed
wants to merge 2 commits into from
Closed
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
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
13 changes: 13 additions & 0 deletions pyzx/graph/multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def __init__(self, s: int=0, h: int=0, w_io: int=0):
self.h = h
self.w_io = w_io

def __repr__(self):
s = f"s={self.s}" if self.s else ""
h = f"h={self.h}" if self.h else ""
w_io = f"w_io={self.w_io}" if self.w_io else ""
return f"Edge({', '.join([s, h, w_io])})"

def add(self, s: int=0, h: int=0, w_io: int=0):
self.s += s
self.h += h
Expand Down Expand Up @@ -348,6 +354,13 @@ 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: Edge) -> None:
v1, v2 = edge
e = self.graph[v1][v2]
if e.w_io:
raise ValueError(f'Cannot toggle {repr(e)}')
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
raise ValueError(f'Cannot toggle {repr(ty)}')

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