class_docstr: Unit tests for the pennylane.qnn.torch.TorchLayer class.
class_docstr_tok: ["Unit","tests","for","the","pennylane.qnn.torch.TorchLayer","class","."]
docstr: Test if the gradients of the TorchLayer are equal to the gradients of the circuit when
  taken with respect to the trainable variables
docstr_tok: ["Test","if","the","gradients","of","the","TorchLayer","are","equal","to","the","gradients","of","the","circuit","when","taken","with","respect","to","the","trainable","variables"]
code: def test_gradients(
      self, get_circuit, n_qubits
  ):  # pylint: disable=no-self-use
      """Test if the gradients of the TorchLayer are equal to the gradients of the circuit when
      taken with respect to the trainable variables"""
      c, w = get_circuit
      layer = TorchLayer(c, w)
      x = torch.ones(n_qubits)
  
      weights = layer.qnode_weights.values()
  
      out_layer = layer(x)
      out_layer.backward()
  
      g_layer = [w.grad for w in weights]
  
      out_circuit = torch.hstack(c(x, *weights)).type(x.dtype)
      out_circuit.backward()
  
      g_circuit = [w.grad for w in weights]
  
      for g1, g2 in zip(g_layer, g_circuit):
          assert torch.allclose(g1, g2)
      assert len(weights) == len(list(layer.parameters()))
  
imports: from unittest import mock
  import math
  from collections import defaultdict
  import numpy
  import pytest
  import pennylane
  from pennylane.qnn.torch import TorchLayer
label_desc: Use image node_id 23 for calling the TestTorchLayer obj's underlying member method code with example usage: obj.test_gradients(get_circuit, n_qubits) without return types