pyqpanda_alg.QLuoShu.VarModNeg

Module Contents

Functions

VarModNeg(qvec, N)

Quantum Circuit of Modular Negation for Any Constant Modular.

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

Quantum Circuit of Modular Negation for Any Constant Modular.

Parameters:

qvec : qlist

holds the integer \(x\)

\(N\) : int

the constant modulo

Return:

circuit: pq.QCircuit

The circuit computes a modular inverse \(-x \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 \mod N \rangle\). It can not be extended to compute modulo negation for integer \(N\) of large bit length.

Example:

If \(x =7, N=11\), putting \(x\) held in \(|qvec \rangle\). By the circuit, the result \(|00100 \rangle\) will be held in the \(|qvec \rangle\), i.e., \(-7 \mod 11=4\).

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

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) \
         << VarModNeg.VarModNeg(qvec, N)

    result = prob_run_dict(prog, qvec, 1)

    for key in result:
        print(key + ":" + str(result[key]))
        c = int(key, 2)
    print("-%d mod %d=%d" % (x, N, c))
0100:1.0
-7 mod 11=4