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=5))

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

QFT_gate = qt.QFT(circ, xreg, wrap=True)
circ.append(QFT_gate, xreg);

circ.draw(output="latex")

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

print(np.sum(np.abs(unit_mat-unit_mat_wrap)**2))

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

QFT_gate = qt.QFT(circ, xreg, wrap=True)
circ.append(QFT_gate, xreg);

iQFT_gate = qt.QFT(circ, xreg, wrap=True, inverse=True)
circ.append(iQFT_gate, 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=5))

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