%matplotlib inline
import numpy as np
import IPython
import matplotlib.pyplot as plt
from qiskit import QuantumCircuit
from qiskit.tools.jupyter import *
from qiskit.visualization import *
import seaborn as sns
sns.set()

from helper import *
import os
import glob
import moviepy.editor as mpy

figs = []
QCs = []


qc_e1 = QuantumCircuit(1)
figs.append(getBlochSphere(qc_e1.copy()))
QCs.append(qc_e1.copy())


qc_e1.h(0)
qc_e1.barrier()
figs.append(getBlochSphere(qc_e1.copy()))
QCs.append(qc_e1.copy())


for i in range(10):
    qc_e1.rz(np.pi/5, 0)
    figs.append(getBlochSphere(qc_e1.copy()))
    QCs.append(qc_e1.copy())
    
qc_e1.barrier()
qc_e1.h(0)

figs.append(getBlochSphere(qc_e1.copy()))
QCs.append(qc_e1.copy())

style = {'backgroundcolor': 'lavender'}
qc_e1.draw(output='mpl', style = style)

path = "plots/bloch1"
fname = "plots/bloch1"
fps = 5
getBlochSequence(path, figs)
getBlochGif(figs,path,fname,fps,remove = False)
print("gif file is ready!")

path = "plots/phase1/"
fname = "plots/phase1"
fps =5
getPhaseSequence(QCs,path)
getPhaseGif(path,fname,fps,remove = False)
print("gif file is ready!")

figs = []
QCs = []
qc_e2 = QuantumCircuit(2)
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())
qc_e2.h(0)
qc_e2.u3(np.pi/4,np.pi/4,0,1)
qc_e2.barrier()
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())
for i in range(8):
    qc_e2.rz(np.pi/4, 0) 
    qc_e2.rz(np.pi/4, 1) 
    figs.append(getBlochSphere(qc_e2.copy()))
    QCs.append(qc_e2.copy())
qc_e2.barrier()
qc_e2.h(0)
qc_e2.u3(-np.pi/4,-np.pi/4,0,1)
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())

style = {'backgroundcolor': 'lavender'}
qc_e2.draw(output='mpl', style = style)

path = "plots/bloch2/"
fname = "plots/bloch2"
fps = 5
getBlochSequence(path, figs)
getBlochGif(figs,path,fname,fps,remove = False)
print("gif file is ready!")

path = "plots/phase2/"
fname =  "plots/phase2"
fps = 5
getPhaseSequence(QCs,path)
getPhaseGif(path,fname,fps=5,remove = False)
print("gif file is ready!")

figs = []
QCs = []
qc_e2 = QuantumCircuit(3)
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())
qc_e2.h(0)
qc_e2.h(1)
qc_e2.h(2)
qc_e2.x(0)
qc_e2.y(1)
qc_e2.z(2)
qc_e2.barrier()
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())
for i in range(8):
    qc_e2.u3(0,0,np.pi/4,0)
    qc_e2.u3(np.pi/4,0,0,1) 
    qc_e2.u3(0,np.pi/4,0,2)
    figs.append(getBlochSphere(qc_e2.copy()))
    QCs.append(qc_e2.copy())
qc_e2.barrier()
qc_e2.x(0)
qc_e2.y(1)
qc_e2.z(2)
qc_e2.h(0)
qc_e2.h(1)
qc_e2.h(2)
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())

style = {'backgroundcolor': 'lavender'}
qc_e2.draw(output='mpl', style = style)

path = "plots/bloch3/"
fname = "plots/bloch3"
fps = 5
getBlochSequence(path, figs)
getBlochGif(figs,path,fname,fps,remove = False)
print("gif file is ready!")

path = "plots/phase3/"
fname =  "plots/phase3"
fps = 5
getPhaseSequence(QCs,path)
getPhaseGif(path,fname,fps=5,remove = False)
print("gif file is ready!")

figs = []
QCs = []
qc_e2 = QuantumCircuit(4)
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())
qc_e2.h(0)
qc_e2.h(1)
qc_e2.h(2)
qc_e2.h(3)
qc_e2.x(0)
qc_e2.y(1)
qc_e2.z(2)
qc_e2.x(3)
qc_e2.barrier()
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())
for i in range(8):
    qc_e2.u3(0,0,np.pi/4,0)
    qc_e2.u3(np.pi/4,0,0,1) 
    qc_e2.u3(0,np.pi/4,0,2)
    qc_e2.rz(np.pi/4, 3) 
    figs.append(getBlochSphere(qc_e2.copy()))
    QCs.append(qc_e2.copy())
qc_e2.barrier()
qc_e2.x(0)
qc_e2.y(1)
qc_e2.z(2)
qc_e2.x(3)
qc_e2.h(0)
qc_e2.h(1)
qc_e2.h(2)
qc_e2.h(3)
figs.append(getBlochSphere(qc_e2.copy()))
QCs.append(qc_e2.copy())

style = {'backgroundcolor': 'lavender'}
qc_e2.draw(output='mpl', style = style)

path = "plots/bloch4/"
fname = "plots/bloch4"
fps = 5
getBlochSequence(path, figs)
getBlochGif(figs,path,fname,fps,remove = False)
print("gif file is ready!")

path = "plots/phase4/"
fname =  "plots/phase4"
fps = 5
getPhaseSequence(QCs,path,rotated=True)
#getPhaseSequence(QCs,path)
getPhaseGif(path,fname,fps=5,remove = False)
print("gif file is ready!")

figs = []
QCs = []
n = 3
q = QuantumRegister(n)
c = ClassicalRegister(n)
qc = QuantumCircuit(q,c)
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


qc.h(q[2])
qc.barrier()
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


qc.cu1(np.pi/2, q[1], q[2])
qc.barrier()
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())

qc.h(q[1])
qc.barrier()
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


qc.cu1(np.pi/4, q[0], q[2])
qc.barrier()
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


qc.cu1(np.pi/2, q[0], q[1])
qc.barrier()
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


qc.h(q[0])
qc.barrier()
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


qc.swap(q[0], q[2])
figs.append(getBlochSphere(qc.copy()))
QCs.append(qc.copy())


path = "plots/qftb/"
fname = "plots/qftb"
fps = 5
getBlochSequence(path, figs)
getBlochGif(figs,path,fname,fps,remove = False)
print("gif file is ready!")

path = "plots/qftp/"
fname =  "plots/qftp"
fps = 5
getPhaseSequence(QCs,path,rotated=True)
#getPhaseSequence(QCs,path)
getPhaseGif(path,fname,fps=5,remove = False)
print("gif file is ready!")







