Skip to content

Commit c861bf3

Browse files
committed
add operation
1 parent 095baa6 commit c861bf3

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

qlasskit/ast2logic/t_expression.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ def unfold(v_exps, op):
199199
elif isinstance(expr, ast.BinOp):
200200
# Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift
201201
# | BitOr | BitXor | BitAnd | FloorDiv
202-
raise exceptions.ExpressionNotHandledException(expr)
202+
# print(ast.dump(expr))
203+
tleft = translate_expression(expr.left, env)
204+
tright = translate_expression(expr.right, env)
205+
206+
if isinstance(expr.op, ast.Add):
207+
return tleft[0].add(tleft, tright)
208+
else:
209+
raise exceptions.ExpressionNotHandledException(expr)
203210

204211
# Call
205212
elif isinstance(expr, ast.Call):

qlasskit/types/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from typing import Any
1717

18-
from sympy.logic import Not, Xor
18+
from sympy.logic import Not, Xor, And, Or
1919

2020

2121
def _neq(a, b):
@@ -26,6 +26,14 @@ def _eq(a, b):
2626
return Not(_neq(a, b))
2727

2828

29+
def _half_adder(a, b): # Carry x Sum
30+
return And(a, b), Xor(a, b)
31+
32+
33+
def _full_adder(c, a, b): # Carry x Sum
34+
return Or(And(a, b), And(b, c), And(a, c)), Xor(a, b, c)
35+
36+
2937
from .qtype import Qtype, TExp, TType # noqa: F401, E402
3038
from .qbool import Qbool # noqa: F401, E402
3139
from .qint import Qint, Qint2, Qint4, Qint8, Qint12, Qint16 # noqa: F401, E402

qlasskit/types/qint.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sympy import Symbol
1818
from sympy.logic import And, Not, Or, false, true
1919

20-
from . import _eq, _neq
20+
from . import _eq, _full_adder, _neq
2121
from .qtype import Qtype, TExp
2222

2323

@@ -26,7 +26,10 @@ class Qint(int, Qtype):
2626

2727
def __init__(self, value):
2828
super().__init__()
29-
self.value = value
29+
self.value = value % 2**self.BIT_SIZE
30+
31+
def __add__(self, b):
32+
return (self.value + b) % 2**self.BIT_SIZE
3033

3134
@classmethod
3235
def from_bool(cls, v: List[bool]):
@@ -140,9 +143,21 @@ def gte(tleft: TExp, tcomp: TExp) -> TExp:
140143
"""Compare two Qint for greater than - equal"""
141144
return (bool, Not(Qint.lt(tleft, tcomp)[1]))
142145

143-
# @staticmethod
144-
# def add(tleft: TExp, tright: TExp) -> TExp:
145-
# """Add two Qint"""
146+
# Operations
147+
148+
@classmethod
149+
def add(cls, tleft: TExp, tright: TExp) -> TExp:
150+
"""Add two Qint"""
151+
if len(tleft[1]) != len(tright[1]): # TODO: handle this
152+
raise Exception("Ints have differnt sizes")
153+
154+
carry = False
155+
sums = []
156+
for x in zip(tleft[1], tright[1]):
157+
carry, sum = _full_adder(carry, x[0], x[1])
158+
sums.append(sum)
159+
160+
return (cls, sums)
146161

147162

148163
class Qint2(Qint):

qlasskit/types/qtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ def lt(tleft: TExp, tcomp: TExp) -> TExp:
9898
@staticmethod
9999
def lte(tleft: TExp, tcomp: TExp) -> TExp:
100100
raise Exception("abstract")
101+
102+
# Operations
103+
104+
@staticmethod
105+
def add(tleft: TExp, tcomp: TExp) -> TExp:
106+
raise Exception("abstract")

test/test_qlassf_int.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,15 @@ def test_composed_comparators(self):
291291
# return a + 1
292292
# def test(a: Qint2, b: Qint2) -> Qint2:
293293
# return a + b
294+
295+
296+
class TestQlassfIntAdd(unittest.TestCase):
297+
def test_add(self):
298+
f = "def test(a: Qint2, b: Qint2) -> Qint2: return a + b"
299+
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
300+
compute_and_compare_results(self, qf)
301+
302+
def test_add_const(self):
303+
f = "def test(a: Qint2) -> Qint2: return a + 1"
304+
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
305+
compute_and_compare_results(self, qf)

0 commit comments

Comments
 (0)