-
Notifications
You must be signed in to change notification settings - Fork 3k
Name-ordered circuit parameters #5759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 44 commits
9ae2fb9
86e4e90
6c5962a
51ebc78
3e87ec4
2a901ed
26e30f2
9e5bb10
f85df15
0c6580f
fce5872
082297e
670c947
b66964d
3ecd42a
b791c5b
b6e41e9
44ab651
a5b4a06
e5db1b3
e775265
63ed6e1
9016715
0360063
44b2f55
5579937
5b41291
c2b81fb
bbe14ee
27813bd
5c0cccc
0f0680f
7ac2950
48275f1
34cf7f0
f15fc12
512759e
456a4ca
42e7a0a
a6fdf57
c533b99
bb768a9
7a4b0f5
a5b06dd
7f76114
721b4d3
ff9cdf9
f93cfd1
253942d
bba4b64
5681083
266d9ed
78f67e4
176b206
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,9 @@ | |
| """ | ||
| Look-up table for variable parameters in QuantumCircuit. | ||
| """ | ||
| from collections.abc import MutableMapping | ||
| import warnings | ||
| import functools | ||
| from collections.abc import MutableMapping, MappingView | ||
|
|
||
| from .instruction import Instruction | ||
|
|
||
|
|
@@ -79,3 +81,208 @@ def __len__(self): | |
|
|
||
| def __repr__(self): | ||
| return 'ParameterTable({})'.format(repr(self._table)) | ||
|
|
||
|
|
||
| def _deprecated_set_method(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this different to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It allows to print the method name in without having to hardcode it in the deprecation call. E.g. a solution using def deprecation_msg(funcname):
return f'The ParameterView.{funcname} method is deprecated as of '
'Qiskit Terra 0.17.0 and will be removed no sooner than 3 months '
'after the release date. Circuit parameters are returned as View '
'object, not set. To use set methods you can explicitly cast to a '
'set.'
# inside ParameterView
@deprecate_function(deprecation_msg('ParameterView.difference'))
def difference(...)I can change it to that if you prefer, I was just being lazy and added a quick new method instead of having to type out the name of every method I want to deprecated 😉 |
||
| def deprecate(func): | ||
| @functools.wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| # warn only once | ||
| if not wrapper._warned: | ||
| warnings.warn(f'The ParameterView.{func.__name__} method is deprecated as of ' | ||
| 'Qiskit Terra 0.17.0 and will be removed no sooner than 3 months ' | ||
| 'after the release date. Circuit parameters are returned as View ' | ||
| 'object, not set. To use set methods you can explicitly cast to a ' | ||
| 'set.', DeprecationWarning, stacklevel=2) | ||
| wrapper._warned = True | ||
| return func(*args, **kwargs) | ||
| wrapper._warned = False | ||
| return wrapper | ||
| return deprecate | ||
|
|
||
|
|
||
| class ParameterView(MappingView): | ||
| """Temporary class to transition from a set return-type to list. | ||
|
|
||
| Derives from a list but implements all set methods, but all set-methods emit deprecation | ||
| warnings. | ||
| """ | ||
|
|
||
| def __init__(self, iterable=None): | ||
| if iterable is not None: | ||
| self.data = list(iterable) | ||
| else: | ||
| self.data = [] | ||
|
|
||
| super().__init__(self.data) | ||
|
|
||
| @_deprecated_set_method() | ||
| def add(self, x): | ||
| """Add a new element.""" | ||
| if x not in self.data: | ||
| self.data.append(x) | ||
|
|
||
| def copy(self): | ||
| """Copy the ParameterView.""" | ||
| return self.__class__(self.data.copy()) | ||
|
|
||
| @_deprecated_set_method() | ||
| def difference(self, *s): | ||
| """Get the difference between self and the input.""" | ||
| return self.__sub__(s) | ||
|
|
||
| @_deprecated_set_method() | ||
| def difference_update(self, *s): | ||
| """Get the difference between self and the input in-place.""" | ||
| for element in self: | ||
| if element in s: | ||
| self.remove(element) | ||
|
|
||
| @_deprecated_set_method() | ||
| def discard(self, x): | ||
| """Remove an element from self.""" | ||
| if x in self: | ||
| self.remove(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def intersection(self, *x): | ||
| """Get the intersection between self and the input.""" | ||
| return self.__and__(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def intersection_update(self, *x): | ||
| """Get the intersection between self and the input in-place.""" | ||
| return self.__iand__(x) | ||
|
|
||
| def isdisjoint(self, x): | ||
| """Check whether self and the input are disjoint.""" | ||
| return not any(element in self for element in x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def issubset(self, x): | ||
| """Check whether self is a subset of the input.""" | ||
| return self.__le__(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def issuperset(self, x): | ||
| """Check whether self is a superset of the input.""" | ||
| return self.__ge__(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def symmetric_difference(self, x): | ||
| """Get the symmetric difference of self and the input.""" | ||
| return self.__xor__(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def symmetric_difference_update(self, x): | ||
| """Get the symmetric difference of self and the input in-place.""" | ||
| backward = x.difference(self) | ||
| self.difference_update(x) | ||
| self.update(backward) | ||
|
|
||
| @_deprecated_set_method() | ||
| def union(self, *x): | ||
| """Get the union of self and the input.""" | ||
| return self.__or__(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def update(self, *x): | ||
| """Update self with the input.""" | ||
| for element in x: | ||
| self.add(element) | ||
|
|
||
| def remove(self, x): | ||
| """Remove an existing element from the view.""" | ||
| self.data.remove(x) | ||
|
|
||
| def __repr__(self): | ||
| """Format the class as string.""" | ||
| return f'ParameterView({self.data})' | ||
|
|
||
| def __getitem__(self, index): | ||
| """Get items.""" | ||
| return self.data[index] | ||
|
|
||
| def __and__(self, x): | ||
| """Get the intersection between self and the input.""" | ||
| inter = [] | ||
| for element in self: | ||
| if element in x: | ||
| inter.append(element) | ||
|
|
||
| return self.__class__(inter) | ||
|
|
||
| def __rand__(self, x): | ||
| """Get the intersection between self and the input.""" | ||
| return self.__and__(x) | ||
|
|
||
| def __iand__(self, x): | ||
| """Get the intersection between self and the input in-place.""" | ||
| for element in self: | ||
| if element not in x: | ||
| self.remove(element) | ||
| return self | ||
|
|
||
| def __len__(self): | ||
| """Get the length.""" | ||
| return len(self.data) | ||
|
|
||
| def __or__(self, x): | ||
| """Get the union of self and the input.""" | ||
| return set(self) | set(x) | ||
|
|
||
| def __ior__(self, x): | ||
| """Update self with the input.""" | ||
| self.update(*x) | ||
| return self | ||
|
|
||
| def __sub__(self, x): | ||
| """Get the difference between self and the input.""" | ||
| return set(self) - set(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def __isub__(self, x): | ||
| """Get the difference between self and the input in-place.""" | ||
| return self.difference_update(*x) | ||
|
|
||
| def __xor__(self, x): | ||
| """Get the symmetric difference between self and the input.""" | ||
| return set(self) ^ set(x) | ||
|
|
||
| @_deprecated_set_method() | ||
| def __ixor__(self, x): | ||
| """Get the symmetric difference between self and the input in-place.""" | ||
| self.symmetric_difference_update(x) | ||
| return self | ||
|
|
||
| def __ne__(self, other): | ||
| return set(other) != set(self) | ||
|
|
||
| def __eq__(self, other): | ||
| return set(other) == set(self) | ||
|
|
||
| def __le__(self, x): | ||
| return all(element in x for element in self) | ||
|
|
||
| def __lt__(self, x): | ||
| if x != self: | ||
| return self <= x | ||
| return False | ||
|
|
||
| def __ge__(self, x): | ||
| return all(element in self for element in x) | ||
|
|
||
| def __gt__(self, x): | ||
| if x != self: | ||
| return self >= x | ||
| return False | ||
|
|
||
| def __iter__(self): | ||
| return iter(self.data) | ||
|
|
||
| def __contains__(self, x): | ||
| return x in self.data | ||
|
|
||
| __hash__: None # type: ignore | ||
| __rand__ = __and__ | ||
| __ror__ = __or__ | ||
Uh oh!
There was an error while loading. Please reload this page.