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

qc = QuantumCircuit(1) #Quantum circuit with one qubit


initial_state = [0,1]   # Define initial_state as |1>
qc.initialize(initial_state, 0) # Apply initialisation operation to the 0th qubit
qc.draw()  # Let's view our circuit

sim = Aer.get_backend('aer_simulator')  # Tell Qiskit how to simulate our circuit

qc.save_statevector()   # Tell simulator to save statevector
qobj = assemble(qc)     # Create a Qobj from the circuit for the simulator to run
result = sim.run(qobj).result() # Do the simulation and return the result

out_state = result.get_statevector()
print(out_state) # Display the output state vector

qc.measure_all()
qc.draw()

#instead of the statevector we will get the counts for the 0 and 1 results using .get_counts():
qobj = assemble(qc)
result = sim.run(qobj).result()
counts = result.get_counts()
plot_histogram(counts)

initial_state = [1/sqrt(2), 1j/sqrt(2)]  # Define state |q_0>


qc = QuantumCircuit(1) # Must redefine qc
qc.initialize(initial_state, 0) # Initialize the 0th qubit in the state `initial_state`
qc.save_statevector() # Save statevector
qobj = assemble(qc) # # Create a Qobj from the circuit for the simulator to run
state = sim.run(qobj).result().get_statevector() # Execute the circuit
print(state)  

results = sim.run(qobj).result().get_counts()
plot_histogram(results)

#State with 1/3 probability of measuring |0> and 2/3 of measuring |1>
initial_state2 = [1/sqrt(3), sqrt(2)/sqrt(3)]  # Define state |q_0>
qc2 = QuantumCircuit(1) # Must redefine qc
qc2.initialize(initial_state2, 0) # Initialize the 0th qubit in the state `initial_state`
qc2.save_statevector() # Save statevector
qobj2 = assemble(qc2) # # Create a Qobj from the circuit for the simulator to run
state2 = sim.run(qobj2).result().get_statevector() # Execute the circuit
print(state)  
results2 = sim.run(qobj2).result().get_counts()
plot_histogram(results2)

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

#Plotting \0>
coords = [0,1,1] # [Theta, Phi, Radius]
plot_bloch_vector_spherical(coords)

#Plotting \1>
coords = [pi,1,1] # [Theta, Phi, Radius]
plot_bloch_vector_spherical(coords)

from qiskit_textbook.widgets import bloch_calc
bloch_calc()