-
Notifications
You must be signed in to change notification settings - Fork 13
feat(py): a ComposablePass protocol for hugr-py
#2636
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
base: main
Are you sure you want to change the base?
Conversation
hugr-py/src/hugr/hugr/pass.py
Outdated
| @classmethod | ||
| def from_dict(cls, dictionary: dict) -> Self: ... | ||
|
|
||
| def to_dict(self) -> dict: ... |
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.
We won't support encoding/decoding every pass, as some may have non encodable context.
Should we do a best effort to_dict encoding, and signal when the result is not complete? (so handlers downstream can get some partial info about it), or outright error-out if not supported?
For the from_dict implementation, we'll need some kind of pass registry that knows a set of passes it can decode. We can think about that 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.
What would be an example of a pass with a non-encodable context? A user defined pass where the user passes a function into the builder?
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.
Also do you think I should remove the to/from_dict for now? Not sure pass serialisation belongs in the initial work for Guppy optimisation.
We should add it at some point for nexus integration though I think.
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.
Yeah, I think it needs more design discussion.
I'm also guessing that we'd want to return a dataclass rather than an arbitrary dict, to keep some structure...
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.
Yes, good point. I'll remove it for now.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2636 +/- ##
=======================================
Coverage 83.37% 83.37%
=======================================
Files 259 260 +1
Lines 50703 50708 +5
Branches 46264 46264
=======================================
+ Hits 42275 42280 +5
Misses 6061 6061
Partials 2367 2367
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
After chatting to Alec it may be worth removing the |
Makes sense. Passes are more generic here than just rewrites over specific sets of gates, so fixing a closed set of optypes here seems counter-productive. |
|
Using |
ComposablePass protocol for hugr-pyComposablePass protocol for hugr-py
hugr-py/src/hugr/hugr/__init__.py
Outdated
| """The main HUGR structure.""" | ||
|
|
||
| from .base import Hugr, NodeData | ||
| from .composable_pass import ComposablePass |
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.
Open to better suggestions for this module name. I opted to not use pass.py as pass is a keyword in Python.
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.
LGTM
Though maybe I'd move it out of hugr.hugr, and into its own thing?
hugr.passes.ComposablePass?
|
sorry for commit spam. Was undoing some changes made by my editor. |
acl-cqc
left a comment
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.
Looks good thank you @CalMacCQ. The main issue I remember from the hugr_passes ComposablePass was wanting to pass a single runtime boolean (whether to validate or not) around all the passes...which could be plumbed in, but we didn't.
In Python...I guess we could add an options: Options | None = None parameter to run, and indeed, we could do that later, so probably no real need to do that now.
| def is_global(self) -> bool: ... | ||
|
|
||
| @property | ||
| def is_recursive(self) -> bool: ... |
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.
What does this actually mean? In particular in conjunction with is_global, I guess?
is_global == True might mean, transforms across the whole Hugr (or whole entrypoint subregion), in which case, "is_recursive" may be meaningless?
If is_global == False, perhaps that means it only affects the immediate children of the entrypoint (so the entrypoint identifies a flat circuit), in which case, recursive could mean, is also automatically applied to subcircuits beneath the entrypoint, fair enough.
But I wonder whether it'd be better to combine these into an enum-like thing where we can be more specific (and extensible) about what the possible variants mean?
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.
For some transformations (this is perhaps getting a bit complex so maybe never mind) there are actually two variables re. scope:
- how much of the Hugr to analyze (without changing) - the more you analyze, the better/more accurate your results, but it takes more time and/or memory
- how much of the Hugr to transform/change. I.e. how disruptive it can be.
(1.) probably needs to be a superset of (2.), but you might analyze the whole Hugr in order to change only one function, say (and this would enable more optimization in that function, than if you only analyzed that function alone, but the latter would be cheaper).
|
|
||
| def then(self, other: Self) -> Self: ... | ||
|
|
||
| def with_entrypoint(self, entrypoint: Node) -> Self: ... |
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.
I think it should be
def with_entrypoint(self, hugr: Hugr, entrypoint: Node) -> Self: ...rather than
def with_entrypoint(self, entrypoint: Node) -> Self: ...Implementing the with_entrypoint method could also be problematic for reasons I dicussed with Agustin earlier.
As far as I know there isn't a way to get the Hugr subgraph given a entrypoint Node in Python. I think in rust there is something like SiblingSubgraph::try_from_nodes
Agustin suggested that we could modify the entrpoint before we apply the pass and then change it back?
def with_entrypoint(self, hugr: Hugr, entrypoint: Node) -> Self:
original_entrypoint = hugr.entrypoint
hugr.entrypoint = entrypoint
# apply some transformation pass
hugr.entrypoint = orignal entrypoint
return somethingNot immediately obvious to me how this would work as we aren't returing an object of type Self. Maybe we could do something with a context manager here?
closes #2617
Mostly follows Agustin's suggestion see eeb4c50
I'm also added some
to/from_dictmethods as well as asupported_opsproperty. EDIT: removed these additional methods for now.