Skip to content

Commit

Permalink
qfixed.add
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Mar 16, 2024
1 parent 99fb9cf commit 058d68d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
1 change: 1 addition & 0 deletions qlasskit/qlassfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .qcircuit import QCircuitWrapper
from .types import * # noqa: F403, F401
from .types import Qtype, format_outcome, interpret_as_qtype, type_repr
from .types.qfixed import * # noqa: F403, F401

MAX_TRUTH_TABLE_SIZE = 20

Expand Down
9 changes: 4 additions & 5 deletions qlasskit/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ def const_to_qtype(value: Any) -> TExp:

elif isinstance(value, float):
for det_type in QFIXED_TYPES: # type: ignore
v_s = str(value).split(".")

# TODO: check also for the fractional part
if int(v_s[0]) < 2**det_type.BIT_SIZE_INTEGER: # type: ignore
return det_type.const(value) # type: ignore
v = det_type.const(value) # type: ignore
c_val = det_type.from_bool(v[1])
if c_val > value - 0.05 and c_val < value + 0.05:
return v

raise Exception(f"Unable to infer type of constant: {value}")

Expand Down
6 changes: 3 additions & 3 deletions qlasskit/types/qfixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def from_bool(cls, v: List[bool]):
return cls(integer_value + fractional_value)

def to_bool(self) -> List[bool]:
integer_part = bin_to_bool_list(bin(int(self.value)), self.BIT_SIZE_INTEGER)[
::-1
]
integer_part = bin_to_bool_list(
bin(int(self.value))[::-1], self.BIT_SIZE_INTEGER
)

fractional_part = []
c_val = self.value
Expand Down
36 changes: 22 additions & 14 deletions test/qlassf/test_fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TestQfixedEncoding(unittest.TestCase):
(Qfixed1_3, "1000", 1.0),
(Qfixed2_3, "01000", 2.0),
(Qfixed2_3, "01100", 2.5),
# (Qfixed2_3, "00000", 4.0),
]
)
def test_fixed_from_bool_and_to_bin(self, qft, bin_v, val):
Expand All @@ -58,6 +59,8 @@ def test_fixed_gt(self, qft, a, b, r):
[Qfixed2_3, 0.5, 0.5],
[Qfixed2_3, 0.75, 0.75],
[Qfixed2_3, 1.0, 0.75],
[Qfixed2_3, 1.0, 0.5],
[Qfixed2_3, 3.5, 0.5],
]
)
def test_fixed_add(self, qft, a, b):
Expand All @@ -76,10 +79,10 @@ def test_fixed_const(self):
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

# def test_equal_const(self):
# f = "def test(a: Qfixed[1,4]) -> bool:\n\treturn a == 0.1"
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
# compute_and_compare_results(self, qf)
def test_equal_const(self):
f = "def test(a: Qfixed[2,4]) -> bool:\n\treturn a == Qfixed2_4(0.5)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_equal(self):
f = "def test(a: Qfixed[1,4], b: Qfixed[1,4]) -> bool:\n\treturn a == b"
Expand All @@ -96,6 +99,11 @@ def test_gt(self):
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_gt_const(self):
f = "def test(a: Qfixed[1,4], b: Qfixed[1,4]) -> bool:\n\treturn a > Qfixed1_4(0.5)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_lt(self):
f = "def test(a: Qfixed[1,4], b: Qfixed[1,4]) -> bool:\n\treturn a < b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
Expand All @@ -111,6 +119,16 @@ def test_gte(self):
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_sum_const(self):
f = "def test(a: Qfixed[2,4]) -> Qfixed[2, 4]:\n\treturn Qfixed2_4(0.5) + a"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_sum(self):
f = "def test(a: Qfixed[1,4], b: Qfixed[1,4]) -> Qfixed[1,4]:\n\treturn a + b"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

# def test_to_int(self):
# f = "def test(a: Qfixed[2,4]) -> Qint2:\n\treturn int(a)"
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
Expand All @@ -120,13 +138,3 @@ def test_gte(self):
# f = "def test(a: Qint2) -> Qfixed[2,4]:\n\treturn float(a)"
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
# compute_and_compare_results(self, qf)

# def test_sum_const(self):
# f = "def test(a: Qfixed[1,4]) -> Qfixed[1, 3]:\n\treturn 0.1 + a"
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
# compute_and_compare_results(self, qf)

# def test_sum(self):
# f = "def test(a: Qfixed[1,4], b: Qfixed[1,4]) -> Qfixed[1,4]:\n\treturn a + b"
# qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
# compute_and_compare_results(self, qf)
13 changes: 10 additions & 3 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,16 @@ def res_to_str(res):
return "1" if res else "0"
elif type(res) is tuple or type(res) is list:
return "".join([res_to_str(x) for x in res])
elif type(res) is int or type(res) is str or type(res) is float:
elif type(res) is int or type(res) is str:
qc = const_to_qtype(res)
try:
qi = qf.returns.ttype.from_bool(qc[1])
except:
qi = qc[0].from_bool(qc[1])
return qi.to_bin()
return qi.to_bin()
elif type(res) is float:
qi = qf.returns.ttype(res)
return qi.to_bin()
else:
return res.to_bin()

Expand All @@ -188,7 +191,11 @@ def res_to_str(res):

res_original = qf.original_f(*args)
res_original_str = res_to_str(res_original)
# print('\n\n', args, res_original, res_original_str, truth_line)

# truth_str = "".join(
# map(lambda x: "1" if x else "0", truth_line[-qf.output_size :])
# )
# print('\n\n', args, res_original, res_original_str, truth_str)
# print (qf.expressions)

cls.assertEqual(len(res_original_str), qf.output_size)
Expand Down

0 comments on commit 058d68d

Please sign in to comment.