Skip to content

Commit

Permalink
In full_reduce, raise error if input graph contains H_BOX.
Browse files Browse the repository at this point in the history
The function requires that the input is a ZX-diagram (i.e., no `H_BOX`es). See zxcalc#161 and zxcalc#200 for context.

Note: This does not guard `clifford_simp` or other simplification subroutines, which will still silently fail if there are `H_BOX`es. Putting a check into each subroutine would be redundant and expensive.
  • Loading branch information
dlyongemallo committed Apr 17, 2024
1 parent 8c09da7 commit 1acb199
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pyzx/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ def reduce_scalar(g: BaseGraph[VT,ET], quiet:bool=True, stats:Optional[Stats]=No
def full_reduce(g: BaseGraph[VT,ET], 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)
while True:
Expand Down
19 changes: 18 additions & 1 deletion tests/test_simplify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PyZX - Python library for quantum circuit rewriting
# PyZX - Python library for quantum circuit rewriting
# and optimization using the ZX-calculus
# Copyright (C) 2018 - Aleks Kissinger and John van de Wetering

Expand All @@ -21,6 +21,8 @@
from types import ModuleType
from typing import Optional

from pyzx import VertexType

if __name__ == '__main__':
sys.path.append('..')
sys.path.append('.')
Expand Down Expand Up @@ -113,6 +115,21 @@ def test_to_graph_like_introduce_boundary_vertices(self):
to_graph_like(g)
self.assertTrue(compare_tensors(c,g))

def test_full_reduce_with_h_box(self):
"""Test that calls to :func:`full_reduce` with a graph containing H-boxes raises an error.
This is a common mistake made by users (e.g., see issues #161 and #200).
"""
g = Graph()
v0 = g.add_vertex(VertexType.BOUNDARY, 0, 0)
v1 = g.add_vertex(VertexType.H_BOX, 0, 1)
v2 = g.add_vertex(VertexType.BOUNDARY, 0, 2)
g.add_edge((v0, v1))
g.add_edge((v1, v2))

with self.assertRaises(ValueError) as context:
full_reduce(g)
self.assertTrue("Input graph is not a ZX-diagram" in str(context.exception))


qasm_1 = """OPENQASM 2.0;
include "qelib1.inc";
Expand Down

0 comments on commit 1acb199

Please sign in to comment.