How to Pauli decompose a sparse matrix

Hello! I am trying to do pauli_decompose on a CSR matrix. I tried SparseHamiltonian on a sparse matrix, but looks like the qml.expval and qml.exp doesn’t return the same result as dense version.

Here is how I do this

arr = np.array([[0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0]])
qml.pauli_decompose(arr)

Then for the sparse part

row = np.array([0, 1, 2, 3, 4, 5, 6, 7])
col = np.array([3, 2, 1, 0, 7, 6, 5, 4])
data = np.array([1, 1, 1, 1, 1, 1, 1, 1])
sparse = csr_matrix((data, (row, col)), shape=(8, 8))
qml.SparseHamiltonian(sparse, wires=[0,1,2])

How can I correctly do the Pauli decomposition for a sparse matrix?

Hi @mchau ! Thank you for your question.

What is csr_matrix? Can you please share the code for this? We need this in order to try to reproduce the issue. Thanks!

1 Like

solved :slight_smile: Thank you Catalina

I’m glad you could solve it! What was the issue @mchau ?