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: Test hamiltonian with trainable parameters.
docstr_tok: ["Test","hamiltonian","with","trainable","parameters","."]
code: def test_multiple_hamiltonians_trainable(
      self, execute_kwargs, cost_fn, shots, use_new_op_math
  ):
      """Test hamiltonian with trainable parameters."""
      if execute_kwargs["gradient_fn"] == "adjoint":
          pytest.skip(
              "trainable hamiltonians not supported with adjoint"
          )
      if use_new_op_math:
          pytest.skip(
              "parameter shift derivatives do not yet support sums."
          )
  
      coeffs1 = torch.tensor([0.1, 0.2, 0.3], requires_grad=True)
      coeffs2 = torch.tensor([0.7], requires_grad=True)
      weights = torch.tensor([0.4, 0.5], requires_grad=True)
  
      res = cost_fn(weights, coeffs1, coeffs2)
      expected = self.cost_fn_expected(weights, coeffs1, coeffs2)
      if shots.has_partitioned_shots:
          assert torch.allclose(
              res[:2], expected, atol=atol_for_shots(shots), rtol=0
          )
          assert torch.allclose(
              res[2:], expected, atol=atol_for_shots(shots), rtol=0
          )
      else:
          assert torch.allclose(
              res, expected, atol=atol_for_shots(shots), rtol=0
          )
  
      res = torch.hstack(
          torch.autograd.functional.jacobian(
              cost_fn, (weights, coeffs1, coeffs2)
          )
      )
      expected = self.cost_fn_jacobian(weights, coeffs1, coeffs2)
      if shots.has_partitioned_shots:
          pytest.xfail(
              "multiple hamiltonians with shot vectors does not seem to be differentiable."
          )
      else:
          assert torch.allclose(
              res, expected, atol=atol_for_shots(shots), rtol=0
          )
  
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 5 for calling the TestHamiltonianWorkflows obj's underlying member method code with example usage: obj.test_multiple_hamiltonians_trainable(execute_kwargs, cost_fn, shots, use_new_op_math) without return types