class_docstr: Test that tapes ending with expectations
  of Hamiltonians provide correct results and gradients
class_docstr_tok: ["Test","that","tapes","ending","with","expectations","of","Hamiltonians","provide","correct","results","and","gradients"]
docstr: Cost function for gradient tests
docstr_tok: ["Cost","function","for","gradient","tests"]
code: def cost_fn(self, execute_kwargs, shots, device, use_new_op_math):
      """Cost function for gradient tests"""
  
      def _cost_fn(weights, coeffs1, coeffs2):
          obs1 = [
              qml.PauliZ(0),
              qml.PauliZ(0) @ qml.PauliX(1),
              qml.PauliY(0),
          ]
          H1 = qml.Hamiltonian(coeffs1, obs1)
          if use_new_op_math:
              H1 = qml.pauli.pauli_sentence(H1).operation()
  
          obs2 = [qml.PauliZ(0)]
          H2 = qml.Hamiltonian(coeffs2, obs2)
          if use_new_op_math:
              H2 = qml.pauli.pauli_sentence(H2).operation()
  
          with qml.queuing.AnnotatedQueue() as q:
              qml.RX(weights[0], wires=0)
              qml.RY(weights[1], wires=1)
              qml.CNOT(wires=[0, 1])
              qml.expval(H1)
              qml.expval(H2)
  
          tape = qml.tape.QuantumScript.from_queue(q, shots=shots)
          res = execute([tape], device, **execute_kwargs)[0]
          if shots.has_partitioned_shots:
              return torch.hstack(res[0] + res[1])
          return torch.hstack(res)
  
      return _cost_fn
  
imports: import numpy
  import pytest
  from param_shift_dev import ParamShiftDerivativesDevice
  import pennylane
  from pennylane.devices import DefaultQubit
  from pennylane.gradients import param_shift
  from pennylane import execute
  from pennylane.measurements import Shots
label_desc: Use image node_id 1 for calling the TestHamiltonianWorkflows obj's underlying member method code with example usage: obj.cost_fn(execute_kwargs, shots, device, use_new_op_math) and returns: _cost_fn, torch, torch