Skip to content

Commit

Permalink
qchar type
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Feb 19, 2024
1 parent ff21937 commit 162927d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qlasskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
const_to_qtype,
interpret_as_qtype,
Qtype,
Qchar,
Qint,
Qint2,
Qint3,
Qint4,
Qint5,
Qint6,
Qint7,
Qint8,
Qint12,
Qint16,
Expand Down
14 changes: 14 additions & 0 deletions qlasskit/ast2ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ def __call_anyall(self, node):
op = ast.Or() if node.func.id == "any" else ast.And()
return ast.BoolOp(op=op, values=args)

def __call_chr(self, node):
# TODO
raise Exception()

def __call_ord(self, node):
# TODO
raise Exception()

def visit_Call(self, node):
node.args = [self.visit(ar) for ar in node.args]
if not hasattr(node.func, "id"):
Expand All @@ -477,6 +485,12 @@ def visit_Call(self, node):
elif node.func.id == "sum":
return self.__call_sum(node)

elif node.func.id == "ord":
return self.__call_ord(node)

elif node.func.id == "chr":
return self.__call_chr(node)

elif node.func.id in ["any", "all"]:
return self.__call_anyall(node)

Expand Down
4 changes: 4 additions & 0 deletions qlasskit/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ def _full_adder(c, a, b): # Carry x Sum
from .qbool import Qbool # noqa: F401, E402
from .qlist import Qlist # noqa: F401, E402
from .qmatrix import Qmatrix # noqa: F401, E402
from .qchar import Qchar # noqa: F401, E402
from .qint import ( # noqa: F401, E402
Qint,
Qint2,
Qint3,
Qint4,
Qint5,
Qint6,
Qint7,
Qint8,
Qint12,
Qint16,
Expand All @@ -66,9 +68,11 @@ def _full_adder(c, a, b): # Carry x Sum
Qint4,
Qint5,
Qint6,
Qint7,
Qint8,
Qint12,
Qint16,
Qchar,
Qlist,
Qmatrix,
]
Expand Down
41 changes: 41 additions & 0 deletions qlasskit/types/qchar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2024 Davide Gessa

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .qint import Qint7
from .qtype import TExp


class Qchar(Qint7):
BIT_SIZE = 7

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

@classmethod
def comparable(cls, other_type=None) -> bool:
"""Return true if the type is comparable with itself or
with [other_type]"""
if not other_type or issubclass(other_type, Qchar):
return True
return False

@classmethod
def const(cls, v: str) -> TExp:
"""Return the list of bool representing a char"""
assert len(v) == 1
cval = list(map(lambda c: True if c == "1" else False, bin(ord(v[0]))[2:]))[
::-1
]
return cls.fill((cls, cval))
4 changes: 4 additions & 0 deletions qlasskit/types/qint.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ class Qint6(Qint):
BIT_SIZE = 6


class Qint7(Qint):
BIT_SIZE = 7


class Qint8(Qint):
BIT_SIZE = 8

Expand Down

0 comments on commit 162927d

Please sign in to comment.