from qiskit import QuantumCircuit, execute, Aer
from qiskit.visualization import plot_histogram, plot_bloch_vector
from math import sqrt, pi

%config InlineBackend.figure_format = 'svg'

qc = QuantumCircuit(1)

initialize_state = [1,0]
qc.initialize(initialize_state, 0)
qc.draw()

backend = Aer.get_backend('statevector_simulator')

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

out_state = result.get_statevector()
print(out_state)

qc.measure_all()
qc.draw()

counts = execute(qc, backend).result().get_counts()
plot_histogram(counts)

initial_state = [1/sqrt(3), complex(0, sqrt(2)/sqrt(3))]
qc = QuantumCircuit(1)
qc.initialize(initial_state, 0)
state = execute(qc, backend).result().get_statevector()
print(state)

count = execute(qc, backend).result().get_counts()
plot_histogram(count)

qc = QuantumCircuit(1) # Redefine qc
initial_state = [0.+1.j/sqrt(2),1/sqrt(2)+0.j]
qc.initialize(initial_state, [0])
qc.draw()

qc.measure_all()
qc.draw()

state = execute(qc,backend).result().get_statevector()
print("State of Measured Qubit = " + str(state))

from qiskit_textbook.widgets import plot_bloch_vector_spherical
coords = [pi/2,0,1] # [Theta, Phi, Radius]
plot_bloch_vector_spherical(coords) 

