pyqpanda_alg.QLuoShu.VarModMul
¶
Module Contents¶
Functions¶
|
Quantum Circuit of Variant Modular Multiplication for Odd Modulo. |
- pyqpanda_alg.QLuoShu.VarModMul.VarModMul(qvec1, qvec2, qvec3, auxadd, aux, N)¶
Quantum Circuit of Variant Modular Multiplication for Odd Modulo.
- Parameters:
qvec1 & qvec2 :
qlist
the qubits list, holds the integer \(x\) & \(y\)
qvec3 :
qlist
store the result \(x*y \mod N\)
auxadd & aux :
qubit
an auxiliary qubit for the operation of QAdder and control constant modulo addition
\(N\) :
int
the constant modulo
- Return:
circuit:
pq.QCircuit
The circuit is used to compute modular multiplication that \(|x \rangle|y \rangle|0 \rangle \rightarrow |x \rangle|y \rangle|x*y \mod N \rangle\) and replaces the third input with the result. Modular multiplication can be computed by repeated modular doublings and conditional modular additions. The circuit computes the product \(z=x \cdot y \mod N\) for constant odd integer \(N\) by using a simple expansion of the product along a binary decomposition of the first multiplicand, i.e.,
\(x * y=\sum_{i=0}^{n-1} x_{i} 2^i * y=x_0 y+2(x_1 y+2(x_2 y+...+2(x_{n-2} y+2(x_{n-1} y)) ...))\).
It uses \(3n + 2\) qubits with \(n=\lceil \log_{2}N \rceil\).
- Example:
If \(x =7,y=5, N=11\), putting \(x\) held in \(|qvec1\rangle\) and putting \(y\) held in \(|qvec2 \rangle\). By the circuit, the result \(|0010 \rangle\) will be held in the \(|qvec3 \rangle\), i.e., \(7*5 \mod 11=2\).
from pyqpanda import * import math from pyqpanda_alg.QLuoShu import VarModMul if __name__ == "__main__": N = 11 x = 7 y = 5 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() aux = qvm.qAlloc_many(1) prog << bind_nonnegative_data(x, qvec1) \ << bind_nonnegative_data(y, qvec2) \ << VarModMul.VarModMul(qvec1, qvec2, qvec3, auxadd, aux, N) 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" % (x, y, N, c))
0010:1.0000000000000047 7*5 mod 11=2
Note that: N is an odd integer.