class_docstr: Tests for weighted random Hamiltonian term sampling
docstr: Checks that cost functions that are expval costs can
  make use of weighted random sampling
docstr_tok: ["Checks","that","cost","functions","that","are","expval","costs","can","make","use","of","weighted","random","sampling"]
code: def test_wrs_expval_cost(self, mocker):
      """Checks that cost functions that are expval costs can
      make use of weighted random sampling"""
      coeffs = [0.2, 0.1]
      dev = qml.device("default.qubit", wires=2, shots=100)
      H = qml.Hamiltonian(
          coeffs, [qml.PauliZ(0), qml.PauliZ(0) @ qml.PauliZ(1)]
      )
  
      expval_cost = catch_warn_ExpvalCost(
          qml.templates.StronglyEntanglingLayers, H, dev
      )
      weights = np.random.random(
          qml.templates.StronglyEntanglingLayers.shape(3, 2)
      )
  
      opt = qml.ShotAdaptiveOptimizer(
          min_shots=10, term_sampling="weighted_random_sampling"
      )
      spy = mocker.spy(opt, "weighted_random_sampling")
  
      _ = opt.step(expval_cost, weights)
      spy.assert_called_once()
  
      grads = opt.weighted_random_sampling(
          expval_cost.qnodes, coeffs, 10, [0], weights
      )
      assert len(grads) == 1
      assert grads[0].shape == (10, *weights.shape)
  
imports: import pytest
  from flaky import flaky
  import pennylane
  from pennylane import numpy
label_desc: Use image node_id 1 for calling the TestWeightedRandomSampling obj's underlying member method code with example usage: obj.test_wrs_expval_cost(mocker) without return types