pyqpanda_alg.QLuoShu.VarModInv

Module Contents

Functions

VarModInv(qvec, N)

Quantum Circuit of Modular Inverse for Any Constant Modular.

pyqpanda_alg.QLuoShu.VarModInv.VarModInv(qvec, N)

Quantum Circuit of Modular Inverse for Any Constant Modular.

Parameters:

qvec : qlist

the qubits list, holds the integer \(x\)

\(N\) : int

the constant modulo

Return:

circuit: pq.QCircuit

The circuit computes a modular inverse \(x^{-1} \mod N\). The circuit use classical pre-processing to get a unitary matrix and use an operation “QOracle” in the QPanda. The circuit is an in-place circuit that performs the operation \(|x \rangle \rightarrow |x^{-1} \mod N \rangle\). It can not be extended to compute modulo inverse for integer \(N\) of large bit length.

Example:

If \(x =7, N=11\), putting \(x\) held in \(|qvec \rangle\). By the circuit, the result \(|1000 \rangle\) will be held in the \(|qvec \rangle\), i.e., \(7^{-1} \mod 11=8\).

from pyqpanda import *
import numpy as np
import math
from pyqpanda_alg.QLuoShu import VarModInv

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

    qvec = qvm.qAlloc_many(n)

    prog << bind_nonnegative_data(x, qvec) \
         << VarModInv.VarModInv(qvec, N)

    result = prob_run_dict(prog, qvec, 1)

    for key in result:
        print(key + ":" + str(result[key]))
        c = int(key, 2)
    print("%d**{-1} mod %d=%d" % (x, N, c))
1000:1.0
7**{-1} mod 11=8