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 multiple expval outputs
docstr_tok: ["Tests","correct","output","shape","and","evaluation","for","a","tape","with","multiple","expval","outputs"]
code: def test_multiple_expectation_values(
      self, approx_order, strategy, validate
  ):
      """Tests correct output shape and evaluation for a tape
      with multiple expval outputs"""
      rng = np.random.default_rng(42)
      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.expval(qml.PauliX(1))
  
      tape = qml.tape.QuantumScript.from_queue(
          q, shots=many_shots_shot_vector
      )
      tapes, fn = spsa_grad(
          tape,
          approx_order=approx_order,
          strategy=strategy,
          validate_params=validate,
          sampler=coordinate_sampler,
          h=h_val,
          num_directions=20,
          sampler_rng=rng,
      )
      all_res = fn(dev.execute(tapes))
  
      assert isinstance(all_res, tuple)
      assert len(all_res) == len(default_shot_vector)
  
      for res in all_res:
          assert isinstance(res, tuple)
          assert len(res) == 2
  
          assert isinstance(res[0], tuple)
          assert len(res[0]) == 2
          assert isinstance(res[0][0], numpy.ndarray)
          assert isinstance(res[0][1], numpy.ndarray)
  
          assert isinstance(res[1], tuple)
          assert len(res[1]) == 2
          assert isinstance(res[1][0], numpy.ndarray)
          assert isinstance(res[1][1], numpy.ndarray)
  
          # 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.
          res_0 = (2 * res[0][0], 2 * res[0][1])
          assert np.allclose(
              res_0, [-np.sin(x), 0], atol=spsa_shot_vec_tol, rtol=0
          )
  
          res_1 = (2 * res[1][0], 2 * res[1][1])
          assert np.allclose(
              res_1, [0, np.cos(y)], 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 6 for calling the TestSpsaGradientIntegration obj's underlying member method code with example usage: obj.test_multiple_expectation_values(approx_order, strategy, validate) without return types