V. Vedral, A. Barenco, A. Ekert, "Quantum Networks for Elementary Arithmetic Operations", Phys. Rev. A 54, 147 (1996).
arXiv:quant-ph/9511018
The VBE adder is one of the classic implementations of quantum arithmetic. It uses a ripple-carry design to perform the addition of two
-
In-place: The result of the sum
$A + B$ is stored in register$B$ itself. -
Reversible: Register
$A$ is preserved and all ancillas (auxiliary qubits) are cleaned (restored to$|0\rangle$ ) at the end. -
Qubit Efficiency: Uses
$3n + 1$ qubits to add two$n$ -bit numbers (including the carry-out bit).
The implementation strictly follows the original design, validated against visualizations in the Quirk simulator.
-
Forward CARRY Pass (Propagation): Applies
$n$ CARRYgates sequentially from position$0$ to$n-1$ .- Qubits:
(c[i], a[i], b[i], c[i+1]). - Result: Qubit
c[n]stores the final carry-out.
- Qubits:
-
MSB (Most Significant Bit) Sum Step:
-
CX(a[n-1], b[n-1]): Prepares the most significant sum bit. -
SUM(c[n-1], a[n-1], b[n-1]): Finalizes the sum for bit$n-1$ . Note: The CARRY(n-1) gate is NOT inverted to preserve the carry-out in c[n].
-
-
Backward CARRY† + SUM Pass (Uncompute & Finalization): For each position
$i$ from$n-2$ down to$0$ :-
CARRY†(c[i], a[i], b[i], c[i+1]): Reverses carry propagation to clear the$c[i+1]$ ancilla. -
SUM(c[i], a[i], b[i]): Calculates the final sum bit for position$i$ .
-
-
b[0..n-1]: Contains the lower$n$ bits of the sum. -
c[n]: Contains the carry-out bit (MSB of the sum). -
a[0..n-1]: Keeps the original value (read-only). -
c[0..n-1]: Restored to$|0\rangle$ .
- Main Module:
src/algorithms/vbe_adder.py - Test Suite:
tests/test_vbe_adder.py- Validates 55 test cases, including overflow sums, zero, and the original example from the paper.
The module src.algorithms.vbe_adder provides both high-level convenience functions and low-level circuit builders.
The easiest way to use the adder. It handles bit encoding, circuit building, simulation, and decoding.
-
Parameters:
-
a,b: Non-negative integers to add. -
n(Optional): Bit-width of the registers. Defaults to the minimum required size.
-
-
Returns: The integer result of
$a + b$ .
Constructs the full ripple-carry adder circuit.
-
Parameters:
-
n: Number of bits for the input registers. -
a_bits,b_bits(Optional): Lists of 0s and 1s to initialize the$A$ and$B$ registers.
-
-
Returns: A
qiskit.QuantumCircuitobject containing the adder logic and measurements.
Executes the circuit on the AerSimulator.
- Returns: A dictionary of measurement counts (e.g.,
{'1111': 1024}).
Translates the raw bitstring counts from the simulator into a decimal integer.
-
Note: It correctly interprets the Qiskit MSB-first bitstring convention for the
$n+1$ result bits.
from src.algorithms.vbe_adder import add
# High-level API: Add 9 + 6
result = add(9, 6)
print(f"Result: {result}") # Output: 15
# Low-level API: Custom circuit building
from src.algorithms.vbe_adder import create_vbe_adder_circuit, run_simulation, decode_result
# 1. Build a 4-bit adder (9 + 6)
qc = create_vbe_adder_circuit(4, a_bits=[1,0,0,1], b_bits=[0,1,1,0])
# 2. Run simulation
counts = run_simulation(qc)
value, _ = decode_result(counts, 4)
print(f"Decoded: {value}")