-
Notifications
You must be signed in to change notification settings - Fork 5
add Stim REPEAT support for scf.For loops #736
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
johnzl-777
wants to merge
34
commits into
main
Choose a base branch
from
john/complete-repeat-support
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.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
4a1f708
feat: add Stim REPEAT support for scf.For loops
johnzl-777 f3cb43b
Let claude work out this hintlen problem
johnzl-777 d5a81b6
Merge branch 'main' into john/complete-repeat-support
johnzl-777 c6cecec
get claude to clean things up
johnzl-777 3232f35
fix validation vs. flatten ordering, I want validation to happen befo…
johnzl-777 eb5ac9a
remove the address as attribute system, keep logic simpler by just pa…
johnzl-777 ef091e8
use early terminating constprop
johnzl-777 02532a3
make the loop hint const infra more idiomatic
johnzl-777 ff1f81e
clarify early termination in constprop behavior
johnzl-777 d788336
cut down on unnecessary code
johnzl-777 61cce14
get rid of awkward in-function lib imports
johnzl-777 9446db8
make unit tests more robust, assert that all measurements accumulated…
johnzl-777 98b4d01
allow for standard empty list syntax as opposed to less idiomatic mea…
johnzl-777 facc82b
simplify empty ilist check even further
johnzl-777 eccd0db
Merge branch 'main' into john/complete-repeat-support
johnzl-777 45253cb
make squin noise rewrite more robust
johnzl-777 5562f27
get rid of non idiomatic statement deletion handling
johnzl-777 455cb42
remove monkeypatching
johnzl-777 fe6eba4
opt for analysis level solution to support appending to empty lists
johnzl-777 2fa1060
make count argument to repeat an attribute
johnzl-777 7277919
Have default factory that creates region w/ block
johnzl-777 283c636
Ensure conformity with is_subseteq behavior
johnzl-777 5713c2d
accidentally applied suggestion to wrong type, fixed here
johnzl-777 2d286a4
Merge branch 'main' into john/complete-repeat-support
johnzl-777 7473a8a
remove unnecessary inline predicate
johnzl-777 3275b15
make non stim cleanup safer
johnzl-777 3a45843
rework RemoveDeadNonStimStatements rule
johnzl-777 3a4fa24
rework RemoveDeadNonStimStatements rule (let DCE handle more of the p…
johnzl-777 1eb1b92
remove unnecessary dangling register cleaning (superceded by cleanup_…
johnzl-777 da78e0a
split and rename qubit to stim rewrite rule
johnzl-777 af6e883
remove early return support
johnzl-777 fce0f7e
revert removal of early return handling in AddressAnalysis loop
johnzl-777 45f603a
lean more towards inlining then validating
johnzl-777 86c337b
avoid monkeypatching altogether on constprop fix
johnzl-777 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1 @@ | ||
| from .wrap_analysis import ( | ||
| AddressAttribute as AddressAttribute, | ||
| WrapAddressAnalysis as WrapAddressAnalysis, | ||
| ) | ||
| from .U3_to_clifford import SquinU3ToClifford as SquinU3ToClifford | ||
| from .remove_dangling_qubits import RemoveDeadRegister as RemoveDeadRegister |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| from . import gate as gate, noise as noise, collapse as collapse, auxiliary as auxiliary | ||
| from . import ( | ||
| gate as gate, | ||
| noise as noise, | ||
| stim_cf as stim_cf, | ||
| collapse as collapse, | ||
| auxiliary as auxiliary, | ||
| ) |
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,3 @@ | ||
| from .emit import EmitStimCfMethods as EmitStimCfMethods | ||
| from .stmts import Repeat as Repeat | ||
| from ._dialect import dialect as dialect |
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,3 @@ | ||
| from kirin import ir | ||
|
|
||
| dialect = ir.Dialect("stim.cf") |
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 @@ | ||
| from kirin.interp import MethodTable, impl | ||
|
|
||
| from bloqade.stim.emit.stim_str import EmitStimMain, EmitStimFrame | ||
|
|
||
| from .stmts import Repeat | ||
| from ._dialect import dialect | ||
|
|
||
|
|
||
| @dialect.register(key="emit.stim") | ||
| class EmitStimCfMethods(MethodTable): | ||
|
|
||
| @impl(Repeat) | ||
| def emit_repeat(self, emit: EmitStimMain, frame: EmitStimFrame, stmt: Repeat): | ||
| frame.write_line(f"REPEAT {stmt.count} {{") | ||
| frame._indent += 1 | ||
|
|
||
| for block in stmt.body.blocks: | ||
| frame.current_block = block | ||
| for body_stmt in block.stmts: | ||
| frame.current_stmt = body_stmt | ||
| res = emit.frame_eval(frame, body_stmt) | ||
| if isinstance(res, tuple): | ||
| frame.set_values(body_stmt.results, res) | ||
|
|
||
| frame._indent -= 1 | ||
| frame.write_line("}") | ||
|
|
||
| return () |
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,14 @@ | ||
| from kirin import ir | ||
| from kirin.decl import info, statement | ||
|
|
||
| from ._dialect import dialect | ||
|
|
||
|
|
||
| @statement(dialect=dialect) | ||
| class Repeat(ir.Statement): | ||
| name = "REPEAT" | ||
| traits = frozenset({ir.HasCFG(), ir.SSACFG()}) | ||
| count: int = info.attribute() | ||
| body: ir.Region = info.region( | ||
| multi=False, default_factory=lambda: ir.Region(ir.Block()) | ||
| ) |
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,61 @@ | ||
| """Remove leftover impure non-stim statements after conversion. | ||
|
|
||
| After the full squin-to-stim conversion pipeline, some impure non-stim | ||
| statements survive because DCE only removes pure ops. This pass deletes | ||
| dead impure statements from an explicit list of expected leftovers and | ||
| warns if an unexpected impure non-stim statement survives. | ||
| """ | ||
|
|
||
| import warnings | ||
|
|
||
| from kirin import ir | ||
| from kirin.rewrite.abc import RewriteRule, RewriteResult | ||
|
|
||
| from bloqade.qubit import stmts as qubit_stmts | ||
|
|
||
| # Impure statements expected to be left over after squin-to-stim conversion. | ||
| # These don't have stim equivalents — their side effects are subsumed by | ||
| # the stim statements that replaced their consumers (e.g. qubit.New | ||
| # becomes irrelevant once all gates using that qubit are converted). | ||
| EXPECTED_IMPURE_DEAD: tuple[type[ir.Statement], ...] = ( | ||
| qubit_stmts.New, | ||
| qubit_stmts.Measure, | ||
| ) | ||
|
|
||
|
|
||
| class RemoveDeadNonStimStatements(RewriteRule): | ||
| """Remove dead impure non-stim statements after conversion. | ||
|
|
||
| - Dead impure statements in EXPECTED_IMPURE_DEAD: deleted. | ||
| - Dead impure statements NOT in the list: warned about (likely a missed rewrite). | ||
| - Pure statements are left for DCE to handle. | ||
| """ | ||
|
|
||
| def __init__(self, keep: ir.DialectGroup): | ||
| self.keep = keep | ||
|
|
||
| def rewrite_Statement(self, node: ir.Statement) -> RewriteResult: | ||
| if not isinstance(node, EXPECTED_IMPURE_DEAD): | ||
| # Warn about unexpected dead impure non-stim statements. | ||
| if ( | ||
| node.dialect is not None | ||
| and node.dialect not in self.keep | ||
| and not node.regions | ||
| and not node.has_trait(ir.IsTerminator) | ||
| and not node.has_trait(ir.Pure) | ||
| and not ( | ||
| (trait := node.get_trait(ir.MaybePure)) and trait.is_pure(node) | ||
| ) | ||
| and all(len(r.uses) == 0 for r in node.results) | ||
| ): | ||
| warnings.warn( | ||
| f"Unexpected non-stim statement survived conversion: " | ||
| f"{type(node).__name__} from {node.dialect}", | ||
| ) | ||
| return RewriteResult() | ||
|
|
||
| if any(len(r.uses) > 0 for r in node.results): | ||
| return RewriteResult() | ||
|
|
||
| node.delete() | ||
| return RewriteResult(has_done_something=True) |
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.
This is an equality, not
subseteq. It means thatself.is_subseteq(AnyMeasureId)will returnFalse, but every element must besubseteq(Top()).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.
This equality is still a little weird. Could someone draw this lattice for me? 😄 At which point does the
ConstantCarrierfit into the hierarchy?I also noticed now that
ConcreteMeasureIdhas a similar issue in that it implementsis_subseteqas equality, meaning it's notsubseteqtoAnyMeasureId. But that's a pre-existing bug.@weinbe58 I think you originally implemented this lattice, correct? What's the reasoning behind this?