## import some libraries
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer, execute, BasicAer, IBMQ
from math import sqrt

import qiskit
print(qiskit.__qiskit_version__)

qc =QuantumCircuit(3,3)
qc.h(0)
qc.cx(0,1)
qc.cx(0,2)
qc.h(2)
qc.cx(2,0)

print(qc.depth())

qc = QuantumCircuit(1)
qc.h(0)
qc.t(0)
simulator = Aer.get_backend('statevector_simulator')
result = execute(qc,simulator).result()

from qiskit.visualization import plot_bloch_multivector

plot_bloch_multivector(result.get_statevector())

qc = QuantumCircuit(2)
qc.cx(0,1)

 ## you won't see the next line in the exam.
qc.draw('mpl')

#Answer is D
qc.draw('mpl',filename='test.png') ## check the folder where the notebook is located

qc = QuantumCircuit(2,2)
qc.h(0)
qc.x(1)
qc.measure([0,1],[0,1])
simulator=Aer.get_backend('qasm_simulator')


## Answer C

## here is the check
from qiskit.visualization import plot_histogram
job = execute(qc,simulator).result()
counts = job.get_counts()

print(counts)
plot_histogram(counts)


qreg_a = QuantumRegister(2)
qreg_b = QuantumRegister(2)
creg = ClassicalRegister(4)
qc = QuantumCircuit(qreg_a,qreg_b,creg)
qc.x(qreg_a[0])
qc.measure(qreg_a,creg[0:2])
qc.measure(qreg_b,creg[2:4])
simulator= BasicAer.get_backend('qasm_simulator')
result= execute(qc,simulator).result()
counts = result.get_counts(qc)


## check the answer

print(counts)
print("The answer is C ")
qc.draw('mpl')

from qiskit import QuantumCircuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)

##Answer is C

qc.draw('png')

# import image module
from IPython.display import Image
  
### you won't see the following lines in the exam just the plot  
# get the image
Image(url="random-unitary.png", width=600, height=600)
### in the exam you will just see the image

# import image module
from IPython.display import Image
  
# get the image
Image(url="circui1.png", width=300, height=300)
### in the exam you will just see the image

# A.

'''OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h.q[0];
barrier (q[0],q[1]);
z.q[1];
barrier (q[0], q[1]);
measure (q[0], c[0]);
measure (q[1], c[1]);
'''

# B

qc = QuantumCircuit(2,2)
qc.h(q[0])
qc.barrier(q[0],q[1])
qc.z(q[1])
qc.barrier(q[0],q[1])
m = measure(q[0] -> c[0])
m += measure(q[1] -> c[1])
qc=qc+m

# C

qc = QuantumCircuit(2,2)
qc.h(0)
qc.barrier(0,1)
qc.z(1)
qc.barrier(0,1)
qc.measure([0,1],[0,1])



#D
qc = QuantumCircuit(2,2)
qc.h(q[0])
qc.barrier(q[0],q[1])
qc.z(q[1])
qc.barrier(q[0],q[1])
m = measure(q[0], c[0])
m = measure(q[1], c[1])
qc=qc+m



### you won't see the following lines of code in the exam
from IPython.display import Image

Image(url="circui2.png", width=150, height=150)

# A
qr = QuantumRegister(2,'q')
a = QuantumRegister(1,'a')
cr = ClassicalRegister(3,'c')
qc = QuantumCircuit(qr,a,cr)
qc.h(qr[0:2])
qc.x(a[0])


# B

qr = QuantumRegister(2,'q')
a = QuantumRegister (1,'a')
cr = ClassicalRegister(3,'c')
qc = QuantumCircuit(cr,a,qr)
qc.h(qr[0:2])
qc.x(a[0])


#C 
qr = QuantumRegister(2,'q')
a = QuantumRegister (1,'a')
cr = ClassicalRegister(3,'c')
qc = QuantumCircuit(qr,a,cr)
qc.h(qr[0:1])
qc.x(a[0])

#D

qr = QReg(2,'q')
a = QReg (1,'a')
cr = CReg(3,'c')
qc = QuantumCircuit(qr,a,cr)
qc.h(qr[0:2])
qc.x(a[0])

from qiskit.tools.monitor import *

provider = IBMQ.load_account()



#provider.backends() ## this line of code can be important becuase it could be a question of your exam. 
#In other words, how do you know the backends of the provider? 

