8000 Deprecate legacy primitive attributes in base classes by chriseclectic · Pull Request #11051 · Qiskit/qiskit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Deprecate legacy primitive attributes in base classes #11051

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

Merged
merged 5 commits into from
Nov 1, 2023

Conversation

chriseclectic
Copy link
Member

Summary

These attributes should have already been deprecated when the Estimator and Sampler run signatures were changed to take circuits instead of indices.

Details and comments

These attributes should have already  been deprecated when the Estimator and Sampler run signatures were changed to take circuits instead of indices.
@chriseclectic chriseclectic requested review from ikkoham, t-imamichi and a team as code owners October 19, 2023 17:25
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core
  • @ajavadia
  • @ikkoham
  • @levbishop
  • @t-imamichi

@mtreinish mtreinish added this to the 0.46.0 milestone Oct 19, 2023
@jakelishman
Copy link
Member

I've nothing against this PR, I've just killed its tests til tomorrow to free up CI runners for the release-queue pipeline.

@chriseclectic chriseclectic added the Changelog: Deprecation Include in "Deprecated" section of changelog label Oct 19, 2023
@chriseclectic chriseclectic changed the title Deprecate legacy primitive attributes Deprecate legacy primitive attributes in base classes Oct 19, 2023
@woodsp-ibm woodsp-ibm added the mod: primitives Related to the Primitives module label Oct 19, 2023
@t-imamichi
Copy link
Member
t-imamichi commented Oct 23, 2023

It causes an infinite recursion for other primitive implementations.
E.g.,

from qiskit_aer.primitives import Sampler as AerSampler
from qiskit import QuantumCircuit

qc = QuantumCircuit(2, 2)
qc.measure([0, 1], [0, 1])
sampler = AerSampler()
_ = sampler.run(qc).result()

output

Traceback (most recent call last):
  File "/Users/ima/tasks/3_2023/qiskit/terra/tmp/aer_sampler.py", line 7, in <module>
    _ = sampler.run(qc).result()
  File "/Users/ima/tasks/3_2023/qiskit/terra/qiskit/primitives/base/base_sampler.py", line 165, in run
    return self._run(
  File "/Users/ima/envs/dev310/lib/python3.10/site-packages/qiskit_aer/primitives/sampler.py", line 149, in _run
    circuit_indices.append(len(self._circuits))
  File "/Users/ima/tasks/3_2023/qiskit/terra/qiskit/primitives/base/base_sampler.py", line 120, in __getattr__
    if name in dep_defaults and not hasattr(self, name):
  File "/Users/ima/tasks/3_2023/qiskit/terra/qiskit/primitives/base/base_sampler.py", line 120, in __getattr__
    if name in dep_defaults and not hasattr(self, name):
  File "/Users/ima/t
8000
asks/3_2023/qiskit/terra/qiskit/primitives/base/base_sampler.py", line 120, in __getattr__
    if name in dep_defaults and not hasattr(self, name):
  [Previous line repeated 496 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

According to Python documentation, hasattr is realized by calling getattr. So, calling hasattr in getattr causes the infinite recursion.

This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.

https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr

It might be good to check attributes by dir(self).

@t-imamichi
Copy link
Member
t-imamichi commented Oct 23, 2023

It is also necessary to raise appropriate AttributeError if an unknown attribute is given.

from qiskit.primitives import Sampler

sampler = Sampler()
sampler.foo

output

Traceback (most recent call last):
  File "/Users/ima/tasks/3_2023/qiskit/terra/tmp/11051.py", line 4, in <module>
    sampler.foo
  File "/Users/ima/tasks/3_2023/qiskit/terra/qiskit/primitives/base/base_sampler.py", line 129, in __getattr__
    return super().__getattr__(name)
AttributeError: 'super' object has no attribute '__getattr__'. Did you mean: '__setattr__'?

Should be AttributeError: 'Sampler' object has no attribute 'foo'

@t-imamichi
Copy link
Member

I made a PR to address the two issues above chriseclectic#57.

@jakelishman
Copy link
Member

The whole hasattr/dir stuff is actually skippable in general: __getattr__ is only called if the standard attribute lookups fail (i.e. the attribute doesn't exist statically), which is the exact circumstance you're trying to catch here. You can just ask the part about "is it one of the attributes we want to warn about?", because if you reach __getattr__, it already didn't exist:

In [1]: class A:
   ...:     def __init__(self):
   ...:         self.a = "hello"
   ...:     def __getattr__(self, key):
   ...:         print(f"__getattr__: '{key}'")
   ...:         raise AttributeError(f"'{type(self).__name__}' object has no attribute '{key}'")
   ...:
   ...: a = A()
   ...: a.a, a.b
__getattr__ : 'b'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line <
8000
span class="pl-c1">9
      6         raise AttributeError(f"'{type(self).__name__}' object has no attribute '{key}'")
      8 a = A()
----> 9 a.a, a.b

Cell In[1], line 6, in A.__getattr__(self, key)
      4 def __getattr__(self, key):
      5     print(f"__getattr__     : '{key}'")
----> 6     raise AttributeError(f"'{type(self).__name__}' object has no attribute '{key}'")

AttributeError: 'A' object has no attribute 'b'

Note that there's no __getattr__: 'a' line.

(If in the future you want a magic method that's always called: __getattribute__ is called on every get, before __getattr__ if applicable.)

@t-imamichi
Copy link
Member

Thank you, Jake. I simplified the PR chriseclectic#57

* fix attribute access

* simplify
@chriseclectic
Copy link
Member Author

Thanks for fixing that @t-imamichi

Copy link
Contributor
@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Looks good to me.

@ikkoham ikkoham added this pull request to the merge queue Nov 1, 2023
@coveralls
Copy link

Pull Request Test Coverage Report for Build 6714657297

  • 24 of 36 (66.67%) changed or added relevant lines in 7 files are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.001%) to 86.91%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/primitives/base/base_estimator.py 6 12 50.0%
qiskit/primitives/base/base_sampler.py 5 11 45.45%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 1 91.16%
qiskit/pulse/library/waveform.py 3 93.75%
Totals Coverage Status
Change from base Build 6713152360: 0.001%
Covered Lines: 73996
Relevant Lines: 85141

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Deprecation Include in "Deprecated" section of changelog mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0