Latitude and Longitude of a Qubit

Bloch sphere · global phase · relative phase · complex numbers · angles

Every one-qubit state can be drawn as a point on a globe, the Bloch sphere, and two angles say where. A state is a pair [a, b] of complex numbers, meaning a|0\rangle + b|1\rangle, with |a|^2 + |b|^2 = 1. Multiplying both by the same unit complex number (a global phase) gives the same state, so after removing it every state can be written as

|\psi\rangle = \cos\tfrac{\theta}{2}\,|0\rangle + e^{i\varphi}\sin\tfrac{\theta}{2}\,|1\rangle, \qquad 0 \le \theta \le \pi,\quad 0 \le \varphi < 2\pi.

So \cos\frac\theta2 is the size |a|, \sin\frac\theta2 is the size |b|, and \varphi is how far the angle of b is ahead of the angle of a. The angle of a complex number z is cmath.phase(z), between -\pi and \pi. \theta = 0 is the north pole |0\rangle and \theta = \pi the south pole |1\rangle. At the poles one of the amplitudes is 0 and \varphi means nothing, so the convention is: \varphi = 0 whenever \theta is within 1e-9 of 0 or of \pi.

Write bloch_angles(state) that returns the pair (theta, phi).

Examples

Input:  state = [0.7071067811865476, 0.7071067811865476j]
Output: (1.5707963267948966, 1.5707963267948966)
Explanation: equal sizes, so theta / 2 = pi / 4; b is a quarter turn ahead of a.

Input:  state = [0.6j, -0.8]
Output: (1.8545904360032246, 1.5707963267948966)
Explanation: theta = 2 * acos(0.6). The angle of a is pi / 2 and of b is pi:
b is pi / 2 ahead. Multiplying by -1j gives [0.6, 0.8j], the same state.

Input:  state = [0, 1j]
Output: (3.141592653589793, 0.0)
Explanation: the south pole; phi is 0 by the convention.

Constraints

  • answers are compared with a tolerance of 1e-6
  • the tests never have \varphi just below 2\pi, where a tiny rounding error would wrap it around to 0

Goals

  • Remove the global phase of a qubit state and keep only the relative phase
  • Turn a qubit state into its two Bloch angles theta and phi
  • Follow an angle convention exactly, including its range and its special cases
Starting Python…