Skip to content

Commit

Permalink
Merge pull request zxcalc#201 from dlyongemallo/fix_tests_mypy
Browse files Browse the repository at this point in the history
Add tests subdirectory to mypy coverage and fix mypy errors in test files.
  • Loading branch information
jvdwetering authored Mar 29, 2024
2 parents da80447 + d97f2e0 commit 8b5d0cc
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 63 deletions.
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions runtests.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python -m unittest discover -s "tests" -t "."
mypy pyzx
pause
mypy pyzx tests
pause
2 changes: 1 addition & 1 deletion runtests.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
python3 -m unittest discover -s "tests" -t "."
mypy pyzx
mypy pyzx tests
15 changes: 9 additions & 6 deletions tests/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@
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
import math
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):

Expand Down
14 changes: 9 additions & 5 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 13 additions & 6 deletions tests/test_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
52 changes: 27 additions & 25 deletions tests/test_quimb.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 @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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()

19 changes: 12 additions & 7 deletions tests/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):

Expand Down Expand Up @@ -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];
Expand Down
16 changes: 10 additions & 6 deletions tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,33 @@

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):

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()
unittest.main()
13 changes: 8 additions & 5 deletions tests/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit 8b5d0cc

Please sign in to comment.