class_docstr: Tests for integration with the PassthruQNode
class_docstr_tok: ["Tests","for","integration","with","the","PassthruQNode"]
docstr: Tests that the gradient of an arbitrary U3 gate is correct
  using the TF interface, using a variety of differentiation methods.
docstr_tok: ["Tests","that","the","gradient","of","an","arbitrary","U3","gate","is","correct","using","the","TF","interface",",","using","a","variety","of","differentiation","methods","."]
code: def test_torch_interface_gradient(self, operation, diff_method, tol):
      """Tests that the gradient of an arbitrary U3 gate is correct
      using the TF interface, using a variety of differentiation methods.
      """
      dev = qml.device("default.mixed", wires=1)
      state = torch.tensor(
          1j * np.array([1, -1]) / np.sqrt(2),
          requires_grad=False,
          dtype=torch.complex128,
      )
  
      @qml.qnode(dev, diff_method=diff_method, interface="torch")
      def circuit(x, weights, w):
          """In this example, a mixture of scalar
          arguments, array arguments, and keyword arguments are used."""
          qml.StatePrep(state, wires=w)
          operation(x, weights[0], weights[1], wires=w)
          return qml.expval(qml.PauliX(w))
  
      def cost(params):
          """Perform some classical processing"""
          return circuit(params[0], params[1:], w=0) ** 2
  
      theta = 0.543
      phi = -0.234
      lam = 0.654
  
      params = torch.tensor(
          [theta, phi, lam], dtype=torch.float64, requires_grad=True
      )
  
      res = cost(params)
      expected_cost = (
          np.sin(lam) * np.sin(phi)
          - np.cos(theta) * np.cos(lam) * np.cos(phi)
      ) ** 2
      assert torch.allclose(
          res, torch.tensor(expected_cost), atol=tol, rtol=0
      )
  
      # Check that the correct differentiation method is being used.
      if diff_method == "backprop":
          assert circuit.gradient_fn == "backprop"
      elif diff_method == "parameter-shift":
          assert circuit.gradient_fn is qml.gradients.param_shift
      else:
          assert circuit.gradient_fn is qml.gradients.finite_diff
  
      res.backward()
      res = params.grad
  
      expected_grad = (
          np.array(
              [
                  np.sin(theta) * np.cos(lam) * np.cos(phi),
                  np.cos(theta) * np.cos(lam) * np.sin(phi)
                  + np.sin(lam) * np.cos(phi),
                  np.cos(theta) * np.sin(lam) * np.cos(phi)
                  + np.cos(lam) * np.sin(phi),
              ]
          )
          * 2
          * (
              np.sin(lam) * np.sin(phi)
              - np.cos(theta) * np.cos(lam) * np.cos(phi)
          )
      )
      assert torch.allclose(
          res, torch.tensor(expected_grad), atol=tol, rtol=0
      )
  
imports: import pytest
  import numpy
  import pennylane
  from pennylane import numpy
  from pennylane.devices.default_mixed import DefaultMixed
label_desc: Use image node_id 12 for calling the TestPassthruIntegration obj's underlying member method code with example usage: obj.test_torch_interface_gradient(operation, diff_method, tol) and returns: qml, unknown