Skip to content

Commit

Permalink
subtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 20, 2023
1 parent adec21c commit e81367c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
- [x] Int arithmetic: +
- [x] Qtype: bitwise not
- [x] Qtype: shift right / left
- [ ] Int: subtraction
- [x] Int: subtraction
- [ ] Publish doc on github

## Month 2:
Expand Down
30 changes: 30 additions & 0 deletions docs/source/supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ Comparators
a > b or b <= c
Unary Op
^^^^^^^^^

.. code-block:: python
~a
Bin Op
^^^^^^^^^

.. code-block:: python
a << 1
.. code-block:: python
a >> 2
.. code-block:: python
a + b
.. code-block:: python
a - b
Function call
^^^^^^^^^^^^^

Expand Down
22 changes: 13 additions & 9 deletions examples/ex4.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from qiskit import QuantumCircuit

from qlasskit import Qint4, qlassf
from qlasskit import Qint2, qlassf


@qlassf
def f1(n: Qint4) -> Qint4:
return n + 1
def f1(n: Qint2, q: Qint2) -> Qint2:
return n + q


@qlassf
def f2(n: Qint4) -> Qint4:
return n + 3
def f2(n: Qint2, z: Qint2) -> Qint2:
return n + z


@qlassf
def f_comp(n: Qint4) -> Qint4:
return n + 1 + 3
def f_comp(n: Qint2, q: Qint2, z: Qint2) -> Qint2:
return n + q + z


print(f_comp.expressions)
Expand All @@ -25,9 +25,13 @@ def f_comp(n: Qint4) -> Qint4:
print(qc.decompose().draw("text"))


print(f1.expressions, f2.expressions)
gate1 = f1.gate()
gate2 = f2.gate()
qc = QuantumCircuit(max(gate1.num_qubits, gate2.num_qubits))
qc = QuantumCircuit(-2 + gate1.num_qubits + gate2.num_qubits)
qc.append(gate1, list(range(gate1.num_qubits)))
qc.append(gate2, list(range(gate2.num_qubits)))
qc.barrier()
qc.append(
gate2, list(range(gate1.num_qubits - 2, gate1.num_qubits + gate2.num_qubits - 2))
)
print(qc.decompose().draw("text"))
2 changes: 2 additions & 0 deletions qlasskit/ast2logic/t_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ def unfold(v_exps, op):

if isinstance(expr.op, ast.Add) and hasattr(tleft[0], "add"):
return tleft[0].add(tleft, tright)
elif isinstance(expr.op, ast.Sub) and hasattr(tleft[0], "sub"):
return tleft[0].sub(tleft, tright)
elif (
isinstance(expr.op, ast.LShift)
and hasattr(tleft[0], "shift_left")
Expand Down
10 changes: 10 additions & 0 deletions qlasskit/types/qint.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __init__(self, value):
def __add__(self, b):
return (self.value + b) % 2**self.BIT_SIZE

def __sub__(self, b):
return (self.value - b) % 2**self.BIT_SIZE

@classmethod
def from_bool(cls, v: List[bool]):
return cls(int("".join(map(lambda x: "1" if x else "0", v[::-1])), 2))
Expand Down Expand Up @@ -161,6 +164,13 @@ def add(cls, tleft: TExp, tright: TExp) -> TExp:

return (cls if cls.BIT_SIZE > tleft[0].BIT_SIZE else tleft[0], sums) # type: ignore

@classmethod
def sub(cls, tleft: TExp, tright: TExp) -> TExp:
"""Subtract two Qint"""
an = cls.bitwise_not(cls.fill(tleft)) # type: ignore
su = cls.add(an, cls.fill(tright)) # type: ignore
return cls.bitwise_not(su) # type: ignore


class Qint2(Qint):
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 @@ -119,3 +119,7 @@ def shift_left(v: TExp, i: int = 1) -> TExp:
@staticmethod
def add(tleft: TExp, tcomp: TExp) -> TExp:
raise Exception("abstract")

@staticmethod
def sub(tleft: TExp, tcomp: TExp) -> TExp:
raise Exception("abstract")
17 changes: 17 additions & 0 deletions test/test_qlassf_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,20 @@ def test_add_const4(self):
f = "def test(a: Qint2) -> Qint2: return a + 2"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)


class TestQlassfIntSub(unittest.TestCase):
def test_sub_const(self):
f = "def test(a: Qint2) -> Qint2: return a - 1"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

def test_sub_const2(self):
f = "def test(a: Qint2) -> Qint2: return a - 3"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

def test_sub_const3(self):
f = "def test(a: Qint4) -> Qint4: return a - 8"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

0 comments on commit e81367c

Please sign in to comment.