code: def rect(
      x: Union[float, Callable],
      windows: Union[Tuple[float], List[Tuple[float]]] = None,
  ):
      """Takes a scalar or a scalar-valued function, x, and applies a rectangular window to it, such that the
      returned function is x inside the window and 0 outside it.
  
      Creates a callable for defining a :class:`~.ParametrizedHamiltonian`.
  
      Args:
          x (Union[float, Callable]): either a scalar, or a function that accepts two arguments: the trainable
              parameters and time
          windows (Union[Tuple[float], List[Tuple[float]]]): List of tuples containing time windows where ``x`` is
              evaluated. If ``None`` it is always evaluated. Defaults to ``None``.
  
      Returns:
          callable: A callable ``f(p, t)`` which evaluates the given function/scalar ``x`` inside the time windows defined in
          ``windows``, and otherwise returns 0.
  
      .. note::
          If ``x`` is a function, it must accept two arguments: the trainable parameters and time. The primary use
          of ``rect`` is for numerical simulations via :class:`ParametrizedEvolution`, which assumes ``t`` to be a single scalar
          argument. If you need to efficiently compute multiple times, you need to broadcast over ``t`` via
          `jax.vmap <https://jax.readthedocs.io/en/latest/_autosummary/jax.vmap.html>`_ (see examples below).
  
      **Example**
  
      Here we use :func:`~.rect` to create a parametrized coefficient that has a value of ``0`` outside the time interval
      ``t=(1, 7)``, and is defined by ``jnp.polyval(p, t)`` within the interval:
  
      .. code-block:: python3
  
          def f(p, t):
              return jnp.polyval(p, t)
  
          p = jnp.array([1, 2, 3])
          time = jnp.linspace(0, 10, 1000)
          windows = [(1, 7)]
  
          windowed_f = qml.pulse.rect(f, windows=windows)
  
          y1 = f(p, time)
          y2 = jax.vmap(windowed_f, (None, 0))(p, time)
  
          plt.plot(time, y1, label=f"polyval(p={p}, t)")
          plt.plot(time, y2, label=f"rect(polyval, windows={windows})(p={p}, t)")
          plt.legend()
          plt.xlabel("t")
          plt.show()
  
      .. figure:: ../../_static/pulse/rect_example.png
              :align: center
              :width: 60%
              :target: javascript:void(0);
  
      Note that in order to efficiently create ``y2``, we broadcasted ``windowed_f`` over the
      time argument using `jax.vmap <https://jax.readthedocs.io/en/latest/_autosummary/jax.vmap.html>`_.
  
      ``rect`` can be used to create a :class:`~.ParametrizedHamiltonian` in the following way:
  
      >>> H = qml.pulse.rect(jnp.polyval, windows=[(1, 7)]) * qml.PauliX(0)
  
      The resulting Hamiltonian will be non-zero only inside the window.
  
      >>> H([[1, 3]], t=2)  # inside the window
      5.0*(PauliX(wires=[0]))
  
      >>> H([[1, 3]], t=0.5 )  # outside the window
      0.0*(PauliX(wires=[0]))
  
      It is also possible to define multiple windows for the same function:
  
      .. code-block:: python
  
          windows = [(1, 7), (9, 14)]
          H = qml.pulse.rect(jnp.polyval, windows) * qml.PauliX(0)
  
      When calling the :class:`.ParametrizedHamiltonian`, ``rect`` will evaluate the given function only
      inside the time windows, and otherwise return 0.
  
      One can also pass a scalar to the ``rect`` function
  
      >>> H = qml.pulse.rect(10, (1, 7)) * qml.PauliX(0)
  
      In this case, ``rect`` will return the given scalar only when the time is inside the provided
      time windows
  
      >>> params = [None]  # the parameter value won't be used!
      >>> H(params, t=8)
      0.0*(PauliX(wires=[0]))
  
      >>> H(params, t=5)
      10.0*(PauliX(wires=[0]))
      """
      if not has_jax:
          raise ImportError(
              "Module jax is required for any pulse-related convenience function. "
              "You can install jax via: pip install jax==0.4.10 jaxlib==0.4.10"
          )
      if windows is not None:
          is_nested = any(hasattr(w, "__len__") for w in windows)
          single_window = len(windows) == 2 and not is_nested
          if single_window:
              windows = [windows]
          elif not all(
              hasattr(w, "__len__") and len(w) == 2 for w in windows
          ):
              raise ValueError(
                  "At least one provided window is not a two-element sequence."
              )
  
      if not callable(x):
  
          def _f(_, __):
              return jnp.array(x, dtype=float)
  
      else:
          _f = x
  
      def f(p, t):
          p = jnp.array(
              p, dtype=float
          )  # if p is an integer, f(p, t) will be cast to an integer
          if windows is not None:
              ti, tf = zip(*windows)
              ti, tf = jnp.array(ti), jnp.array(tf)
              return jnp.where(
                  jnp.any((t >= ti) & (t <= tf)), _f(p, t), 0
              )
          return _f(p, t)
  
      return f
  
imports: from typing import Callable, List, Tuple, Union
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: rect(x, windows) and returns: f, _f, jnp, jnp