Skip to content

Commit

Permalink
fix oraclize
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Nov 1, 2023
1 parent 0f0d9a7 commit 952033e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion qlasskit/algorithms/qalgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def oraclize(qf: QlassF, element: Any, name="oracle"):

elif qf.args[0].ttype == bool:
argt_name = "bool"
elif sys.version_info < (3, 9):
else:
argt_name = "Tuple["
argt_name += ",".join([x.__name__ for x in get_args(qf.args[0].ttype)])
argt_name += "]"
Expand Down
18 changes: 14 additions & 4 deletions test/test_algo.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 And, Not, Symbol

from qlasskit import Qint2, Qint4, Qlist, qlassf
Expand All @@ -26,6 +27,8 @@
oraclize,
)

from .utils import ENABLED_COMPILERS


class TestAlgo_format_outcome(unittest.TestCase):
def test_format_outcome_str(self):
Expand Down Expand Up @@ -96,27 +99,34 @@ def test_interpret_counts2(self):
self.assertEqual(c, {(False, True): 5, (True, True): 1})


@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestAlgo_oraclize(unittest.TestCase):
def test_constant_oracle(self):
q = qlassf("def test(a: bool) -> bool: return True")
q = qlassf("def test(a: bool) -> bool: return True", compiler=self.compiler)
self.assertRaises(ConstantOracleException, lambda x: oraclize(q, True), True)

def test_oracle_identity(self):
q = qlassf("def test(a: bool) -> bool: return a")
q = qlassf("def test(a: bool) -> bool: return a", compiler=self.compiler)
orac = oraclize(q, True)
orac.compile(compiler=self.compiler)
self.assertEqual(orac.name, "oracle")
self.assertEqual(orac.expressions, [(Symbol("_ret"), Symbol("v"))])

def test_oracle_tuple(self):
q = qlassf("def test(a: Tuple[bool, bool]) -> bool: return a[0] and a[1]")
q = qlassf(
"def test(a: Tuple[bool, bool]) -> bool: return a[0] and a[1]",
compiler=self.compiler,
)
orac = oraclize(q, True)
orac.compile(compiler=self.compiler)
self.assertEqual(
orac.expressions, [(Symbol("_ret"), And(Symbol("v.0"), Symbol("v.1")))]
)

def test_oracle_qtype(self):
q = qlassf("def test(a: Qint2) -> Qint2: return a")
q = qlassf("def test(a: Qint2) -> Qint2: return a", compiler=self.compiler)
orac = oraclize(q, 1)
orac.compile(compiler=self.compiler)
self.assertEqual(
orac.expressions, [(Symbol("_ret"), And(Symbol("v.0"), Not(Symbol("v.1"))))]
)

0 comments on commit 952033e

Please sign in to comment.