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

# Bitwise Xor

<Card title="View on GitHub" icon="github" href="https://github.com/Classiq/classiq-library/blob/main/functions/function_usage_examples/arithmetic/bitwise_xor/bitwise_xor_example.ipynb">
  Open this notebook in GitHub to run it yourself
</Card>

The Bitwise Xor (denoted as '^') is implemented by applying this truth table between each pair of qubits (or qubit and bit) in variables A and B.

<center>| a | b | a ^ b | | :-: | :-: | :---: | | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 |</center>

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 variable size mismatch.

For example, the positive signed number $(110)_2=6$ is expressed as $(00110)_2$ when operating with a five-qubit variable.
Similarly, the negative signed number $(110)_2=-2$ is expressed as $(11110)_2$.

Examples:

5 ^ 3 = 6 since 101 ^ 011 = 110

5 ^ -3 = -8 since 0101 ^ 1101 = 1000

-5 ^ -3 = 6 since 1011 ^ 1101 = 0110

## Examples

#

### Example 1: Two Quantum Variables

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

The left arg is a signed with five qubits and the right arg is unsigned with three qubits.

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


@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
    allocate(5, SIGNED, 0, a)
    allocate(3, UNSIGNED, 0, b)
    a ^= 4
    b ^= 5
    res |= a ^ b


qmod = create_model(main)
```

```python theme={null}

qprog = synthesize(qmod)

result = execute(qprog).result_value()
print(result.parsed_counts)
print(result.counts_of_multiple_outputs(["a", "b", "res"]))
```

<Info>
  **Output:**

  ```
  [{'a': 4.0, 'b': 5.0, 'res': 1.0}: 1000]
    {('00100', '101', '10000'): 1000}
    

  ```
</Info>

#

### Example 2: Integer and Quantum Variable

This example generates a quantum program that performs a bitwise 'xor' between a quantum variable and an integer.

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

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


qmod = create_model(main)
```

```python theme={null}

qprog = synthesize(qmod)

result = execute(qprog).result_value()
result.parsed_counts
```

<Info>
  **Output:**

  ```
  [{'a': 4.0, 'res': 7.0}: 1000]
    

  ```
</Info>
