pyqpanda_alg.QLuoShu.ConModaddmul

Module Contents

Functions

ConModaddmul(a, N, qvec1, qvec2, auxadd)

Quantum Circuit of Constant Multiplication and Modulo Addition.

pyqpanda_alg.QLuoShu.ConModaddmul.ConModaddmul(a, N, qvec1, qvec2, auxadd)

Quantum Circuit of Constant Multiplication and Modulo Addition.

Parameters:

\(a\): int

the integer to be added

\(N\) : int

the modulo

qvec1 & qvec2 : qlist

the qubits list

auxadd : qubit

an auxiliary qubit

Return:

circuit: pq.QCircuit

Based on the constant QFT modulo addition, the circuit can compute \(b+a*x \mod N\) with a constant integer \(a\) and a constant modulo \(N\), where the the \(|qvec1 \rangle\) register holds an integer \(x\) and the \(|qvec2 \rangle\) register holds an integer \(b\). The result of modulo addition-multiplication is deposited in the register \(|qvec2 \rangle\). The circuit needs \(2n+1\) qubits with \(n=\lceil \log_{2}N \rceil\). The inverse of the circuit can compute \(b-a*x \mod N\).

Example:

If \(a =2, b=10, x=8, N=11\), putting x held in \(|qvec1 \rangle\). By the circuit, the result \(|0100 \rangle\) will be held in the \(|qvec2 \rangle\).

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

if __name__ == "__main__":
    N = 11
    a = 2
    b = 10
    x = 8
    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_many(1)

    prog << bind_nonnegative_data(x, qvec1) \
         << bind_nonnegative_data(b, qvec2) \
         << ConModaddmul.ConModaddmul(a, N, qvec1, qvec2, auxadd)

    result = prob_run_dict(prog, qvec2, 1)

    for key in result:
        print(key + ":" + str(result[key]))
        c = int(key, 2)
    print("%d+%d*%d mod %d=%d" % (b,x,a, N, c))
0100:1.0000000000000067
10+8*2 mod 11=4

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

Here, we give the circuit graph of the example in the above with 9 qubits in the following:

          ┌─┐                                                                                                   >
q_0:  |0>─┤X├ ─■─ ───────■────── ───────■────── ───────■────── ───────■────── ─■─ ───────■────── ───────■────── >
          ├─┤  │         │              │              │              │        │         │              │       >
q_1:  |0>─┤X├ ─┼─ ───────┼────── ───────┼────── ───────┼────── ───────┼────── ─┼─ ───────┼────── ───────┼────── >
          └─┘  │         │              │              │              │        │         │              │       >
q_2:  |0>──── ─┼─ ───────┼────── ───────┼────── ───────┼────── ───────┼────── ─┼─ ───────┼────── ───────┼────── >
          ┌─┐  │         │              │              │              │        │         │              │       >
q_3:  |0>─┤X├ ─┼─ ───────┼────── ───────┼────── ───────┼────── ───────┼────── ─┼─ ───────┼────── ───────┼────── >
          ├─┤  │         │              │              │              │        │         │              │       >
q_4:  |0>─┤X├ ─┼─ ───────┼────── ───────┼────── ───────┼────── ───────■────── ─┼─ ───────┼────── ───────┼────── >
          ├─┤  │         │              │              │              │        │         │              │       >
q_5:  |0>─┤X├ ─┼─ ───────┼────── ───────┼────── ───────■────── ───────┼────── ─┼─ ───────┼────── ───────■────── >
          ├─┤  │         │              │              │              │        │         │              │       >
q_6:  |0>─┤X├ ─┼─ ───────┼────── ───────■────── ───────┼────── ───────┼────── ─┼─ ───────■────── ───────┼────── >
          └─┘  │         │              │              │              │       ┌┴┐ ┌──────┴─────┐ ┌──────┴─────┐ >
q_7:  |0>──── ─┼─ ───────■────── ───────┼────── ───────┼────── ───────┼────── ┤H├ ┤CR(1.570796)├ ┤CR(0.785398)├ >
              ┌┴┐ ┌──────┴─────┐ ┌──────┴─────┐ ┌──────┴─────┐ ┌──────┴─────┐ └─┘ └────────────┘ └────────────┘ >
q_8:  |0>──── ┤H├ ┤CR(1.570796)├ ┤CR(0.785398)├ ┤CR(0.392699)├ ┤CR(0.196350)├ ─── ────────────── ────────────── >
              └─┘ └────────────┘ └────────────┘ └────────────┘ └────────────┘                                   >