Skip to content

Commit

Permalink
test on both supported compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 29, 2023
1 parent 661ee94 commit 04d2cfc
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 115 deletions.
6 changes: 4 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@

### Week 2: (30 Oct 23)

- [ ] Test on multiple py version
- [ ] Fixed size list support
- [ ] Groover algorithm tests
- [ ] Slideshow for UF midterm

Expand Down Expand Up @@ -111,7 +113,7 @@
- [ ] Lambda
- [ ] FixedList type
- [ ] Builtin functions: map, count
- [ ] Ast2logic: fixed size loops unrolling
- [x] Ast2logic: fixed size loops unrolling
- [ ] Builtin functions: sum(), all(), any()
- [ ] First beta release

Expand All @@ -127,7 +129,7 @@
- [ ] Datatype: Fixed
- [ ] Datatype: Enum
- [ ] While loop
- [ ] For loop
- [x] For loop
- [ ] Recursion
- [ ] Parameter bind

Expand Down
18 changes: 16 additions & 2 deletions test/test_qlassf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

from parameterized import parameterized_class

from qlasskit import Qint, Qint4, Qint12, QlassF, exceptions, qlassf

from . import utils
Expand All @@ -26,17 +28,29 @@ def test_decorator(self):
self.assertTrue(isinstance(c, QlassF))


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
class TestQlassfCustomTypes(unittest.TestCase):
def test_custom_qint3(self):
qf = qlassf(
utils.test_qint3, types=[utils.Qint3], to_compile=COMPILATION_ENABLED
utils.test_qint3,
types=[utils.Qint3],
to_compile=COMPILATION_ENABLED,
compiler=self.compiler,
)
compute_and_compare_results(self, qf)

def test_custom_qint3_notfound(self):
self.assertRaises(
exceptions.UnknownTypeException,
lambda f: qlassf(f, types=[], to_compile=COMPILATION_ENABLED),
lambda f: qlassf(
f, types=[], to_compile=COMPILATION_ENABLED, compiler=self.compiler
),
utils.test_qint3,
)

Expand Down
51 changes: 33 additions & 18 deletions test/test_qlassf_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import unittest

from parameterized import parameterized_class
from sympy import Symbol, symbols
from sympy.logic import ITE, And, Not, Or, false, simplify_logic, true

Expand All @@ -25,6 +26,13 @@
_ret = Symbol("_ret")


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
class TestQlassfBoolean(unittest.TestCase):
def test_unbound(self):
f = "def test() -> bool:\n\treturn a"
Expand All @@ -40,7 +48,7 @@ def test_no_return_type(self):

def test_bool_const(self):
f = "def test(a: bool) -> bool:\n\tc=True\n\treturn c"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], True)
Expand All @@ -49,7 +57,7 @@ def test_bool_const(self):
def test_arg_identity(self):
ex = a
f = "def test(a: bool) -> bool:\n\treturn a"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -58,7 +66,7 @@ def test_arg_identity(self):
def test_not_arg(self):
ex = Not(a)
f = "def test(a: bool) -> bool:\n\treturn not a"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -67,30 +75,30 @@ def test_not_arg(self):
def test_and(self):
ex = And(Not(a), b)
f = "def test(a: bool, b: bool) -> bool:\n\treturn not a and b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
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)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
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)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
compute_and_compare_results(self, qf)

