Modulo¶
The modulo operation (denoted as '%') returns the remainder (called "modulus") of a division. Given two numbers \(a\) and \(n\), the result of (\(a \% n\)) is the remainder of the division of a by n. The modulo operation is supported only for \(n = 2^m\) for an integer \(m\), its result is the \(m\) least significant bits.
For example, the binary representation of the number \(53\) is \(0b110101\). The expression (\(53 \% 8\)) equals \(0b101 = 5\), because \(8 = 2^3\), which means only accounting for the \(3\) least significant bits of \(53\).
Stand-alone implementation¶
Function: Modulo
Parameters:
left_arg: RegisterUserInput
(see RegisterUserInput)right_arg: int
output_size: Optional[PositiveInt]
inplace_arg: Optional[str] = None
Register names:
left_arg
:arg
result
:modulus
Example 1¶
{
"functions": [
{
"name": "main",
"body": [
{
"function": "Modulo",
"function_params": {
"left_arg": {"size": 7},
"right_arg": 8,
"inplace_arg": "left"
}
}
]
}
]
}
from classiq import Model, RegisterUserInput, synthesize
from classiq.builtin_functions import Modulo
from classiq.builtin_functions.binary_ops import ArgToInplace
params = Modulo(
left_arg=RegisterUserInput(size=7), right_arg=8, inplace_arg=ArgToInplace.LEFT
)
model = Model()
model.Modulo(params)
quantum_program = synthesize(model.get_model())
Implementation in Expressions¶
If an expression is defined using a modulo operation, the output size is set recursively to all of its subexpressions. But if for some sub-expressions, another modulo operation is used, the sub-expression's output_size is determined by the minimal value between the output_size of the sub-expression and the expression.
See this example: \(((a + b) \% 4) + (c + d) \% 8\). The result of expression \(a + b\) is saved on a two-qubit register, and the results of expressions \(c + d\) and \(((a + b) \% 4) + (c + d)\) are saved using three qubits each.
Example 2: Modulo after an adder function¶
{
"functions": [
{
"name": "main",
"body": [
{
"function": "Arithmetic",
"function_params": {
"expression": "(a + b) % 4 == 3",
"definitions": {"a": {"size": 5}, "b": {"size": 5}}
}
}
]
}
]
}
from classiq import Model, RegisterUserInput, synthesize, show
from classiq.builtin_functions import Arithmetic
expression = "(a + b) % 4 == 3"
definitions = dict(a=RegisterUserInput(size=5), b=RegisterUserInput(size=5))
oracle_params = Arithmetic(expression=expression, definitions=definitions)
model = Model()
model.Arithmetic(oracle_params)
quantum_program = synthesize(model.get_model())
show(quantum_program)
This example generates a circuit that adds two five-qubit arguments: a on qubits 0-4, and b on qubits 5-9. The adder result should have been calculated on a 6-qubit register. However, the modulo operation decides that the output register of the adder only contains its two least significant qubits. Thus, the adder result is written to a two-qubit register, on qubits 10-11.
Output circuit: