Skip to content

Commit

Permalink
Fixes bug in to_graph_like. See zxcalc#195
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdwetering committed Feb 22, 2024
1 parent a6e34d9 commit 54c6cf5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pyzx/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'pivot_gadget_simp', 'pivot_boundary_simp', 'gadget_simp',
'lcomp_simp', 'clifford_simp', 'tcount', 'to_gh', 'to_rg',
'full_reduce', 'teleport_reduce', 'reduce_scalar', 'supplementarity_simp',
'to_clifford_normal_form_graph']
'to_clifford_normal_form_graph', 'to_graph_like', 'is_graph_like']

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

Expand Down Expand Up @@ -492,13 +492,20 @@ def to_graph_like(g: BaseGraph[VT,ET]) -> None:

# add dummy spiders for all but one
for b in boundary_ns[:-1]:
z1 = g.add_vertex(ty=VertexType.Z)
z2 = g.add_vertex(ty=VertexType.Z)
e = g.edge(v,b)
if g.edge_type(e) == EdgeType.SIMPLE:
z1 = g.add_vertex(ty=VertexType.Z,row=0.3*g.row(v)+0.7*g.row(b),qubit=0.3*g.qubit(v)+0.7*g.qubit(b))
z2 = g.add_vertex(ty=VertexType.Z,row=0.7*g.row(v)+0.3*g.row(b),qubit=0.7*g.qubit(v)+0.3*g.qubit(b))

g.remove_edge(g.edge(v, b))
g.add_edge(g.edge(z1, z2), edgetype=EdgeType.HADAMARD)
g.add_edge(g.edge(b, z1), edgetype=EdgeType.SIMPLE)
g.add_edge(g.edge(z2, v), edgetype=EdgeType.HADAMARD)
g.remove_edge(e)
g.add_edge(g.edge(z1, z2), edgetype=EdgeType.HADAMARD)
g.add_edge(g.edge(b, z1), edgetype=EdgeType.SIMPLE)
g.add_edge(g.edge(z2, v), edgetype=EdgeType.HADAMARD)
elif g.edge_type(e) == EdgeType.HADAMARD:
z = g.add_vertex(ty=VertexType.Z,row=0.5*g.row(v)+0.5*g.row(b),qubit=0.5*g.qubit(v)+0.5*g.qubit(b))
g.remove_edge(e)
g.add_edge(g.edge(b,z),EdgeType.SIMPLE)
g.add_edge(g.edge(z,v),EdgeType.HADAMARD)

assert(is_graph_like(g))

Expand Down
14 changes: 14 additions & 0 deletions tests/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ def test_teleport_reduce(self):
c2 = Circuit.from_graph(teleport_reduce(g))
self.assertTrue(c.verify_equality(c2))

def test_to_graph_like_introduce_boundary_vertices(self):
c = qasm(qasm_5)
g = c.to_graph()
to_graph_like(g)
self.assertTrue(compare_tensors(c,g))

qasm_1 = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
Expand Down Expand Up @@ -155,6 +161,14 @@ def test_teleport_reduce(self):
cx q[1], q[2];
"""

qasm_5 = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
h q[0];
h q[1];
cz q[0],q[1];
"""


if __name__ == '__main__':
unittest.main()

0 comments on commit 54c6cf5

Please sign in to comment.