1

This is the inverse quantum Fourier transform. What changes do I have to do to obtain the quantum Fourier transform (not the inverse)? Is this in the right path at all? How would I encode values x1,x2,x3,x4 into the basis state?

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)
Cryoris
  • 319
  • 1
  • 2
  • 10

1 Answers1

1

There's several ways you can obtain the quantum Fourier transform (QFT).

  1. The circuit for the QFT is the inverse of the circuit for the IQFT. If you have a circuit that implements the inverse QFT, you can simply invert that circuit to get the circuit for the QFT. The inversion can be done manually, or by using the built-in inverse method.
  2. You can look up the circuit for the QFT in a textbook, e.g. in Nielsen & Chuang, Chapter 5.1, or in some other documentation, e.g. in Qiskit's circuit library.
  3. Some software packages implement the QFT circuit. You can look how it's implemented there; e.g. in Qiskit.

For instance, you could restructure your code a little to make it do the inverse and "regular" 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)

Alternatively, instead of writing the QFT function yourself you could just import it from Qiskit's circuit library:

from qiskit.circuit.library import QFT
Cryoris
  • 319
  • 1
  • 2
  • 10
  • Thank you so much! Very helpful. Im very new to this and I have a class that gave me this project as a homework. Will definitely look into that book, and sadly I can't import a library's code for this one. – Steve Bermeo Aug 11 '20 at 15:08