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:

dev = qml.device("default.qubit.jax", wires=1)
@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.40808904, dtype=float32)
>>> jax.grad(circuit)(params)
Array([-3.6517754], dtype=float32)