Skip to content

Commit

Permalink
Add uuid property to Parameter (Qiskit#11647)
Browse files Browse the repository at this point in the history
* Add uuid property to Parameter

This change adds a public, read-only uuid property to Parameter which
can be used in advanced use cases for getting the uuid value to pass to
the Parameter constructor.

* Add intersphinx reference to UUID

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Reference uuid instead of _uuid

* Add Parameter equality test

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
wshanks and mtreinish authored Jan 26, 2024
1 parent f35d157 commit f783fe8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
10 changes: 10 additions & 0 deletions qiskit/circuit/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ def name(self):
"""Returns the name of the :class:`Parameter`."""
return self._name

@property
def uuid(self) -> UUID:
"""Returns the :class:`~uuid.UUID` of the :class:`Parameter`.
In advanced use cases, this property can be passed to the
:class:`Parameter` constructor to produce an instance that compares
equal to another instance.
"""
return self._uuid

def __str__(self):
return self.name

Expand Down
8 changes: 4 additions & 4 deletions qiskit/qpy/binary_io/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@


def _write_parameter(file_obj, obj):
name_bytes = obj._name.encode(common.ENCODE)
file_obj.write(struct.pack(formats.PARAMETER_PACK, len(name_bytes), obj._uuid.bytes))
name_bytes = obj.name.encode(common.ENCODE)
file_obj.write(struct.pack(formats.PARAMETER_PACK, len(name_bytes), obj.uuid.bytes))
file_obj.write(name_bytes)


Expand All @@ -46,7 +46,7 @@ def _write_parameter_vec(file_obj, obj):
formats.PARAMETER_VECTOR_ELEMENT_PACK,
len(name_bytes),
obj._vector._size,
obj._uuid.bytes,
obj.uuid.bytes,
obj._index,
)
)
Expand Down Expand Up @@ -215,7 +215,7 @@ def _read_parameter_vec(file_obj, vectors):
if name not in vectors:
vectors[name] = (ParameterVector(name, data.vector_size), set())
vector = vectors[name][0]
if vector[data.index]._uuid != param_uuid:
if vector[data.index].uuid != param_uuid:
vectors[name][1].add(data.index)
vector._params[data.index] = ParameterVectorElement(vector, data.index, uuid=param_uuid)
return vector[data.index]
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/parameter-uuid-60dd46a739afabce.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
features:
- |
A :attr:`~qiskit.circuit.Parameter.uuid` property was added to the
:class:`qiskit.circuit.Parameter` class. In advanced use cases,
this property can be used for creating
:class:`qiskit.circuit.Parameter` instances that compare equal to
each other.
10 changes: 10 additions & 0 deletions test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def raise_if_parameter_table_invalid(circuit):
class TestParameters(QiskitTestCase):
"""Test Parameters."""

def test_equality(self):
"""Test Parameter equality"""
param = Parameter("a")
param_copy = Parameter(param.name, uuid=param.uuid)
param_different = Parameter("a")

self.assertEqual(param, param, "Parameter does not equal itself")
self.assertEqual(param, param_copy, "Parameters with same data are not equal")
self.assertNotEqual(param, param_different, "Different Parameters are treated as equal")

def test_gate(self):
"""Test instantiating gate with variable parameters"""
theta = Parameter("θ")
Expand Down

0 comments on commit f783fe8

Please sign in to comment.