diff --git a/mypy.ini b/mypy.ini index 7e3f5c07..5dc68393 100644 --- a/mypy.ini +++ b/mypy.ini @@ -32,3 +32,9 @@ ignore_missing_imports = True [mypy-IPython.*] ignore_missing_imports = True + +[mypy-quimb.*] +ignore_missing_imports = True + +[mypy-qiskit.*] +ignore_missing_imports = True diff --git a/runtests.bat b/runtests.bat index 0a44a822..76d3711f 100644 --- a/runtests.bat +++ b/runtests.bat @@ -1,3 +1,3 @@ python -m unittest discover -s "tests" -t "." -mypy pyzx -pause \ No newline at end of file +mypy pyzx tests +pause diff --git a/runtests.sh b/runtests.sh index 72f2e502..64402060 100755 --- a/runtests.sh +++ b/runtests.sh @@ -1,3 +1,3 @@ #!/bin/bash python3 -m unittest discover -s "tests" -t "." -mypy pyzx +mypy pyzx tests diff --git a/tests/test_circuit.py b/tests/test_circuit.py index 114f24f9..3e600314 100644 --- a/tests/test_circuit.py +++ b/tests/test_circuit.py @@ -19,12 +19,19 @@ import random import sys import os +from types import ModuleType +from typing import Optional + if __name__ == '__main__': sys.path.append('..') sys.path.append('.') - mydir = os.path.dirname(__file__) +from pyzx.generate import cliffordT, cliffords +from pyzx.simplify import clifford_simp +from pyzx.extract import extract_circuit +from pyzx.circuit import Circuit +np: Optional[ModuleType] try: import numpy as np from pyzx.tensor import tensorfy, compare_tensors @@ -32,13 +39,9 @@ except ImportError: np = None -from pyzx.generate import cliffordT, cliffords -from pyzx.simplify import clifford_simp -from pyzx.extract import extract_circuit -from pyzx.circuit import Circuit - SEED = 1337 + @unittest.skipUnless(np, "numpy needs to be installed for this to run") class TestCircuit(unittest.TestCase): diff --git a/tests/test_extract.py b/tests/test_extract.py index a3c267a3..c1e70221 100644 --- a/tests/test_extract.py +++ b/tests/test_extract.py @@ -18,21 +18,25 @@ import unittest import random import sys +from types import ModuleType +from typing import Optional + if __name__ == '__main__': sys.path.append('..') sys.path.append('.') +from pyzx.circuit import Circuit +from pyzx.circuit.gates import CNOT +from pyzx.generate import cliffordT +from pyzx.simplify import clifford_simp +from pyzx.extract import extract_circuit +np: Optional[ModuleType] try: import numpy as np from pyzx.tensor import tensorfy, compare_tensors except ImportError: np = None -from pyzx.circuit import Circuit -from pyzx.circuit.gates import CNOT -from pyzx.generate import cliffordT, cliffords -from pyzx.simplify import clifford_simp -from pyzx.extract import extract_circuit SEED = 1337 diff --git a/tests/test_qasm.py b/tests/test_qasm.py index 0f33ef92..59fafc1b 100644 --- a/tests/test_qasm.py +++ b/tests/test_qasm.py @@ -16,7 +16,20 @@ import unittest +import os +import sys +from types import ModuleType +from typing import Optional + +if __name__ == '__main__': + sys.path.append('..') + sys.path.append('.') +from pyzx.simplify import full_reduce +from pyzx.extract import extract_circuit +from pyzx.circuit import Circuit +from fractions import Fraction +np: Optional[ModuleType] try: import numpy as np from pyzx.tensor import compare_tensors @@ -31,12 +44,6 @@ except ImportError: QuantumCircuit = None -from pyzx.simplify import full_reduce -from pyzx.extract import extract_circuit -from pyzx.circuit import Circuit -from fractions import Fraction -import os - @unittest.skipUnless(np, "numpy needs to be installed for this to run") class TestQASM(unittest.TestCase): diff --git a/tests/test_quimb.py b/tests/test_quimb.py index d52c5f13..d7caebb7 100644 --- a/tests/test_quimb.py +++ b/tests/test_quimb.py @@ -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 @@ -16,12 +16,19 @@ import unittest -import random import sys +from types import ModuleType +from typing import Optional + if __name__ == '__main__': sys.path.append('..') sys.path.append('.') +from pyzx.graph import Graph +from pyzx.utils import EdgeType, VertexType +from pyzx.quimb import to_quimb_tensor +from pyzx.simplify import full_reduce +np: Optional[ModuleType] try: import numpy as np from pyzx.tensor import tensorfy, compare_tensors @@ -34,10 +41,6 @@ except ImportError: qu = None -from pyzx.graph import Graph -from pyzx.utils import EdgeType, VertexType -from pyzx.quimb import to_quimb_tensor -from pyzx.simplify import full_reduce @unittest.skipUnless(np, "numpy needs to be installed for this to run") @unittest.skipUnless(qu, "quimb needs to be installed for this to run") @@ -47,29 +50,29 @@ def test_id_tensor(self): x = g.add_vertex(VertexType.BOUNDARY) y = g.add_vertex(VertexType.BOUNDARY) g.add_edge(g.edge(x, y), edgetype = EdgeType.SIMPLE) - + tn = to_quimb_tensor(g) - self.assertTrue((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) + self.assertTrue((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) & qtn.Tensor(data = [0, 1], inds = ("1",))) .contract(output_inds = ()) == 1) - self.assertTrue((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) + self.assertTrue((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) & qtn.Tensor(data = [1, 0], inds = ("1",))) .contract(output_inds = ()) == 1) - + def test_hadamard_tensor(self): g = Graph() x = g.add_vertex(VertexType.BOUNDARY) y = g.add_vertex(VertexType.BOUNDARY) g.add_edge(g.edge(x, y), edgetype = EdgeType.HADAMARD) - + tn = to_quimb_tensor(g) - self.assertTrue(abs((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) + self.assertTrue(abs((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) & qtn.Tensor(data = [1 / np.sqrt(2), 1 / np.sqrt(2)], inds = ("1",))) .contract(output_inds = ()) - 1) < 1e-9) - self.assertTrue(abs((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) + self.assertTrue(abs((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) & qtn.Tensor(data = [1 / np.sqrt(2), -1 / np.sqrt(2)], inds = ("1",))) .contract(output_inds = ()) - 1) < 1e-9) - + def test_xor_tensor(self): g = Graph() x = g.add_vertex(VertexType.BOUNDARY) @@ -81,16 +84,16 @@ def test_xor_tensor(self): g.add_edge(g.edge(y, v), edgetype = EdgeType.HADAMARD) g.add_edge(g.edge(v, z), edgetype = EdgeType.HADAMARD) tn = to_quimb_tensor(g) - + for x in range(2): for y in range(2): for z in range(2): self.assertTrue(abs((tn & qtn.Tensor(data = [1 - x, x], inds = ("0",)) & qtn.Tensor(data = [1 - y, y], inds = ("1",)) & - qtn.Tensor(data = [1 - z, z], inds = ("3",))).contract(output_inds = ()) - + qtn.Tensor(data = [1 - z, z], inds = ("3",))).contract(output_inds = ()) - ((x ^ y) == z) / np.sqrt(2)) < 1e-9) - + def test_phases_tensor(self): # This diagram represents a 1-input 1-output Z-spider of phase pi/2, # but written using two Z-spiders of phases pi/6 and pi/3 that are @@ -100,31 +103,30 @@ def test_phases_tensor(self): v = g.add_vertex(VertexType.Z, phase = 1. / 6.) w = g.add_vertex(VertexType.Z, phase = 1. / 3.) y = g.add_vertex(VertexType.BOUNDARY) - + g.add_edge(g.edge(x, v), edgetype = EdgeType.SIMPLE) g.add_edge(g.edge(v, w), edgetype = EdgeType.SIMPLE) g.add_edge(g.edge(w, y), edgetype = EdgeType.SIMPLE) tn = to_quimb_tensor(g) - - self.assertTrue(abs((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) + + self.assertTrue(abs((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) & qtn.Tensor(data = [1, 0], inds = ("3",))) .contract(output_inds = ()) - 1) < 1e-9) - self.assertTrue(abs((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) + self.assertTrue(abs((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) & qtn.Tensor(data = [0, 1j], inds = ("3",))) .contract(output_inds = ()) + 1) < 1e-9) - + def test_scalar(self): g = Graph() x = g.add_vertex(VertexType.Z, row = 0, phase = 1 / 2) y = g.add_vertex(VertexType.Z, row = 1, phase = 1 / 4) g.add_edge(g.edge(x, y), edgetype = EdgeType.SIMPLE) - + full_reduce(g) val = to_quimb_tensor(g).contract(output_inds = ()) expected_val = 1 + np.exp(1j * np.pi * 3 / 4) self.assertTrue(abs(val - expected_val) < 1e-9) - + if __name__ == '__main__': unittest.main() - diff --git a/tests/test_simplify.py b/tests/test_simplify.py index e11c8052..394442ca 100644 --- a/tests/test_simplify.py +++ b/tests/test_simplify.py @@ -18,16 +18,12 @@ import unittest import random import sys +from types import ModuleType +from typing import Optional + if __name__ == '__main__': sys.path.append('..') sys.path.append('.') - -try: - import numpy as np - from pyzx.tensor import tensorfy, compare_tensors -except ImportError: - np = None - from pyzx.graph import Graph from pyzx.circuit import Circuit from pyzx.circuit.qasmparser import qasm @@ -36,8 +32,16 @@ from pyzx.simplify import * from pyzx.simplify import supplementarity_simp +np: Optional[ModuleType] +try: + import numpy as np + from pyzx.tensor import tensorfy, compare_tensors +except ImportError: + np = None + SEED = 1337 + @unittest.skipUnless(np, "numpy needs to be installed for this to run") class TestSimplify(unittest.TestCase): @@ -109,6 +113,7 @@ def test_to_graph_like_introduce_boundary_vertices(self): to_graph_like(g) self.assertTrue(compare_tensors(c,g)) + qasm_1 = """OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; diff --git a/tests/test_simulate.py b/tests/test_simulate.py index 9beb09bb..6a29ab81 100644 --- a/tests/test_simulate.py +++ b/tests/test_simulate.py @@ -17,18 +17,21 @@ import unittest import sys +from types import ModuleType +from typing import Optional + if __name__ == '__main__': sys.path.append('..') sys.path.append('.') +from pyzx.circuit import Circuit +from pyzx.simulate import replace_magic_states +np: Optional[ModuleType] try: import numpy as np - from pyzx.tensor import tensorfy, compare_tensors except ImportError: np = None -from pyzx.circuit import Circuit -from pyzx.simulate import replace_magic_states @unittest.skipUnless(np, "numpy needs to be installed for this to run") class TestSimulate(unittest.TestCase): @@ -36,10 +39,11 @@ class TestSimulate(unittest.TestCase): def test_magic_state_decomposition_is_correct(self): c = Circuit(6) for i in range(6): - c.add_gate("T",i) + c.add_gate("T", i) g = c.to_graph() gsum = replace_magic_states(g) - self.assertTrue(np.allclose(g.to_tensor(),gsum.to_tensor())) + self.assertTrue(np.allclose(g.to_tensor(), gsum.to_tensor())) + if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/test_tensor.py b/tests/test_tensor.py index 9a6cb653..d7e1d966 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -17,24 +17,27 @@ import unittest import random -from fractions import Fraction import sys +from types import ModuleType +from typing import Optional + if __name__ == '__main__': sys.path.append('..') sys.path.append('.') +from pyzx.graph import Graph +from pyzx.generate import cliffords +from pyzx.circuit import Circuit +np: Optional[ModuleType] try: import numpy as np from pyzx.tensor import tensorfy, compare_tensors, compose_tensors, adjoint except ImportError: np = None -from pyzx.graph import Graph -from pyzx.generate import cliffords -from pyzx.circuit import Circuit - SEED = 1337 + @unittest.skipUnless(np, "numpy needs to be installed for this to run") class TestTensor(unittest.TestCase):