Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
The modular_exp function raises a classical integer a to the power of a quantum number power modulo classical integer n, times a quantum number x. The function performs: xpower=(apowermodn)xpower|x\rangle |power\rangle = |(a^{power} \mod n)\cdot x\rangle | power\rangle Specifically if at the input x=1x=1, at the output x=apowermodnx=a^{power} \mod n.

Example

This example generates a quantum program that initializes a power variable with a uniform superposition, and exponentiate the classical value A with power as the exponent, in superposition. The result is calculated inplace to the variable x module N. Notice that x should have size of at least (log2(N)\lceil(log_2(N) \rceil, so it is first allocated with a fixed size, then initialized with the value ‘1’.
Output:
Verify all results are as expected: