pyqpanda_alg.QLuoShu.VarModSqr

Module Contents

Functions

VarModSqr(qvec1, qvec2, auxadd, aux, auxsqr, N)

Quantum Circuit of Variant Modular Square for Odd Modulo.

pyqpanda_alg.QLuoShu.VarModSqr.VarModSqr(qvec1, qvec2, auxadd, aux, auxsqr, N)

Quantum Circuit of Variant Modular Square for Odd Modulo.

Parameters:

qvec1 & qvec2 : qlist

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

auxadd & aux & auxsqr : qubit

auxiliary qubit for the operation of QAdder and control constant modulo addition

\(N\) : int

the constant modulo

Return:

circuit: pq.QCircuit

The circuit performs the operation that \(|x \rangle|0 \rangle \rightarrow |x \rangle|x^{2} \mod N \rangle\). It uses \(2n + 3\) qubits with \(n=\lceil \log_{2}N \rceil\) by removing the \(n\) qubits for the second input multiplicand, and adding one ancilla qubit, which is used in round \(i\) to copy out the current bit \(x_{i}\) of the input in order to add \(x\) to the accumulator conditioned on the value of \(x_{i}\).

Example:

If \(x =7, N=11\), putting \(x\) held in \(|qvec1 \rangle\). By the circuit, the result \(|00101 \rangle\) will be held in the \(|qvec2 \rangle\), i.e., \(7^{2} \mod 11=5\).

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

if __name__ == "__main__":
    x = 7
    N = 11
    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)
    auxadd = qvm.qAlloc()
    aux = qvm.qAlloc_many(1)
    auxsqr = qvm.qAlloc()

    prog << bind_nonnegative_data(x, qvec1) \
         << VarModSqr.VarModSqr(qvec1, qvec2, auxadd, aux, auxsqr, N)


    result = prob_run_dict(prog, qvec2, 1)

    for key in result:
        print(key + ":" + str(result[key]))
        c = int(key, 2)
    print("%d**2 mod %d=%d" % (x, N, c))
0101:1.0000000000000013
7**2 mod 11=5

Note that: \(N\) is an odd integer.