class_docstr: Differentiation tests
class_docstr_tok: ["Differentiation","tests"]
docstr: Test that differentiation works correctly when using Torch
docstr_tok: ["Test","that","differentiation","works","correctly","when","using","Torch"]
code: def test_torch(self, v):
      """Test that differentiation works correctly when using Torch"""
  
      import torch
  
      def circuit(theta):
          qml.RX(theta, wires=0)
          qml.PauliZ(wires=0)
          qml.CNOT(wires=[0, 1])
  
      def loss(theta):
          U = qml.eigvals(circuit)(theta)
          return qml.math.sum(qml.math.real(U))
  
      x = torch.tensor(v, requires_grad=True)
  
      with pytest.warns(
          UserWarning,
          match="the eigenvalues will be computed numerically",
      ):
          l = loss(x)
          l.backward()
  
      dl = x.grad
  
      assert isinstance(l, torch.Tensor)
      assert np.allclose(l.detach(), 2 * np.cos(v / 2))
      assert np.allclose(dl.detach(), -np.sin(v / 2))
  
imports: from functools import reduce
  import pytest
  import scipy
  from gate_data import CNOT, H, I, S, X, Y, Z
  import pennylane
  from pennylane import numpy
  from pennylane.transforms.op_transforms import OperationTransformError
label_desc: Use image node_id 2 for calling the TestDifferentiation obj's underlying member method code with example usage: obj.test_torch(v) and returns: qml