Besides fixed gates like X and H, a qubit can be turned by any angle \theta (in radians) with the three rotation gates (i is 1j):
Note the half angle \theta/2 inside. A matrix is a list of rows, and a gate U turns the state [a, b] into [U[0][0]*a + U[0][1]*b, U[1][0]*a + U[1][1]*b]. After the gate, a measurement reads 1 with probability |b'|^2, the squared size of the new second amplitude.
Write turn(axis, theta, state) that applies R_x(\theta), R_y(\theta) or R_z(\theta) (for axis equal to "x", "y" or "z") to state and returns a pair (new_state, p1): the new state as a list [a', b'] and the probability |b'|^2 of reading 1.
Any global phase of new_state is accepted: [1j, 0] is the same state as [1, 0]; a different relative phase is a different state. p1 is compared as a number.
Examples
Input: axis = "y", theta = 2, state = [1, 0]
Output: ([0.5403023058681398, 0.8414709848078965], 0.7080734182735712)
Explanation: [cos 1, sin 1], and sin(1)^2 = 0.708...
Input: axis = "x", theta = math.pi / 2, state = [1, 0]
Output: ([0.7071067811865476, -0.7071067811865475j], 0.5)
Explanation: the second amplitude is -i sin(pi/4), with a minus sign.
Input: axis = "z", theta = math.pi / 2, state = [0.6, 0.8]
Output: ([0.6, 0.8j], 0.64)
Explanation: the exact product is [0.4243 - 0.4243j, 0.5657 + 0.5657j], which is
e^{-i pi/4} times [0.6, 0.8j]: the same state up to a global phase. Rz never changes p1.
Constraints
-20 <= theta <= 20- answers are compared with a tolerance of
1e-6
Goals
- Build the rotation gates Rx, Ry and Rz from an angle
- Use the half angle theta / 2 inside the matrices
- Read the probability of outcome 1 from the new state