class_docstr: Tests that correct gradients are obtained for `CommutingEvolution` when frequencies
  are specified.
class_docstr_tok: ["Tests","that","correct","gradients","are","obtained","for","`","CommutingEvolution","`","when","frequencies","are","specified","."]
docstr: Tests correct gradients are produced when the Hamiltonian is differentiable.
docstr_tok: ["Tests","correct","gradients","are","produced","when","the","Hamiltonian","is","differentiable","."]
code: def test_differentiable_hamiltonian(self):
      """Tests correct gradients are produced when the Hamiltonian is differentiable."""
  
      n_wires = 2
      dev = qml.device("default.qubit", wires=n_wires)
      obs = [
          qml.PauliX(0) @ qml.PauliY(1),
          qml.PauliY(0) @ qml.PauliX(1),
      ]
      diff_coeffs = np.array([1.0, -1.0], requires_grad=True)
      frequencies = (2, 4)
  
      def parameterized_hamiltonian(coeffs):
          return qml.Hamiltonian(coeffs, obs)
  
      @qml.qnode(dev)
      def circuit(time, coeffs):
          qml.PauliX(0)
          qml.CommutingEvolution(
              parameterized_hamiltonian(coeffs), time, frequencies
          )
          return qml.expval(qml.PauliZ(0))
  
      x_vals = [
          np.array(x, requires_grad=True)
          for x in np.linspace(-np.pi, np.pi, num=10)
      ]
  
      grads_finite_diff = [
          np.hstack(qml.gradients.finite_diff(circuit)(x, diff_coeffs))
          for x in x_vals
      ]
      grads_param_shift = [
          np.hstack(qml.gradients.param_shift(circuit)(x, diff_coeffs))
          for x in x_vals
      ]
  
      assert np.isclose(
          grads_finite_diff, grads_param_shift, atol=1e-6
      ).all()
  
file_path: pennylane/tests/templates/test_subroutines/test_commuting_evolution.py
imports: import pytest
  from scipy.linalg import expm
  import pennylane
  from pennylane import numpy
label_desc: Use image node_id 3 for calling the TestGradients obj's underlying member method code with example usage: obj.test_differentiable_hamiltonian() and returns: qml, qml