adder

forest.benchmarking.classical_logic.adder(num_a: Sequence[int], num_b: Sequence[int], register_a: Sequence[int], register_b: Sequence[int], carry_ancilla: int, z_ancilla: int, in_x_basis: bool = False, use_param_program: bool = False) → pyquil.quil.Program

Produces a program implementing reversible adding on a quantum computer to compute a + b.

This implementation is based on [CDKM96], which is easy to implement, if not the most efficient. Each register of qubit labels should be provided such that the first qubit in each register is expected to carry the least significant bit of the respective number. This method also requires two extra ancilla, one initialized to 0 that acts as a dummy initial carry bit and another (which also probably ought be initialized to 0) that stores the most significant bit of the addition (should there be a final carry). The most straightforward ordering of the registers and two ancilla for adding n-bit numbers follows the pattern:

carry_ancilla
b_0
a_0
...
b_j
a_j
...
b_n
a_n
z_ancilla

With this layout, all gates in the circuit act on sets of three adjacent qubits. Such a layout is provided by calling get_qubit_registers_for_adder on the quantum resource. Note that even with this layout some of the gates used to implement the circuit may not be native. In particular there are CCNOT gates which must be decomposed and CNOT(q1, q3) gates acting on potentially non-adjacenct qubits (the layout only ensures q2 is adjacent to both q1 and q3).

The output of the circuit falls on the qubits initially labeled by the b bits (and z_ancilla).

The default option is to compute the addition in the computational (aka Z) basis. By setting in_x_basis true, the gates primitives.CNOT_X_basis() and primitives.CCNOT_X_basis() will replace CNOT and CCNOT so that the computation happens in the X basis.

[CDKM96]“A new quantum ripple-carry addition circuit” S. Cuccaro, T. Draper, s. Kutin, D. Moulton https://arxiv.org/abs/quant-ph/0410184
Parameters:
  • num_a – the bitstring representation of the number a with least significant bit last
  • num_b – the bitstring representation of the number b with least significant bit last
  • register_a – list of qubit labels for register a, with least significant bit labeled first
  • register_b – list of qubit labels for register b, with least significant bit labeled first
  • carry_ancilla – qubit labeling a zero-initialized qubit, ideally adjacent to b_0
  • z_ancilla – qubit label, a zero-initialized qubit, ideally adjacent to register_a[-1]
  • in_x_basis – if true, prepare the bitstring-representation of the numbers in the x basis and subsequently performs all addition logic in the x basis.
  • use_param_program – if true, the input num_a and num_b should be arrays of the proper length, but their contents will be disregarded. Instead, the program returned will be parameterized and the input bitstrings to add must be specified at run time.
Returns:

pyQuil program that implements the addition a+b, with output falling on the qubits formerly storing the input b. The output of a measurement will list the lsb as the last bit.