Skip to content

Subtraction

Subtraction (denoted as '-') is implemented by negation and addition in 2-complement representation.

\[ a - b \longleftrightarrow a + (-b) \longleftrightarrow a + \sim{b} + lsb \\_ value \]

Where '~' is bitwise not and lsb_value is the least significant bit value.

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 = 5 + (-3) = 2, 0101 + 1101 = 0010

-5 + -3 = -8, 1011 + 1101 = 1000

Syntax

Function: Subtractor

Parameters:

  • left_arg: Union[float, int, RegisterUserInput] (see RegisterUserInput).
  • right_arg: Union[float, int, RegisterUserInput] (see RegisterUserInput).
  • output_size: Optional[PositiveInt]
  • inplace_arg: Optional[str] = None

Register names:

  • left_arg: left_arg
  • right_arg: right_arg
  • result: difference
{
  "functions": [
    {
      "name": "main",
      "body": [
          {
              "function": "Subtractor",
              "function_params": {
                  "left_arg": 3.5,
                  "right_arg": { "size": 3 }
              }
          }
      ]
    }
  ]
}

Example 1: Two Register

{
  "functions": [
    {
      "name": "main",
      "body": [
          {
              "function": "Subtractor",
              "function_params": {
                  "left_arg": {"size": 3},
                  "right_arg": {"size": 3}
              }
          }
      ]
    }
  ]
}
from classiq import Model, QUInt, synthesize
from classiq.builtin_functions import Subtractor

params = Subtractor(
    left_arg=QUInt(size=3).to_register_user_input(),
    right_arg=QUInt(size=3).to_register_user_input(),
)
model = Model()
model.Subtractor(params)
quantum_program = synthesize(model.get_model())

This example generates a quantum program that subtracts two arguments. The left_arg is defined to be a quantum register of size three. The right_arg is defined to be a quantum register of size three.

Output quantum program:

img_1.png

Example 2: Float and Register

{
  "functions": [
    {
      "name": "main",
      "body": [
          {
              "function": "Subtractor",
              "function_params": {
                  "left_arg": 3.5,
                  "right_arg": { "size": 3 }
              }
          }
      ]
    }
  ]
}
from classiq import Model, RegisterUserInput, synthesize
from classiq.builtin_functions import Subtractor

params = Subtractor(left_arg=3.5, right_arg=RegisterUserInput(size=3))
model = Model()
model.Subtractor(params)
quantum_program = synthesize(model.get_model())

This example generates a quantum program which subtracts two argument. The left_arg is defined to be a fix point number \((11.1)_2\). The right_arg is defined to be a quantum register of size of three.

Output quantum program:

img.png