Skip to content

Commit

Permalink
Merge pull request #56 from diehoq/custom-mod-adder
Browse files Browse the repository at this point in the history
Implement custom modular adder
  • Loading branch information
positr0nium authored Jun 11, 2024
2 parents c782e18 + 08c8018 commit 3bf1f24
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
47 changes: 45 additions & 2 deletions src/qrisp/arithmetic/modular_arithmetic/modular_qft_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qrisp.qtypes.quantum_bool import QuantumBool
from qrisp.arithmetic import multi_controlled_U_g, hybrid_mult, U_g
from qrisp.core.library import QFT, h, cx, swap
from qrisp.environments import conjugate, control, invert
from qrisp.environments import conjugate, control, invert, custom_control
from qrisp.circuit import Operation
from qrisp.arithmetic.modular_arithmetic.mod_tools import modinv, montgomery_decoder, montgomery_encoder

Expand All @@ -49,7 +49,7 @@ def montgomery_addition(a, b):
def beauregard_adder(a, b, modulus):

if modulus > 2**a.size:
raise Exception("Tried to perform modular addition on QuantumFloat with to few qubits")
raise Exception("Tried to perform modular addition on QuantumFloat with too few qubits")
if modulus == 2**a.size:
with conjugate(QFT)(a, exec_swap = False):
qft_basis_adder(b, a)
Expand Down Expand Up @@ -95,4 +95,47 @@ def beauregard_adder(a, b, modulus):
sign.delete()
reduction_not_necessary.delete()

@custom_control
def mod_adder(a, b, inpl_adder, modulus, ctrl = None):

reduction_not_necessary = QuantumBool()
sign = QuantumBool()


if isinstance(a, int):
a = a%modulus

b = list(b) + [sign[0]]

if ctrl is None:
inpl_adder(a, b)
else:
with control(ctrl):
inpl_adder(a, b)

with invert():
inpl_adder(modulus, b)

cx(sign, reduction_not_necessary)

with control(reduction_not_necessary):
inpl_adder(modulus, b)

with invert():
if ctrl is None:
inpl_adder(a, b)
else:
with control(ctrl):
inpl_adder(a, b)

cx(sign, reduction_not_necessary)
reduction_not_necessary.flip()

if ctrl is None:
inpl_adder(a, b)
else:
with control(ctrl):
inpl_adder(a, b)

sign.delete()
reduction_not_necessary.delete()
20 changes: 10 additions & 10 deletions src/qrisp/qtypes/quantum_modulus.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ def __add__(self, other):
raise Exception("Tried to add a QuantumFloat and QuantumModulus with non-zero Montgomery shift")


from qrisp.arithmetic.modular_arithmetic import beauregard_adder
from qrisp.arithmetic.modular_arithmetic import mod_adder

res = self.duplicate(init = True)

beauregard_adder(res, other, self.modulus)
mod_adder(other, res, self.inpl_adder, self.modulus)

return res

Expand All @@ -258,9 +258,9 @@ def __iadd__(self, other):
if self.m != 0:
raise Exception("Tried to add a QuantumFloat and QuantumModulus with non-zero Montgomery shift")

from qrisp.arithmetic.modular_arithmetic import beauregard_adder
from qrisp.arithmetic.modular_arithmetic import mod_adder

beauregard_adder(self, other, self.modulus)
mod_adder(other, self, self.inpl_adder, self.modulus)
return self

@gate_wrap(permeability="args", is_qfree=True)
Expand All @@ -275,11 +275,11 @@ def __sub__(self, other):
raise Exception("Tried to subtract a QuantumFloat and QuantumModulus with non-zero Montgomery shift")


from qrisp.arithmetic.modular_arithmetic import beauregard_adder
from qrisp.arithmetic.modular_arithmetic import mod_adder
res = self.duplicate(init = True)

with invert():
beauregard_adder(res, other, self.modulus)
mod_adder(other, res, self.inpl_adder, self.modulus)

return res

Expand All @@ -294,12 +294,12 @@ def __rsub__(self, other):
if self.m != 0:
raise Exception("Tried to subtract a QuantumFloat and QuantumModulus with non-zero Montgomery shift")

from qrisp.arithmetic.modular_arithmetic import beauregard_adder
from qrisp.arithmetic.modular_arithmetic import mod_adder
res = self.duplicate()

res -= self

beauregard_adder(res, other, self.modulus)
mod_adder(other, res, self.inpl_adder, self.modulus)

return res

Expand All @@ -314,9 +314,9 @@ def __isub__(self, other):
if self.m != 0:
raise Exception("Tried to subtract a QuantumFloat and QuantumModulus with non-zero Montgomery shift")

from qrisp.arithmetic.modular_arithmetic import beauregard_adder
from qrisp.arithmetic.modular_arithmetic import mod_adder
with invert():
beauregard_adder(self, other, self.modulus)
mod_adder(other, self, self.inpl_adder, self.modulus)

return self

Expand Down

0 comments on commit 3bf1f24

Please sign in to comment.