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 expectation.
docstr_tok: ["Test","that","the","derivative","of","a","pulse","with","a","sine","wave","envelope","is","computed","correctly","when","returning","expectation","."]
code: def test_sin_envelope_rx_expval_probs(self, dev_name, t):
      """Test that the derivative of a pulse with a sine wave envelope
      is computed correctly when returning expectation."""
      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.PauliX(0)
      op = qml.evolve(ham)(params, t)
      tape = qml.tape.QuantumScript(
          [op], [qml.expval(qml.PauliZ(0)), qml.probs(wires=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)[0]
      exp = (
          jnp.cos(2 * theta),
          jnp.array([jnp.cos(theta) ** 2, jnp.sin(theta) ** 2]),
      )
      assert isinstance(r, tuple) and len(r) == 2
      assert qml.math.allclose(r[0], exp[0])
      assert qml.math.allclose(r[1], exp[1])
  
      num_split_times = 5
      tapes, fn = stoch_pulse_grad(
          tape, num_split_times=num_split_times
      )
      assert len(tapes) == 2 * num_split_times
  
      jac = fn(qml.execute(tapes, dev, None))
      expval_jac = -2 * jnp.sin(2 * theta)
      probs_jac = jnp.array([-1, 1]) * (
          2 * jnp.sin(theta) * jnp.cos(theta)
      )
      exp_jac = (
          expval_jac * theta_jac,
          jnp.tensordot(probs_jac, theta_jac, axes=0),
      )
      # classical Jacobian is being estimated with the Monte Carlo sampling -> coarse tolerance
      for j, e in zip(jac, exp_jac):
          assert qml.math.allclose(j, e, 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 8 for calling the TestStochPulseGrad obj's underlying member method code with example usage: obj.test_sin_envelope_rx_expval_probs(dev_name, t) without return types