%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 *
import math
# Loading your IBM Q account(s)
IBMQ.load_accounts()

q = QuantumRegister(3)
c = ClassicalRegister(3)

qft3 = QuantumCircuit(q,c)

qft3.h(q[0])
qft3.cu1(math.pi/2, q[1], q[0])
qft3.cu1(math.pi/4, q[2], q[0])
qft3.h(q[1])
qft3.cu1(math.pi/2, q[2], q[1])
qft3.h(q[2])

qft3.draw()

def input_state(circ, q, n):
    """n-bit input state for QFT that produces output 1."""
    for j in range(n):
        circ.h(q[j])
        circ.u1(math.pi/float(2**j) * (-1), q[j])#.inverse()
        
def qft(circ, q, n):
    """n-bit QFT on q in circ"""
    for j in range(n):
        circ.h(q[j])
        for k in range(j+1, n):
            circ.cu1(math.pi/float(2**(k-j)), q[k], q[j])
        circ.barrier()

q = QuantumRegister(3, 'x')
c = ClassicalRegister(3, 'c')
qft3 = QuantumCircuit(q, c)

qft(qft3, q, 3)
# qft(qft3, q, 3)
for i in range(3):
    qft3.measure(q[i], c[i])

#print(qft3.qasm())
qft3.draw()

backend = Aer.get_backend("qasm_simulator")
simulate = execute(qft3, backend=backend, shots=4069).result()
simulate.get_counts()

plot_histogram(simulate.get_counts())

q = QuantumRegister(8, 'x')
c = ClassicalRegister(8, 'c')

qft8a = QuantumCircuit(q, c)
# for i in range(4):
#     qft8a.x(q[2*i])
    
qft(qft8a, q, 8)
qft(qft8a, q, 8)

for i in range(8):
    qft8a.measure(q[i], c[i])

qft8a.draw()

backend = Aer.get_backend("qasm_simulator")
simulate = execute(qft8a, backend=backend, shots=1024).result()
plot_histogram(simulate.get_counts())

simulate.get_counts()

qft(qft3)
qft.measure(q,c)

