> ## 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.

# Multiplication

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

The multiplication operation, denoted '$*$', is a series of additions ("long multiplication").

The multiplier has different implementations, depending on the type of adder in use.

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 working with a five-qubit register.
Similarly, the negative signed number $(110)_2=-2$ is expressed as $(11110)_2$.

## Examples

The calculation of -5 \* 3 = -

1

5. The left arg -5 is represented as 1011 and 3 as

1

1. The number of digits needed to store the answer is 4+2-1 =

2. The multiplication is done in the 'regular' manner where each number is extended to five bits and only five digits are kept in the intermediary
   results.

$$
\begin{array}{c}
\phantom{\times}11011\\
\underline{\times\phantom{000}11}\\
\phantom{\times}11011\\
\underline{\phantom\times1011\phantom9}\\
\phantom\times10001
\end{array}
$$

## Examples

#

## Example 1: Two Quantum Variables Multiplication

This code example generates a quantum program that multiplies two arguments.

Both of them are defined as quantum variables of size

3.

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


@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
    a |= 4
    b |= 5
    res |= a * b
```

```python theme={null}

qprog = synthesize(main)

result = sample(qprog)
result
```

<Info>
  **Output:**

  ```

  Submitting job to simulator
    Job: https://platform.classiq.io/jobs/fd80ef81-6bcd-4abd-869f-fbd7bd5aec80
    

  ```
</Info>

|   | a | b | res | counts | probability | bitstring   |
| - | - | - | --- | ------ | ----------- | ----------- |
| 0 | 4 | 5 | 20  | 2048   | 1.0         | 10100101100 |

#

## Example 2: Float and Quantum Variable Multiplication

This code example generates a quantum program that multiplies two arguments.
Here, the left argument is a fixed-point number $(11.1)_2$ (3.5),
and the right argument is a quantum variable of size

2.

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

    hadamard_transform(a)
    res |= 3.5 * a
```

```python theme={null}

qprog = synthesize(main)

result = sample(qprog)
result
```

<Info>
  **Output:**

  ```

  Submitting job to simulator
    Job: https://platform.classiq.io/jobs/fca22608-e8f9-4465-9942-aca0056ca6ff
    

  ```
</Info>

|   | a | res  | counts | probability | bitstring |
| - | - | ---- | ------ | ----------- | --------- |
| 0 | 0 | 0.0  | 527    | 0.257324    | 0000000   |
| 1 | 3 | 10.5 | 512    | 0.250000    | 1010111   |
| 2 | 2 | 7.0  | 507    | 0.247559    | 0111010   |
| 3 | 1 | 3.5  | 502    | 0.245117    | 0011101   |
