## Matrix to pauli string

Here we summarize how a $d$-level matrix can be written as a sum of pauli strings. 

A matrix A with elements $a_{ij}$ can be written as, 
\begin{equation}
    A=\sum_{i,j} a_{ij} \mid i \rangle \langle j \mid
\end{equation}
where $i,j$ are integers labelling the basis elements.  To map this to qubits, we first translate the integers from base 10 to binary, using some encoding $\mathcal{R}$.  which transforms basis elements as
\begin{equation}
    \mid i \rangle \rightarrow\mid\mathcal{R}(i) \rangle
        =\mid i_{n-1} \rangle \cdots \mid i_0 \rangle,
\end{equation}
where the $x_i\in\{0,1\}$. Which maps our matrix $A$ to a tensor product over local qubit terms
\begin{equation}
    A=\sum_{i,j}a_{ij}\otimes_n \mid i_{n}\rangle\langle j_n \mid.
\end{equation}
Each qubit local term can be written as a pauli string using the formulas
\begin{align}
    \mid 0 \rangle \langle 1 \mid &= \frac{1}{2}\left( X + i Y \right) \\
    \mid 1 \rangle \langle 0 \mid &= \frac{1}{2}\left( X - i Y \right) \\
    \mid 0 \rangle \langle 0 \mid &= \frac{1}{2}\left( 1 + Z \right)   \\
    \mid 1 \rangle \langle 1 \mid &= \frac{1}{2}\left( 1 - Z \right).
\end{align}


Diagonal matrices with size equal to a power of 2 have efficient implementations.  As a concrete example consider the number operator with a cutoff of 4 possible states.

\begin{equation}
    n = \begin{pmatrix} 0 & 0 & 0 & 0 \\
                        0 & 1 & 0 & 0 \\
                        0 & 0 & 2 & 0 \\
                        0 & 0 & 0 & 3 \\
        \end{pmatrix}
\end{equation}
This can be written succinctly as 
\begin{equation}
    n = 1 \mid 1 \rangle\langle 1 \mid + 2 \mid 2 \rangle\langle 2 \mid + 3 \mid 3 \rangle\langle 3 \mid
\end{equation}
By using the standard binary encoding this becomes
\begin{equation}
    n = 1 \mid 01 \rangle\langle 01 \mid + 2 \mid 10 \rangle\langle 10 \mid + 3 \mid 11 \rangle\langle 11 \mid
\end{equation}
With the above definitions for replacing qubit local terms with pauli operators this becomes 
\begin{align}
    n &= \frac{1}{4}\left[(1^1 + Z^1)(1^0 - Z^0)\right] + \frac{2}{4}\left[(1^1-Z^1)(1^0+Z^0)\right] + \frac{3}{4}\left[(1^1-Z^1)(1^0-Z^0)\right] \\
      &= \frac{1}{4}\left[1^11^0 + Z^11^0 - 1^1Z^0 - Z^1Z^0\right] \\
      &+ \frac{2}{4}\left[1^11^0 - Z^11^0 + 1^1Z^0 - Z^1Z^0\right] \\
      &+ \frac{3}{4}\left[1^11^0 - Z^11^0 - 1^1Z^0 + Z^1Z^0\right] \\
      &= \frac{3}{2}1^11^0 - Z^11^0 - \frac{1}{2}1^1Z^0
\end{align}


```python
# the code version requires you to input a matrix and an encoding
import sys
sys.path.append('..')
from src.MatrixToPauliString import *
from src.BinaryEncodings import *

import numpy as np
import sympy as sp
```


```python
matrix = np.array([[0,0,0,0],[0,1,0,0],[0,0,2,0],[0,0,0,3]])
pauli_strings = matrix_to_pauli_strings(matrix, standard_encode)
```


```python
sp.expand(pauli_strings)
```




$\displaystyle 1.5 I^{0} I^{1} - 1.0 I^{0} Z^{1} - 0.5 I^{1} Z^{0}$



f459588b8c3514406f62940797d1b78d2d114ad4
ipynb
Jupyter Notebook
tutorials/MatrixToPauliString.ipynb
daschaich/SUSY_QuantumComputing
fdf2b50c2e80a1bd5d1ebdf36629dfdd0aaf69aa
MIT
tutorials/MatrixToPauliString.ipynb
daschaich/SUSY_QuantumComputing
fdf2b50c2e80a1bd5d1ebdf36629dfdd0aaf69aa
MIT
tutorials/MatrixToPauliString.ipynb
daschaich/SUSY_QuantumComputing
fdf2b50c2e80a1bd5d1ebdf36629dfdd0aaf69aa
MIT
Qwen/Qwen-72B
1. YES
2. YES