class_docstr: Test integration of pwc functions with the pulse module.
class_docstr_tok: ["Test","integration","of","pwc","functions","with","the","pulse","module","."]
docstr: Test that the evolution of a ParametrizedHamiltonian defined with a function decorated by pwc_from_function
  can be executed on a QNode.
docstr_tok: ["Test","that","the","evolution","of","a","ParametrizedHamiltonian","defined","with","a","function","decorated","by","pwc_from_function","can","be","executed","on","a","QNode","."]
code: def test_qnode_pwc_from_function(self):
      """Test that the evolution of a ParametrizedHamiltonian defined with a function decorated by pwc_from_function
      can be executed on a QNode."""
      import jax
  
      @qml.pulse.pwc_from_function((2, 5), 20)
      def f1(params, t):
          return params + t
  
      @qml.pulse.pwc_from_function((3, 7), 10)
      def f2(params, t):
          return params[0] + params[1] * t**2
  
      H = f1 * qml.PauliX(0) + f2 * qml.PauliY(1)
  
      t = (1, 4)
  
      dev = qml.device("default.qubit", wires=2)
  
      @qml.qnode(dev, interface="jax")
      def circuit(params):
          qml.evolve(H)(params=params, t=t)
          return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
  
      @qml.qnode(dev, interface="jax")
      def true_circuit(params):
          # ops X0 and Y1 are commuting - time evolution of f1*X0 + f2*X1 is exp(-i*F1*X0)exp(-i*F2*Y1)
          # Where Fj = integral of fj(p,t)dt over evolution time t
          coeff1 = partial(self.integral_pwc, 2, 5, 20, (1, 4), f1)
          coeff2 = partial(self.integral_pwc, 3, 7, 10, (1, 4), f2)
          qml.prod(
              qml.exp(
                  qml.PauliX(0),
                  -1j * coeff1(params[0], pwc_from_function=True),
              ),
              qml.exp(
                  qml.PauliY(1),
                  -1j * coeff2(params[1], pwc_from_function=True),
              ),
          )
          return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
  
      params = [1.2, [2.3, 3.4]]
  
      circuit_grad_flattened, _ = jax.flatten_util.ravel_pytree(
          jax.grad(circuit)(params)
      )
      true_grad_flattened, _ = jax.flatten_util.ravel_pytree(
          jax.grad(true_circuit)(params)
      )
  
      assert qml.math.allclose(
          circuit(params), true_circuit(params), atol=5e-2
      )
      assert qml.math.allclose(
          circuit_grad_flattened, true_grad_flattened, atol=5e-2
      )
  
imports: import inspect
  from functools import partial
  import numpy
  import pytest
  import pennylane
  from pennylane.pulse.parametrized_hamiltonian import ParametrizedHamiltonian
label_desc: Use image node_id 6 for calling the TestIntegration obj's underlying member method code with example usage: obj.test_qnode_pwc_from_function() and returns: unknown, unknown, qml, qml