from qiskit import *
import matplotlib.pyplot as plt
from qiskit.extensions import UnitaryGate
from qiskit.circuit import add_control
from IPython.display import display 
from qiskit.tools.visualization import plot_histogram
import numpy as np
import sys  
sys.path.append("..")

from Modules.faster_basic_qpe import fast_QPE

U = np.array([[1, 0],
              [0, np.exp(2*np.pi*1j*(1/5))]])

qpe_circ = fast_QPE(precision=4, unitary=U).get_QPE(show=True)

q = QuantumCircuit(5, 4)
q.x(4)
q.append(qpe_circ, qargs=[0, 1, 2, 3, 4])
# q = q.compose(qpe_circ,qubits = [0,1,2,3,4])
q.draw('mpl')

q.measure([0, 1, 2, 3], [0, 1, 2, 3])
q.draw('mpl')

count = execute(q, backend=Aer.get_backend(
    'qasm_simulator')).result().get_counts()
plot_histogram(count)

q = QuantumCircuit(3,name = 'Unitary')
q.cp(2*np.pi*(1/7),1,2)
display(q.draw('mpl'))
# u = execute(q,backend = Aer.get_backend('unitary_simulator')).result().get_unitary()

qpe_circ = fast_QPE(precision=4,unitary=q).get_QPE(show=True,save = True)

q = QuantumCircuit(7,4)
q.x([5,6])
q.barrier()
q.append(qpe_circ, qargs = [0,1,2,3,4,5,6])
q.measure([0,1,2,3],[0,1,2,3])
q.draw('mpl')

count = execute(q,backend=Aer.get_backend('qasm_simulator')).result().get_counts()
plot_histogram(count)

q = QuantumCircuit(7, 4)
q.x([5,6])
q.append(qpe_circ, qargs=range(7))
# q = q.compose(qpe_circ,qubits = [0,1,2,3,4])
q.draw('mpl')

q.measure([0, 1, 2, 3], [0, 1, 2, 3])
q.draw('mpl')

count = execute(q, backend=Aer.get_backend(
    'qasm_simulator')).result().get_counts()
plot_histogram(count)

