Skip to content

Addition

The Classiq engine implements the addition operation, denoted '+', according to the following truth table. Here, \(a\) and \(b\) denote numbers, \(i\) a 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 two-complement method during function evaluation. The binary number is extended in the case of a register size mismatch. 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 you specify the name field of a RegisterUserInput object, 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 you can override its name using 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 is available, respectively. The qubits of the overriden argument are used for the result.

Two-Register Addition Example

{
        "logic_flow": [
            {
                "function": "Adder",
                "function_params": {
                    "left_arg": {"size": 3},
                    "right_arg": {"size": 3}
                }
            }
        ]
    }
from classiq import Model, RegisterUserInput
from classiq.builtin_functions import Adder

params = Adder(left_arg=RegisterUserInput(size=3), right_arg=RegisterUserInput(size=3))
model = Model()
model.Adder(params)
circuit = model.synthesize()

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

Generated Circuit

img.png

Float and Register Addition Example

{
    "logic_flow": [
        {
            "function": "Adder",
            "function_params": {
                "left_arg": 3.5,
                "right_arg": {
                    "size": 3
                }
            }
        }
    ]
}
from classiq import Model, RegisterUserInput
from classiq.builtin_functions import Adder

params = Adder(left_arg=3.5, right_arg=RegisterUserInput(size=3))
model = Model()
model.Adder(params)
circuit = model.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,” Oct. 2004, Accessed: Aug. 09, 2021. http://arxiv.org/abs/quant-ph/0410184

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