Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unitary Hack] Qint integer division #27 #52

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions qlasskit/ast2logic/t_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ def unfold(v_exps, op):
return tright[0].bitwise_and(tleft, tright) # type: ignore
elif isinstance(expr.op, ast.BitOr) and hasattr(tleft[0], "bitwise_or"):
return tleft[0].bitwise_or(tleft, tright)
elif isinstance(expr.op, ast.FloorDiv) and hasattr(tleft[0], "floor_div"):
return tleft[0].floor_div(tleft, tright)
elif (
isinstance(expr.op, ast.LShift)
and hasattr(tleft[0], "shift_left")
Expand Down
27 changes: 27 additions & 0 deletions qlasskit/types/qint.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,33 @@ def bitwise_and(cls, tleft: TExp, tright: TExp) -> TExp:
def bitwise_or(cls, tleft: TExp, tright: TExp) -> TExp:
return cls.bitwise_generic(Or, tleft, tright)

@classmethod
def floor_div(cls, tleft: TExp, tright: TExp) -> TExp:
"""Perform floor division on two Qint"""
if not issubclass(tleft[0], Qtype):
raise TypeErrorException(tleft[0], Qtype)
if not issubclass(tright[0], Qtype):
raise TypeErrorException(tright[0], Qtype)

tright_e = cast(Qtype, tright)
tleft_e = cast(Qtype, tleft)

if len(tleft_e[1]) != len(tright_e[1]):
raise ValueError("Operands must be of the same bit length")

n = len(tleft_e[1])
Q = 0
R = 0

for i in range(n):
R <<= 1
R |= tleft_e[1][i]
if R >= tright_e[1]:
R ^= tright_e[1]
Q |= (1 << (n - 1 - i))

return (cls, Q)


class Qint2(QintImp):
BIT_SIZE = 2
Expand Down
4 changes: 4 additions & 0 deletions qlasskit/types/qtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,7 @@ def bitwise_and(tleft: TExp, tright: TExp) -> TExp:
@staticmethod
def bitwise_or(tleft: TExp, tright: TExp) -> TExp:
raise Exception("abstract bitwise_or")

@staticmethod
def floor_div(tleft: TExp, tright: TExp) -> TExp:
raise Exception("abstract floor_div")
7 changes: 7 additions & 0 deletions test/qlassf/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ def test_qint2_to_bin(self):
self.assertEqual(c, "10")
self.assertEqual(c, Qint2(1).export("binary"))

def test_floor_div(self):
self.assertEqual(Qint2.floor_div(Qint2(10), Qint2(3)), Qint2(3))
self.assertEqual(Qint2.floor_div(Qint2(20), Qint2(4)), Qint2(5))
self.assertEqual(Qint2.floor_div(Qint2(5), Qint2(2)), Qint2(2))
with self.assertRaises(ZeroDivisionError):
Qint2.floor_div(Qint2(10), Qint2(0))


@parameterized_class(
("ttype_str", "ttype_size", "compiler"),
Expand Down
Loading