def QFT(N):
    '''
    Constructing the fourier transform of size NxN.
    '''
    return np.array([[(np.exp(2.*i*j*1j*np.pi/N))
                   for j in np.arange(N)]
                    for i in np.arange(N)])*1./np.sqrt(N)

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer, IBMQ
import qiskit_tools as qt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

n = 5

xreg = QuantumRegister(n, 'x')
circ = QuantumCircuit(xreg)

circ = qt.QFT(circ, xreg)

circ.draw(output="latex")

backend = Aer.get_backend('unitary_simulator')
job = execute(circ, backend)
result = job.result()
unit_mat = np.array(result.get_unitary(circ, decimals=8))

print(np.sum(np.abs(unit_mat-QFT(2**n))**2))