class_docstr: A Hamiltonian represented directly as a sparse matrix in Compressed Sparse Row (CSR) format.
  
  .. warning::
  
      ``SparseHamiltonian`` observables can only be used to return expectation values.
      Variances and samples are not supported.
  
  **Details:**
  
  * Number of wires: Any
  * Number of parameters: 1
  * Gradient recipe: None
  
  Args:
      H (csr_matrix): a sparse matrix in SciPy Compressed Sparse Row (CSR) format with
          dimension :math:`(2^n, 2^n)`, where :math:`n` is the number of wires.
      wires (Sequence[int]): the wire(s) the operation acts on
      id (str or None): String representing the operation (optional)
  
  **Example**
  
  Sparse Hamiltonians can be constructed directly with a SciPy-compatible sparse matrix.
  
  Alternatively, you can construct your Hamiltonian as usual using :class:`~.Hamiltonian`, and then use
  :meth:`~.Hamiltonian.sparse_matrix` to construct the sparse matrix that serves as the input
  to ``SparseHamiltonian``:
  
  >>> wires = range(20)
  >>> coeffs = [1 for _ in wires]
  >>> observables = [qml.PauliZ(i) for i in wires]
  >>> H = qml.Hamiltonian(coeffs, observables)
  >>> Hmat = H.sparse_matrix()
  >>> H_sparse = qml.SparseHamiltonian(Hmat, wires)
class_docstr_tok: ["A","Hamiltonian","represented","directly","as","a","sparse","matrix","in","Compressed","Sparse","Row","(","CSR",")","format",".","..","warning",":",":","``","SparseHamiltonian","``","observables","can","only","be","used","to","return","expectation","values",".","Variances","and","samples","are","not","supported",".","*","*","Details",":","*","*","*","Number","of","wires",":","Any","*","Number","of","parameters",":","1","*","Gradient","recipe",":","None","Args",":","H","(","csr_matrix",")",":","a","sparse","matrix","in","SciPy","Compressed","Sparse","Row","(","CSR",")","format","with","dimension",":","math",":","`","(","2^n",",","2^n",")","`",",","where",":","math",":","`","n","`","is","the","number","of","wires",".","wires","(","Sequence","[","int","]",")",":","the","wire","(","s",")","the","operation","acts","on","id","(","str","or","None",")",":","String","representing","the","operation","(","optional",")","*","*","Example","*","*","Sparse","Hamiltonians","can","be","constructed","directly","with","a","SciPy-compatible","sparse","matrix",".","Alternatively",",","you","can","construct","your","Hamiltonian","as","usual","using",":","class",":","`","~.Hamiltonian","`",",","and","then","use",":","meth",":","`","~.Hamiltonian.sparse_matrix","`","to","construct","the","sparse","matrix","that","serves","as","the","input","to","``","SparseHamiltonian","``",":",">",">",">","wires","=","range","(","20",")",">",">",">","coeffs","=","[","1","for","_","in","wires","]",">",">",">","observables","=","[","qml.PauliZ","(","i",")","for","i","in","wires","]",">",">",">","H","=","qml.Hamiltonian","(","coeffs",",","observables",")",">",">",">","Hmat","=","H.sparse_matrix","(",")",">",">",">","H_sparse","=","qml.SparseHamiltonian","(","Hmat",",","wires",")"]
code: def __init__(self, H, wires=None, id=None):
      if not isinstance(H, csr_matrix):
          raise TypeError(
              "Observable must be a scipy sparse csr_matrix."
          )
      super().__init__(H, wires=wires, id=id)
      self.H = H
      mat_len = 2 ** len(self.wires)
      if H.shape != (mat_len, mat_len):
          raise ValueError(
              f"Sparse Matrix must be of shape ({mat_len}, {mat_len}). Got {H.shape}."
          )
  
imports: from copy import copy
  from collections.abc import Sequence
  import numpy
  from scipy.sparse import csr_matrix
  import pennylane
  from pennylane.operation import AnyWires, Observable, Operation
  from pennylane.wires import Wires
  from .matrix_ops import QubitUnitary
label_desc: Use image node_id 1 to create a new SparseHamiltonian object from inherited base classes: Observable with example: obj = SparseHamiltonian(H, wires, id)