-
Notifications
You must be signed in to change notification settings - Fork 817
feat: add abstractify helper function to core.operator module
#9694
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
Open
andrijapau
wants to merge
46
commits into
fix/skip-child-constructor-if-abstract
Choose a base branch
from
feat/abstractify-operator
base: fix/skip-child-constructor-if-abstract
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
e2dd4f3
feat: add operator2 dispatch for abstractify
andrijapau 5ebff7e
add op type dispatch
andrijapau b8d6be1
merge base
andrijapau 65b2b58
bring back abstractify
andrijapau e9211c0
Apply suggestion from @andrijapau
andrijapau 25c74e1
revert single dispatch
andrijapau feacd53
Merge branch 'feat/abstractify-operator' of github.com:PennyLaneAI/pe…
andrijapau 466205a
cl
andrijapau fb914fb
improvements
andrijapau 1239b3c
fix
andrijapau 186b2d3
fix fixed_sig type hint
andrijapau 99cadea
whoops
andrijapau 676b178
update changelog
andrijapau 1bd78f6
add tests for equal and hash
andrijapau c8fbc43
fix hashing
andrijapau 1eb4bf5
Trigger CI
andrijapau ec9be1e
add tests for equality
andrijapau ed9efe6
add repr test
andrijapau 1237157
fix wording in equal
andrijapau 31f262a
fix pylint
andrijapau 930d090
whoops, fix
andrijapau 6cdf0ad
rename some vars
andrijapau a085dea
fix up tests
andrijapau b884149
Apply suggestion from @andrijapau
andrijapau 6d2ee9c
fix tach and imports
andrijapau 1686cd0
Merge branch 'fix/skip-child-constructor-if-abstract' into feat/abstr…
andrijapau 54c25fa
use abstractify for metaclass
andrijapau 5ef7abe
Update pennylane/core/operator/operator2.py
andrijapau 0da7a60
fix
andrijapau da00b6a
fix error
andrijapau b7694c1
whoops cc
andrijapau 78631b3
Update pennylane/core/operator/operator2.py
andrijapau 26711f8
remove dead code
andrijapau 04d0c48
Merge branch 'feat/abstractify-operator' of github.com:PennyLaneAI/pe…
andrijapau 18effe1
Update pennylane/core/operator/operator2.py
andrijapau d68e4b3
Merge branch 'fix/skip-child-constructor-if-abstract' into feat/abstr…
andrijapau f314307
fix
andrijapau de9a717
Trigger CI
andrijapau b3422f1
Apply suggestion from @andrijapau
andrijapau cc0ce5a
Merge branch 'fix/skip-child-constructor-if-abstract' into feat/abstr…
andrijapau 73c0436
fix
andrijapau 47a5cc9
fix imports
andrijapau ae54a88
Merge branch 'fix/skip-child-constructor-if-abstract' into feat/abstr…
andrijapau 1a7e7bb
fix
andrijapau fcf32ab
fix logic
andrijapau 1d3962c
use new short cuts
andrijapau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Copyright 2026 Xanadu Quantum Technologies Inc. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Utilities for operators.""" | ||
|
|
||
| from functools import singledispatch | ||
| from numbers import Number | ||
|
|
||
| from pennylane import math | ||
| from pennylane.pytrees import flatten, leaf, unflatten | ||
| from pennylane.typing import AbstractArray, AbstractWires | ||
| from pennylane.wires import Wires | ||
|
|
||
|
|
||
| @singledispatch | ||
| def abstractify(val) -> AbstractArray: | ||
| """Convert the provided value into an abstract type.""" | ||
| leaves, tree = flatten(val, is_leaf=lambda x: isinstance(x, Wires)) | ||
| if tree != leaf: | ||
| abstract_leaves = tuple(abstractify(l) for l in leaves) | ||
| return unflatten(abstract_leaves, tree) | ||
|
|
||
| if isinstance(val, Number): | ||
| return AbstractArray((), type(val)) | ||
|
|
||
| shape = math.shape(val) | ||
| dtype = math.get_dtype_name(val) | ||
| return AbstractArray(shape, dtype) | ||
|
|
||
|
|
||
| @abstractify.register(type) | ||
| def _abstractify_type(val: type) -> AbstractArray: | ||
| """Abstractify a type.""" | ||
| if issubclass(val, Number): | ||
| return AbstractArray((), val) | ||
|
|
||
| raise NotImplementedError(f"Cannot abstractify type '{val}'") | ||
|
|
||
|
|
||
| @abstractify.register(Wires) | ||
| def _abstractify_wires(val: Wires) -> AbstractWires: | ||
| """Abstractify wires.""" | ||
| return AbstractWires(len(val)) | ||
|
|
||
|
|
||
| @abstractify.register(AbstractArray | AbstractWires) | ||
|
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. Should this go in |
||
| def _abstractify_abstract_type( | ||
| val: AbstractArray | AbstractWires, | ||
| ) -> AbstractArray | AbstractWires: | ||
| """Abstractify an abstract type, i.e., do nothing.""" | ||
| return val | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,8 +215,7 @@ def autograd_get_dtype_name(x): | |
| """A autograd version of get_dtype_name that can handle array boxes.""" | ||
| # abstract array comes from PL so is treated as a autograd array | ||
| if x.__class__.__name__ == "AbstractArray": | ||
| # will always be a numpy dtype | ||
| return x.dtype.name | ||
| return np.dtype(x.dtype).name | ||
|
Contributor
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. This can be reverted very soon |
||
| # this function seems to only get called with x is an arraybox. | ||
| return ar.get_dtype_name(x._value) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice