class_docstr: Test working cases of stoch_pulse_grad.
class_docstr_tok: ["Test","working","cases","of","stoch_pulse_grad","."]
docstr: Test that the derivative of a pulse with a sine wave envelope
  is computed correctly when returning an expectation value.
docstr_tok: ["Test","that","the","derivative","of","a","pulse","with","a","sine","wave","envelope","is","computed","correctly","when","returning","an","expectation","value","."]
code: def test_sin_envelope_rz_expval(self, dev_name, t):
      """Test that the derivative of a pulse with a sine wave envelope
      is computed correctly when returning an expectation value."""
      import jax
      import jax.numpy as jnp
  
      T = t if isinstance(t, tuple) else (0, t)
  
      dev = qml.device(dev_name, wires=1)
      params = [jnp.array([2.3, -0.245])]
  
      ham = self.sine * qml.PauliZ(0)
      op = qml.evolve(ham)(params, t)
      tape = qml.tape.QuantumScript(
          [qml.Hadamard(0), op], [qml.expval(qml.PauliX(0))]
      )
  
      # Effective rotation parameter
      x, y = params[0]
      theta = -x / y * (jnp.cos(y * T[1]) - jnp.cos(y * T[0]))
      theta_jac = jnp.array(
          [
              theta / x,
              x / y**2 * (jnp.cos(y * T[1]) - jnp.cos(y * T[0]))
              + x
              / y
              * (jnp.sin(y * T[1]) * T[1] - jnp.sin(y * T[0]) * T[0]),
          ]
      )
      r = qml.execute([tape], dev, None)
      assert qml.math.isclose(r, jnp.cos(2 * theta))
  
      num_split_times = 5
      tapes, fn = stoch_pulse_grad(
          tape, num_split_times=num_split_times
      )
      assert len(tapes) == 2 * num_split_times
  
      res = fn(qml.execute(tapes, dev, None))
      exp_grad = -2 * jnp.sin(2 * theta) * theta_jac
      # classical Jacobian is being estimated with the Monte Carlo sampling -> coarse tolerance
      assert qml.math.allclose(res, exp_grad, atol=0.2)
      jax.clear_caches()
  
imports: import warnings
  import copy
  import pytest
  import numpy
  import pennylane
  from pennylane.gradients.general_shift_rules import eigvals_to_frequencies, generate_shift_rule
  from pennylane.gradients.pulse_gradient import _parshift_and_integrate, _split_evol_ops, _split_evol_tape, stoch_pulse_grad
label_desc: Use image node_id 6 for calling the TestStochPulseGrad obj's underlying member method code with example usage: obj.test_sin_envelope_rz_expval(dev_name, t) without return types