From 08c8018bc3a544b57f5cbc310144c6f089e5fedf Mon Sep 17 00:00:00 2001 From: diehoq Date: Mon, 10 Jun 2024 15:39:55 +0000 Subject: [PATCH] Implement custom modular adder --- .../modular_qft_addition.py | 47 ++++++++++++++++++- src/qrisp/qtypes/quantum_modulus.py | 20 ++++---- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/qrisp/arithmetic/modular_arithmetic/modular_qft_addition.py b/src/qrisp/arithmetic/modular_arithmetic/modular_qft_addition.py index 50733688..265da4e2 100644 --- a/src/qrisp/arithmetic/modular_arithmetic/modular_qft_addition.py +++ b/src/qrisp/arithmetic/modular_arithmetic/modular_qft_addition.py @@ -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 @@ -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) @@ -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() \ No newline at end of file diff --git a/src/qrisp/qtypes/quantum_modulus.py b/src/qrisp/qtypes/quantum_modulus.py index be0684e6..f8a560b5 100644 --- a/src/qrisp/qtypes/quantum_modulus.py +++ b/src/qrisp/qtypes/quantum_modulus.py @@ -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 @@ -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) @@ -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 @@ -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 @@ -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