backend= provider.get_backend('ibmq_qasm_simulator')
qr = QuantumRegister(2)
cr= ClassicalRegister(2)
qc = QuantumCircuit(qr,cr)
qc.h(qr[0])
qc.cx(qr[0],qr[1])
qc.measure(qr,cr)
job = execute(qc,backend)

job.status()

job_monitor(job)

#### another could be job_watcher for jupyternoote book

from qiskit.tools.jupyter import job_watcher
%qiskit_job_watcher

job = backend.retrieve_job('61f20ee81faa0605383485a7')

result = job.result()
counts = result.get_counts()
print(counts)

qc = QuantumCircuit(3)
qc.initialize('01',[0,2])
qc.draw()
print(qc.decompose())

from qiskit.visualization import plot_error_map, plot_gate_map
backend = provider.get_backend('ibmq_quito')

plot_error_map(backend)

plot_gate_map(backend)

#A
from qiskit import QuantumCircuit, Aer, execute
from math import pi
qc = QuantumCircuit(2)

qc.crz(pi,0,1)
qc.crz(-pi,0,1)

u_sim = Aer.get_backend('unitary_simulator')
unitary = execute(qc,u_sim).result().get_unitary()
print(unitary)


#B
from qiskit import QuantumCircuit, Aer, execute
from math import pi
qc = QuantumCircuit(2)

qc.crz(pi,0,1)
qc.cp(pi,0,1)

u_sim = Aer.get_backend('unitary_simulator')
unitary = execute(qc,u_sim).result().get_unitary()
print(unitary)


#C
from qiskit import QuantumCircuit, Aer, execute
from math import pi
qc = QuantumCircuit(2)

qc.cz(0,1)
qc.cz(1,0)

u_sim = Aer.get_backend('unitary_simulator')
unitary = execute(qc,u_sim).result().get_unitary()

print(unitary)
qc.draw()

#D
from qiskit import QuantumCircuit, Aer, execute
from math import pi
qc = QuantumCircuit(2)


qc.cz(0,1)
qc.cp(pi,0,1)

u_sim = Aer.get_backend('unitary_simulator')
unitary = execute(qc,u_sim).result().get_unitary()
print(unitary)

import qiskit.tools.jupyter 
%qiskit_backend_overview

from qiskit import QuantumCircuit
qc = QuantumCircuit(3)

#insert code fragment here
#Output


### you won't see the following lines of code in the exam, just focus on the figure
from IPython.display import Image

Image(url="imageassesment1.png", width=350, height=350)



#A 
qc.measure_all()



#B
qc = QuantumCircuit(3)
qc.measure()


#C
qc = QuantumCircuit(3)
qc.measure(0,0)
qc.measure(1,1)
qc.measure(2,2)



#D

qc = QuantumCircuit(3)
for n in range(len(qc.qubits)):
    qc.measure(n,n)

qc.qubits

## here you need to display each line or lines of code before each barrier in the Qsphere, the question is about 
#to put in order the sequence of states that will appear in the Qsphere. 


qc = QuantumCircuit(3)
qc.x(1)
qc.barrier()
qc.h(0)
qc.h(1)
qc.h(2)
qc.barrier()
qc.z(1)
qc.barrier()
qc.h(0)
qc.h(1)
qc.h(2)

qc.draw('mpl')

from qiskit.visualization import plot_state_qsphere
simulator= Aer.get_backend('statevector_simulator')
result = execute(qc,simulator).result()
statevector = result.get_statevector(qc)
plot_state_qsphere(statevector)

from qiskit import BasicAer, Aer, execute
qc = QuantumCircuit(1)
qc.h(0)

#insert code fragment here

print(unitary)

#A 
simulator = BasicAer.get_backend('unitary_simulator')
unitary = execute(qc,simulator).get_unitary(qc)



#B
simulator = Aer.get_backend('unitary_simulator')
result = execute(qc,simulator).result()
unitary = result.get_unitary(qc)


#C
simulator = Aer.get_backend('statevector_simulator')
result = execute(qc,simulator).result()
unitary = result.get_matrix_result(qc)


#D
simulator = BasicAer.get_backend('statevector_simulator')
result = execute(qc,simulator).result()
unitary = result.get_unitary(qc)


#E
simulator = BasicAer.get_backend('unitary_simulator')
result = execute(qc,simulator).result()
unitary = result.get_unitary()



from qiskit.visualization import plot_bloch_vector
from math import pi, sqrt

plot_bloch_vector(vector)

#A 
vector = [1,-1,0]

