class_docstr: Unit tests testing the integration of convenience functions with parametrized hamiltonians.
class_docstr_tok: ["Unit","tests","testing","the","integration","of","convenience","functions","with","parametrized","hamiltonians","."]
docstr: Test that the evolution of a parametrized hamiltonian defined with convenience functions
  can be executed on a QNode.
docstr_tok: ["Test","that","the","evolution","of","a","parametrized","hamiltonian","defined","with","convenience","functions","can","be","executed","on","a","QNode","."]
code: def test_qnode(self):
      """Test that the evolution of a parametrized hamiltonian defined with convenience functions
      can be executed on a QNode."""
      import jax
      import jax.numpy as jnp
  
      def f1(p, t):
          return p * t
  
      windows1 = [(0, 0.5), (1, 1.5)]
  
      coeffs = [qml.pulse.rect(f1, windows1), qml.pulse.constant]
      ops = [qml.PauliX(0), qml.PauliY(1)]
      H = qml.dot(coeffs, ops)
  
      t = (1, 1.1)
  
      def generator(params):
          time_step = 1e-3
          times = jnp.arange(*t, step=time_step)
          for ti in times:
              yield jax.scipy.linalg.expm(
                  -1j * time_step * qml.matrix(H(params, t=ti))
              )
  
      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))
  
      @jax.jit
      @qml.qnode(dev, interface="jax")
      def jitted_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):
          true_mat = reduce(lambda x, y: y @ x, generator(params))
          qml.QubitUnitary(U=true_mat, wires=[0, 1])
          return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
  
      params = jnp.array([1.0, 2.0])
  
      assert qml.math.allclose(
          circuit(params), true_circuit(params), atol=5e-3
      )
      assert qml.math.allclose(
          jitted_circuit(params), true_circuit(params), atol=5e-3
      )
      assert qml.math.allclose(
          jax.grad(circuit)(params),
          jax.grad(true_circuit)(params),
          atol=5e-3,
      )
      assert qml.math.allclose(
          jax.grad(jitted_circuit)(params),
          jax.grad(true_circuit)(params),
          atol=5e-3,
      )
  
imports: import inspect
  from functools import reduce
  import numpy
  import pytest
  import pennylane
  from pennylane.pulse import ParametrizedHamiltonian
label_desc: Use image node_id 2 for calling the TestIntegration obj's underlying member method code with example usage: obj.test_qnode() and returns: unknown, qml, qml, qml