class_docstr: Tests for the SPSA gradient transform
class_docstr_tok: ["Tests","for","the","SPSA","gradient","transform"]
docstr: Test the case where expectation values are independent of some parameters. For those
  parameters, the gradient should be evaluated to zero without executing the device.
docstr_tok: ["Test","the","case","where","expectation","values","are","independent","of","some","parameters",".","For","those","parameters",",","the","gradient","should","be","evaluated","to","zero","without","executing","the","device","."]
code: def test_independent_parameters(self):
      """Test the case where expectation values are independent of some parameters. For those
      parameters, the gradient should be evaluated to zero without executing the device.
      """
      rng = np.random.default_rng(42)
      dev = qml.device("default.qubit", wires=2)
  
      with qml.queuing.AnnotatedQueue() as q1:
          qml.RX(1.0, wires=[0])
          qml.RX(1.0, wires=[1])
          qml.expval(qml.PauliZ(0))
  
      tape1 = qml.tape.QuantumScript.from_queue(q1)
      with qml.queuing.AnnotatedQueue() as q2:
          qml.RX(1.0, wires=[0])
          qml.RX(1.0, wires=[1])
          qml.expval(qml.PauliZ(1))
  
      tape2 = qml.tape.QuantumScript.from_queue(q2)
      n1 = 5
      tapes, fn = spsa_grad(
          tape1,
          approx_order=1,
          strategy="forward",
          num_directions=n1,
          sampler_rng=rng,
      )
      with qml.Tracker(dev) as tracker:
          j1 = fn(dev.execute(tapes))
  
      assert len(tapes) == tracker.totals["executions"] == n1 + 1
  
      n2 = 11
      tapes, fn = spsa_grad(tape2, num_directions=n2, sampler_rng=rng)
      j2 = fn(dev.execute(tapes))
  
      assert len(tapes) == 2 * n2
  
      # Because there is just a single gate parameter varied for these tapes, the gradient
      # approximation will actually be as good as finite differences.
      exp = -np.sin(1)
  
      assert np.allclose(j1, [exp, 0])
      assert np.allclose(j2, [0, exp])
  
imports: import numpy
  import pytest
  import pennylane
  from pennylane import numpy
  from pennylane.devices import DefaultQubitLegacy
  from pennylane.gradients import spsa_grad
  from pennylane.gradients.spsa_gradient import _rademacher_sampler
  from pennylane.operation import AnyWires, Observable
label_desc: Use image node_id 16 for calling the TestSpsaGradient obj's underlying member method code with example usage: obj.test_independent_parameters() without return types