# Qiskit: is there a quick way to work out the spherical coordinates for a given qubit statevector?

I am trying to calculate the spherical coordinates of different qubit states (i.e. working out $$\theta$$ and $$\phi$$ from: $$|\psi\rangle = e^{-i\phi/2}\cos(\theta/2)|0\rangle + e^{i\phi/2}\sin(\theta/2)|1\rangle$$).

At the moment I am using the below function although I don't think it's working properly since it breaks when theta is close to zero in this line:

        phi = np.arccos((1/np.sin(theta/2))*((alpha_r*beta_r) + (alpha_i*beta_i)))



Is there a quick way to get the spherical coordinates for a state where $$\theta$$ is 0 to $$\pi$$ and $$\phi$$ is 0 to $$2\pi$$? I figure this must be built into Qiskit since when you do plot_bloch_multivector() it converts a state to a vector on a Bloch sphere.

Any ideas would be much appreciated:)

def state_coords(statevector):

alpha = statevector
alpha_r = alpha.real
alpha_i = alpha.imag

beta = statevector
beta_r = beta.real
beta_i = beta.imag

theta = np.arccos((alpha_r**2)-(alpha_i**2))

if theta==0:
phi=0
else:
phi = np.arccos((1/np.sin(theta/2))*((alpha_r*beta_r) + (alpha_i*beta_i)))

if ((alpha_r*beta_i) - (alpha_i*beta_r)) < 0:
phi += pi

return theta, phi, alpha_r, alpha_i, beta_r, beta_i
$$$$
`
• You can compute the Cartesian components with the density matrix $\rho$ via $r_x = Tr(\rho X), r_y = Tr(\rho Y), r_z = Tr(\rho Z)$, and then convert using en.wikipedia.org/wiki/… – chrysaor4 Feb 25 at 11:45