Multiplication¶
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 = -15.
The left arg -5 is represented as 1011 and 3 as 11. The number of digits needed to store the answer is 4+2-1 = 5. 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{equation*}\begin{array}{c} \phantom{\times}11011\\ \underline{\times\phantom{000}11}\\ \phantom{\times}11011\\ \underline{\phantom\times1011\phantom9}\\ \phantom\times10001 \end{array}\end{equation*} $$
from classiq import Output, QArray, QBit, QNum, create_model, prepare_int, qfunc
@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
prepare_int(4, a)
prepare_int(5, b)
res |= a * b
qmod = create_model(main)
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "multiplication_2vars_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
[{'a': 4.0, 'b': 5.0, 'res': 20.0}: 1000]
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.
from classiq import (
Output,
QArray,
QBit,
QNum,
allocate,
create_model,
hadamard_transform,
qfunc,
)
@qfunc
def main(a: Output[QNum], res: Output[QNum]) -> None:
allocate(2, a)
hadamard_transform(a)
res |= 3.5 * a
qmod = create_model(main)
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "multiplication_float_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
[{'a': 0.0, 'res': 0.0}: 265, {'a': 1.0, 'res': 3.5}: 258, {'a': 2.0, 'res': 7.0}: 248, {'a': 3.0, 'res': 10.5}: 229]