Skip to content

Addition

The "Addition" operation, denoted '+', is implemented according to the following truth table. Here \(a\) and \(b\) denote numbers, \(i\) bit index, and \(c_{in / out}\) the incoming and outgoing carries of that step, respectively.

\(a_i\) \(b_i\) \(c_{in}\) \({(a + b)}_i\) \(c_{out}\)
0 0 0 0 0
0 1 0 1 0
1 0 0 1 0
1 1 0 0 1
0 0 1 1 0
0 1 1 0 1
1 0 1 0 1
1 1 1 1 1

Note that integer and fixed-point numbers are represented in a 2-complement method during function evaluation. The binary number is extended in the case of a register size miss-match. For example, the positive signed number \((110)_2=6\) is expressed as \((00110)_2\) when operating with a 5-qubit register. Similarly, the negative signed number \((110)_2=-2\) is expressed as \((11110)_2\).

Examples

  • 5 + 3 = 8 , 0101 + 0011 = 1000
  • 5 + -3 = 2, 0101 + 1101 = 0010
  • -5 + -3 = -8, 1011 + 1101 = 1000

Several adder algorithms, differing in depth, number of gates used, and number of auxiliaries, are implemented [1][2].

Syntax

Function: Adder

Parameters:

{
  "function": "Adder",
  "function_params": {
    "left_arg": 3,
    "right_arg": {
      "size": 3
    },
    "inplace_arg": null
  }
}

Register Names

By default, the input registers are called left_arg and right_arg. If the name field of a RegisterUserInput object is specified, then the name of the register is determined accordingly. If one of the arguments is a constant then it is not available as an input register.

The output registers include the result register. By default, it is called sum, but its name may be overridden by the output_name argument. The inplace_arg argument sets the argument to override. If it is set to None, the input registers are also available as output registers, with the same names. If it is set to left or right, only the right or left argument will be available, respectively. The qubits of the overriden argument will be used for the result.

Example 1: Two Register Addition

{
        "logic_flow": [
            {
                "function": "Adder",
                "function_params": {
                    "left_arg": {"size": 3},
                    "right_arg": {"size": 3}
                }
            }
        ]
    }
from classiq import ModelDesigner
from classiq.builtin_functions import Adder
from classiq.interface.generator.arith.arithmetic import RegisterUserInput

params = Adder(left_arg=RegisterUserInput(size=3), right_arg=RegisterUserInput(size=3))
model_designer = ModelDesigner()
model_designer.Adder(params)
circuit = model_designer.synthesize()

This code example generates a circuit that adds 2 arguments. Both "left_arg" and "right_arg" are defined to be quantum registers of size 3.

Generated Circuit

img.png

Example 2: Float and Register Addition

{
    "logic_flow": [
        {
            "function": "Adder",
            "function_params": {
                "left_arg": 3.5,
                "right_arg": {
                    "size": 3
                }
            }
        }
    ]
}
from classiq import ModelDesigner
from classiq.builtin_functions import Adder
from classiq.interface.generator.arith.arithmetic import RegisterUserInput

params = Adder(left_arg=3.5, right_arg=RegisterUserInput(size=3))
model_designer = ModelDesigner()
model_designer.Adder(params)
circuit = model_designer.synthesize()

This code example generates a circuit that adds 2 arguments. Here "left_arg" is a fixed-point number \((11.1)_2\) and "right_arg" a quantum register of size 3.

Generated Circuit

img_1.png

References

[1]S. A. Cuccaro, T. G. Draper, S. A. Kutin, and D. P. Moulton, “A new quantum ripple-carry addition circuit,” arXiv:quant-ph/0410184, Oct. 2004, Accessed: Aug. 09, 2021. [Online]. Available: http://arxiv.org/abs/quant-ph/0410184

[2]T. G. Draper, “Addition on a Quantum Computer,” arXiv:quant-ph/0008033, Aug. 2000, Accessed: Aug. 09, 2021. [Online]. Available: http://arxiv.org/abs/quant-ph/0008033