diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index 574c4568c1..6a5e4c3c5b 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -1189,6 +1189,24 @@ variational model. :exclude-members: ParallelBFGS +.. _Parameter: + +Parameter +--------- + +It can be useful to define custom parameters in an optimization context. For +example, the rotational angles which encodes information in a Quantum Neural Network +are usually built as a combination of features and trainable parameters. For +doing this, the :class:`qibo.parameter.Parameter` class can be used. It allows +to define custom parameters which can be inserted into a :class:`qibo.models.circuit.Circuit`. +Moreover, it automatically precomputes the analytical derivative of the parameter +function, which can be used to calculate the derivatives of a variational model +with respect to its parameters. + +.. automodule:: qibo.parameter + :members: + :member-order: bysource + .. _Gradients: Gradients diff --git a/src/qibo/parameter.py b/src/qibo/parameter.py index 92f2821dbd..64a99651f3 100644 --- a/src/qibo/parameter.py +++ b/src/qibo/parameter.py @@ -5,7 +5,7 @@ def calculate_derivatives(func): - """Calculates derivatives w.r.t to all parameters of a target function `func`.""" + """Calculates derivatives w.r.t. to all parameters of a target function `func`.""" vars = [] for i in range(func.__code__.co_argcount): vars.append(sp.Symbol(f"p{i}")) @@ -43,11 +43,10 @@ class Parameter: Args: func (function): lambda function which builds the gate parameter. If both features and trainable parameters - compose the function, it must be passed by first providing the features and then the parameters, as - described in the code example above. + compose the function, it must be passed by first providing the features and then the parameters, as + described in the code example above. features (list or np.ndarray): array containing possible input features x. trainable (list or np.ndarray): array with initial trainable parameters theta. - nofeatures (bool): flag to explicitly ban the updating of the features. This simplifies the task of updating Parameter objects simultaneously when some have embedded features and some do not. """ def __init__(self, func, trainable=None, features=None): @@ -106,7 +105,7 @@ def nfeat(self): @property def ncomponents(self): - """Returns the number of elements which compose the Parameter""" + """Return the number of elements which compose the Parameter""" return self.nparams + self.nfeat def trainable_parameter_indices(self, start_index):