# qml.pulse.constant¶

constant(scalar, time)[source]

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

Parameters
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.PauliX(0)


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

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

>>> H(params, t=5)
5*(PauliX(wires=[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.PauliZ(0))

>>> params = jnp.array([5.0])
>>> circuit(params)
Array(0.40808904, dtype=float32)

>>> jax.grad(circuit)(params)
Array([-3.6517754], dtype=float32)


Using PennyLane

Development

API

Internals