One Pulse on the Test Chip

quantum gates · matrix-vector product · complex numbers · qubit state

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

\begin{pmatrix}a'\\b'\end{pmatrix}=\begin{pmatrix}U_{00}&U_{01}\\U_{10}&U_{11}\end{pmatrix}\begin{pmatrix}a\\b\end{pmatrix},\qquad a' = U_{00}a+U_{01}b,\quad b' = U_{10}a+U_{11}b.

The gates are named by lower-case letters (i is the imaginary unit, 1j in Python):

I=\begin{pmatrix}1&0\\0&1\end{pmatrix},\ X=\begin{pmatrix}0&1\\1&0\end{pmatrix},\ Y=\begin{pmatrix}0&-i\\i&0\end{pmatrix},\ Z=\begin{pmatrix}1&0\\0&-1\end{pmatrix},\ H=\tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix},\ S=\begin{pmatrix}1&0\\0&i\end{pmatrix},\ T=\begin{pmatrix}1&0\\0&e^{i\pi/4}\end{pmatrix}

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
Starting Python…