class_docstr: Tests for the SPSA gradient transform
class_docstr_tok: ["Tests","for","the","SPSA","gradient","transform"]
docstr: Tests correct output shape and evaluation for a tape
  with a single expval output where all parameters are chosen to compute
  the jacobian
docstr_tok: ["Tests","correct","output","shape","and","evaluation","for","a","tape","with","a","single","expval","output","where","all","parameters","are","chosen","to","compute","the","jacobian"]
code: def test_single_expectation_value_with_argnum_all(
      self, approx_order, strategy, validate
  ):
      """Tests correct output shape and evaluation for a tape
      with a single expval output where all parameters are chosen to compute
      the jacobian"""
      rng = np.random.default_rng(5214)
      dev = qml.device(
          "default.qubit", wires=2, shots=many_shots_shot_vector
      )
      x = 0.543
      y = -0.654
  
      with qml.queuing.AnnotatedQueue() as q:
          qml.RX(x, wires=[0])
          qml.RY(y, wires=[1])
          qml.CNOT(wires=[0, 1])
          qml.expval(qml.PauliZ(0) @ qml.PauliX(1))
  
      tape = qml.tape.QuantumScript.from_queue(
          q, shots=many_shots_shot_vector
      )
      # we choose both trainable parameters
      tapes, fn = spsa_grad(
          tape,
          argnum=[0, 1],
          approx_order=approx_order,
          strategy=strategy,
          validate_params=validate,
          num_directions=10,
          sampler=coordinate_sampler,
          h=h_val,
          sampler_rng=rng,
      )
      all_res = fn(dev.execute(tapes))
  
      assert isinstance(all_res, tuple)
      assert len(all_res) == len(default_shot_vector)
  
      expected = np.array(
          [[-np.sin(y) * np.sin(x), np.cos(y) * np.cos(x)]]
      )
      for res in all_res:
          assert isinstance(res, tuple)
          assert len(res) == 2
  
          assert isinstance(res[0], numpy.ndarray)
          assert res[0].shape == ()
  
          assert isinstance(res[1], numpy.ndarray)
          assert res[1].shape == ()
  
          # The coordinate_sampler produces the right evaluation points, but the tape execution
          # results are averaged instead of added, so that we need to revert the prefactor
          # 1 / num_params here.
          assert np.allclose(
              [2 * r for r in res],
              expected,
              atol=spsa_shot_vec_tol,
              rtol=0,
          )
  
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 3 for calling the TestSpsaGradientIntegration obj's underlying member method code with example usage: obj.test_single_expectation_value_with_argnum_all(approx_order, strategy, validate) without return types