#B
vector = [pi/2,-pi/4,0]

#C
vector = [1/sqrt(2),-1/sqrt(2),0]

#D
vector = [1/sqrt(2),-1/sqrt(2),-1]

from qiskit.visualization import plot_state_qsphere
qc = QuantumCircuit(3)
qc.h(0)
#qc.z(0)
qc.x(1)
qc.cx(0,1)
qc.x(2)
qc.cx(1,2)


backend = BasicAer.get_backend('statevector_simulator')
job = execute(qc, backend).result()
statevector= job.get_statevector()

plot_state_qsphere(statevector)

qc = QuantumCircuit(1)
qc.x(0)
qc.h(0)
simulator = Aer.get_backend('unitary_simulator')
job = execute(qc,simulator)
result = job.result()
outputstate = result.get_unitary(qc,1)
print(outputstate)

qc = QuantumCircuit(3,3)
qc.h([0,1,2])
qc.barrier()
qc.measure([0,1,2],range(3))
qc.draw()

print(qc.qasm())

qasm_sim = Aer.get_backend('qasm_simulator')
qc= QuantumCircuit(3)
qc.x([0,1,2])
qc.ccx(0,1,2)
qc.measure_all()



result = execute(qc,qasm_sim).result()
counts = result.get_counts()
print(counts)

qc= QuantumCircuit(3)
qc.ct()


from qiskit.quantum_info import DensityMatrix

matrix1 = [
    [1,0],[0,0]
]
matrix2 = [
    [0.5,0.5],[0.5,0.5]
]


#A
result= DensityMatrix.tensor(matrix1,matrix2)
print(result)

#B
matrix1 = DensityMatrix(matrix1)
print(matrix1.tensor(matrix2))

#C
print(matrix1.tensor(matrix2))

#D
print(DensityMatrix.tensor(matrix1,matrix2))

from qiskit.visualization import plot_state_city
qc = QuantumCircuit(2)
qc.h(0)
qc.x(1)
qc.cx(0,1)
qc.z(0)

simulator = BasicAer.get_backend('statevector_simulator')
job = execute(qc,simulator).result()
statevector = job.get_statevector()

plot_state_city(statevector)

qc = QuantumCircuit(1)

#A
#qc.ry(pi/2,0)
#qc.s(0)
#qc.rx(pi/2,0)

#B
#qc.ry(pi/2,0)
#qc.rx(pi/2,0)
#qc.s(0)

#C
#qc.s(0)
#qc.ry(pi/2,0)
#qc.rx(pi/2,0)

#D
qc.rx(pi/2,0)
qc.s(0)
qc.ry(pi/2,0)




qc.measure_all()

simulator = BasicAer.get_backend('qasm_simulator')
job = execute(qc,simulator).result()
counts = job.get_counts()

print(counts)













from qiskit.quantum_info import DensityMatrix

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)

qc1= QuantumCircuit(2)
qc1.h(0)
qc1.x(1)
qc1.cx(0,1)

#qc.draw('mpl')

rho_qc=DensityMatrix.from_instruction(qc)
rho_qc.draw()

rho1= DensityMatrix.from_instruction(qc1)
rho1.draw()

qc1new = qc1.decompose()
qc1new.draw()
#tensor1 = DensityMatrix.from_label('[[0,1],[1,0]]')

qc = QuantumCircuit(2)

#v1,v2 = [0,1],[0,1]

v = [1/sqrt(2),0,0,1/sqrt(2)]
qc.initialize(v,[0,1])


qc.draw(output='mpl')

simulator = Aer.get_backend('statevector_simulator')

result = execute(qc, simulator).result()

statevector = result.get_statevector()
print(statevector)

from qiskit.circuit.library import CXGate

ccx = CXGate().control()

qc = QuantumCircuit(3)

qc.append(ccx,[0,1,2])


qc.draw()

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer, execute

qc= QuantumCircuit(3)
qc.barrier()

qc.barrier([0])
qc.draw()

qc = QuantumCircuit.from_qasm_file('myfile.qasm')

qc.measure_all()
qc.draw(output='latex_source')



from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_state_qsphere, plot_state_paulivec, plot_state_city, plot_bloch_vector, plot_state_hinton, plot_bloch_multivector
stavec = Statevector.from_label('001')


plot_state_paulivec(stavec)

import qiskit.tools.jupyter
%qiskit_version_table

import qiskit.tools.jupyter
%qiskit_backend_overview

