Skip to content

Commit

Permalink
Increase documentation of ParameterVector and `ParameterVectorEleme…
Browse files Browse the repository at this point in the history
…nt` (#13761)

These were somewhat lacking before, despite being fairly heavily used.
`ParameterVectorElement` was not publicly documented, despite adding
additional properties that were intended for public use.

(cherry picked from commit d03a61e)
  • Loading branch information
jakelishman authored and mergify[bot] committed Jan 31, 2025
1 parent 724d073 commit f19d1dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
7 changes: 5 additions & 2 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
* :class:`Parameter`, the atom of compile-time expressions
* :class:`ParameterExpression`, a symbolic calculation on parameters
* :class:`ParameterVector`, a convenience collection of many :class:`Parameter`\ s
* :class:`ParameterVectorElement`, a subclass of :class:`Parameter` used by :class:`ParameterVector`
The :mod:`qiskit.circuit` module also exposes some calculation classes that work with circuits to
assist compilation workflows. These include:
Expand Down Expand Up @@ -667,12 +668,14 @@
You may want to use many parameters that are related to each other. To make this easier (and to
avoid you needing to come up with many names), you can use the convenience constructor
:class:`ParameterVector`. The elements of the vector are all valid :class:`Parameter` instances.
:class:`ParameterVector`. The elements of the vector are all valid :class:`Parameter` instances, of
a special subclass :class:`ParameterVectorElement`.
.. autosummary::
:toctree: ../stubs/
ParameterVector
ParameterVectorElement
.. _circuit-control-flow-repr:
Expand Down Expand Up @@ -1289,7 +1292,7 @@ def __array__(self, dtype=None, copy=None):
from .reset import Reset
from .store import Store
from .parameter import Parameter
from .parametervector import ParameterVector
from .parametervector import ParameterVector, ParameterVectorElement
from .parameterexpression import ParameterExpression
from .quantumcircuitdata import CircuitInstruction
from .equivalence import EquivalenceLibrary
Expand Down
30 changes: 25 additions & 5 deletions qiskit/circuit/parametervector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@


class ParameterVectorElement(Parameter):
"""An element of a ParameterVector."""
"""An element of a :class:`ParameterVector`.
.. note::
There is very little reason to ever construct this class directly. Objects of this type are
automatically constructed efficiently as part of creating a :class:`ParameterVector`.
"""

___slots__ = ("_vector", "_index")

Expand Down Expand Up @@ -48,7 +53,18 @@ def __setstate__(self, state):


class ParameterVector:
"""ParameterVector class to quickly generate lists of parameters."""
"""A container of many related :class:`Parameter` objects.
This class is faster to construct than constructing many :class:`Parameter` objects
individually, and the individual names of the parameters will all share a common stem (the name
of the vector). For a vector called ``v`` with length 3, the individual elements will have
names ``v[0]``, ``v[1]`` and ``v[2]``.
The elements of a vector are sorted by the name of the vector, then the numeric value of their
index.
This class fulfill the :class:`collections.abc.Sequence` interface.
"""

__slots__ = ("_name", "_params", "_root_uuid")

Expand All @@ -62,16 +78,20 @@ def __init__(self, name, length=0):

@property
def name(self):
"""Returns the name of the ParameterVector."""
"""The name of the :class:`ParameterVector`."""
return self._name

@property
def params(self):
"""Returns the list of parameters in the ParameterVector."""
"""A list of the contained :class:`ParameterVectorElement` instances.
It is not safe to mutate this list."""
return self._params

def index(self, value):
"""Returns first index of value."""
"""Find the index of a :class:`ParameterVectorElement` within the list.
It is typically much faster to use the :attr:`ParameterVectorElement.index` property."""
return self._params.index(value)

def __getitem__(self, key):
Expand Down

0 comments on commit f19d1dd

Please sign in to comment.