Skip to content

Commit

Permalink
Merge pull request #214 from y-richie-y/matchf
Browse files Browse the repository at this point in the history
`matchf` everywhere
  • Loading branch information
jvdwetering committed Apr 23, 2024
2 parents 4e1ab6e + bb84ac0 commit 0bfc20a
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions pyzx/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'full_reduce', 'teleport_reduce', 'reduce_scalar', 'supplementarity_simp',
'to_clifford_normal_form_graph', 'to_graph_like', 'is_graph_like']

from optparse import Option
from typing import List, Callable, Optional, Union, Generic, Tuple, Dict, Iterator, cast

from .utils import EdgeType, VertexType, toggle_edge, vertex_is_zx, toggle_vertex
Expand Down Expand Up @@ -125,8 +126,8 @@ def spider_simp(g: BaseGraph[VT,ET], matchf:Optional[Callable[[VT],bool]]=None,
def id_simp(g: BaseGraph[VT,ET], matchf:Optional[Callable[[VT],bool]]=None, quiet:bool=False, stats:Optional[Stats]=None) -> int:
return simp(g, 'id_simp', match_ids_parallel, remove_ids, matchf=matchf, quiet=quiet, stats=stats)

def gadget_simp(g: BaseGraph[VT,ET], quiet:bool=False, stats:Optional[Stats]=None) -> int:
return simp(g, 'gadget_simp', match_phase_gadgets, merge_phase_gadgets, quiet=quiet, stats=stats)
def gadget_simp(g: BaseGraph[VT,ET], matchf: Optional[Callable[[VT],bool]]=None, quiet:bool=False, stats:Optional[Stats]=None) -> int:
return simp(g, 'gadget_simp', match_phase_gadgets, merge_phase_gadgets, matchf=matchf, quiet=quiet, stats=stats)

def supplementarity_simp(g: BaseGraph[VT,ET], quiet:bool=False, stats:Optional[Stats]=None) -> int:
return simp(g, 'supplementarity_simp', match_supplementarity, apply_supplementarity, quiet=quiet, stats=stats)
Expand All @@ -143,28 +144,28 @@ def phase_free_simp(g: BaseGraph[VT,ET], quiet:bool=False, stats:Optional[Stats]
i2 = bialg_simp(g, quiet=quiet, stats=stats)
return i1+i2

def interior_clifford_simp(g: BaseGraph[VT,ET], quiet:bool=False, stats:Optional[Stats]=None) -> int:
def interior_clifford_simp(g: BaseGraph[VT,ET], matchf: Optional[Callable[[Union[VT, ET]],bool]]=None, quiet:bool=False, stats:Optional[Stats]=None) -> int:
"""Keeps doing the simplifications ``id_simp``, ``spider_simp``,
``pivot_simp`` and ``lcomp_simp`` until none of them can be applied anymore."""
spider_simp(g, quiet=quiet, stats=stats)
spider_simp(g, matchf=matchf, quiet=quiet, stats=stats)
to_gh(g)
i = 0
while True:
i1 = id_simp(g, quiet=quiet, stats=stats)
i2 = spider_simp(g, quiet=quiet, stats=stats)
i3 = pivot_simp(g, quiet=quiet, stats=stats)
i4 = lcomp_simp(g, quiet=quiet, stats=stats)
i1 = id_simp(g, matchf=matchf, quiet=quiet, stats=stats)
i2 = spider_simp(g, matchf=matchf, quiet=quiet, stats=stats)
i3 = pivot_simp(g, matchf=matchf, quiet=quiet, stats=stats)
i4 = lcomp_simp(g, matchf=matchf, quiet=quiet, stats=stats)
if i1+i2+i3+i4==0: break
i += 1
return i

def clifford_simp(g: BaseGraph[VT,ET], quiet:bool=True, stats:Optional[Stats]=None) -> int:
def clifford_simp(g: BaseGraph[VT,ET], matchf: Optional[Callable[[Union[VT, ET]],bool]]=None, quiet:bool=True, stats:Optional[Stats]=None) -> int:
"""Keeps doing rounds of :func:`interior_clifford_simp` and
:func:`pivot_boundary_simp` until they can't be applied anymore."""
i = 0
while True:
i += interior_clifford_simp(g, quiet=quiet, stats=stats)
i2 = pivot_boundary_simp(g, quiet=quiet, stats=stats)
i += interior_clifford_simp(g, matchf=matchf, quiet=quiet, stats=stats)
i2 = pivot_boundary_simp(g, matchf=matchf, quiet=quiet, stats=stats)
if i2 == 0:
break
return i
Expand Down Expand Up @@ -192,20 +193,19 @@ def reduce_scalar(g: BaseGraph[VT,ET], quiet:bool=True, stats:Optional[Stats]=No
return i



def full_reduce(g: BaseGraph[VT,ET], quiet:bool=True, stats:Optional[Stats]=None) -> None:
def full_reduce(g: BaseGraph[VT,ET], matchf: Optional[Callable[[Union[VT, ET]],bool]]=None, quiet:bool=True, stats:Optional[Stats]=None) -> None:
"""The main simplification routine of PyZX. It uses a combination of :func:`clifford_simp` and
the gadgetization strategies :func:`pivot_gadget_simp` and :func:`gadget_simp`."""
if any(g.types()[h] == VertexType.H_BOX for h in g.vertices()):
raise ValueError("Input graph is not a ZX-diagram as it contains an H-box. "
"Maybe call pyzx.hsimplify.from_hypergraph_form(g) first?")
interior_clifford_simp(g, quiet=quiet, stats=stats)
pivot_gadget_simp(g,quiet=quiet, stats=stats)
interior_clifford_simp(g, matchf=matchf, quiet=quiet, stats=stats)
pivot_gadget_simp(g, matchf=matchf, quiet=quiet, stats=stats)
while True:
clifford_simp(g,quiet=quiet, stats=stats)
i = gadget_simp(g, quiet=quiet, stats=stats)
interior_clifford_simp(g,quiet=quiet, stats=stats)
j = pivot_gadget_simp(g,quiet=quiet, stats=stats)
clifford_simp(g, matchf=matchf, quiet=quiet, stats=stats)
i = gadget_simp(g, matchf=matchf, quiet=quiet, stats=stats)
interior_clifford_simp(g, matchf=matchf, quiet=quiet, stats=stats)
j = pivot_gadget_simp(g, matchf=matchf, quiet=quiet, stats=stats)
if i+j == 0:
break

Expand Down

0 comments on commit 0bfc20a

Please sign in to comment.