add_ints#
View source on GitHub
|
Performs addition modulo \(2^\mathrm{num\_bits}\) of (un)signed in a reversible way.
qualtran.simulation.classical_sim.add_ints(
a: int, b: int, *, num_bits: Optional[int] = None, is_signed: bool = False
) -> int
Addition of signed integers can result in an overflow. In most classical programming languages (e.g. C++) what happens when an overflow happens is left as an implementation detail for compiler designers. However, for quantum subtraction, the operation should be unitary and that means that the unitary of the bloq should be a permutation matrix.
If we hold a constant then the valid range of values of \(b \in [-2^{\mathrm{num\_bits}-1}, 2^{\mathrm{num\_bits}-1})\)
gets shifted forward or backward by a. To keep the operation unitary overflowing values wrap around. This is the same
as moving the range \(2^\mathrm{num\_bits}\) by the same amount modulo \(2^\mathrm{num\_bits}\). That is add
\(2^{\mathrm{num\_bits}-1})\) before addition modulo and then remove it.
Args
aleft operand of addition.
bright operand of addition.
num_bitsoptional num_bits. When specified addition is done in the interval [0, 2num_bits) or [-2(num_bits-1), 2**(num_bits-1)) based on the value of
is_signed.is_signedboolean whether the numbers are unsigned or signed ints. This value is only used when
num_bitsis provided.
View source on GitHub