Source code for qrisp.alg_primitives.arithmetic.adders.fourier_adder
"""\********************************************************************************* Copyright (c) 2025 the Qrisp authors** This program and the accompanying materials are made available under the* terms of the Eclipse Public License 2.0 which is available at* http://www.eclipse.org/legal/epl-2.0.** This Source Code may also be made available under the following Secondary* Licenses when the conditions for such availability set forth in the Eclipse* Public License, v. 2.0 are satisfied: GNU General Public License, version 2* with the GNU Classpath Exception which is* available at https://www.gnu.org/software/classpath/license.html.** SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0********************************************************************************/"""importnumpyasnpfromqrisp.alg_primitivesimportQFTfromqrisp.core.gate_application_functionsimportp,czfromqrisp.environmentsimportQuantumEnvironment,conjugatefromqrisp.circuitimportOperation,PGate,QuantumCircuit
[docs]deffourier_adder(a,b,perform_QFT=True):""" In-place adder function based on `this paper <https://arxiv.org/abs/quant-ph/0410184>`__ Performs the addition :: b += a Parameters ---------- a : int or QuantumVariable or list[Qubit] The value that should be added. b : QuantumVariable or list[Qubit] The value that should be modified in the in-place addition. perform_QFT : bool, optional If set to ``False``, no QFT is executed. The default is ``True``. Examples -------- We add two integers: >>> from qrisp import QuantumFloat, fourier_adder >>> a = QuantumFloat(4) >>> b = QuantumFloat(4) >>> a[:] = 4 >>> b[:] = 5 >>> fourier_adder(a,b) >>> print(b) {9: 1.0} """ifperform_QFT:env=conjugate(QFT)(b,exec_swap=False)else:env=QuantumEnvironment()withenv:b=list(b)b=b[::-1]ifisinstance(a,int):foriinrange(len(b)):p(a*np.pi*2**(1+i-len(b)),b[i])else:iflen(a)>len(b):raiseException("Tried to add QuantumFloat of higher precision onto QuantumFloat of lower precision")phase_correction_a=np.zeros(len(a))phase_correction_b=np.zeros(len(b))forjinrange(len(a)):foriinrange(len(b)):if1+j+i-len(b)>=1:continueif1+j+i-len(b)==0:cz(a[j],b[i])else:b[i].qs().append(QuasiRZZ(-np.pi*2**(1+j+i-len(b))/2),[a[j],b[i]])phase_correction_a[j]+=np.pi*2**(1+j+i-len(b))/2phase_correction_b[i]+=np.pi*2**(1+j+i-len(b))/2foriinrange(len(b)):ifphase_correction_b[i]%(2*np.pi)!=0:p(phase_correction_b[i],b[i])foriinrange(len(a)):ifphase_correction_a[i]%(2*np.pi)!=0:p(phase_correction_a[i],a[i])