code: def test_gradient_expvalD():
      r"""Test that the gradient of expval(D) computed with ``qml.grad`` is equal to the value
      obtained with the finite difference method."""
      symbols = ["H", "H", "H"]
      geometry = np.array(
          [[0.0, 0.0, 0.0], [1.0, 1.7, 0.0], [2.0, 0.0, 0.0]],
          requires_grad=False,
      )
      alpha = np.array(
          [
              [3.42525091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
          ],
          requires_grad=True,
      )
  
      mol = qchem.Molecule(symbols, geometry, charge=1, alpha=alpha)
      args = [mol.alpha]
      dev = qml.device("default.qubit", wires=6)
  
      def dipole(mol):
          @qml.qnode(dev)
          def circuit(*args):
              qml.PauliX(0)
              qml.PauliX(1)
              qml.DoubleExcitation(0.0, wires=[0, 1, 2, 3])
              qml.DoubleExcitation(0.0, wires=[0, 1, 4, 5])
              d_qubit = qchem.dipole_moment(mol)(*args)
              return qml.expval(d_qubit[0])
  
          return circuit
  
      grad_qml = qml.grad(dipole(mol))(*args)
  
      alpha_1 = np.array(
          [
              [3.42515091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
          ],
          requires_grad=False,
      )  # alpha[0][0] -= 0.0001
  
      alpha_2 = np.array(
          [
              [3.42535091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
              [3.42525091, 0.62391373, 0.1688554],
          ],
          requires_grad=False,
      )  # alpha[0][0] += 0.0001
  
      d_1 = dipole(mol)(*[alpha_1])
      d_2 = dipole(mol)(*[alpha_2])
  
      grad_finitediff = (d_2 - d_1) / 0.0002
  
      assert np.allclose(grad_qml[0][0], grad_finitediff)
  
file_path: pennylane/tests/qchem/test_dipole.py
imports: import pytest
  import pennylane
  from pennylane import PauliX, PauliY, PauliZ
  from pennylane import numpy
  from pennylane import qchem
  from pennylane.fermi import from_string
  from pennylane.operation import disable_new_opmath, enable_new_opmath
label_desc: Use image node_id 6 for calling a global function with example usage: test_gradient_expvalD() and returns: circuit, qml