1

这就是逆量子傅里叶变换。为了获得量子傅里叶变换(而不是逆变换),我需要做哪些改变?这是在正确的道路上吗?我如何将值 x1,x2,x3,x4 编码为基础状态?

from math import  pi,pow
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, BasicAer, execute

def IQFT(circuit, qin, n):
    for i in range (int(n/2)):
        circuit.swap(qin[i], qin[n -1 -i])
    for i in range (n):
        circuit.h(qin[i])
        for j in range (i +1, n, 1):
            circuit.cu1(-pi/ pow(2, j-i), qin[j], qin[i])

n = 3
qin = QuantumRegister(n)
cr = ClassicalRegister(n)
circuit = QuantumCircuit(qin, cr, name="Inverse_Quantum_Fourier_Transform")

circuit.h(qin)
circuit.z(qin[2])
circuit.s(qin[1])
circuit.z(qin[0])
circuit.t(qin[0])

IQFT(circuit, qin, n)
circuit.measure (qin, cr)


backend = BasicAer.get_backend("qasm_simulator")
result = execute(circuit, backend, shots = 500).result()
counts = result.get_counts(circuit)
print(counts)
4

1 回答 1

1

有几种方法可以获得量子傅里叶变换 (QFT)。

  1. QFT 的电路与 IQFT 的电路相反。如果您有一个实现反向 QFT 的电路,您可以简单地将该电路反转以获得 QFT 电路。反转可以手动完成,也可以使用内置inverse方法完成。
  2. 您可以在教科书(例如Nielsen & Chuang的第 5.1 章)或其他文档(例如Qiskit 的电路库)中查找 QFT 的电路。
  3. 一些软件包实现了 QFT 电路。你可以看看它是如何在那里实现的;例如在Qiskit中。

例如,您可以稍微重组您的代码,使其执行逆向和“常规”QFT。

from math import  pi,pow
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, BasicAer, execute

def QFT(n, inverse=False):
    """This function returns a circuit implementing the (inverse) QFT."""

    circuit = QuantumCircuit(n, name='IQFT' if inverse else 'QFT')
   
    # here's your old code, building the inverse QFT
    for i in range(int(n/2)):
        # note that I removed the qin register, since registers are not 
        # really needed and you can just use the qubit indices 
        circuit.swap(i, n - 1 - i)
    for i in range(n):
        circuit.h(i)
        for j in range(i + 1, n, 1):
            circuit.cu1(-pi / pow(2, j - i), j, i)
 
    # now we invert it to get the regular QFT
    if inverse:
        circuit = circuit.inverse()
    
    return circuit

n = 3
qin = QuantumRegister(n)
cr = ClassicalRegister(n)
circuit = QuantumCircuit(qin, cr)

circuit.h(qin)
circuit.z(qin[2])
circuit.s(qin[1])
circuit.z(qin[0])
circuit.t(qin[0])

# get the IQFT and add it to your circuit with ``compose``
# if you want the regular QFT, just set inverse=False
iqft = QFT(n, inverse=True)   
circuit.compose(iqft, inplace=True) 

circuit.measure (qin, cr)


backend = BasicAer.get_backend("qasm_simulator")
result = execute(circuit, backend, shots = 500).result()
counts = result.get_counts(circuit)
print(counts)

或者,您可以从 Qiskit 的电路库中导入它,而不是自己编写 QFT 函数:

from qiskit.circuit.library import QFT
于 2020-08-11T08:12:38.800 回答