class_docstr: Test that tape expansion within the QNode integrates correctly
  with the JAX interface
class_docstr_tok: ["Test","that","tape","expansion","within","the","QNode","integrates","correctly","with","the","JAX","interface"]
docstr: Test that the Hamiltonian is expanded if there
  are non-commuting groups and the number of shots is finite
  and the first and second order gradients are correctly evaluated
docstr_tok: ["Test","that","the","Hamiltonian","is","expanded","if","there","are","non-commuting","groups","and","the","number","of","shots","is","finite","and","the","first","and","second","order","gradients","are","correctly","evaluated"]
code: def test_hamiltonian_expansion_finite_shots(
      self,
      dev_name,
      diff_method,
      grad_on_execution,
      interface,
      max_diff,
      mocker,
  ):
      """Test that the Hamiltonian is expanded if there
      are non-commuting groups and the number of shots is finite
      and the first and second order gradients are correctly evaluated
      """
      gradient_kwargs = {}
      tol = 0.3
      if diff_method in ("adjoint", "backprop", "finite-diff"):
          pytest.skip(
              "The adjoint and backprop methods do not yet support sampling"
          )
      elif diff_method == "hadamard":
          pytest.skip("Hadamard does not yet support Hamiltonians.")
      elif diff_method == "spsa":
          gradient_kwargs = {
              "h": H_FOR_SPSA,
              "sampler_rng": np.random.default_rng(SEED_FOR_SPSA),
              "num_directions": 20,
          }
          tol = TOL_FOR_SPSA
  
      dev = qml.device(dev_name, wires=3, shots=50000)
      spy = mocker.spy(qml.transforms, "hamiltonian_expand")
      obs = [
          qml.PauliX(0),
          qml.PauliX(0) @ qml.PauliZ(1),
          qml.PauliZ(0) @ qml.PauliZ(1),
      ]
  
      @qnode(
          dev,
          interface=interface,
          diff_method=diff_method,
          grad_on_execution=grad_on_execution,
          max_diff=max_diff,
          **gradient_kwargs,
      )
      def circuit(data, weights, coeffs):
          weights = weights.reshape(1, -1)
          qml.templates.AngleEmbedding(data, wires=[0, 1])
          qml.templates.BasicEntanglerLayers(weights, wires=[0, 1])
          H = qml.Hamiltonian(coeffs, obs)
          H.compute_grouping()
          return qml.expval(H)
  
      d = jax.numpy.array([0.1, 0.2])
      w = jax.numpy.array([0.654, -0.734])
      c = jax.numpy.array([-0.6543, 0.24, 0.54])
  
      # test output
      res = circuit(d, w, c)
      expected = c[2] * np.cos(d[1] + w[1]) - c[1] * np.sin(
          d[0] + w[0]
      ) * np.sin(d[1] + w[1])
      assert np.allclose(res, expected, atol=tol)
      spy.assert_called()
  
      # test gradients
      grad = jax.grad(circuit, argnums=[1, 2])(d, w, c)
      expected_w = [
          -c[1] * np.cos(d[0] + w[0]) * np.sin(d[1] + w[1]),
          -c[1] * np.cos(d[1] + w[1]) * np.sin(d[0] + w[0])
          - c[2] * np.sin(d[1] + w[1]),
      ]
      expected_c = [
          0,
          -np.sin(d[0] + w[0]) * np.sin(d[1] + w[1]),
          np.cos(d[1] + w[1]),
      ]
      assert np.allclose(grad[0], expected_w, atol=tol)
      assert np.allclose(grad[1], expected_c, atol=tol)
  
imports: import pytest
  import pennylane
  from pennylane import numpy
  from pennylane import qnode
  from pennylane.tape import QuantumScript
label_desc: Use image node_id 3 for calling the TestTapeExpansion obj's underlying member method code with example usage: obj.test_hamiltonian_expansion_finite_shots(dev_name, diff_method, grad_on_execution, interface, max_diff, mocker) and returns: qml