-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qchar type
- Loading branch information
Showing
9 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# 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 typing import Any, List | ||
|
||
from sympy.logic import And, Or, false, true | ||
|
||
from . import _eq, _neq | ||
from .qint import Qint | ||
from .qtype import Qtype, TExp | ||
|
||
|
||
class Qchar(str, Qtype): | ||
BIT_SIZE = 8 | ||
|
||
def __init__(self, value: str): | ||
super().__init__() | ||
assert len(value) == 1 | ||
self.value = value[0] | ||
|
||
def to_bin(self) -> str: | ||
s = bin(ord(self.value))[2:][0 : self.BIT_SIZE] | ||
return ("0" * (self.BIT_SIZE - len(s)) + s)[::-1] | ||
|
||
def to_amplitudes(self) -> List[float]: | ||
ampl = [0.0] * 2**self.BIT_SIZE | ||
ampl[ord(self.value)] = 1 | ||
return ampl | ||
|
||
@classmethod | ||
def from_bool(cls, v: List[bool]): | ||
bin_str = "".join(map(lambda x: "1" if x else "0", v)) | ||
return cls(chr(int(bin_str[::-1], 2))) | ||
|
||
@classmethod | ||
def comparable(cls, other_type=None) -> bool: | ||
return other_type == cls or issubclass(other_type, Qint) | ||
|
||
@classmethod | ||
def fill(cls, v: TExp) -> TExp: | ||
if len(v[1]) < cls.BIT_SIZE: # type: ignore | ||
v = ( | ||
cls, | ||
v[1] + (cls.BIT_SIZE - len(v[1])) * [False], # type: ignore | ||
) | ||
return v | ||
|
||
@classmethod | ||
def const(cls, value: Any) -> TExp: | ||
assert len(value) == 1 | ||
cval = list( | ||
map(lambda c: True if c == "1" else False, bin(ord(value))[2:][::-1]) | ||
) # [::-1] | ||
return cls.fill((cls, cval)) | ||
|
||
# Comparators | ||
|
||
@staticmethod | ||
def eq(tleft: TExp, tcomp: TExp) -> TExp: | ||
ex = true | ||
for x in zip(tleft[1], tcomp[1]): | ||
ex = And(ex, _eq(x[0], x[1])) | ||
|
||
return (bool, ex) | ||
|
||
@staticmethod | ||
def neq(tleft: TExp, tcomp: TExp) -> TExp: | ||
ex = false | ||
for x in zip(tleft[1], tcomp[1]): | ||
ex = Or(ex, _neq(x[0], x[1])) | ||
|
||
return (bool, ex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,10 @@ class Qint6(Qint): | |
BIT_SIZE = 6 | ||
|
||
|
||
class Qint7(Qint): | ||
BIT_SIZE = 7 | ||
|
||
|
||
class Qint8(Qint): | ||
BIT_SIZE = 8 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2023 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. | ||
|
||
import unittest | ||
|
||
from parameterized import parameterized_class | ||
from sympy import Symbol | ||
from sympy.logic import And, Not | ||
|
||
from qlasskit import Qchar, qlassf | ||
|
||
from ..utils import COMPILATION_ENABLED, ENABLED_COMPILERS, compute_and_compare_results | ||
|
||
|
||
@parameterized_class(("compiler"), ENABLED_COMPILERS) | ||
class TestQchar(unittest.TestCase): | ||
def test_qchar_to_bin_and_from_bool(self): | ||
c = Qchar("a").to_bin() | ||
self.assertEqual(c, "01100001"[::-1]) | ||
self.assertEqual(c, Qchar("a").export("binary")) | ||
self.assertEqual( | ||
Qchar.from_bool( | ||
[False, True, True, False, False, False, False, True][::-1] | ||
), | ||
"a", | ||
) | ||
|
||
def test_char_arg_eq(self): | ||
f = "def test(a: Qchar) -> bool:\n\treturn a == 'a'" | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
self.assertEqual(len(qf.expressions), 1) | ||
self.assertEqual(qf.expressions[0][0], Symbol("_ret")) | ||
a = [Symbol(f"a.{i}") for i in range(8)] | ||
self.assertEqual( | ||
qf.expressions[0][1], | ||
And( | ||
a[0], a[5], a[6], Not(a[1]), Not(a[2]), Not(a[3]), Not(a[4]), Not(a[7]) | ||
), | ||
) | ||
compute_and_compare_results(self, qf) | ||
|
||
def test_char_arg_neq(self): | ||
f = "def test(a: Qchar) -> bool:\n\treturn a != 'a'" | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
self.assertEqual(len(qf.expressions), 1) | ||
self.assertEqual(qf.expressions[0][0], Symbol("_ret")) | ||
a = [Symbol(f"a.{i}") for i in range(8)] | ||
self.assertEqual( | ||
qf.expressions[0][1], | ||
Not( | ||
And( | ||
a[0], | ||
a[5], | ||
a[6], | ||
Not(a[1]), | ||
Not(a[2]), | ||
Not(a[3]), | ||
Not(a[4]), | ||
Not(a[7]), | ||
) | ||
), | ||
) | ||
compute_and_compare_results(self, qf) | ||
|
||
def test_char_return(self): | ||
f = "def test(a: Qchar) -> Qchar:\n\treturn 'z' if a == 'a' else 'a'" | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
compute_and_compare_results(self, qf) | ||
|
||
def test_char_return2(self): | ||
f = "def test(a: Qchar) -> Qchar:\n\treturn a" | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
compute_and_compare_results(self, qf) | ||
|
||
def test_char_return_tuple(self): | ||
f = ( | ||
"def test(a: Qchar) -> Tuple[Qchar, bool]:\n" | ||
"\tb = a == 'z'\n\treturn (a, b)" | ||
) | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
self.assertEqual(len(qf.expressions), 9) | ||
compute_and_compare_results(self, qf) | ||
|
||
def test_char_ord(self): | ||
f = "def test(a: Qchar) -> bool:\n\treturn ord(a) == 97" | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
compute_and_compare_results(self, qf) | ||
|
||
def test_char_chr(self): | ||
f = "def test(a: Qchar) -> bool:\n\treturn a == chr(97)" | ||
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler) | ||
compute_and_compare_results(self, qf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters