Skip to content

Commit

Permalink
fixed #42
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Apr 23, 2024
1 parent 8bab24c commit 11ccd9b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions qlasskit/ast2logic/t_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def unroll_subscripts(sub, st):
return unroll_subscripts(sub.value, st)
elif isinstance(sub.value, ast.Name):
return f"{sub.value.id}.{_sval.value}.{st}"
elif (
isinstance(sub.value, ast.Constant)
and hasattr(sub.value.value, "elts")
and isinstance(sub.slice, ast.Constant)
):
return sub.value.value.elts[sub.slice.value]
else:
raise Exception(f"Subscript not handled: {ast.dump(sub)} {st}")

_sval = expr.slice # type: ignore

Expand All @@ -70,6 +78,9 @@ def unroll_subscripts(sub, st):
else:
sn = unroll_subscripts(expr, "")

if isinstance(sn, ast.Constant):
return (bool, sn.value)

if sn.split(".")[0] not in env:
raise exceptions.UnboundException(sn, env)

Expand Down
29 changes: 29 additions & 0 deletions test/qlassf/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,32 @@ def test_list_access_with_var_on_tuple2(self):
)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

# TODO: handle this case
# def test_list_of_tuple_of_tuple(self):
# f = (
# "def oracle(io_list: Parameter[List[Tuple[Tuple[bool, bool], bool]]],"
# " f: bool) -> bool:\n"
# "\tv = True\n"
# "\tfor io in io_list:\n"
# "\t\tv = v and (io[0][0] and io[0][1]) == io[1]\n"
# "\treturn v"
# )
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
# ttable = [(False, False), (True, False), (False, True), (True, True)]
# tt = list(map(lambda e: (e, e[0] or e[1]), ttable))
# qfb = qf.bind(io_list=tt)
# print(qfb)

def test_list_of_tuple_of_tuple2(self):
f = (
"def oracle(io_list: Parameter[List[Tuple[bool, bool, bool]]], f: bool) -> bool:\n"
"\tv = True\n"
"\tfor io in io_list:\n"
"\t\tv = v and (io[0] or io[1]) == io[2]\n"
"\treturn v"
)
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
ttable = [(False, False), (True, False), (False, True), (True, True)]
tt = list(map(lambda e: (e[0], e[1], e[0] or e[1]), ttable))
qf.bind(io_list=tt)

0 comments on commit 11ccd9b

Please sign in to comment.