class_docstr: Tests for sparse hamiltonian observable
class_docstr_tok: ["Tests","for","sparse","hamiltonian","observable"]
docstr: Test that expectation values of sparse hamiltonians are properly calculated.
docstr_tok: ["Test","that","expectation","values","of","sparse","hamiltonians","are","properly","calculated","."]
code: def test_sparse_hamiltonian_expval(
      self, qubits, operations, hamiltonian, expected_output, tol
  ):
      """Test that expectation values of sparse hamiltonians are properly calculated."""
      # pylint: disable=too-many-arguments
  
      hamiltonian = csr_matrix(hamiltonian)
  
      dev = qml.device("default.qubit", wires=qubits, shots=None)
      qs = qml.tape.QuantumScript(
          operations,
          [
              qml.expval(
                  (qml.SparseHamiltonian(hamiltonian, range(qubits)))
              )
          ],
      )
      expval = dev.execute(qs)
  
      assert np.allclose(expval, expected_output, 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 9 for calling the TestSparse obj's underlying member method code with example usage: obj.test_sparse_hamiltonian_expval(qubits, operations, hamiltonian, expected_output, tol) without return types