Skip to content

Conversation

@CalMacCQ
Copy link
Contributor

@CalMacCQ CalMacCQ commented Oct 27, 2025

closes #2617

Mostly follows Agustin's suggestion see eeb4c50

I'm also added some to/from_dict methods as well as a supported_ops property. EDIT: removed these additional methods for now.

BEGIN_COMMIT_OVERRIDE

feat: ComposablePass protocol and ComposedPass for hugr-py (unstable) (#2636)

END_COMMIT_OVERRIDE

@CalMacCQ CalMacCQ requested a review from a team as a code owner October 27, 2025 11:00
@CalMacCQ CalMacCQ requested a review from croyzor October 27, 2025 11:00
@CalMacCQ CalMacCQ marked this pull request as draft October 27, 2025 11:00
Comment on lines 19 to 22
@classmethod
def from_dict(cls, dictionary: dict) -> Self: ...

def to_dict(self) -> dict: ...
Copy link
Collaborator

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.

Copy link
Contributor Author

@CalMacCQ CalMacCQ Oct 27, 2025

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?

Copy link
Contributor Author

@CalMacCQ CalMacCQ Oct 27, 2025

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.

Copy link
Collaborator

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...

Copy link
Contributor Author

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
Copy link

codecov bot commented Oct 27, 2025

Codecov Report

❌ Patch coverage is 54.16667% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.38%. Comparing base (5e354b9) to head (01b7126).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
hugr-py/src/hugr/passes/composable_pass.py 54.16% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2636      +/-   ##
==========================================
- Coverage   83.46%   83.38%   -0.08%     
==========================================
  Files         262      261       -1     
  Lines       51297    50562     -735     
  Branches    46858    46033     -825     
==========================================
- Hits        42813    42160     -653     
+ Misses       6105     6055      -50     
+ Partials     2379     2347      -32     
Flag Coverage Δ
python 91.32% <54.16%> (-0.14%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CalMacCQ CalMacCQ marked this pull request as ready for review October 27, 2025 11:48
@CalMacCQ
Copy link
Contributor Author

After chatting to Alec it may be worth removing the supported_ops property. In TKET1 having a similar property for supported gates causes issues when we compose passes with mismatching GateSetPredicates. Its been a bit of a maintence headache. What do you think @aborgna-q?

@aborgna-q
Copy link
Collaborator

it may be worth removing the supported_ops property

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.

@CalMacCQ
Copy link
Contributor Author

Using typing_extensions rather than typing as the Self type was introduced in Python 3.11 and we still support 3.10.

@CalMacCQ CalMacCQ changed the title feat: a ComposablePass protocol for hugr-py feat(py): a ComposablePass protocol for hugr-py Oct 27, 2025
"""The main HUGR structure."""

from .base import Hugr, NodeData
from .composable_pass import ComposablePass
Copy link
Contributor Author

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.

Copy link
Collaborator

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?

@CalMacCQ CalMacCQ requested a review from ss2165 October 27, 2025 14:04
@CalMacCQ CalMacCQ requested a review from aborgna-q October 28, 2025 09:50
@CalMacCQ CalMacCQ requested a review from aborgna-q November 12, 2025 16:02
@CalMacCQ
Copy link
Contributor Author

C.I. failure to do with coverage checks or something?

Copy link
Collaborator

Choose a reason for hiding this comment

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

C.I. failure to do with coverage checks or something?

Yeah, it's complaining that there are no tests here.

You can see the codecov report linked from the message.

We could add a test with a dummy no-op pass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 01b7126

Some maybe the isinstance checks are not needed. I also made ComposablePass runtime checkable.

@CalMacCQ CalMacCQ requested a review from aborgna-q November 12, 2025 17:26
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +10 to +12
def then(self, other: ComposablePass) -> ComposablePass:
return ComposedPass([self, other])

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let it use the default implementation (we want to test that rather than test code)

Suggested change
def then(self, other: ComposablePass) -> ComposablePass:
return ComposedPass([self, other])

Copy link
Member

Choose a reason for hiding this comment

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

This suggestion wasn't implemented?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@CalMacCQ CalMacCQ added this pull request to the merge queue Nov 12, 2025
Merged via the queue into main with commit 45dc3fc Nov 12, 2025
28 of 29 checks passed
@CalMacCQ CalMacCQ deleted the cm/composable_pass branch November 12, 2025 17:46
github-merge-queue bot pushed a commit that referenced this pull request Nov 13, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.14.2](hugr-py-v0.14.1...hugr-py-v0.14.2)
(2025-11-13)


### Features

* **cli, python:** programmatic interface to cli with python bindings
([#2677](#2677))
([0fd0332](0fd0332))
* ComposablePass protocol and ComposedPass for hugr-py (unstable)
([[#2636](https://github.com/CQCL/hugr/issues/2636)](https://github.com/CQCL/hugr/pull/2636))
([45dc3fc](45dc3fc))
* return description output to python on error
([#2681](#2681))
([f483146](f483146))
* track package descriptions when loading
([#2639](#2639))
([349dd61](349dd61))


### Documentation

* Fix typo in docstring.
([#2656](#2656))
([a1ce622](a1ce622))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: Seyon Sivarajah <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Nov 18, 2025
Suggestion missing from #2636, makes sure we test the actual
implementation rather than the dummy one.

drive-by: Improve `name` impl for ComposedPass
aborgna-q added a commit that referenced this pull request Nov 24, 2025
Suggestion missing from #2636, makes sure we test the actual
implementation rather than the dummy one.

drive-by: Improve `name` impl for ComposedPass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Define a composable pass interface in Python

5 participants