-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add inlining annotation + pass #1449
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
Draft
acl-cqc
wants to merge
29
commits into
main
Choose a base branch
from
acl/inline
base: main
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.
Draft
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
05e3fb5
WIP add inlining annotation + pass
acl-cqc 51a4e89
tests
acl-cqc 9e9e722
Simplify cycle check, only need first cycle
acl-cqc 30d924f
inline always funcs leaves first
acl-cqc ef247fe
Respect entrypoint? Oooph!
acl-cqc 2a70f5b
Strange derive_more, make compile
acl-cqc 865b6dd
Don't RemDeadFuncs (be selective); skip cycles for only-called-once
acl-cqc 4ad40ea
Fix test: inlined func is removed
acl-cqc 896e36a
TODO test entrypoint_scope
acl-cqc 07871d5
cycles return iter, clippy
acl-cqc 849bf73
more TODOs
acl-cqc eecf9b7
rm doclink
acl-cqc d1d1615
Merge remote-tracking branch 'origin/main' into acl/inline
acl-cqc 1dd388a
Move annotation
acl-cqc c25a2cd
Remove only-called-once, simplify test
acl-cqc 2bd976d
Simplify, clarify/comment
acl-cqc 6fd9f0e
simplify: inline do_inline
acl-cqc 5bbabec
renaming, re-export
acl-cqc 0e3ad97
entrypoint_scope test
acl-cqc 9fd49cf
test cycle of one always and one not
acl-cqc 66e02c1
Add python (needs test)
acl-cqc 677e48e
clippy
acl-cqc 9284958
doc
acl-cqc 312f07b
fmt + missing,
acl-cqc dbcf612
first python test
acl-cqc 4554462
test failure, fiddling with imports, including type: ignore
acl-cqc 8151e43
ruff format
acl-cqc a521495
improve tket-py rs comment
acl-cqc ce8d5b7
Declare InlineAlwaysError in passes.pyi
acl-cqc 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //! Python bindings for the `InlineFunctions` pass. | ||
|
|
||
| use pyo3::prelude::*; | ||
|
|
||
| use tket::passes::{ComposablePass, WithScope}; | ||
|
|
||
| use super::PyPassScope; | ||
| use crate::state::CompilationState; | ||
| use crate::utils::ConvertPyErr; | ||
|
|
||
| /// Inline acyclic function calls below the selected scope. | ||
| /// | ||
| /// Parameters: | ||
| /// - heuristic: Heuristic used to choose which non-recursive functions to | ||
| /// inline. Defaults to `tket.passes.MaxSize(64)`. | ||
| /// - follow_inline_hints: Whether to follow compiler hints for inlining | ||
| /// functions. | ||
| #[pyfunction] | ||
| #[pyo3(signature = (circ, *, scope = None))] | ||
| pub(super) fn inline_always( | ||
| circ: &mut CompilationState, | ||
| scope: Option<PyPassScope>, | ||
| ) -> PyResult<()> { | ||
| let py_scope = scope.unwrap_or_default(); | ||
| let pass = tket::passes::InlineAlwaysPass::default_with_scope(py_scope.scope); | ||
| pass.run(&mut circ.hugr).convert_pyerrs()?; | ||
| Ok(()) | ||
| } |
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 |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| from tket.passes import ( | ||
| _badger_optimise, | ||
| _greedy_depth_reduce, | ||
| InlineAlwaysError, | ||
| InlineAlwaysPass, | ||
| InlineFunctions, | ||
| inline_funcs, | ||
| NormalizeGuppy, | ||
|
|
@@ -23,6 +25,7 @@ | |
| from tket.passes import PytketHugrPass | ||
| from pytket.passes import CliffordSimp, SquashRzPhasedX, SequencePass | ||
| from hugr.build.base import Hugr | ||
| import hugr.tys as tys | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
@@ -285,6 +288,52 @@ def test_modifier_execution() -> None: | |
| np.testing.assert_allclose(computed_statevector, expected_statevector) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("annotate", [True, False]) | ||
| def test_inline_always(annotate: bool) -> None: | ||
| import hugr.ops as ops | ||
| from hugr.build.dfg import Dfg | ||
|
|
||
| d = Dfg(tys.Tuple(tys.Qubit, tys.Qubit)) | ||
|
|
||
| f_id = d.module_root_builder().define_function( | ||
| "id", | ||
| [tys.Qubit], | ||
| ) | ||
| f_id.set_outputs(f_id.input_node[0]) | ||
|
|
||
| if annotate: | ||
| f_id.metadata["tket.inline"] = "always" | ||
|
|
||
| (tup,) = d.inputs() | ||
| (q1, q2) = d.add(ops.UnpackTuple()(tup)) | ||
| call1 = d.call(f_id, q1) | ||
| call2 = d.call(f_id, q2) | ||
| (tup,) = d.add(ops.MakeTuple()(call1, call2)) | ||
|
|
||
| d.set_outputs(tup) | ||
|
|
||
| # validate(d.hugr) | ||
|
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. I guess there is no way to do this here? |
||
|
|
||
| InlineAlwaysPass()(d.hugr) | ||
| # validate(d.hugr) | ||
| assert _count_ops(d.hugr, "Call") == 0 if annotate else 2 | ||
|
|
||
|
|
||
| def test_inline_always_cycle() -> None: | ||
| from hugr.build.function import Module | ||
|
|
||
| mod = Module() | ||
|
|
||
| f_recursive = mod.define_function("recurse", [tys.Qubit]) | ||
| f_recursive.declare_outputs([tys.Qubit]) | ||
| call = f_recursive.call(f_recursive, f_recursive.input_node[0]) | ||
| f_recursive.set_outputs(call) | ||
|
|
||
| f_recursive.metadata["tket.inline"] = "always" | ||
| with pytest.raises(InlineAlwaysError): | ||
| InlineAlwaysPass()(mod.hugr) | ||
|
|
||
|
|
||
| def test_inline_functions() -> None: | ||
| hugr = _hugr_from_path("test_files/guppy_examples/fn_calls.hugr") | ||
|
|
||
|
|
||
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.
We should export the metadata key from rust, as hugr-py/rust/metadata.rs does, but we don't have a module set up for that yet