class_docstr: Tests for sparse hamiltonian observable
class_docstr_tok: ["Tests","for","sparse","hamiltonian","observable"]
docstr: Test that the matrix property of the SparseHamiltonian class returns the correct matrix.
docstr_tok: ["Test","that","the","matrix","property","of","the","SparseHamiltonian","class","returns","the","correct","matrix","."]
code: def test_sparse_matrix(self, sparse_hamiltonian, tol):
      """Test that the matrix property of the SparseHamiltonian class returns the correct matrix."""
      num_wires = int(np.log2(len(sparse_hamiltonian[0])))
      sparse_hamiltonian_csr = csr_matrix(sparse_hamiltonian)
      res_dynamic = qml.SparseHamiltonian(
          sparse_hamiltonian_csr, range(num_wires)
      ).sparse_matrix()
      res_static = qml.SparseHamiltonian.compute_sparse_matrix(
          sparse_hamiltonian_csr
      )
      assert isinstance(res_dynamic, csr_matrix)
      assert isinstance(res_static, csr_matrix)
      assert np.allclose(
          res_dynamic.toarray(), sparse_hamiltonian, atol=tol, rtol=0
      )
      assert np.allclose(
          res_static.toarray(), sparse_hamiltonian, atol=tol, rtol=0
      )
  
file_path: pennylane/tests/ops/qubit/test_sparse.py
imports: import pytest
  import numpy
  from scipy.sparse import coo_matrix, csr_matrix
  import pennylane
label_desc: Use image node_id 4 for calling the TestSparse obj's underlying member method code with example usage: obj.test_sparse_matrix(sparse_hamiltonian, tol) without return types