Skip to content

Comparators

The following comparators are supported:

  • Equal (denoted as '==')
  • NotEqual (denoted as '!=')
  • GreaterThan (denoted as '>')
  • GreaterEqual (denoted as '>=')
  • LessThan (denoted as '<')
  • LessEqual (denoted as '<=')

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) = 0

(5 == 5) = 1

(\((011)_2\) == \((11)_2\)) = 1

(signed \((101)_2\) < unsigned \((101)_2\)) = 1

Syntax

Function: Equal/NotEqual/GreaterThan/GreaterEqual/LessThan/LessEqual

Parameters:

  • left_arg: Union[float, int, RegisterUserInput] (see RegisterUserInput)
  • right_arg: Union[float, int, RegisterUserInput] (see RegisterUserInput)
  • output_size: Optional[PositiveInt]

Register names:

  • left_arg: left_arg
  • right_arg: right_arg
  • result: is_equal/ is_not_equal/ is_greater_than/ is_greater_equal/ is_less_than/ is_less_equal, respectively.
{
  "functions": [
    {
      "name": "main",
      "body": [
        {
          "function": "Equal",
          "function_params": {
            "left_arg": 3,
            "right_arg": {
              "size": 3
            }
          }
        }
      ]
    }
  ]
}

Example 1: Two Register

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

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

This example generates a quantum program that performs 'equal' between two registers. The left arg is a signed register with 5 qubits and the right arg is an unsigned register with 3 qubits.

img_11.png

Example 2: Integer and Register

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

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

This example generates a quantum program that performs 'equal' between a quantum register and an integer. The left arg is an integer equal to three and the right arg is an unsigned quantum register with 3 qubits.

img_12.png