One Matrix for the Whole Schedule

quantum circuits · matrix product · gate order · unitary matrix

A pulse schedule for one qubit is a list of gate names, applied left to right to the state. Running a long schedule on many different starting states is faster with a single matrix M that does the whole schedule at once: new = M @ state.

Each gate is a 2\times 2 matrix (a list of rows, U[row][col]). The names are (i is 1j):

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}

written "i", "x", "y", "z", "h", "s", "t". The product of two matrices is (AB)_{rc}=\sum_k A_{rk}B_{kc}.

Write circuit_matrix(gates) that returns M, the 2\times 2 matrix with M @ state equal to the state after running the schedule on state, for every state. An empty schedule gives the identity matrix. You can test yourself: the first column of M ([M[0][0], M[1][0]]) must be the result of running the schedule on [1, 0], and the second column the result on [0, 1].

Any global phase is accepted: [[1j, 0], [0, -1j]] counts as the same matrix as [[1, 0], [0, -1]] (one common factor i). Every other difference counts.

Examples

Input:  gates = ["h", "s"]
Output: [[0.7071067811865475, 0.7071067811865475], [0.7071067811865475j, -0.7071067811865475j]]
Explanation: H acts first and S second, so M = S times H. The first column,
[0.7071..., 0.7071...j], is what the schedule does to [1, 0].

Input:  gates = ["s", "h"]
Output: [[0.7071067811865475, 0.7071067811865475j], [0.7071067811865475, -0.7071067811865475j]]

Input:  gates = []
Output: [[1, 0], [0, 1]]

Constraints

  • 0 <= len(gates) <= 1000
  • entries are compared with a tolerance of 1e-6

Goals

  • Combine a list of gates into one matrix by multiplying in reverse order
  • Check the circuit matrix against running the gates on a state
Starting Python…