class_docstr: Test that tape expansion within the QNode integrates correctly
  with the Torch interface
class_docstr_tok: ["Test","that","tape","expansion","within","the","QNode","integrates","correctly","with","the","Torch","interface"]
docstr: Test that a *supported* operation with no gradient recipe is
  expanded for both parameter-shift and finite-differences, but not for execution.
docstr_tok: ["Test","that","a","*","supported","*","operation","with","no","gradient","recipe","is","expanded","for","both","parameter-shift","and","finite-differences",",","but","not","for","execution","."]
code: def test_gradient_expansion(
      self, dev, diff_method, grad_on_execution, device_vjp
  ):
      """Test that a *supported* operation with no gradient recipe is
      expanded for both parameter-shift and finite-differences, but not for execution.
      """
      if diff_method not in (
          "parameter-shift",
          "finite-diff",
          "spsa",
          "hadamard",
      ):
          pytest.skip("Only supports gradient transforms")
  
      class PhaseShift(
          qml.PhaseShift
      ):  # pylint:disable=too-few-public-methods
          grad_method = None
  
          def decomposition(self):
              return [qml.RY(3 * self.data[0], wires=self.wires)]
  
      @qnode(
          dev,
          diff_method=diff_method,
          grad_on_execution=grad_on_execution,
          max_diff=2,
          device_vjp=device_vjp,
          interface="torch",
      )
      def circuit(x):
          qml.Hadamard(wires=0)
          PhaseShift(x, wires=0)
          return qml.expval(qml.PauliX(0))
  
      x = torch.tensor(0.5, requires_grad=True, dtype=torch.float64)
  
      loss = circuit(x)
  
      loss.backward()
      res = x.grad
  
      assert torch.allclose(res, -3 * torch.sin(3 * x))
  
      if diff_method == "parameter-shift":
          # test second order derivatives
          res = torch.autograd.functional.hessian(circuit, x)
          assert torch.allclose(res, -9 * torch.cos(3 * x))
  
imports: import numpy
  import pytest
  import pennylane
  from pennylane import qnode
  from pennylane.devices import DefaultQubit
label_desc: Use image node_id 1 for calling the TestTapeExpansion obj's underlying member method code with example usage: obj.test_gradient_expansion(dev, diff_method, grad_on_execution, device_vjp) and returns: qml, list