Skip to content

Commit

Permalink
Merge pull request #1029 from qiboteam/docs
Browse files Browse the repository at this point in the history
Parameter docs
  • Loading branch information
scarrazza authored Oct 11, 2023
2 parents 8731949 + 56bcfa9 commit 72d6e67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 18 additions & 0 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/qibo/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 72d6e67

Please sign in to comment.