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 = 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
  • 1
    $\begingroup$ 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/… $\endgroup$ – chrysaor4 Feb 25 at 11:45

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.