From a45a2c0ceb7dfbce5f183a707189b90781e65ee3 Mon Sep 17 00:00:00 2001 From: "Davide Gessa (dakk)" Date: Wed, 8 Nov 2023 17:57:03 +0100 Subject: [PATCH] fix tests for new boolean optimizer --- qlasskit/boolopt/deprecated.py | 54 +---------- test/test_qlassf_bool.py | 95 ++++++++++---------- test/test_qlassf_builtin.py | 18 ++-- test/test_qlassf_int.py | 158 ++++++++++++++------------------- test/test_qlassf_list.py | 6 +- test/test_qlassf_tuple.py | 75 ++++++++-------- 6 files changed, 163 insertions(+), 243 deletions(-) diff --git a/qlasskit/boolopt/deprecated.py b/qlasskit/boolopt/deprecated.py index 4b54a982..7983e409 100644 --- a/qlasskit/boolopt/deprecated.py +++ b/qlasskit/boolopt/deprecated.py @@ -15,7 +15,7 @@ from typing import Dict from sympy import Symbol -from sympy.logic.boolalg import Boolean, simplify_logic +from sympy.logic.boolalg import Boolean, simplify_logic, to_anf from ..ast2logic import BoolExpList @@ -37,34 +37,6 @@ def remove_const_exps(exps: BoolExpList) -> BoolExpList: return n_exps -# def subsitute_exps(exps: BoolExpList) -> BoolExpList: -# """Subsitute exps (replace a = ~a, a = ~a, a = ~a => a = ~a)""" -# const: Dict[Symbol, Boolean] = {} -# n_exps: BoolExpList = [] -# print(exps) - -# for i in range(len(exps)): -# (s, e) = exps[i] -# e = e.subs(const) -# const[s] = e - -# for x in e.free_symbols: -# if x in const: -# n_exps.append((x, const[x])) -# del const[x] - -# for (s,e) in const.items(): -# if s == e: -# continue - -# n_exps.append((s,e)) - -# print(n_exps) -# print() -# print() -# return n_exps - - def remove_unnecessary_assigns(exps: BoolExpList) -> BoolExpList: """Remove exp like: __a.0 = a.0, ..., a.0 = __a.0""" n_exps: BoolExpList = [] @@ -89,23 +61,6 @@ def should_add(s, e, n_exps2): return n_exps - # for s, e in exps: - # n_exps2 = [] - # ename = f"__{s.name}" - # n_exps.append((s, e)) - - # for s_, e_ in reversed(n_exps): - # if s_.name == ename: - # continue - # else: - # _replaced = e_.subs(Symbol(ename), Symbol(s.name)) - # if s_ != _replaced: - # n_exps2.append((s_, _replaced)) - - # n_exps = n_exps2[::-1] - - # return n_exps - def merge_unnecessary_assigns(exps: BoolExpList) -> BoolExpList: """Translate exp like: __a.0 = !a, a = __a.0 ===> a = !a""" @@ -185,8 +140,5 @@ def exps_simplify(exps: BoolExpList) -> BoolExpList: return list(map(lambda e: (e[0], simplify_logic(e[1])), exps)) -# [(h, a_list.0.0 & a_list.0.1), (h, a_list.1.0 & a_list.1.1 & h), -# (h, a_list.2.0 & a_list.2.1 & h), (_ret, a_list.3.0 & a_list.3.1 & h)] -# TO -# (_ret, a_list_3_0 & a_list_3_1 & a_list_2_0 & a_list_2_1 & a_list_1_0 & a_list_1_1 & -# a_list_0_0 & a_list_0_1) +def exps_to_anf(exps: BoolExpList) -> BoolExpList: + return list(map(lambda e: (e[0], to_anf(e[1])), exps)) diff --git a/test/test_qlassf_bool.py b/test/test_qlassf_bool.py index c253851f..d9b4b67a 100644 --- a/test/test_qlassf_bool.py +++ b/test/test_qlassf_bool.py @@ -43,120 +43,115 @@ 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual(len(qf.expressions[0][1], True) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[0][1], True) compute_and_compare_results(self, qf) def test_arg_identity(self): ex = a f = "def test(a: bool) -> bool:\n\treturn a" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_not_arg(self): ex = Not(a) f = "def test(a: bool) -> bool:\n\treturn not a" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + 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) + ex = Not(And(a, Not(b))) f = "def test(a: bool, b: bool) -> bool:\n\treturn not a or b" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_multiple_arg2(self): - ex = And(a, And(Not(b), Or(a, c))) + ex = And(a, Not(b)) 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], ex) + 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_ifexp3(self): - exp = ITE( - And(a, And(Not(b), c)), - And(c, Not(b)), - And(a, Not(c)), - ) + exp = And(a, Not(And(b, c))) f = ( "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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], exp) + 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, compiler=self.compiler) - print(qf.expressions) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], And(a, b)) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[0][1], And(a, b)) compute_and_compare_results(self, qf) def test_assign2(self): @@ -166,9 +161,9 @@ def test_assign2(self): + "\treturn True if d else False" ) qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # # self.assertEqual(len(qf.expressions[0][1], And(a, And(Not(b), c))) - # self.assertEqual(len(qf.expressions[0][0], _ret) + 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) compute_and_compare_results(self, qf) def test_assign3(self): @@ -181,8 +176,8 @@ def test_assign3(self): + "\treturn g if d and e else h" ) qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 5) - # # self.assertEqual(len(qf.expressions[-1][1], ITE(d & e, g, h)) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][1], And(b, Not(a), Not(c))) compute_and_compare_results(self, qf) diff --git a/test/test_qlassf_builtin.py b/test/test_qlassf_builtin.py index 5e34032d..670c2eaa 100644 --- a/test/test_qlassf_builtin.py +++ b/test/test_qlassf_builtin.py @@ -29,30 +29,30 @@ 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) + 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions[0][1], False) - # self.assertEqual(len(qf.expressions[1][1], True) + 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions[-2][1], False) - # self.assertEqual(len(qf.expressions[-1][1], True) + 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions[0][1], False) - # self.assertEqual(len(qf.expressions[1][1], False) - # self.assertEqual(len(qf.expressions[2][1], True) - # self.assertEqual(len(qf.expressions[3][1], False) + self.assertEqual(qf.expressions[0][1], False) + self.assertEqual(qf.expressions[1][1], False) + self.assertEqual(qf.expressions[2][1], True) + self.assertEqual(qf.expressions[3][1], False) compute_and_compare_results(self, qf) def test_min(self): diff --git a/test/test_qlassf_int.py b/test/test_qlassf_int.py index 596a2dd7..18d0a7f2 100644 --- a/test/test_qlassf_int.py +++ b/test/test_qlassf_int.py @@ -57,20 +57,20 @@ class TestQlassfIntParametrized_2_4_8(unittest.TestCase): def test_int_arg(self): f = f"def test(a: {self.ttype_str}) -> bool:\n\treturn a[0]" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual(len(qf.expressions[0][1], Symbol("a.0")) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[0][1], Symbol("a.0")) compute_and_compare_results(self, qf) def test_int_arg2(self): f = f"def test(a: {self.ttype_str}, b: bool) -> bool:\n\treturn a[1] if (a[0] and b) else a[0]" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], - # ITE(And(Symbol("a.0"), b), Symbol("a.1"), Symbol("a.0")), - # ) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual( + qf.expressions[0][1], + And(Symbol("a.0"), Not(And(Symbol("b"), Not(Symbol("a.1"))))), + ) compute_and_compare_results(self, qf) def test_int_arg_unbound_index(self): @@ -82,31 +82,31 @@ def test_int_arg_unbound_index(self): def test_int_return_tuple(self): f = f"def test(a: {self.ttype_str}) -> Tuple[{self.ttype_str}, bool]:\n\tb = a[0] and a[1]\n\treturn (a, b)" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), self.ttype_size + 2) + self.assertEqual(len(qf.expressions), self.ttype_size + 1) compute_and_compare_results(self, qf) def test_int_tuple(self): f = f"def test(a: Tuple[{self.ttype_str}, {self.ttype_str}]) -> bool:\n\treturn a[0][0] and a[1][1]" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], And(Symbol("a.0.0"), Symbol("a.1.1"))) + 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"))) compute_and_compare_results(self, qf) def test_int_identity(self): f = f"def test(a: {self.ttype_str}) -> {self.ttype_str}:\n\treturn a" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), self.ttype_size) - # for i in range(self.ttype_size): - # # self.assertEqual(len(qf.expressions[i][0], Symbol(f"_ret.{i}")) - # # self.assertEqual(len(qf.expressions[i][1], Symbol(f"a.{i}")) + self.assertEqual(len(qf.expressions), self.ttype_size) + for i in range(self.ttype_size): + self.assertEqual(qf.expressions[i][0], Symbol(f"_ret.{i}")) + self.assertEqual(qf.expressions[i][1], Symbol(f"a.{i}")) compute_and_compare_results(self, qf) def test_int_const_compare_eq(self): f = f"def test(a: {self.ttype_str}) -> bool:\n\treturn a == {int(self.ttype_size/2-1)}" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) compute_and_compare_results(self, qf) @@ -123,29 +123,28 @@ class TestQlassfIntParametrized_2_4(unittest.TestCase): def test_int_int_compare_eq(self): f = f"def test(a: {self.ttype_str}, b: {self.ttype_str}) -> bool:\n\treturn a == b" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) compute_and_compare_results(self, qf) def test_int_int_compare_neq(self): f = f"def test(a: {self.ttype_str}, b: {self.ttype_str}) -> bool:\n\treturn a != b" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) compute_and_compare_results(self, qf) def test_const_int_compare_gt(self): f = f"def test(a: {self.ttype_str}) -> bool:\n\treturn a > 1" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[-1][0], _ret) compute_and_compare_results(self, qf) def test_const_int_compare_lt(self): f = f"def test(a: {self.ttype_str}) -> bool:\n\treturn a < 2" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) compute_and_compare_results(self, qf) @@ -154,86 +153,68 @@ class TestQlassfInt(unittest.TestCase): def test_int_const(self): f = "def test(a: Qint2) -> Qint2:\n\tc=2\n\treturn c" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 2) - # self.assertEqual(len(qf.expressions[-2][1], False) - # self.assertEqual(len(qf.expressions[-1][1], True) + self.assertEqual(len(qf.expressions), 2) + self.assertEqual(qf.expressions[-2][1], False) + self.assertEqual(qf.expressions[-1][1], True) compute_and_compare_results(self, qf) def test_int_const_compare_eq(self): f = "def test(a: Qint2) -> bool:\n\treturn a == 2" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], And(Symbol("a.1"), Not(Symbol("a.0")))) + 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")))) 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" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], - # And( - # Symbol("a.1"), - # Not(Symbol("a.0")), - # Not(Symbol("a.2")), - # Not(Symbol("a.3")), - # ), - # ) + 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")), + Not(Symbol("a.2")), + Not(Symbol("a.3")), + ), + ) 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" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], - # And( - # Symbol("a.1"), - # Not(Symbol("a.0")), - # Not(Symbol("a.2")), - # Not(Symbol("a.3")), - # ), - # ) + 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")), + Not(Symbol("a.2")), + Not(Symbol("a.3")), + ), + ) 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" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], - # Or( - # Not(Symbol("a.1")), - # Symbol("a.0"), - # Symbol("a.2"), - # Symbol("a.3"), - # ), - # ) + self.assertEqual(qf.expressions[-1][0], _ret) 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" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], - # Or( - # Xor(Symbol("a.0"), Symbol("b.0")), - # Xor(Symbol("a.1"), Symbol("b.1")), - # ), - # ) + self.assertEqual(qf.expressions[-1][0], _ret) compute_and_compare_results(self, qf) def test_const_int_compare_gt(self): f = f"def test(a: Qint4) -> bool:\n\treturn a > 6" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) compute_and_compare_results(self, qf) # def test_int4_int4_compare_gt(self): @@ -246,46 +227,39 @@ def test_const_int_compare_gt(self): def test_const_int4_compare_lt(self): f = "def test(a: Qint4) -> bool:\n\treturn a < 6" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[-1][0], _ret) 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[-1][0], _ret) 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" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[-1][0], _ret) compute_and_compare_results(self, qf) def test_const_int_compare_lte(self): f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a <= b" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[-1][0], _ret) compute_and_compare_results(self, qf) def test_ite_return_qint(self): f = "def test(a: bool, b: Qint2, c: Qint2) -> Qint2:\n\treturn b if a else c" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 2) - # self.assertEqual(len(qf.expressions[0][0], Symbol("_ret.0")) - # # self.assertEqual(len(qf.expressions[0][1], ITE(a, Symbol("b.0"), Symbol("c.0"))) - # self.assertEqual(len(qf.expressions[1][0], Symbol("_ret.1")) - # # self.assertEqual(len(qf.expressions[1][1], ITE(a, Symbol("b.1"), Symbol("c.1"))) + self.assertEqual(qf.expressions[-2][0], Symbol("_ret.0")) + self.assertEqual(qf.expressions[-1][0], Symbol("_ret.1")) compute_and_compare_results(self, qf) def test_composed_comparators(self): diff --git a/test/test_qlassf_list.py b/test/test_qlassf_list.py index ce59c8b0..efb0c97c 100644 --- a/test/test_qlassf_list.py +++ b/test/test_qlassf_list.py @@ -41,9 +41,9 @@ def test_list_const(self): def test_list(self): f = "def test(a: Qlist[bool, 2]) -> bool:\n\treturn a[0] and a[1]" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual(len(qf.expressions[0][1], And(a_0, a_1)) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[0][1], And(a_0, a_1)) compute_and_compare_results(self, qf) def test_list_item_swap(self): diff --git a/test/test_qlassf_tuple.py b/test/test_qlassf_tuple.py index b33f6951..c16a020f 100644 --- a/test/test_qlassf_tuple.py +++ b/test/test_qlassf_tuple.py @@ -16,7 +16,7 @@ from typing import Tuple from parameterized import parameterized_class -from sympy import Symbol, symbols +from sympy import Symbol, symbols, sympify from sympy.logic import ITE, And, Not, Or, false, simplify_logic, true from qlasskit import QlassF, exceptions, qlassf @@ -41,9 +41,9 @@ def test_tuple_const(self): def test_tuple_arg(self): f = "def test(a: Tuple[bool, bool]) -> bool:\n\treturn a[0] and a[1]" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # # self.assertEqual(len(qf.expressions[0][1], And(a_0, a_1)) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[0][1], And(a_0, a_1)) compute_and_compare_results(self, qf) def test_tuple_item_swap(self): @@ -54,9 +54,6 @@ def test_tuple_item_swap(self): 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" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 2) - # # self.assertEqual(len(qf.expressions[0][1], ITE(b, a_1, a_0)) - # # self.assertEqual(len(qf.expressions[1][1], ITE(b, a_0, a_1)) compute_and_compare_results(self, qf) def test_tuple_arg_assign(self): @@ -67,19 +64,19 @@ def test_tuple_arg_assign(self): + "\treturn b and c" ) qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 3) - # self.assertEqual(len(qf.expressions[-1][0], _ret) - # self.assertEqual(len(qf.expressions[-1][1], And(b, c)) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[-1][0], _ret) + self.assertEqual(qf.expressions[-1][1], And(Symbol("a.0"), Symbol("a.1"))) 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]" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], And(Symbol("a.0.0"), And(Symbol("a.0.1"), a_1)) - # ) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual( + qf.expressions[0][1], And(Symbol("a.0.0"), And(Symbol("a.0.1"), a_1)) + ) compute_and_compare_results(self, qf) def test_tuple_of_tuple_of_tuple_arg(self): @@ -88,20 +85,20 @@ def test_tuple_of_tuple_of_tuple_arg(self): + "\treturn a[0][0][0] and a[0][0][1] and a[0][1] and a[1]" ) qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 1) - # self.assertEqual(len(qf.expressions[0][0], _ret) - # self.assertEqual( - # qf.expressions[0][1], - # And(Symbol("a.0.0.0"), And(Symbol("a.0.0.1"), And(Symbol("a.0.1"), a_1))), - # ) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual( + qf.expressions[0][1], + And(Symbol("a.0.0.0"), And(Symbol("a.0.0.1"), And(Symbol("a.0.1"), a_1))), + ) 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, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 3) - # self.assertEqual(len(qf.expressions[-1][0], _ret) - # self.assertEqual(len(qf.expressions[-1][1], And(b_0, b_1)) + self.assertEqual(len(qf.expressions), 1) + self.assertEqual(qf.expressions[0][0], _ret) + self.assertEqual(qf.expressions[0][1], And(a_0, a_1)) compute_and_compare_results(self, qf) def test_tuple_assign2(self): @@ -111,9 +108,11 @@ def test_tuple_assign2(self): + "\treturn b[0] and b[1] and b[2]" ) qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 4) - # self.assertEqual(len(qf.expressions[-1][0], _ret) - # self.assertEqual(len(qf.expressions[-1][1], And(b_0, And(b_1, Symbol("b.2")))) + 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.0.1"), Symbol("a.1")) + ) compute_and_compare_results(self, qf) def test_tuple_assign3(self): @@ -123,11 +122,11 @@ def test_tuple_assign3(self): + "\treturn b[0] and b[1][0] and b[1][1]" ) qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 4) - # self.assertEqual(len(qf.expressions[-1][0], _ret) - # self.assertEqual( - # qf.expressions[-1][1], And(b_0, And(Symbol("b.1.0"), Symbol("b.1.1"))) - # ) + 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.0.1"), Symbol("a.1")) + ) compute_and_compare_results(self, qf) def test_multi_assign(self): @@ -148,12 +147,12 @@ def test_multi_assign3(self): def test_tuple_result(self): f = "def test(a: bool, b: bool) -> Tuple[bool,bool]:\n\treturn a,b" qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) - # self.assertEqual(len(qf.expressions), 2) - # self.assertEqual(len(qf.expressions[0][0], Symbol("_ret.0")) - # self.assertEqual(len(qf.expressions[0][1], a) - # self.assertEqual(len(qf.expressions[1][0], Symbol("_ret.1")) - # self.assertEqual(len(qf.expressions[1][1], b) - # compute_and_compare_results(self, qf) + self.assertEqual(len(qf.expressions), 2) + self.assertEqual(qf.expressions[0][0], Symbol("_ret.0")) + self.assertEqual(qf.expressions[0][1], a) + self.assertEqual(qf.expressions[1][0], Symbol("_ret.1")) + self.assertEqual(qf.expressions[1][1], b) + compute_and_compare_results(self, qf) def test_tuple_compare(self): f = "def test(a: Tuple[bool, bool], b: Tuple[bool, bool]) -> bool:\n\treturn a == b"