class_docstr: Unit tests for the pennylane.qnn.keras.KerasLayer class.
class_docstr_tok: ["Unit","tests","for","the","pennylane.qnn.keras.KerasLayer","class","."]
docstr: Test if the gradients of the KerasLayer are equal to the gradients of the circuit when
  taken with respect to the trainable variables
docstr_tok: ["Test","if","the","gradients","of","the","KerasLayer","are","equal","to","the","gradients","of","the","circuit","when","taken","with","respect","to","the","trainable","variables"]
code: def test_gradients(
      self, get_circuit, output_dim, n_qubits
  ):  # pylint: disable=no-self-use
      """Test if the gradients of the KerasLayer are equal to the gradients of the circuit when
      taken with respect to the trainable variables"""
      c, w = get_circuit
      layer = KerasLayer(c, w, output_dim)
      x = tf.ones((1, n_qubits))
  
      with tf.GradientTape() as tape:
          out_layer = layer(x)
  
      g_layer = tape.gradient(out_layer, layer.trainable_variables)
  
      circuit_weights = layer.trainable_variables.copy()
      circuit_weights[3] = tf.convert_to_tensor(
          circuit_weights[3]
      )  # To allow for slicing
      circuit_weights[5] = tf.convert_to_tensor(circuit_weights[5])
  
      with tf.GradientTape() as tape:
          out_circuit = c(x[0], *circuit_weights)
  
      g_circuit = tape.gradient(out_circuit, layer.trainable_variables)
  
      for i in range(len(out_layer)):
          assert np.allclose(g_layer[i], g_circuit[i])
  
imports: from collections import defaultdict
  import numpy
  import pytest
  import pennylane
label_desc: Use image node_id 18 for calling the TestKerasLayer obj's underlying member method code with example usage: obj.test_gradients(get_circuit, output_dim, n_qubits) without return types