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 generated by a piecewise constant Hamiltonian
  is computed correctly.
docstr_tok: ["Test","that","the","derivative","of","a","pulse","generated","by","a","piecewise","constant","Hamiltonian","is","computed","correctly","."]
code: def test_pwc_envelope_rx(self, dev_name, t):
      """Test that the derivative of a pulse generated by a piecewise constant Hamiltonian
      is computed correctly."""
      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([0.24, 0.9, -0.1, 2.3, -0.245])]
      op = qml.evolve(qml.pulse.pwc(t) * qml.PauliZ(0))(params, t)
      tape = qml.tape.QuantumScript(
          [qml.Hadamard(0), op], [qml.expval(qml.PauliX(0))]
      )
  
      # Effective rotation parameter
      p = jnp.mean(params[0]) * (T[1] - T[0])
      r = qml.execute([tape], dev, None)
      assert qml.math.isclose(r, jnp.cos(2 * p))
      num_split_times = 5
      tapes, fn = stoch_pulse_grad(
          tape, num_split_times=num_split_times, sampler_seed=7512
      )
      assert len(tapes) == 2 * num_split_times
  
      res = fn(qml.execute(tapes, dev, None))
      # The sampling of pwc functions does not automatically reduce to the analytically
      # correct time integrals, leading to approximations -> coarse tolerance
      assert qml.math.allclose(
          res,
          -2 * jnp.sin(2 * p) * (T[1] - T[0]) / len(params[0]),
          atol=0.01,
      )
      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 9 for calling the TestStochPulseGrad obj's underlying member method code with example usage: obj.test_pwc_envelope_rx(dev_name, t) without return types