- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1
 
Refactor SquinToStimPass for clifford dialect + performance #510
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 15 commits
      Commits
    
    
            Show all changes
          
          
            25 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3fabd5a
              
                refactor squin to stim pass to be simpler + more performant with rep …
              
              
                johnzl-777 012ce87
              
                Merge branch 'main' into john/squin-to-stim-performance-refactor
              
              
                johnzl-777 f21059d
              
                Merge branch 'main' into john/squin-to-stim-performance-refactor
              
              
                johnzl-777 95e651a
              
                add reset op, get a decent set of tests to work properly
              
              
                johnzl-777 9d86c00
              
                undo unecessary qubit refactor
              
              
                johnzl-777 79c919b
              
                Shuffle reset into qubit, drop ResetToOne. Also try to make cirq runt…
              
              
                johnzl-777 38a9ce6
              
                remove reset from these tests for now considering different semantics
              
              
                johnzl-777 462a8b8
              
                bring back the old reset to keep tests happy, this is driving me nuts
              
              
                johnzl-777 acb3364
              
                add measurement tests back
              
              
                johnzl-777 f83b68a
              
                merge main in
              
              
                johnzl-777 d1b798b
              
                Merge branch 'main' into john/squin-to-stim-performance-refactor
              
              
                johnzl-777 275ea82
              
                move away from old clifford dialect
              
              
                johnzl-777 2ad43be
              
                complete U3 to clifford rewrite + test refactor
              
              
                johnzl-777 6e2c997
              
                bring back the SquinToStim U3 to Clifford test
              
              
                johnzl-777 4f71a51
              
                update to newer version of kirin with FlattenAdd to get CI to be happy
              
              
                johnzl-777 9fec057
              
                Merge branch 'main' into john/squin-to-stim-performance-refactor
              
              
                david-pl ce77998
              
                get rid of duplicate aggressive unroll, reuse the one from QASM2
              
              
                johnzl-777 26116f0
              
                fix types in SquinU3ToClifford
              
              
                johnzl-777 afd901f
              
                make lifting stricter
              
              
                johnzl-777 676c257
              
                remove leftover tinkering tests
              
              
                johnzl-777 626bf3a
              
                just reroute SSA values instead of generating new constants
              
              
                johnzl-777 2821a33
              
                get rid of is_measurement_used considering only one place uses it
              
              
                johnzl-777 cb89601
              
                get out of qubit to stim rewrite sooner if address attr not available…
              
              
                johnzl-777 1315d89
              
                get rid of DummyIdentity, can use None
              
              
                johnzl-777 b8a034f
              
                fix types in SquinU3ToClifford (again)
              
              
                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
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
    
  
  
    
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| from .squin_to_stim import ( | ||
| SquinToStimPass as SquinToStimPass, | ||
| StimSimplifyIfs as StimSimplifyIfs, | ||
| ) | 
  
    
      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,101 @@ | ||
| # Taken from Phillip Weinberg's bloqade-shuttle implementation | ||
| from dataclasses import field, dataclass | ||
| 
     | 
||
| from kirin import ir | ||
| from kirin.passes import Pass, HintConst, TypeInfer | ||
| from kirin.rewrite import ( | ||
| Walk, | ||
| Chain, | ||
| Inline, | ||
| Fixpoint, | ||
| Call2Invoke, | ||
| ConstantFold, | ||
| CFGCompactify, | ||
| InlineGetItem, | ||
| InlineGetField, | ||
| DeadCodeElimination, | ||
| ) | ||
| from kirin.dialects import scf, ilist | ||
| from kirin.ir.method import Method | ||
| from kirin.rewrite.abc import RewriteResult | ||
| from kirin.rewrite.cse import CommonSubexpressionElimination | ||
| from kirin.passes.inline import InlinePass | ||
| from kirin.passes.aggressive import UnrollScf | ||
| 
     | 
||
| from bloqade.stim.passes.simplify_ifs import StimSimplifyIfs | ||
| 
     | 
