pyqpanda_alg.QLuoShu.ConModExp

Module Contents

Functions

ConModExp(a, N, qvec1, qvec2, qvec3, auxadd)

Quantum Circuit of Constant Modulo Exponential.

pyqpanda_alg.QLuoShu.ConModExp.ConModExp(a, N, qvec1, qvec2, qvec3, auxadd)

Quantum Circuit of Constant Modulo Exponential.

Parameters:

\(a\) : int

the integer to be added

\(N\) : int

the modulo

qvec1 & qvec2 & qvec3 : qlist

the qubits list

auxadd : qubit

an auxiliary qubit

Return:

circuit: pq.QCircuit

With a constant integer \(a\), we can compute \(a^x\) mod N by the formula \(a^{\sum_{i=0}^{len(x)-1}2^{i}x_{i}}\). We construct a circuit by some controlled constant multiplication circuits according to the binary expansion of \(a\). The quantum register \(|qvec1 \rangle\) holds the value of integer \(x\in [0,N-1]\) and the quantum register \(|qvec2 \rangle\) is an auxiliary register with the same size as that of the register \(|qvec1 \rangle\). The result of modulo exponential is deposited in the register \(|qvec3 \rangle\). The circuit needs \(3n+1\) qubits with \(n=\lceil \log_{2}N \rceil\).

Example:

If \(a =2,x=4,N=11\), putting \(x\) held in \(|qvec1 \rangle\). By the circuit, the result \(|00101 \rangle\) will be held in the \(|qvec3 \rangle\).

from pyqpanda import *
from pyqpanda_alg.QLuoShu import ConModExp
import math

if __name__ == "__main__":
    N = 11
    a = 2
    x = 4
    n = math.ceil(math.log(N, 2))
    qvm = init_quantum_machine(QMachineType.CPU)
    prog = QProg()

    qvec1 = qvm.qAlloc_many(n)
    qvec2 = qvm.qAlloc_many(n)
    qvec3 = qvm.qAlloc_many(n)
    auxadd = qvm.qAlloc_many(1)

    prog << bind_nonnegative_data(x, qvec1) \
         << ConModExp.ConModExp(a, N, qvec1, qvec2, qvec3, auxadd)

    result = prob_run_dict(prog, qvec3, 1)

    for key in result:
        print(key + ":" + str(result[key]))
        c = int(key, 2)
    print("%d**%d mod %d=%d" % (a,x,N,c))
00101:1.000000000000025
2**4 mod 11=5

Note that: if \(N\) is a power of 2, we need to make \(n=\lceil \log_{2}N \rceil+1\) in the Example.