Skip to content

Commit d243568

Browse files
committed
refactor test.compute_and_compare_results
1 parent 9b9531e commit d243568

File tree

5 files changed

+129
-112
lines changed

5 files changed

+129
-112
lines changed

test/test_compiler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@
1616

1717
from qlasskit import compiler, qlassf
1818

19-
from .utils import compare_circuit_truth_table
19+
from .utils import compute_and_compare_results
2020

2121

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

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

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

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

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

4848
def test_or_not(self):
4949
f = "def test(a: bool, b: bool) -> bool:\n\treturn not a or b"
5050
qf = qlassf(f, to_compile=True)
51-
compare_circuit_truth_table(self, qf)
51+
compute_and_compare_results(self, qf)

test/test_qlassf_bool.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from qlasskit import QlassF, exceptions, qlassf
2121

22-
from .utils import COMPILATION_ENABLED, compare_circuit_truth_table
22+
from .utils import COMPILATION_ENABLED, compute_and_compare_results
2323

2424
a, b, c, d, e, g, h = symbols("a,b,c,d,e,g,h")
2525
_ret = Symbol("_ret")
@@ -51,7 +51,7 @@ def test_arg_identity(self):
5151
self.assertEqual(len(qf.expressions), 1)
5252
self.assertEqual(qf.expressions[0][0], _ret)
5353
self.assertEqual(qf.expressions[0][1], ex)
54-
compare_circuit_truth_table(self, qf)
54+
compute_and_compare_results(self, qf)
5555

5656
def test_not_arg(self):
5757
ex = Not(a)
@@ -60,7 +60,7 @@ def test_not_arg(self):
6060
self.assertEqual(len(qf.expressions), 1)
6161
self.assertEqual(qf.expressions[0][0], _ret)
6262
self.assertEqual(qf.expressions[0][1], ex)
63-
compare_circuit_truth_table(self, qf)
63+
compute_and_compare_results(self, qf)
6464

6565
def test_and(self):
6666
ex = And(Not(a), b)
@@ -69,21 +69,21 @@ def test_and(self):
6969
self.assertEqual(len(qf.expressions), 1)
7070
self.assertEqual(qf.expressions[0][0], _ret)
7171
self.assertEqual(qf.expressions[0][1], ex)
72-
compare_circuit_truth_table(self, qf)
72+
compute_and_compare_results(self, qf)
7373

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

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

8888
def test_or_not(self):
8989
ex = Or(Not(a), b)
@@ -92,7 +92,7 @@ def test_or_not(self):
9292
self.assertEqual(len(qf.expressions), 1)
9393
self.assertEqual(qf.expressions[0][0], _ret)
9494
self.assertEqual(qf.expressions[0][1], ex)
95-
compare_circuit_truth_table(self, qf)
95+
compute_and_compare_results(self, qf)
9696

9797
def test_multiple_arg(self):
9898
ex = And(a, And(Not(b), c))
@@ -101,7 +101,7 @@ def test_multiple_arg(self):
101101
self.assertEqual(len(qf.expressions), 1)
102102
self.assertEqual(qf.expressions[0][0], _ret)
103103
self.assertEqual(qf.expressions[0][1], ex)
104-
compare_circuit_truth_table(self, qf)
104+
compute_and_compare_results(self, qf)
105105

106106
def test_multiple_arg2(self):
107107
ex = And(a, And(Not(b), Or(a, c)))
@@ -110,7 +110,7 @@ def test_multiple_arg2(self):
110110
self.assertEqual(len(qf.expressions), 1)
111111
self.assertEqual(qf.expressions[0][0], _ret)
112112
self.assertEqual(qf.expressions[0][1], ex)
113-
compare_circuit_truth_table(self, qf)
113+
compute_and_compare_results(self, qf)
114114

115115
def test_ifexp(self):
116116
ex = ITE(a, true, false)
@@ -119,7 +119,7 @@ def test_ifexp(self):
119119
self.assertEqual(len(qf.expressions), 1)
120120
self.assertEqual(qf.expressions[0][0], _ret)
121121
self.assertEqual(qf.expressions[0][1], ex)
122-
compare_circuit_truth_table(self, qf)
122+
compute_and_compare_results(self, qf)
123123

124124
def test_ifexp2(self):
125125
ex = ITE(And(a, And(Not(b), c)), true, false)
@@ -128,7 +128,7 @@ def test_ifexp2(self):
128128
self.assertEqual(len(qf.expressions), 1)
129129
self.assertEqual(qf.expressions[0][0], _ret)
130130
self.assertEqual(qf.expressions[0][1], ex)
131-
compare_circuit_truth_table(self, qf)
131+
compute_and_compare_results(self, qf)
132132

133133
def test_ifexp3(self):
134134
exp = ITE(
@@ -144,7 +144,7 @@ def test_ifexp3(self):
144144
self.assertEqual(len(qf.expressions), 1)
145145
self.assertEqual(qf.expressions[0][0], _ret)
146146
self.assertEqual(qf.expressions[0][1], exp)
147-
compare_circuit_truth_table(self, qf)
147+
compute_and_compare_results(self, qf)
148148

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

159159
def test_assign2(self):
160160
f = (
@@ -168,7 +168,7 @@ def test_assign2(self):
168168
self.assertEqual(qf.expressions[0][1], And(a, And(Not(b), c)))
169169
self.assertEqual(qf.expressions[1][0], _ret)
170170
self.assertEqual(qf.expressions[1][1], d)
171-
compare_circuit_truth_table(self, qf)
171+
compute_and_compare_results(self, qf)
172172

173173
def test_assign3(self):
174174
f = (

test/test_qlassf_int.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from qlasskit import QlassF, exceptions, qlassf # Qint2
2121

22-
from .utils import COMPILATION_ENABLED, compare_circuit_truth_table
22+
from .utils import COMPILATION_ENABLED, compute_and_compare_results
2323

2424
a, b, c, d = symbols("a,b,c,d")
2525
_ret = Symbol("_ret")
@@ -43,7 +43,7 @@ def test_int_arg2(self):
4343
qf.expressions[0][1],
4444
ITE(And(Symbol("a.0"), b), Symbol("a.1"), Symbol("a.0")),
4545
)
46-
compare_circuit_truth_table(self, qf)
46+
compute_and_compare_results(self, qf)
4747

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

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

7474
def test_int_identity(self):
7575
f = "def test(a: Qint2) -> Qint2:\n\treturn a"
@@ -79,7 +79,7 @@ def test_int_identity(self):
7979
self.assertEqual(qf.expressions[0][1], Symbol("a.0"))
8080
self.assertEqual(qf.expressions[1][0], Symbol("_ret.1"))
8181
self.assertEqual(qf.expressions[1][1], Symbol("a.1"))
82-
compare_circuit_truth_table(self, qf)
82+
compute_and_compare_results(self, qf)
8383

8484
# TODO: need consts
8585
# def test_int_const(self):
@@ -92,7 +92,7 @@ def test_int_const_compare_eq(self):
9292
self.assertEqual(len(qf.expressions), 1)
9393
self.assertEqual(qf.expressions[0][0], _ret)
9494
self.assertEqual(qf.expressions[0][1], And(Symbol("a.1"), Not(Symbol("a.0"))))
95-
compare_circuit_truth_table(self, qf)
95+
compute_and_compare_results(self, qf)
9696

9797
def test_int_const_compare_eq_different_type(self):
9898
f = "def test(a: Qint4) -> bool:\n\treturn a == 2"
@@ -108,7 +108,7 @@ def test_int_const_compare_eq_different_type(self):
108108
Not(Symbol("a.3")),
109109
),
110110
)
111-
compare_circuit_truth_table(self, qf)
111+
compute_and_compare_results(self, qf)
112112

113113
def test_const_int_compare_eq_different_type(self):
114114
f = "def test(a: Qint4) -> bool:\n\treturn 2 == a"
@@ -124,7 +124,7 @@ def test_const_int_compare_eq_different_type(self):
124124
Not(Symbol("a.3")),
125125
),
126126
)
127-
compare_circuit_truth_table(self, qf)
127+
compute_and_compare_results(self, qf)
128128

129129
def test_const_int_compare_neq_different_type(self):
130130
f = "def test(a: Qint4) -> bool:\n\treturn 2 != a"
@@ -140,7 +140,7 @@ def test_const_int_compare_neq_different_type(self):
140140
Symbol("a.3"),
141141
),
142142
)
143-
compare_circuit_truth_table(self, qf)
143+
compute_and_compare_results(self, qf)
144144

145145
def test_int_int_compare_eq(self):
146146
f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a == b"
@@ -154,7 +154,7 @@ def test_int_int_compare_eq(self):
154154
Not(Xor(Symbol("a.1"), Symbol("b.1"))),
155155
),
156156
)
157-
compare_circuit_truth_table(self, qf)
157+
compute_and_compare_results(self, qf)
158158

159159
def test_int_int_compare_neq(self):
160160
f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a != b"
@@ -168,21 +168,21 @@ def test_int_int_compare_neq(self):
168168
Xor(Symbol("a.1"), Symbol("b.1")),
169169
),
170170
)
171-
compare_circuit_truth_table(self, qf)
171+
compute_and_compare_results(self, qf)
172172

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

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

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

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

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

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

222222
# def test_const_int_compare_gte(self):
223223
# f = "def test(a: Qint2, b: Qint2) -> bool:\n\treturn a >= b"

test/test_qlassf_tuple.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from qlasskit import QlassF, exceptions, qlassf
2222

23-
from .utils import COMPILATION_ENABLED, compare_circuit_truth_table
23+
from .utils import COMPILATION_ENABLED, compute_and_compare_results
2424

2525
a, b, c, d = symbols("a,b,c,d")
2626
_ret = Symbol("_ret")
@@ -37,7 +37,7 @@ def test_tuple_arg(self):
3737
self.assertEqual(len(qf.expressions), 1)
3838
self.assertEqual(qf.expressions[0][0], _ret)
3939
self.assertEqual(qf.expressions[0][1], And(a_0, a_1))
40-
compare_circuit_truth_table(self, qf)
40+
compute_and_compare_results(self, qf)
4141

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

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

7373
def test_tuple_of_tuple_of_tuple_arg(self):
7474
f = (
@@ -82,15 +82,15 @@ def test_tuple_of_tuple_of_tuple_arg(self):
8282
qf.expressions[0][1],
8383
And(Symbol("a.0.0.0"), And(Symbol("a.0.0.1"), And(Symbol("a.0.1"), a_1))),
8484
)
85-
compare_circuit_truth_table(self, qf)
85+
compute_and_compare_results(self, qf)
8686

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

9595
def test_tuple_assign2(self):
9696
f = (
@@ -102,7 +102,7 @@ def test_tuple_assign2(self):
102102
self.assertEqual(len(qf.expressions), 4)
103103
self.assertEqual(qf.expressions[-1][0], _ret)
104104
self.assertEqual(qf.expressions[-1][1], And(b_0, And(b_1, Symbol("b.2"))))
105-
compare_circuit_truth_table(self, qf)
105+
compute_and_compare_results(self, qf)
106106

107107
def test_tuple_assign3(self):
108108
f = (
@@ -116,7 +116,7 @@ def test_tuple_assign3(self):
116116
self.assertEqual(
117117
qf.expressions[-1][1], And(b_0, And(Symbol("b.1.0"), Symbol("b.1.1")))
118118
)
119-
compare_circuit_truth_table(self, qf)
119+
compute_and_compare_results(self, qf)
120120

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

0 commit comments

Comments
 (0)