%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit,QuantumRegister,ClassicalRegister, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *

def qft_1():
    n=1
    qc = QuantumCircuit()
    q = QuantumRegister(n, 'q')
    c = ClassicalRegister(n, 'c')

    qc.add_register(q)
    qc.add_register(c)

    qc.h(q[0])
    return qc,q,c

qc,q,c = qft_1()
qc.measure(q[0], c[0])

# Change the background color in mpl
style = {'backgroundcolor': 'lightgreen'}
qc.draw(output='mpl', style = style)

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator).result()
counts = result.get_counts(qc)
plot_histogram(counts, title='QFT counts')

from qiskit.providers.aer import UnitarySimulator
qc,q,c = qft_1()
# Select the UnitarySimulator from the Aer provider
simulator = Aer.get_backend('unitary_simulator')
# Execute and get counts
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
print( unitary)

HI = np.array([[1,0,1,0],[0,1,0,1],[1,0,-1,0],[0,1,0,-1]])
CU1 = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,0.+1.j]])
IH = np.array([[1,1,0,0],[1,-1,0,0],[0,0,1,1],[0,0,1,-1]])
SWAP = np.array([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])

np.dot(HI,np.dot(CU1,np.dot(IH,SWAP)))

import numpy as np

def qft_2():
    n=2
    qc = QuantumCircuit()
    q = QuantumRegister(n, 'q')
    c = ClassicalRegister(n, 'c')

    qc.add_register(q)
    qc.add_register(c)

    qc.h(q[1])
    qc.cu1(np.pi / 2, q[0], q[1])
    qc.h(q[0])
    qc.swap(q[0], q[1])
    return qc,q,c


qc,q,c = qft_2()

qc.measure(q, c)
# Change the background color in mpl
style = {'backgroundcolor': 'lightgreen'}
qc.draw(output='mpl', style = style)

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator).result()
counts = result.get_counts(qc)
plot_histogram(counts, title='QFT counts')

from qiskit.providers.aer import UnitarySimulator
qc,q,c = qft_2()
# Select the UnitarySimulator from the Aer provider
simulator = Aer.get_backend('unitary_simulator')
# Execute and get counts
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
print( unitary)

def qft_3():
    n = 3
    qc = QuantumCircuit()
    q = QuantumRegister(n, 'q')
    c = ClassicalRegister(n, 'c')

    qc.add_register(q)
    qc.add_register(c)

    qc.h(q[2])
    qc.cu1(np.pi / 2, q[1], q[2])
    qc.h(q[1])
    qc.cu1(np.pi / 4, q[0], q[2])
    qc.cu1(np.pi / 2, q[0], q[1])
    qc.h(q[0])
    
    qc.swap(q[0], q[2])
   
    return qc,q,c

qc,q,c = qft_3()
qc.measure(q,c)
# Change the background color in mpl
style = {'backgroundcolor': 'lightgreen'}
qc.draw(output='mpl', style = style)

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator).result()
counts = result.get_counts(qc)
plot_histogram(counts, title='QFT counts')

from qiskit.providers.aer import UnitarySimulator
qc,q,c = qft_3()
# Select the UnitarySimulator from the Aer provider
simulator = Aer.get_backend('unitary_simulator')
# Execute and get counts
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
print( unitary)

def qft_4():
    n = 4
    qc = QuantumCircuit()
    q = QuantumRegister(n, 'q')
    c = ClassicalRegister(n, 'c')

    qc.add_register(q)
    qc.add_register(c)

    qc.h(q[3])
    qc.cu1(np.pi / 2, q[2], q[3])
    qc.h(q[2])
    qc.cu1(np.pi / 4, q[1], q[3])
    qc.cu1(np.pi / 2, q[1], q[2])
    qc.h(q[1])
    qc.cu1(np.pi / 8, q[0], q[3])
    qc.cu1(np.pi / 4, q[0], q[2])
    qc.cu1(np.pi / 2, q[0], q[1])
    qc.h(q[0])
    qc.swap(q[0], q[3])
    qc.swap(q[1], q[2])
    
    
    return qc,q,c

qc,q,c = qft_4()
qc.measure(q,c)
# Change the background color in mpl
style = {'backgroundcolor': 'lightgreen'}
qc.draw(output='mpl', style = style)

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator).result()
counts = result.get_counts(qc)
plot_histogram(counts, title='QFT counts')

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute
from qiskit.providers.aer import UnitarySimulator

qc = qft_circuit(4)    
# Select the UnitarySimulator from the Aer provider
simulator = Aer.get_backend('unitary_simulator')
# Execute and get counts
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
print( unitary)