A chip test log records one gate pulse at a time. Before the pulse the qubit is in the state state = [a, b], two complex numbers with |a|^2+|b|^2=1. A gate is a 2\times 2 matrix U, stored as a list of rows (U[row][col]), and the pulse replaces the state by the matrix-vector product
The gates are named by lower-case letters (i is the imaginary unit, 1j in Python):
Write apply_gate(name, state) that returns the new state [a', b'] after the gate called name (one of "i", "x", "y", "z", "h", "s", "t").
Any global phase is accepted: [1j, 0] is the same state as [1, 0], because multiplying both amplitudes by one number e^{i\phi} changes nothing that can be measured. A change of the relative phase between a and b does count: [0.6, 0.8j] is a different state from [0.6, 0.8].
Examples
Input: name = "h", state = [1, 0]
Output: [0.7071067811865475, 0.7071067811865475]
Input: name = "s", state = [0.6, 0.8]
Output: [0.6, 0.8j]
Explanation: S keeps a and multiplies b by i.
Input: name = "y", state = [0.6, 0.8]
Output: [-0.8j, 0.6j]
Explanation: a' = -i * 0.8 and b' = i * 0.6.
Constraints
- amplitudes are compared with a tolerance of
1e-6
Goals
- Apply a 2x2 gate to a qubit state with a matrix-vector product
- See which gates swap amplitudes and which only change a phase