Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
The modular_exponentiate function raises a classical integer a to the power of a quantum number p modulo classical integer modulus, times a quantum number x. The function performs: ∣x⟩∣p⟩=∣(apmod  modulus)⋅x⟩∣p⟩|x\rangle |p\rangle = |(a^{p} \mod \text{modulus})\cdot x\rangle | p\rangle Specifically if at the input x=1x=1, at the output x=apmod  modulusx=a^{p} \mod \text{modulus}.

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:
Output:
Verify all results are as expected: