qml.pulse.constant

constant(scalar, time)[source]

Returns the given scalar, for use in defining a ParametrizedHamiltonian with a trainable coefficient.

Parameters
  • scalar (float) – the scalar to be returned

  • time (float) – Time. This argument is not used, but is required to match the call signature of ParametrizedHamiltonian.

Returns

The input scalar.

Return type

float

This function is mainly used to build a ParametrizedHamiltonian that can be differentiated with respect to its time-independent term. It is an alias for lambda scalar, t: scalar.

Example

The constant function can be used to create a parametrized Hamiltonian

>>> H = qml.pulse.constant * qml.X(0)

When calling the parametrized Hamiltonian, constant will always return the input parameter

>>> params = [5]
>>> H(params, t=8)
5 * X(0)
>>> H(params, t=5)
5 * X(0)

We can differentiate the parametrized Hamiltonian with respect to the constant parameter:

import jax

jax.config.update("jax_enable_x64", True)

dev = qml.device("default.qubit")

@qml.qnode(dev, interface="jax")
def circuit(params):
    qml.evolve(H)(params, t=2)
    return qml.expval(qml.Z(0))
>>> params = jnp.array([5.0])
>>> circuit(params)
Array(0.40808193, dtype=float64)
>>> jax.grad(circuit)(params)
Array([-3.65178003], dtype=float64)