Skip to content

Commit

Permalink
Restore mutation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelishman committed Dec 20, 2023
1 parent bbe7fc0 commit 31009c9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,15 @@ def num_parameters(self) -> int:
return len(self._parameter_table)

def _unsorted_parameters(self) -> set[Parameter]:
"""Efficiently get all parameters in the circuit, without any sorting overhead."""
"""Efficiently get all parameters in the circuit, without any sorting overhead.
.. warning::
The returned object may directly view onto the ``ParameterTable`` internals, and so
should not be mutated. This is an internal performance detail. Code outside of this
package should not use this method.
"""
# This should be free, by accessing the actual backing data structure of the table, but that
# means that we need to copy it if adding keys from the global phase.
return self._parameter_table.get_keys()

@overload
Expand Down Expand Up @@ -3045,6 +3053,7 @@ def assign_parameters( # pylint: disable=missing-raises-doc
# 'target' so we can take advantage of any caching we might be doing.
if isinstance(parameters, dict):
raw_mapping = parameters if flat_input else self._unroll_param_dict(parameters)
# Remember that we _must not_ mutate the output of `_unsorted_parameters`.
our_parameters = self._unsorted_parameters()
if strict and (extras := raw_mapping.keys() - our_parameters):
raise CircuitError(
Expand Down

0 comments on commit 31009c9

Please sign in to comment.