Skip to content

Commit

Permalink
make qint as generic
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Mar 17, 2024
1 parent 921b575 commit d3a0c1a
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions qlasskit/types/qint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .qtype import Qtype, TExp, bin_to_bool_list, bool_list_to_bin


class Qint(int, Qtype):
class QintImp(int, Qtype):
BIT_SIZE = 8

def __init__(self, value):
Expand Down Expand Up @@ -122,17 +122,20 @@ def gt(tleft: TExp, tcomp: TExp) -> TExp:
@staticmethod
def lt(tleft: TExp, tcomp: TExp) -> TExp:
"""Compare two Qint for lower than"""
return (bool, And(Not(Qint.gt(tleft, tcomp)[1]), Not(Qint.eq(tleft, tcomp)[1])))
return (
bool,
And(Not(QintImp.gt(tleft, tcomp)[1]), Not(QintImp.eq(tleft, tcomp)[1])),
)

@staticmethod
def lte(tleft: TExp, tcomp: TExp) -> TExp:
"""Compare two Qint for lower than - equal"""
return (bool, Not(Qint.gt(tleft, tcomp)[1]))
return (bool, Not(QintImp.gt(tleft, tcomp)[1]))

@staticmethod
def gte(tleft: TExp, tcomp: TExp) -> TExp:
"""Compare two Qint for greater than - equal"""
return (bool, Not(Qint.lt(tleft, tcomp)[1]))
return (bool, Not(QintImp.lt(tleft, tcomp)[1]))

# Operations

Expand Down Expand Up @@ -234,40 +237,52 @@ def bitwise_or(cls, tleft: TExp, tright: TExp) -> TExp:
return cls.bitwise_generic(Or, tleft, tright)


class Qint2(Qint):
class Qint2(QintImp):
BIT_SIZE = 2


class Qint3(Qint):
class Qint3(QintImp):
BIT_SIZE = 3


class Qint4(Qint):
class Qint4(QintImp):
BIT_SIZE = 4


class Qint5(Qint):
class Qint5(QintImp):
BIT_SIZE = 5


class Qint6(Qint):
class Qint6(QintImp):
BIT_SIZE = 6


class Qint7(Qint):
class Qint7(QintImp):
BIT_SIZE = 7


class Qint8(Qint):
class Qint8(QintImp):
BIT_SIZE = 8


class Qint12(Qint):
class Qint12(QintImp):
BIT_SIZE = 12


class Qint16(Qint):
class Qint16(QintImp):
BIT_SIZE = 16


QINT_TYPES = [Qint2, Qint3, Qint4, Qint5, Qint6, Qint7, Qint8, Qint12, Qint16]


class QintMeta(type):
def __getitem__(cls, params):
if isinstance(params, tuple) and len(params) == 1:
i = params
if isinstance(i, int) and i >= 2:
return f"Qint{i}" # TODO: transform to type


class Qint(metaclass=QintMeta):
pass

0 comments on commit d3a0c1a

Please sign in to comment.