qrisp.cuccaro_adder#
- cuccaro_adder(a: int | str | BigInteger | QuantumRegister, b: QuantumRegister, c_in: OptionalQubit | None = None, c_out: OptionalQubit | None = None, ctrl: OptionalQubit | None = None) None[source]#
In-place adder as introduced in https://arxiv.org/abs/quant-ph/0410184
This function works in both static and dynamic modes.
The two inputs may both be quantum, or one may be quantum and the other classical. All supported quantum types (e.g. QuantumFloat, QuantumBool, QuantumModulus, …) can be used as quantum inputs, along with lists of Qubits and
DynamicQubitArrayobjects.A
DynamicQubitArrayis simply an array of qubits that is created automatically in Jasp mode; you never need to construct one yourself.A quantum input can also be a list of bare Qubits. Bare Qubits are obtained by indexing a quantum variable, e.g.
qv[0]yields a single Qubit andqv[0:2]yields a list of Qubits; a bare Qubit may likewise be passed directly as the carry or control argument.
The table below lists the accepted input forms and the behaviour in the special cases.
Input
Behaviour
Quantum first input
aAny quantum type,
list[Qubit]orDynamicQubitArray. It is adjusted to the size ofbbefore the addition.Quantum second input
bMust be a quantum register (
QuantumVariable,list[Qubit]orDynamicQubitArray); it holds the result in place and its size sets the modulus of the addition.Classical first input
An
int, a little-endian binarystr, or aBigInteger(dynamic mode only). It is encoded into a temporary quantum variable of the same size asband uncomputed afterwards, so no ancilla is left over. The second input cannot be classical, since the addition is performed in place on it.Classical addend larger than
bTruncated modulo
2**len(b)before it is encoded.Quantum
alarger thanbais truncated by slicing off its high-order qubits, so the addition is performed modulo2**len(b). No qubits are added or removed andakeeps its value and size.Quantum
asmaller thanbais padded with ancilla qubits in the|0>state so it can be processed at the size ofb. The ancillas are deleted once the addition is done, andaitself is not modified.QuantumBoolA single-qubit register, so the addition wraps around modulo 2.
QuantumModulusWorks as long as the sum stays below the modulus.
c_inandc_outOptional carry-in and carry-out; each accepts a
QuantumBoolor a bareQubit.c_inadds an extra 1 to the sum, andc_outrecords the overflow.ctrlOptional control qubit; accepts a
QuantumBoolor a bareQubit. The addition is only applied when the control is in the|1>state. Equivalently, the call can be wrapped in acontrol()environment.Dynamic mode
Inside a
jaspify()function the quantum inputs may beDynamicQubitArrayobjects such asa.regor a full slicea[:]. The result is read out withmeasure.The custom control implementation is based on Theorem 2.12 of https://arxiv.org/abs/2407.20167
Note
If the first input is quantum and the second classical, the function cannot work as addition is performed “in-place” on the second input.
- Parameters:
- aint, str, BigInteger, QuantumVariable, list[Qubit] or DynamicQubitArray
The value that should be added. A
stris interpreted as a little-endian binary string ("10"means bit 0 is 1, i.e. the value 1). ABigIntegeris only supported in dynamic (tracing) mode.- bQuantumVariable or list[Qubit] or DynamicQubitArray
The value that should be modified in the in-place addition.
- c_inQuantumBool or Qubit, optional
An optional carry in value. The default is None.
- c_outQuantumBool or Qubit, optional
An optional carry out value. The default is None.
- ctrlQuantumBool or Qubit, optional
An optional control qubit. If provided, the addition is only applied when the control qubit is in the
|1>state. The default is None.
- Returns:
- None
The function modifies the second input in place.
- Raises:
- AttributeError
If
ctrlis not a QuantumBool or Qubit. Thecustom_controldecorator inspects the control qubit before the function body runs.- TypeError
If carry in or carry out is not of type QuantumBool or Qubit in static mode.
- TypeError
If the first argument is classical but not an integer, binary string or BigInteger in static mode.
- ValueError
If the first argument is a
strthat is not a valid binary string.- ValueError
If the second argument is not a quantum register, i.e. if
bis not a QuantumVariable, DynamicQubitArray or a non-emptylist[Qubit].- ValueError
If the first argument is a
listthat does not contain only Qubits.- ValueError
If
aorbis aQuantumCharorQuantumString, which are not supported numeric register types.
Examples
The examples below use
QuantumFloatinputs; any other supported quantum type works the same way. In static mode,printalready simulates and shows the outcome:>>> from qrisp import QuantumFloat, cuccaro_adder >>> a = QuantumFloat(4) >>> b = QuantumFloat(4) >>> a[:] = 4 >>> b[:] = 5 >>> cuccaro_adder(a, b) >>> print(b) {9: 1.0}
In dynamic mode, the call is wrapped in a
jaspify()function and the result is collapsed withmeasurebefore it is returned:>>> from qrisp import QuantumFloat, cuccaro_adder, measure >>> from qrisp.jasp import jaspify >>> @jaspify ... def main(): ... a = QuantumFloat(4) ... b = QuantumFloat(4) ... a[:] = 4 ... b[:] = 5 ... cuccaro_adder(a, b) ... return measure(b) >>> main() Array(9., dtype=float64)