import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute
from qiskit.circuit import Gate
from qiskit.visualization import plot_bloch_multivector
from math import pi

q = QuantumRegister(3, name='q')
c = ClassicalRegister(2, name='c')
circ = QuantumCircuit(q, c)
circ.reset(q)
circ.u3(pi/2,pi/2,pi/2,q[2])
circ.h(q[1])
circ.cx(q[1],q[0])
circ.barrier()
circ.cx(q[2],q[1])
circ.h(q[2])
circ.barrier()
circ.measure([q[2],q[1]],[c[1],c[0]])
circ.barrier()
circ.x(0).c_if(c,1)
circ.z(0).c_if(c,2)
circ.x(0).c_if(c,3)
circ.z(0).c_if(c,3)
circ.draw('mpl', reverse_bits=True)

theta = pi/3
phi = pi/2
q = QuantumRegister(3, name='q')
c = ClassicalRegister(2, name='c')
circ = QuantumCircuit(q, c)
circ.reset(q)
circ.u3(theta,phi,pi,q[2])
circ.h(q[1])
circ.cx(q[1],q[0])
circ.barrier()
circ.draw('mpl', reverse_bits=True)

backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
state = job.result().get_statevector(circ)
plot_bloch_multivector(state)

circ.cx(q[2],q[1])
circ.h(q[2])
circ.barrier()
circ.measure([q[2],q[1]],[c[1],c[0]])
circ.barrier()
circ.x(0).c_if(c,1)
circ.z(0).c_if(c,2)
circ.x(0).c_if(c,3)
circ.z(0).c_if(c,3)
circ.draw('mpl', reverse_bits=True)

backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
state = job.result().get_statevector(circ)
plot_bloch_multivector(state)