Skip to content

Commit

Permalink
translate_argument now hold the original type
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 6, 2023
1 parent 4ad476b commit ddc1a13
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
13 changes: 12 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@
- [x] Poc compiler 2 using qcircuit abstraction
- [x] OpenQASM3 exporter

#### Typechecker branch
- [x] Translate_expr should returns ttype*expr
- [x] Args should also hold the original type
- [ ] Transform Env to a class holding also the original types
- [ ] Typecheck all the expressions

### Week 3: (9 Oct 23)
- [ ] Int: comparison - eq
- [ ] Qubit garbage uncomputing and recycling
- [ ] Test: add qubit usage check
- [ ] Compiler: remove consecutive X gates
Expand Down Expand Up @@ -97,4 +104,8 @@
- [ ] QuTip
- [ ] Pennylane
- [ ] Cirq
- [ ] Sympy quantum computing expressions
- [ ] Sympy quantum computing expressions

### Tools

- [ ] py2qasm tools
2 changes: 1 addition & 1 deletion qlasskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@

from .qcircuit import QCircuit # noqa: F401
from .qlassf import QlassF, qlassf # noqa: F401
from .typing import Qint2, Qint4, Qint8, Qint12, Qint16, Qtype # noqa: F401
from .typing import Qint, Qint2, Qint4, Qint8, Qint12, Qint16, Qtype # noqa: F401
from .ast2logic import exceptions # noqa: F401
25 changes: 15 additions & 10 deletions qlasskit/ast2logic/t_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,44 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
from typing import List
from typing import List, Tuple

from ..typing import Args
from . import exceptions, flatten
from ..typing import Arg, Args, Qint
from . import exceptions
from .t_expression import TType


def translate_argument(ann, base="") -> List[str]:
def translate_argument(ann, base="") -> Arg:
def to_name(a):
return a.attr if isinstance(a, ast.Attribute) else a.id

# Tuple
if isinstance(ann, ast.Subscript) and ann.value.id == "Tuple": # type: ignore
al = []
ind = 0
ttypes: List[TType] = []
for i in ann.slice.value.elts: # type: ignore
if isinstance(i, ast.Name) and to_name(i) == "bool":
al.append(f"{base}.{ind}")
ttypes.append(bool)
else:
inner_list = translate_argument(i, base=f"{base}.{ind}")
al.extend(inner_list)
inner_arg = translate_argument(i, base=f"{base}.{ind}")
ttypes.append(inner_arg.ttype)
al.extend(inner_arg.bitvec)
ind += 1
return al
ttypes_t = tuple(ttypes)
return Arg(base, Tuple[ttypes_t], al)

# QintX
elif to_name(ann)[0:4] == "Qint":
n = int(to_name(ann)[4::])
arg_list = [f"{base}.{i}" for i in range(n)]
# arg_list.append((f"{base}{arg.arg}", n))
return arg_list
return Arg(base, Qint, arg_list)

# Bool
elif to_name(ann) == "bool":
return [f"{base}"]
return Arg(base, bool, [f"{base}"])

else:
raise exceptions.UnknownTypeException(ann)
Expand All @@ -55,4 +60,4 @@ def translate_arguments(args) -> Args:
args_unrolled = map(
lambda arg: translate_argument(arg.annotation, base=arg.arg), args
)
return flatten(list(args_unrolled))
return list(args_unrolled)
2 changes: 1 addition & 1 deletion qlasskit/qlassf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __add__(self, qf2) -> "QlassF":

def truth_table_header(self) -> List[str]:
"""Returns the list of string containing the truth table header"""
header = [x for x in self.args]
header = [x.name for x in self.args]
header.extend([sym.name for (sym, retex) in self.expressions[-self.ret_size :]])
return header

Expand Down
18 changes: 17 additions & 1 deletion qlasskit/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@
from sympy import Symbol
from sympy.logic.boolalg import Boolean

Args = List[str]
# from .ast2logic.t_expression import TType


class Arg:
def __init__(self, name: str, ttype: object, bitvec: List[str]):
self.name = name
self.ttype = ttype
self.bitvec = bitvec

def __len__(self) -> int:
return len(self.bitvec)

def to_exp(self) -> List[Symbol]:
return list(map(Symbol, self.bitvec))


Args = List[Arg]
BoolExpList = List[Tuple[Symbol, Boolean]]
LogicFun = Tuple[str, Args, int, BoolExpList]

Expand Down
31 changes: 23 additions & 8 deletions test/test_ast2logic_t_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

import ast
import unittest
from typing import Tuple

from qlasskit import ast2logic, exceptions
from qlasskit import Qint, ast2logic, exceptions


class TestAst2Logic_translate_argument(unittest.TestCase):
Expand All @@ -32,44 +33,58 @@ def test_bool(self):
f = "a: bool"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c, ["a"])
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, bool)
self.assertEqual(c.bitvec, ["a"])

def test_qint2(self):
f = "a: Qint2"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c, ["a.0", "a.1"])
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, Qint)
self.assertEqual(c.bitvec, ["a.0", "a.1"])

def test_qint4(self):
f = "a: Qint4"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c, ["a.0", "a.1", "a.2", "a.3"])
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, Qint)
self.assertEqual(c.bitvec, ["a.0", "a.1", "a.2", "a.3"])

def test_tuple(self):
f = "a: Tuple[bool, bool]"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c, ["a.0", "a.1"])
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, Tuple[bool, bool])
self.assertEqual(c.bitvec, ["a.0", "a.1"])

def test_tuple_of_tuple(self):
f = "a: Tuple[Tuple[bool, bool], bool]"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c, ["a.0.0", "a.0.1", "a.1"])
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, Tuple[Tuple[bool, bool], bool])
self.assertEqual(c.bitvec, ["a.0.0", "a.0.1", "a.1"])

def test_tuple_of_tuple2(self):
f = "a: Tuple[bool, Tuple[bool, bool]]"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c, ["a.0", "a.1.0", "a.1.1"])
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, Tuple[bool, Tuple[bool, bool]])
self.assertEqual(c.bitvec, ["a.0", "a.1.0", "a.1.1"])

def test_tuple_of_int2(self):
f = "a: Tuple[Qint2, Qint2]"
ann_ast = ast.parse(f).body[0].annotation
c = ast2logic.translate_argument(ann_ast, "a")
self.assertEqual(c.name, "a")
self.assertEqual(c.ttype, Tuple[Qint, Qint])
self.assertEqual(
c,
c.bitvec,
[
"a.0.0",
"a.0.1",
Expand Down

0 comments on commit ddc1a13

Please sign in to comment.