qml.math.get_trainable_indices

get_trainable_indices(values, like=None)[source]

Returns a set containing the trainable indices of a sequence of values.

Parameters

values (Iterable[tensor_like]) – Sequence of tensor-like objects to inspect

Returns

Set containing the indices of the trainable tensor-like objects within the input sequence.

Return type

set[int]

Example

>>> from pennylane import numpy as np
>>> def cost_fn(params):
...     print("Trainable:", qml.math.get_trainable_indices(params))
...     return np.sum(np.sin(params[0] * params[1]))
>>> values = [np.array([0.1, 0.2], requires_grad=True),
... np.array([0.5, 0.2], requires_grad=False)]
>>> cost_fn(values)
Trainable: {0}
tensor(0.0899685, requires_grad=True)

Contents

Using PennyLane

Release news

Development

API

Internals