From 3600f88ed99d2946e9c0a663ab97b0533e4ea970 Mon Sep 17 00:00:00 2001 From: rafaelha <34611791+rafaelha@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:17:43 +0000 Subject: [PATCH 1/2] Fix bug in to_clifford_normal_form_graph where Hadamards where not correctly extracted --- pyzx/simplify.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pyzx/simplify.py b/pyzx/simplify.py index a0cda7f8..e25b45aa 100644 --- a/pyzx/simplify.py +++ b/pyzx/simplify.py @@ -546,9 +546,10 @@ def to_clifford_normal_form_graph(g: BaseGraph[VT,ET]) -> None: v = v_inputs[q] i = inputs[q] e = g.edge(v,i) - if g.edge_type(e) == EdgeType.HADAMARD or g.phase(v) != 0: + e_type = g.edge_type(e) + if e_type == EdgeType.HADAMARD or g.phase(v) != 0: h = g.add_vertex(VertexType.Z, q, row=1, phase=g.phase(v)) - g.add_edge((i,h),EdgeType.HADAMARD) + g.add_edge((i,h),e_type) g.add_edge((h,v),EdgeType.SIMPLE) g.remove_edge(e) g.set_phase(v,0) @@ -558,9 +559,10 @@ def to_clifford_normal_form_graph(g: BaseGraph[VT,ET]) -> None: v = v_outputs[q] o = outputs[q] e = g.edge(v,o) - if g.edge_type(e) == EdgeType.HADAMARD or g.phase(v) != 0: + e_type = g.edge_type(e) + if e_type == EdgeType.HADAMARD or g.phase(v) != 0: h = g.add_vertex(VertexType.Z, q, row=7, phase=g.phase(v)) - g.add_edge((h,o),EdgeType.HADAMARD) + g.add_edge((h,o),e_type) g.add_edge((v,h),EdgeType.SIMPLE) g.remove_edge(e) g.set_phase(v,0) From 8cf74db19f381fc5de2e82401d0e5c7335758698 Mon Sep 17 00:00:00 2001 From: rafaelha <34611791+rafaelha@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:18:01 +0000 Subject: [PATCH 2/2] Add unit test --- tests/test_simplify.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_simplify.py b/tests/test_simplify.py index ab8edff2..3223a31e 100644 --- a/tests/test_simplify.py +++ b/tests/test_simplify.py @@ -32,7 +32,9 @@ from fractions import Fraction from pyzx.generate import cliffordT from pyzx.simplify import * -from pyzx.simplify import supplementarity_simp +from pyzx.simplify import supplementarity_simp, to_clifford_normal_form_graph +from pyzx import compare_tensors +from pyzx.generate import cliffordT np: Optional[ModuleType] try: @@ -130,6 +132,13 @@ def test_full_reduce_with_h_box(self): full_reduce(g) self.assertTrue("Input graph is not a ZX-diagram" in str(context.exception)) + def test_to_clifford_normal_form_graph(self): + for _ in range(10): + g = cliffordT(4, 20, p_t=0) + g0 = g.copy() + to_clifford_normal_form_graph(g) + self.assertTrue(compare_tensors(g0, g, preserve_scalar=True)) + qasm_1 = """OPENQASM 2.0; include "qelib1.inc";