def test_or_not(self):
ex = Or(Not(a), b)
f = "def test(a: bool, b: bool) -> bool:\n\treturn not a or b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -99,7 +107,7 @@ def test_or_not(self):
def test_multiple_arg(self):
ex = And(a, And(Not(b), c))
f = "def test(a: bool, b: bool, c: bool) -> bool:\n\treturn a and (not b) and c"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -108,7 +116,7 @@ def test_multiple_arg(self):
def test_multiple_arg2(self):
ex = And(a, And(Not(b), Or(a, c)))
f = "def test(a: bool, b: bool, c: bool) -> bool:\n\treturn a and (not b) and (a or c)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -117,7 +125,7 @@ def test_multiple_arg2(self):
def test_ifexp(self):
ex = ITE(a, true, false)
f = "def test(a: bool) -> bool:\n\treturn True if a else False"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -126,7 +134,7 @@ def test_ifexp(self):
def test_ifexp2(self):
ex = ITE(And(a, And(Not(b), c)), true, false)
f = "def test(a: bool, b: bool, c: bool) -> bool:\n\treturn True if a and (not b) and c else False"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
Expand All @@ -142,15 +150,15 @@ def test_ifexp3(self):
"def test(a: bool, b: bool, c: bool) -> bool:\n"
+ "\treturn (c and not b) if a and ((not b) and c) else (a and not c)"
)
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], exp)
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"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], And(a, b))
Expand All @@ -162,7 +170,7 @@ def test_assign2(self):
+ "\td = a and (not b) and c\n"
+ "\treturn True if d else False"
)
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][1], And(a, And(Not(b), c)))
self.assertEqual(qf.expressions[0][0], _ret)
Expand All @@ -177,7 +185,7 @@ def test_assign3(self):
+ "\th = (not a) and b and (not c)\n"
+ "\treturn g if d and e else h"
)
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 5)
self.assertEqual(qf.expressions[-1][1], ITE(d & e, g, h))
compute_and_compare_results(self, qf)
Expand All @@ -197,18 +205,25 @@ def test_assign3(self):
# self.assertEqual(qf.expressions[-1][1], ITE(d & e, g, h))


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
class TestQlassfBoolBitwise(unittest.TestCase):
def test_bitwise_and(self):
f = f"def test(a: bool, b: bool) -> bool:\n\treturn a & b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_bitwise_or(self):
f = f"def test(a: bool, b: bool) -> bool:\n\treturn a | b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_bitwise_xor(self):
f = f"def test(a: bool, b: bool) -> bool:\n\treturn a ^ b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)
32 changes: 20 additions & 12 deletions test/test_qlassf_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest
from typing import Tuple

from parameterized import parameterized_class
from sympy import Symbol, symbols
from sympy.logic import ITE, And, Not, Or, false, simplify_logic, true

Expand All @@ -23,30 +24,37 @@
from .utils import COMPILATION_ENABLED, compute_and_compare_results


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
class TestQlassfBuiltinFunctions(unittest.TestCase):
def test_print_call(self):
f = "def test(a: bool) -> bool:\n\tprint(a)\n\treturn a"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
compute_and_compare_results(self, qf)

def test_len(self):
f = "def test(a: Tuple[bool, bool]) -> Qint2:\n\treturn len(a)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(qf.expressions[0][1], False)
self.assertEqual(qf.expressions[1][1], True)
compute_and_compare_results(self, qf)

def test_len2(self):
f = "def test(a: Tuple[bool, bool]) -> Qint2:\n\tc=a\n\treturn len(c)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(qf.expressions[-2][1], False)
self.assertEqual(qf.expressions[-1][1], True)
compute_and_compare_results(self, qf)

def test_len4(self):
f = "def test(a: Tuple[bool, bool, bool, bool]) -> Qint4:\n\treturn len(a)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(qf.expressions[0][1], False)
self.assertEqual(qf.expressions[1][1], False)
self.assertEqual(qf.expressions[2][1], True)
Expand All @@ -55,41 +63,41 @@ def test_len4(self):

def test_min(self):
f = "def test(a: Qint2, b: Qint2) -> Qint2:\n\treturn min(a,b)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_min_const(self):
f = "def test(a: Qint2) -> Qint2:\n\treturn min(a,3)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_max(self):
f = "def test(a: Qint2, b: Qint2) -> Qint2:\n\treturn max(a,b)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_max_of3(self):
f = "def test(a: Qint2, b: Qint2) -> Qint2:\n\treturn max(a,b,3)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_max_const(self):
f = "def test(a: Qint2) -> Qint2:\n\treturn max(a,3)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

# TODO: fixed by cast
# def test_max_const2(self):
# f = "def test(a: Qint4) -> Qint2:\n\treturn max(a,3)"
# qf = qlassf(f, to_compile=COMPILATION_ENABLED)
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
# compute_and_compare_results(self, qf)

def test_max_tuple(self):
f = "def test(a: Tuple[Qint2, Qint2]) -> Qint2:\n\treturn max(a)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_max_tuple_const(self):
f = "def test(a: Qint2, b: Qint2) -> Qint2:\n\treturn max((a, b))"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)
Loading

0 comments on commit 04d2cfc

Please sign in to comment.