docstr: Test that the gradient of expval(H) computed with ``jax.grad`` is equal to the value
  obtained with the finite difference method.
docstr_tok: ["Test","that","the","gradient","of","expval","(","H",")","computed","with","``","jax.grad","``","is","equal","to","the","value","obtained","with","the","finite","difference","method","."]
code: def test_gradient_expvalH(self):
      r"""Test that the gradient of expval(H) computed with ``jax.grad`` is equal to the value
      obtained with the finite difference method."""
      import jax
  
      symbols = ["H", "H"]
      geometry = (
          np.array(
              [[0.0, 0.0, -0.3674625962], [0.0, 0.0, 0.3674625962]],
              requires_grad=False,
          )
          / 0.529177210903
      )
      alpha = np.array(
          [
              [3.42525091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
          ],
          requires_grad=True,
      )
  
      mol = qchem.Molecule(symbols, geometry, alpha=alpha)
      args = [jax.numpy.array(alpha)]
      dev = qml.device("default.qubit", wires=4)
  
      def energy(mol):
          @qml.qnode(dev, interface="jax")
          def circuit(*args):
              qml.PauliX(0)
              qml.PauliX(1)
              qml.DoubleExcitation(
                  0.22350048111151138, wires=[0, 1, 2, 3]
              )
              enable_new_opmath()
              h_qubit = qchem.diff_hamiltonian(mol)(*args)
              disable_new_opmath()
              return qml.expval(h_qubit)
  
          return circuit
  
      grad_jax = jax.grad(energy(mol), argnums=0)(*args)
  
      alpha_1 = np.array(
          [
              [3.42425091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
          ],
      )  # alpha[0][0] -= 0.001
  
      alpha_2 = np.array(
          [
              [3.42625091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
          ],
      )  # alpha[0][0] += 0.001
  
      e_1 = energy(mol)(*[alpha_1])
      e_2 = energy(mol)(*[alpha_2])
  
      grad_finitediff = (e_2 - e_1) / 0.002
  
      assert np.allclose(grad_jax[0][0], grad_finitediff, rtol=1e-02)
  
file_path: pennylane/tests/qchem/test_hamiltonians.py
imports: import pytest
  import pennylane
  from pennylane import Identity, PauliX, PauliY, PauliZ
  from pennylane import numpy
  from pennylane import qchem
  from pennylane.operation import disable_new_opmath, enable_new_opmath
  from pennylane.fermi import from_string
label_desc: Use image node_id 1 for calling the TestJax obj's underlying member method code with example usage: obj.test_gradient_expvalH() and returns: circuit, qml