Fix logic to find real module path #2339
Open
+84
−47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
With python device kernel interoperability, users can write quantum kernels in C++ and bind them to python. In such cases, the common pattern is to have a C++ module that gets imported into a python module.
For example, if we have a python package named
foo
to which we add C++ extensions using pybind11. The common pattern is to end up with a with a module named_cppfoo
(or whaterver). Then, we import all of its symbols tofoo
:Now, if
_cppfoo
contains a binded device kernel namedbar
, then users are able to access it usingfoo.bar(...)
. This, however, is not the real path ofbar
, the real path isfoo._cppfoo.bar(..)
.Curently, binded device kernels get registered with their real path name, and thus when the python AST bridge parse another kernel that uses
foo.bar(...)
, it needs to figure it out if that is its real path or not.This commit attemps to improve the robustness of discovering this real path because as-is it fails on some simple cases.
This is how it works: In Python, many objects have a module attribute, which indicates the module in which the object was defined. This should be the case for functions. Thus the idea here is to walk the provide path until we reach the function object and ask it for its
__module__
.