The Unlabelled Pulse

global phase · gate identification · unitary matrix · tolerance

A calibration run measured the 2\times 2 unitary matrix m (a list of rows) of an unlabelled pulse. Measured matrices carry an arbitrary global phase: the pulse may come out as e^{i\phi}G for a named gate G, and that is the same gate, because the common factor changes nothing a measurement can see.

The named gates 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},\ S^\dagger=\begin{pmatrix}1&0\\0&-i\end{pmatrix},\ T=\begin{pmatrix}1&0\\0&e^{i\pi/4}\end{pmatrix},\ T^\dagger=\begin{pmatrix}1&0\\0&e^{-i\pi/4}\end{pmatrix}

named "i", "x", "y", "z", "h", "s", "sdg", "t", "tdg".

Write identify_gate(m) that returns the name of the gate G for which there is a complex number c with |c| = 1 and every entry of m within 1e-6 of the same entry of c\,G; return None if there is no such gate. No two named gates are equal up to a global phase, so the answer is unique. In the tests a matrix either matches a named gate to rounding error, or differs from every c\,G by more than 0.001 in some entry.

The tests use the helper with_phase(phi, m), which returns m with every entry multiplied by e^{i\phi} (phi in radians); you may use it with Run.

Examples

Input:  m = [[0, 1j], [1j, 0]]
Output: "x"
Explanation: this is i times X.

Input:  m = [[1, 0], [0, -1j]]
Output: "sdg"
Explanation: the entries have the same sizes as in S, but the top-left entries force c = 1,
and then the bottom-right entry fits S-dagger, not S.

Input:  m = [[0.6, -0.8], [0.8, 0.6]]
Output: None

Constraints

  • m is unitary

Goals

  • Recognise a gate from its matrix even when it carries a global phase
  • Compare matrices up to a common factor with a tolerance
  • Return None when no named gate matches
Starting Python…