Skip to content

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. When n equals \(2^m\) for an integer \(m\), the modulo operation's result will be 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 we only account for the \(3\) least significant bits of \(53\).

Implementation

Modulo is not implemented in the Classiq platform as a function like other arithmetic operations. Instead, it changes the size of the output register of the previous function.

The modulo operation is supported only for \(n = 2^m\) for an integer \(m\), and only for the modulo of a function, like (\((a + b)\) % \(4\)). The modulo operation does not currently support modulo of a register directly (e.g. the expression (\(a\) % \(4\))).

Example: Modulo after an adder function

{
  "logic_flow": [
    {
      "function": "Arithmetic",
      "function_params": {
        "expression": "(a + b) % 4 == 3",
        "definitions": {
          "a": {
            "size": 5
          },
          "b": {
            "size": 5
          }
        }
      }
    }
  ]
}
from classiq import ModelDesigner
from classiq.builtin_functions import Arithmetic
from classiq.interface.generator.arith.arithmetic import RegisterUserInput

expression = "(a + b) % 4 == 3"
definitions = dict(a=RegisterUserInput(size=5), b=RegisterUserInput(size=5))
oracle_params = Arithmetic(expression=expression, definitions=definitions)
model_designer = ModelDesigner()
model_designer.Arithmetic(oracle_params)
circuit = model_designer.synthesize()
circuit.show()

This example generates a circuit which adds two 5-qubit arguments: a on qubits 0-4, and b on qubits 5-9. The adder result should have been calculated on a 6 qubits register. However, the modulo operation decides that the output register of the adder will only contain its 2 least significant qubits, and thus the adder result is written to a 2 qubits register, on qubits 10-11.

Output circuit:

img_1.png