Skip to content

Commit

Permalink
add operation
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 18, 2023
1 parent 095baa6 commit c861bf3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
9 changes: 8 additions & 1 deletion qlasskit/ast2logic/t_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ def unfold(v_exps, op):
elif isinstance(expr, ast.BinOp):
# Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift
# | BitOr | BitXor | BitAnd | FloorDiv
raise exceptions.ExpressionNotHandledException(expr)
# print(ast.dump(expr))
tleft = translate_expression(expr.left, env)
tright = translate_expression(expr.right, env)

if isinstance(expr.op, ast.Add):
return tleft[0].add(tleft, tright)
else:
raise exceptions.ExpressionNotHandledException(expr)

# Call
elif isinstance(expr, ast.Call):
Expand Down
10 changes: 9 additions & 1 deletion qlasskit/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from typing import Any

from sympy.logic import Not, Xor
from sympy.logic import Not, Xor, And, Or


def _neq(a, b):
Expand All @@ -26,6 +26,14 @@ def _eq(a, b):
return Not(_neq(a, b))


def _half_adder(a, b): # Carry x Sum
return And(a, b), Xor(a, b)


def _full_adder(c, a, b): # Carry x Sum
return Or(And(a, b), And(b, c), And(a, c)), Xor(a, b, c)


from .qtype import Qtype, TExp, TType # noqa: F401, E402
from .qbool import Qbool # noqa: F401, E402
from .qint import Qint, Qint2, Qint4, Qint8, Qint12, Qint16 # noqa: F401, E402
Expand Down
25 changes: 20 additions & 5 deletions qlasskit/types/qint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sympy import Symbol
from sympy.logic import And, Not, Or, false, true

from . import _eq, _neq
from . import _eq, _full_adder, _neq
from .qtype import Qtype, TExp


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

def __init__(self, value):
super().__init__()
self.value = value
self.value = value % 2**self.BIT_SIZE

def __add__(self, b):
return (self.value + b) % 2**self.BIT_SIZE

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

# @staticmethod
# def add(tleft: TExp, tright: TExp) -> TExp:
# """Add two Qint"""
# Operations

@classmethod
def add(cls, tleft: TExp, tright: TExp) -> TExp:
"""Add two Qint"""
if len(tleft[1]) != len(tright[1]): # TODO: handle this
raise Exception("Ints have differnt sizes")

carry = False
sums = []
for x in zip(tleft[1], tright[1]):
carry, sum = _full_adder(carry, x[0], x[1])
sums.append(sum)

return (cls, sums)


class Qint2(Qint):
Expand Down
6 changes: 6 additions & 0 deletions qlasskit/types/qtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ def lt(tleft: TExp, tcomp: TExp) -> TExp:
@staticmethod
def lte(tleft: TExp, tcomp: TExp) -> TExp:
raise Exception("abstract")

# Operations

@staticmethod
def add(tleft: TExp, tcomp: TExp) -> TExp:
raise Exception("abstract")
12 changes: 12 additions & 0 deletions test/test_qlassf_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,15 @@ def test_composed_comparators(self):
# return a + 1
# def test(a: Qint2, b: Qint2) -> Qint2:
# return a + b


class TestQlassfIntAdd(unittest.TestCase):
def test_add(self):
f = "def test(a: Qint2, b: Qint2) -> Qint2: return a + b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

def test_add_const(self):
f = "def test(a: Qint2) -> Qint2: return a + 1"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

0 comments on commit c861bf3

Please sign in to comment.