class_docstr: Test for caching behaviour
class_docstr_tok: ["Test","for","caching","behaviour"]
docstr: Test that, when using parameter-shift transform,
  caching reduces the number of evaluations to their optimum.
docstr_tok: ["Test","that",",","when","using","parameter-shift","transform",",","caching","reduces","the","number","of","evaluations","to","their","optimum","."]
code: def test_caching_param_shift(self):
      """Test that, when using parameter-shift transform,
      caching reduces the number of evaluations to their optimum."""
      dev = qml.device("default.qubit.legacy", wires=1)
      a = tf.Variable([0.1, 0.2], dtype=tf.float64)
  
      def cost(a, cache):
          with qml.queuing.AnnotatedQueue() as q:
              qml.RY(a[0], wires=0)
              qml.RX(a[1], wires=0)
              qml.probs(wires=0)
  
          tape = qml.tape.QuantumScript.from_queue(q)
          return execute(
              [tape],
              dev,
              gradient_fn=param_shift,
              cache=cache,
              interface="tf",
          )[0]
  
      # Without caching, and non-vectorized, 9 evaluations are required to compute
      # the Jacobian: 1 (forward pass) + 2 (backward pass) * (2 shifts * 2 params)
      with tf.GradientTape(persistent=True) as t:
          res = cost(a, cache=None)
      t.jacobian(res, a, experimental_use_pfor=False)
      assert dev.num_executions == 9
  
      # With caching, and non-vectorized, 5 evaluations are required to compute
      # the Jacobian: 1 (forward pass) + (2 shifts * 2 params)
      dev._num_executions = 0
      with tf.GradientTape(persistent=True) as t:
          res = cost(a, cache=True)
      t.jacobian(res, a)
      assert dev.num_executions == 5
  
      # In vectorized mode, 5 evaluations are required to compute
      # the Jacobian regardless of caching: 1 (forward pass) + (2 shifts * 2 params)
      dev._num_executions = 0
      with tf.GradientTape() as t:
          res = cost(a, cache=None)
      t.jacobian(res, a)
      assert dev.num_executions == 5
  
imports: import numpy
  import pytest
  import pennylane
  from pennylane.gradients import finite_diff, param_shift
  from pennylane import execute
label_desc: Use image node_id 3 for calling the TestCaching obj's underlying member method code with example usage: obj.test_caching_param_shift() and returns: execute