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, shots=many_shots_shot_vector
      )
  
      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, shots=many_shots_shot_vector
      )
      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, shots=many_shots_shot_vector
      )
      n1 = 5
      tapes, fn = spsa_grad(
          tape1,
          approx_order=1,
          strategy="forward",
          num_directions=n1,
          h=h_val,
          sampler_rng=rng,
      )
      with qml.Tracker(dev) as tracker:
          j1 = fn(dev.execute(tapes))
  
      assert len(tapes) == tracker.totals["executions"] == n1 + 1
  
      n2 = 3
      tapes, fn = spsa_grad(
          tape2,
          approx_order=1,
          strategy="forward",
          h=h_val,
          num_directions=n2,
          sampler_rng=rng,
      )
      j2 = fn(dev.execute(tapes))
  
      assert len(tapes) == n2 + 1
  
      # 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 isinstance(j1, tuple)
      assert len(j1) == len(default_shot_vector)
      assert isinstance(j2, tuple)
      assert len(j2) == len(default_shot_vector)
  
      for _j1, _j2 in zip(j1, j2):
          assert np.allclose(_j1, [exp, 0], atol=spsa_shot_vec_tol)
          assert np.allclose(_j2, [0, exp], atol=spsa_shot_vec_tol)
  
imports: import numpy
  import pytest
  from pennylane import numpy
  import pennylane
  from pennylane.gradients import spsa_grad
  from pennylane.devices import DefaultQubitLegacy
  from pennylane.measurements import Shots
  from pennylane.operation import Observable, AnyWires
label_desc: Use image node_id 13 for calling the TestSpsaGradient obj's underlying member method code with example usage: obj.test_independent_parameters() without return types