# 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[0]
alpha_r = alpha.real
alpha_i = alpha.imag

beta = statevector[1]
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/… Feb 25, 2021 at 11:45
• This must be the answer, as this is a quantum mechanics question, not really a qiskit "quantum simulation". Sep 17, 2021 at 6:55

If you look at the source code for plot_bloch_multivector(), the reverse conversion (spherical to cartesian coordinates) appears to be coded but not as a separate function. Therefore (to the best of my knowledge) it apears that the kind of methods that you mention are not available in qiskit currently (or at least they are not well-documented). I believe you can go ahead and raise an issue (potentially an enhancement request) here, as it could be a useful addition.

It is easier to calculate the spherical coordinates if the complex numbers are in polar form:

def get_spherical_coordinates(statevector):
# Convert to polar form:
r0 = np.abs(statevector[0])
ϕ0 = np.angle(statevector[0])

r1 = np.abs(statevector[1])
ϕ1 = np.angle(statevector[1])

# Calculate the coordinates:
r = np.sqrt(r0 ** 2 + r1 ** 2)
θ = 2 * np.arccos(r0 / r)
ϕ = ϕ1 - ϕ0
return [r, θ, ϕ]

# Example:
Ψ = [complex(1 / np.sqrt(2), 0), complex(1 / np.sqrt(2), 0)]
plot_bloch_vector(get_spherical_coordinates(Ψ), coord_type = 'spherical')