||
| 
     | 
||
| @dataclass | ||
| class Fold(Pass): | ||
| hint_const: HintConst = field(init=False) | ||
| 
     | 
||
| def __post_init__(self): | ||
| self.hint_const = HintConst(self.dialects, no_raise=self.no_raise) | ||
| 
     | 
||
| def unsafe_run(self, mt: Method) -> RewriteResult: | ||
| result = RewriteResult() | ||
| result = self.hint_const.unsafe_run(mt).join(result) | ||
| rule = Chain( | ||
| ConstantFold(), | ||
| Call2Invoke(), | ||
| InlineGetField(), | ||
| InlineGetItem(), | ||
| ilist.rewrite.InlineGetItem(), | ||
| ilist.rewrite.HintLen(), | ||
| DeadCodeElimination(), | ||
| CommonSubexpressionElimination(), | ||
| ) | ||
| result = Fixpoint(Walk(rule)).rewrite(mt.code).join(result) | ||
| 
     | 
||
| return result | ||
| 
     | 
||
| 
     | 
||
| @dataclass | ||
| class AggressiveUnroll(Pass): | ||
                
      
                  johnzl-777 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| """Fold pass to fold control flow""" | ||
| 
     | 
||
| fold: Fold = field(init=False) | ||
| typeinfer: TypeInfer = field(init=False) | ||
| scf_unroll: UnrollScf = field(init=False) | ||
| 
     | 
||
| def __post_init__(self): | ||
| self.fold = Fold(self.dialects, no_raise=self.no_raise) | ||
| self.typeinfer = TypeInfer(self.dialects, no_raise=self.no_raise) | ||
| self.scf_unroll = UnrollScf(self.dialects, no_raise=self.no_raise) | ||
| 
     | 
||
| def unsafe_run(self, mt: Method) -> RewriteResult: | ||
| result = RewriteResult() | ||
| result = self.scf_unroll.unsafe_run(mt).join(result) | ||
| result = ( | ||
| Walk(Chain(ilist.rewrite.ConstList2IList(), ilist.rewrite.Unroll())) | ||
| .rewrite(mt.code) | ||
| .join(result) | ||
| ) | ||
| result = self.typeinfer.unsafe_run(mt).join(result) | ||
| result = self.fold.unsafe_run(mt).join(result) | ||
| result = Walk(Inline(self.inline_heuristic)).rewrite(mt.code).join(result) | ||
| result = Walk(Fixpoint(CFGCompactify())).rewrite(mt.code).join(result) | ||
| return result | ||
| 
     | 
||
| @classmethod | ||
| def inline_heuristic(cls, node: ir.Statement) -> bool: | ||
| """The heuristic to decide whether to inline a function call or not. | ||
| inside loops and if-else, only inline simple functions, i.e. | ||
| functions with a single block | ||
| """ | ||
| return not isinstance( | ||
| node.parent_stmt, (scf.For, scf.IfElse) | ||
| ) # always inline calls outside of loops and if-else | ||
| 
     | 
||
| 
     | 
||
| class Flatten(Pass): | ||
| def unsafe_run(self, mt: ir.Method) -> RewriteResult: | ||
| rewrite_result = InlinePass(dialects=mt.dialects, no_raise=self.no_raise)(mt) | ||
| rewrite_result = AggressiveUnroll(dialects=mt.dialects, no_raise=self.no_raise)( | ||
| mt | ||
| ).join(rewrite_result) | ||
| rewrite_result = StimSimplifyIfs(dialects=mt.dialects, no_raise=self.no_raise)( | ||
| mt | ||
| ).join(rewrite_result) | ||
| 
     | 
||
| return rewrite_result | ||
  
    
      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.
        
    
  
  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 statement needs a pyqrack method and a cirq emit method impl. I can also add those, if you like. So either add them here, or a create a new issue and assign it to me, please.
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'll make an issue, thank you! (See #546 )