Skip to content

Commit

Permalink
preprocessing for pauli web prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
akissinger committed Dec 15, 2024
1 parent f1ac449 commit 25e6e2c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 75 deletions.
8 changes: 3 additions & 5 deletions pyzx/gflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

from typing import Dict, Set, Tuple, Optional

from networkx import neighbors

from .extract import bi_adj
from .linalg import Mat2
from .graph.base import BaseGraph, VertexType, VT, ET
from .graph.base import BaseGraph, VT, ET
from .utils import vertex_is_zx


Expand Down Expand Up @@ -99,7 +97,7 @@ def gflow(

k: int = 1
while True:
correct = set()
correct: Set[VT] = set()
processed_prime = [
v
for v in (processed | paulis).difference(pattern_inputs)
Expand All @@ -121,7 +119,7 @@ def gflow(

m = bi_adj(g, processed_prime, clean)
for index, u in enumerate(clean):
if not focus or (u not in processed and any(w in processed_prime for w in g.neighbors(v))):
if not focus or (u not in processed and any(w in processed_prime for w in g.neighbors(u))):
vu = zerovec.copy()
vu.data[index][0] = 1
x = m.solve(vu)
Expand Down
47 changes: 33 additions & 14 deletions pyzx/pauliwebs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,58 @@
from .circuit import Circuit
from .utils import EdgeType, VertexType
from .simplify import gadgetize, to_rg
from .graph.base import BaseGraph
from .graph.base import BaseGraph, VT, ET


def preprocess(g: BaseGraph):
def preprocess(g: BaseGraph[VT,ET]):
gadgetize(g)
to_rg(g) # TODO: i/o should stay Z
in_circ = Circuit()
out_circ = Circuit()
to_rg(g)

in_circ = Circuit(len(g.inputs()))
for j,i in enumerate(g.inputs()):
e = g.incident_edges(i)[0]
v = g.neighbors(i)[0]
v = next(iter(g.neighbors(i)))
p = g.phase(v)
ty = g.type(v)

# remove local cliffords from the inputs
if g.edge_type(e) == EdgeType.HADAMARD:
in_circ.add_gate('H', j)
g.set_edge_type(e, EdgeType.SIMPLE)
if p != 0:
g.set_phase(v, 0)
in_circ.add_gate("ZPhase", j, phase=p)
in_circ.add_gate("ZPhase" if ty == VertexType.Z else "XPhase", j, phase=p)

out_circ = Circuit(len(g.outputs()))
for j,o in enumerate(g.outputs()):
r = g.get_row(o)
g.set_row(o, r + 1)
r = g.row(o)
g.set_row(o, r + 2)
e = g.incident_edges(o)[0]
v = g.neighbors(o)[0]
v = next(iter(g.neighbors(o)))
p = g.phase(v)
ty = g.type(v)

# remove local cliffords from the outputs
if p != 0:
g.set_phase(v, 0)
out_circ.add_gate("ZPhase", j, phase=p)
out_circ.add_gate("ZPhase" if ty == VertexType.Z else "XPhase", j, phase=p)

if g.edge_type(e) == EdgeType.HADAMARD:
out_circ.add_gate('H', j)
g.remove_edge(e)

# introduce ID spiders at the outputs for computing pauli webs
if ty == VertexType.X:
v1 = g.add_vertex(VertexType.Z, qubit=g.qubit(o), row=r)
g.add_edge((v,v1), EdgeType.SIMPLE)
else:
v1 = v
g.set_row(v1, r)

v2 = g.add_vertex(VertexType.X, qubit=g.qubit(o), row=r+1)


v1 = g.add_vertex(VertexType.Z, qubit=g.qubit(o), row=r)
g.add_edge((v,v1), EdgeType.HADAMARD)
g.add_edge((v1,o), EdgeType.HADAMARD)
g.add_edge((v1,v2), EdgeType.SIMPLE)
g.add_edge((v2,o), EdgeType.SIMPLE)

return (in_circ, out_circ)
4 changes: 2 additions & 2 deletions pyzx/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def simp(
Returns:
Number of iterations of ``rewrite`` that had to be applied before no more matches were found."""

auto_simp_value = g.get_auto_simplify()
if auto_simplify_parallel_edges:
auto_simp_value = g.get_auto_simplify()
g.set_auto_simplify(True)
i = 0
new_matches = True
Expand Down Expand Up @@ -361,7 +361,7 @@ def to_rg(g: BaseGraph[VT,ET], select:Optional[Callable[[VT],bool]]=None, change
for e in g.incident_edges(v):
g.set_edge_type(e, toggle_edge(g.edge_type(e)))

def gadgetize(g: BaseGraph):
def gadgetize(g: BaseGraph[VT,ET]):
"""Convert every non-Clifford phase to a phase gadget"""
for v in list(g.vertices()):
p = g.phase(v)
Expand Down
Loading

0 comments on commit 25e6e2c

Please sign in to comment.