Multiplication¶
The "Multiplication" operation, denoted '\(*\)', can be seen as a series of addition ("long multiplication"). Therefore, there are different implementations to the multiplier depending on the of adder being used.
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¶
The calculation of -5 * 3 = -15 is done in the following manner:
The left arg -5 is represented as 1011 and 3 as 11. The number of digits needed to store the answer would be 4+2-1 = 5. The multiplication is done in the 'regular' way where each number is extended to 5 bits and only 5 digit are kept in the intermediary results:
Syntax¶
Function: Multiplier
Parameters:
- left_arg: Union[float, int, FixPointNumber, RegisterUserInput]
- right_arg: Union[float, int, FixPointNumber, RegisterUserInput]
- output_size: Optional[PositiveInt]
- output_name: Optional[str]
{
"function": "Multiplier",
"function_params": {
"left_arg": 3,
"right_arg": {
"size": 3
}
}
}
Register Names¶
By default, the input registers are called left_arg
and right_arg
. If the name field
of a RegisterUserInput
object is specified, then the name of the register is
determined accordingly. If one of the arguments is a constant then it is not available
as an input register.
The output registers include the result register. By default, it is called product
,
but its name may be overridden by the output_name
argument. In addition, since the
computation is done out-of-place, input registers are also available as output
registers, with the same names.
Example 1: Two Register Multiplication¶
{
"logic_flow": [
{
"function": "Multiplier",
"function_params": {
"left_arg": {"size": 3},
"right_arg": {"size": 3}
}
}
]
}
from classiq import Model, QUInt
from classiq.builtin_functions import Multiplier
params = Multiplier(
left_arg=QUInt(size=3).to_register_user_input(),
right_arg=QUInt(size=3).to_register_user_input(),
)
model = Model()
model.Multiplier(params)
circuit = model.synthesize()
circuit.show_interactive()
This code example generates a circuit that multiplies 2 arguments. Both "left_arg" and "right_arg" are defined to be quantum registers of size 3.
Generated Circuit¶
Example 2: Float and Register Multiplication¶
{
"logic_flow": [
{
"function": "Multiplier",
"function_params": {
"left_arg": 3.5,
"right_arg": {
"size": 3
}
}
}
]
}
from classiq import Model, RegisterUserInput
from classiq.builtin_functions import Multiplier
params = Multiplier(left_arg=3.5, right_arg=RegisterUserInput(size=3))
model = Model()
model.Multiplier(params)
circuit = model.synthesize()
circuit.show_interactive()
This code example generates a circuit that multiplies 2 arguments. Here "left_arg" is a fixed-point number \((11.1)_2\) and "right_arg" a quantum register of size 3.