Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
eeb4c50
Agustin's suggested Protocol class
CalMacCQ Oct 27, 2025
c5cf1d7
minor fixes
CalMacCQ Oct 27, 2025
8a3d6ba
more methods
CalMacCQ Oct 27, 2025
7b58c05
import Self from typing_extensions
CalMacCQ Oct 27, 2025
33e325b
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
5e31a3d
fix import
CalMacCQ Oct 27, 2025
c52de85
remove to_dict and from_dict methods as well as supported Ops
CalMacCQ Oct 27, 2025
6d7afc2
remove unused import
CalMacCQ Oct 27, 2025
89fabf0
node -> entrypoint
CalMacCQ Oct 27, 2025
91879cd
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
4878d5f
rename and update __init__.py
CalMacCQ Oct 27, 2025
7045270
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
d20aaaa
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
954730f
make a passes module
CalMacCQ Oct 28, 2025
448b5cf
move passes into hugr root
CalMacCQ Oct 28, 2025
17d1dc8
remove from build
CalMacCQ Oct 28, 2025
b6fa9a5
add __init__.py for passes
CalMacCQ Oct 28, 2025
cc9c151
undo init changes
CalMacCQ Oct 28, 2025
2d10e87
update __all__
CalMacCQ Oct 28, 2025
45bd86a
fix __all__ again
CalMacCQ Oct 28, 2025
6ca653e
minimal methods for ComposablePass
CalMacCQ Nov 12, 2025
9171dba
add ComposedPass dataclass
CalMacCQ Nov 12, 2025
c6fbfc0
fix to comp_pass
CalMacCQ Nov 12, 2025
fcf00c0
docstrings
CalMacCQ Nov 12, 2025
3ae525c
formatting stuff
CalMacCQ Nov 12, 2025
78e7522
Merge branch 'main' into cm/composable_pass
CalMacCQ Nov 12, 2025
af225a3
more ruff check
CalMacCQ Nov 12, 2025
92964a8
provide a default implementation of then() method
CalMacCQ Nov 12, 2025
f139d7c
fix default impl
CalMacCQ Nov 12, 2025
df0ab15
fixup then impl
CalMacCQ Nov 12, 2025
ac63dd5
fixup mypy and ruff
CalMacCQ Nov 12, 2025
9a157b8
yet another fix
CalMacCQ Nov 12, 2025
3db5bdd
more ruff nonsense
CalMacCQ Nov 12, 2025
67d7d38
rm self
CalMacCQ Nov 12, 2025
975ddbb
fixes
CalMacCQ Nov 12, 2025
0165ae7
Update hugr-py/src/hugr/passes/composable_pass.py
CalMacCQ Nov 12, 2025
2def5fc
fmt
CalMacCQ Nov 12, 2025
9673dc2
make name a property
CalMacCQ Nov 12, 2025
01b7126
add a dummy test
CalMacCQ Nov 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hugr-py/src/hugr/passes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""A hugr-py passes module for hugr transformations."""
24 changes: 24 additions & 0 deletions hugr-py/src/hugr/passes/composable_pass.py
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""A Protocol for a composable pass."""

from typing import Protocol

from typing_extensions import Self

from hugr.hugr.base import Hugr
from hugr.hugr.node_port import Node


class ComposablePass(Protocol):
"""A Protocol which represents a composable Hugr transformation."""

def __call__(self, hugr: Hugr) -> None: ...

def then(self, other: Self) -> Self: ...

def with_entrypoint(self, entrypoint: Node) -> Self: ...
Copy link
Contributor Author

@CalMacCQ CalMacCQ Oct 29, 2025

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 something

Not 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?


@property
def is_global(self) -> bool: ...

@property
def is_recursive(self) -> bool: ...
Copy link
Contributor

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?

Copy link
Contributor

@acl-cqc acl-cqc Oct 29, 2025

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:

  1. 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
  2. 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).

Loading