> ## Documentation Index
> Fetch the complete documentation index at: https://docs.classiq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Comparators

> [card] [View on GitHub](https://github.com/Classiq/classiq-library/blob/main/functions/function_usage_examples/arithmetic/comparator/comparator_example.ipynb)
>
> Open this notebook in GitHub to run it yourself

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

## Examples

#

## Example 1: Comparing Two Quantum Variables

This example generates a quantum program that performs 'equal' between two variables.

The left arg is a signed variable with 5 qubits and the right arg is an unsigned varialbe with 3 qubits.

```python theme={null}
from classiq import *


@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
    allocate(5, True, 0, a)
    allocate(3, False, 0, b)

    res |= a == b
```

```python theme={null}

qprog = synthesize(main)
```

#

## Example 2: Comparing Integer and Quantum Variable

This example generates a quantum program that performs 'less equal' between a quantum register and an integer.

The left arg is an unsigned quantum variable with 3 qubits, and the right arg is an integer equal to

2.

```python theme={null}
@qfunc
def main(a: Output[QNum], res: Output[QNum]) -> None:
    allocate(3, a)
    hadamard_transform(a)
    res |= a <= 2
```

```python theme={null}

qprog = synthesize(main)

result = sample(qprog)
result
```

> [!NOTE]
>
> **Output:**
>
> ```
>
> Submitting job to simulator
>   Job: https://platform.classiq.io/jobs/1fafe118-a61c-4d22-bf2f-9c0fa9afd9d9
>
>
> ```

| | a | res | counts | probability | bitstring |
| - | - | - | - | - | - |
| 0 | 6 | 0 | 278 | 0.135742 | 0110 |
| 1 | 1 | 1 | 274 | 0.133789 | 1001 |
| 2 | 7 | 0 | 261 | 0.127441 | 0111 |
| 3 | 3 | 0 | 256 | 0.125000 | 0011 |
| 4 | 5 | 0 | 252 | 0.123047 | 0101 |
| 5 | 0 | 1 | 250 | 0.122070 | 1000 |
| 6 | 4 | 0 | 248 | 0.121094 | 0100 |
| 7 | 2 | 1 | 229 | 0.111816 | 1010 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.