pyqpanda_alg.QLuoShu.ConModAdd

Module Contents

Functions

ConModAdd(a, N, qvec, auxadd)

Quantum Circuit of Constant Modulo Addition Using QFT(Quantum Fourier Transform).

pyqpanda_alg.QLuoShu.ConModAdd.ConModAdd(a, N, qvec, auxadd)

Quantum Circuit of Constant Modulo Addition Using QFT(Quantum Fourier Transform).

Parameters:

\(a\): int

the integer to be added

\(N\) : int

the modulo

qvec : qlist

the qubits list

auxadd : qubit

an auxiliary qubit

Return:

circuit: pq.QCircuit

The circuit works on one quantum register \(|qvec \rangle\) holding the input integer \(x \in [0,N-1]\) and one phase array from classical computation on a constant integer \(a \in [0,N-1]\). to compute modulo \(N\) addition using QFT. The circuit is an in-place circuit, where the result \(x+a \mod N\) of modulo addition is deposited in the register \(|qvec \rangle\). The circuit needs \(n+1\) qubits with \(n=\lceil \log_{2}N \rceil\). The inverse of the circuit can compute \(x-a \mod N\).

Example:

If \(a =7, x=9, N=11\), putting x held in \(|qvec \rangle\). By the circuit, the result \(|0101 \rangle\) will be held in the \(|qvec \rangle\), i.e., \(7+9 \mod 11=5\). If by the inverse of the circuit (by the operator: ConModAdd.dagger()), we can get \(7-9 \mod 11=9\), the result \(|1001 \rangle\) will be held in the \(|qvec \rangle\).

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

if __name__ == "__main__":
    N = 11
    a = 7
    x = 9
    qvm = init_quantum_machine(QMachineType.CPU)
    prog = QProg()
    n = math.ceil(math.log(N, 2))
    qvec = qvm.qAlloc_many(n)
    auxadd = qvm.qAlloc_many(1)
    prog << bind_nonnegative_data(x, qvec) \
         << ConModAdd.ConModAdd(a, N, qvec, auxadd)
    result = prob_run_dict(prog, qvec, 1)

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

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 for constant modolo addition with 5 qubits:

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