Skip to content

Commit

Permalink
refactor test.compute_and_compare_results
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 12, 2023
1 parent 9b9531e commit d243568
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 112 deletions.
14 changes: 7 additions & 7 deletions test/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@

from qlasskit import compiler, qlassf

from .utils import compare_circuit_truth_table
from .utils import compute_and_compare_results


class TestCompiler(unittest.TestCase):
def test_not_arg(self):
f = "def test(a: bool) -> bool:\n\treturn not a"
qf = qlassf(f, to_compile=True)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_and(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn a and b"
qf = qlassf(f, to_compile=True)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_and_long(self):
f = "def test(a: bool, b: bool, c: bool, d: bool) -> bool:\n\treturn a and b and c and d"
qf = qlassf(f, to_compile=True)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_and_long_with_not(self):
f = "def test(a: bool, b: bool, c: bool, d: bool) -> bool:\n\treturn a and b and not c and d"
qf = qlassf(f, to_compile=True)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_or(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn a or b"
qf = qlassf(f, to_compile=True)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_or_not(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn not a or b"
qf = qlassf(f, to_compile=True)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)
28 changes: 14 additions & 14 deletions test/test_qlassf_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from qlasskit import QlassF, exceptions, qlassf

from .utils import COMPILATION_ENABLED, compare_circuit_truth_table
from .utils import COMPILATION_ENABLED, compute_and_compare_results

a, b, c, d, e, g, h = symbols("a,b,c,d,e,g,h")
_ret = Symbol("_ret")
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_arg_identity(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_not_arg(self):
ex = Not(a)
Expand All @@ -60,7 +60,7 @@ def test_not_arg(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_and(self):
ex = And(Not(a), b)
Expand All @@ -69,21 +69,21 @@ def test_and(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_bool_eq(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn a == b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_bool_neq(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn a != b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_or_not(self):
ex = Or(Not(a), b)
Expand All @@ -92,7 +92,7 @@ def test_or_not(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_multiple_arg(self):
ex = And(a, And(Not(b), c))
Expand All @@ -101,7 +101,7 @@ def test_multiple_arg(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_multiple_arg2(self):
ex = And(a, And(Not(b), Or(a, c)))
Expand All @@ -110,7 +110,7 @@ def test_multiple_arg2(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_ifexp(self):
ex = ITE(a, true, false)
Expand All @@ -119,7 +119,7 @@ def test_ifexp(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_ifexp2(self):
ex = ITE(And(a, And(Not(b), c)), true, false)
Expand All @@ -128,7 +128,7 @@ def test_ifexp2(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_ifexp3(self):
exp = ITE(
Expand All @@ -144,7 +144,7 @@ def test_ifexp3(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], exp)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_assign(self):
f = "def test(a: bool, b: bool) -> bool:\n\tc = a and b\n\treturn c"
Expand All @@ -154,7 +154,7 @@ def test_assign(self):
self.assertEqual(qf.expressions[0][1], And(a, b))
self.assertEqual(qf.expressions[1][0], _ret)
self.assertEqual(qf.expressions[1][1], c)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_assign2(self):
f = (
Expand All @@ -168,7 +168,7 @@ def test_assign2(self):
self.assertEqual(qf.expressions[0][1], And(a, And(Not(b), c)))
self.assertEqual(qf.expressions[1][0], _ret)
self.assertEqual(qf.expressions[1][1], d)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_assign3(self):
f = (
Expand Down
34 changes: 17 additions & 17 deletions test/test_qlassf_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from qlasskit import QlassF, exceptions, qlassf # Qint2

from .utils import COMPILATION_ENABLED, compare_circuit_truth_table
from .utils import COMPILATION_ENABLED, compute_and_compare_results

a, b, c, d = symbols("a,b,c,d")
_ret = Symbol("_ret")
Expand All @@ -43,7 +43,7 @@ def test_int_arg2(self):
qf.expressions[0][1],
ITE(And(Symbol("a.0"), b), Symbol("a.1"), Symbol("a.0")),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_const_return(self):
f = "def test(a: Qint2) -> Qint2:\n\treturn 1"
Expand All @@ -61,15 +61,15 @@ def test_int_return_tuple(self):
f = "def test(a: Qint2) -> Tuple[Qint2, bool]:\n\tb = a[0] and a[1]\n\treturn (a, b)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 4)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_tuple(self):
f = "def test(a: Tuple[Qint2, Qint2]) -> bool:\n\treturn a[0][0] and a[1][1]"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], And(Symbol("a.0.0"), Symbol("a.1.1")))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_identity(self):
f = "def test(a: Qint2) -> Qint2:\n\treturn a"
Expand All @@ -79,7 +79,7 @@ def test_int_identity(self):
self.assertEqual(qf.expressions[0][1], Symbol("a.0"))
self.assertEqual(qf.expressions[1][0], Symbol("_ret.1"))
self.assertEqual(qf.expressions[1][1], Symbol("a.1"))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

# TODO: need consts
# def test_int_const(self):
Expand All @@ -92,7 +92,7 @@ def test_int_const_compare_eq(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], And(Symbol("a.1"), Not(Symbol("a.0"))))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_const_compare_eq_different_type(self):
f = "def test(a: Qint4) -> bool:\n\treturn a == 2"
Expand All @@ -108,7 +108,7 @@ def test_int_const_compare_eq_different_type(self):
Not(Symbol("a.3")),
),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_const_int_compare_eq_different_type(self):
f = "def test(a: Qint4) -> bool:\n\treturn 2 == a"
Expand All @@ -124,7 +124,7 @@ def test_const_int_compare_eq_different_type(self):
Not(Symbol("a.3")),
),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_const_int_compare_neq_different_type(self):
f = "def test(a: Qint4) -> bool:\n\treturn 2 != a"
Expand All @@ -140,7 +140,7 @@ def test_const_int_compare_neq_different_type(self):
Symbol("a.3"),
),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_int_compare_eq(self):
f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a == b"
Expand All @@ -154,7 +154,7 @@ def test_int_int_compare_eq(self):
Not(Xor(Symbol("a.1"), Symbol("b.1"))),
),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_int_compare_neq(self):
f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a != b"
Expand All @@ -168,21 +168,21 @@ def test_int_int_compare_neq(self):
Xor(Symbol("a.1"), Symbol("b.1")),
),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_const_int_compare_gt(self):
f = "def test(a: Qint2) -> bool:\n\treturn a > 1"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_const_int4_compare_gt(self):
f = "def test(a: Qint4) -> bool:\n\treturn a > 6"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

# def test_int4_int4_compare_gt(self):
# f = "def test(a: Qint4, b: Qint4) -> bool:\n\treturn a > b"
Expand All @@ -196,28 +196,28 @@ def test_const_int_compare_lt(self):
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_const_int4_compare_lt(self):
f = "def test(a: Qint4) -> bool:\n\treturn a < 6"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_int_compare_gt(self):
f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a > b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_int_int_compare_lt(self):
f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a < b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

# def test_const_int_compare_gte(self):
# f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a >= b"
Expand Down
16 changes: 8 additions & 8 deletions test/test_qlassf_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from qlasskit import QlassF, exceptions, qlassf

from .utils import COMPILATION_ENABLED, compare_circuit_truth_table
from .utils import COMPILATION_ENABLED, compute_and_compare_results

a, b, c, d = symbols("a,b,c,d")
_ret = Symbol("_ret")
Expand All @@ -37,7 +37,7 @@ def test_tuple_arg(self):
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], And(a_0, a_1))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_ite(self):
f = "def test(b: bool, a: Tuple[bool, bool]) -> Tuple[bool,bool]:\n\treturn (a[1],a[0]) if b else a"
Expand All @@ -58,7 +58,7 @@ def test_tuple_arg_assign(self):
self.assertEqual(len(qf.expressions), 3)
self.assertEqual(qf.expressions[-1][0], _ret)
self.assertEqual(qf.expressions[-1][1], And(b, c))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_of_tuple_arg(self):
f = "def test(a: Tuple[Tuple[bool, bool], bool]) -> bool:\n\treturn a[0][0] and a[0][1] and a[1]"
Expand All @@ -68,7 +68,7 @@ def test_tuple_of_tuple_arg(self):
self.assertEqual(
qf.expressions[0][1], And(Symbol("a.0.0"), And(Symbol("a.0.1"), a_1))
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_of_tuple_of_tuple_arg(self):
f = (
Expand All @@ -82,15 +82,15 @@ def test_tuple_of_tuple_of_tuple_arg(self):
qf.expressions[0][1],
And(Symbol("a.0.0.0"), And(Symbol("a.0.0.1"), And(Symbol("a.0.1"), a_1))),
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_assign(self):
f = "def test(a: Tuple[bool, bool]) -> bool:\n\tb = (a[1],a[0])\n\treturn b[0] and b[1]"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 3)
self.assertEqual(qf.expressions[-1][0], _ret)
self.assertEqual(qf.expressions[-1][1], And(b_0, b_1))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_assign2(self):
f = (
Expand All @@ -102,7 +102,7 @@ def test_tuple_assign2(self):
self.assertEqual(len(qf.expressions), 4)
self.assertEqual(qf.expressions[-1][0], _ret)
self.assertEqual(qf.expressions[-1][1], And(b_0, And(b_1, Symbol("b.2"))))
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_assign3(self):
f = (
Expand All @@ -116,7 +116,7 @@ def test_tuple_assign3(self):
self.assertEqual(
qf.expressions[-1][1], And(b_0, And(Symbol("b.1.0"), Symbol("b.1.1")))
)
compare_circuit_truth_table(self, qf)
compute_and_compare_results(self, qf)

def test_tuple_result(self):
f = "def test(a: bool, b: bool) -> Tuple[bool,bool]:\n\treturn a,b"
Expand Down
Loading

0 comments on commit d243568

Please sign in to comment.