Skip to content

Add support for scf.for ops in the convert-quantum-to-qecl pass#2881

Open
joeycarter wants to merge 3 commits into
joeycarter/quantum-to-qecl/scf-iffrom
joeycarter/quantum-to-qecl/scf-for
Open

Add support for scf.for ops in the convert-quantum-to-qecl pass#2881
joeycarter wants to merge 3 commits into
joeycarter/quantum-to-qecl/scf-iffrom
joeycarter/quantum-to-qecl/scf-for

Conversation

@joeycarter
Copy link
Copy Markdown
Contributor

@joeycarter joeycarter commented May 27, 2026

Context: In order to support more general input programs in the experimental QEC compilation pipeline, we must add support for control-flow operations.

Description of the Change: This PR adds support for for loops (via scf.for ops) in the convert-quantum-to-qecl pass. For example, an input program such as

func.func @test_program() {
    %c1 = arith.constant 1 : index
    %c5 = arith.constant 5 : index
    %c0 = arith.constant 0 : index
    %0 = quantum.alloc(1) : !quantum.reg
    %1 = quantum.extract %0[0] : !quantum.reg -> !quantum.bit
    %2 = quantum.custom "PauliX"() %1 : !quantum.bit
    %3 = scf.for %arg0 = %c0 to %c5 step %c1 iter_args(%arg1 = %2) -> (!quantum.bit) {
        %out_qubits = quantum.custom "PauliX"() %arg1 : !quantum.bit
        scf.yield %out_qubits : !quantum.bit
    }
    %4 = quantum.insert %0[0], %3 : !quantum.reg, !quantum.bit
    quantum.dealloc %4 : !quantum.reg
    func.return
}

is lowered to

func.func @test_program() {
    %c1 = arith.constant 1 : index
    %c5 = arith.constant 5 : index
    %c0 = arith.constant 0 : index
    %0 = qecl.alloc() : !qecl.hyperreg<1 x 1>
    %1 = qecl.extract_block %0[0] : !qecl.hyperreg<1 x 1> -> !qecl.codeblock<1>
    %2 = qecl.encode[zero] %1 : !qecl.codeblock<1>
    %3 = qecl.insert_block %0[0], %2 : !qecl.hyperreg<1 x 1>, !qecl.codeblock<1>
    %4 = qecl.extract_block %3[0] : !qecl.hyperreg<1 x 1> -> !qecl.codeblock<1>
    %5 = qecl.qec %4 : !qecl.codeblock<1>
    %6 = qecl.x %5[0] : !qecl.codeblock<1>
    %7 = qecl.qec %6 : !qecl.codeblock<1>
    %8 = scf.for %arg0 = %c0 to %c5 step %c1 iter_args(%arg1 = %7) -> (!qecl.codeblock<1>) {
        %out_qubits = qecl.x %arg1[0] : !qecl.codeblock<1>
        %out_qubits_1 = qecl.qec %out_qubits : !qecl.codeblock<1>
        scf.yield %out_qubits_1 : !qecl.codeblock<1>
    }
    %9 = qecl.insert_block %3[0], %8 : !qecl.hyperreg<1 x 1>, !qecl.codeblock<1>
    qecl.dealloc %9 : !qecl.hyperreg<1 x 1>
    func.return
}

Support for scf.for ops that return !quantum.reg values, and a mix of quantum and classical types, has also been added.

[sc-120266]

@joeycarter joeycarter changed the title Initial commit (add support for scf.for) Add support for scf.for ops in the convert-quantum-to-qecl pass May 27, 2026
@joeycarter joeycarter marked this pull request as ready for review May 27, 2026 17:46
@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.04%. Comparing base (ab54da1) to head (89a9673).

Additional details and impacted files
@@                        Coverage Diff                         @@
##           joeycarter/quantum-to-qecl/scf-if    #2881   +/-   ##
==================================================================
  Coverage                              97.03%   97.04%           
==================================================================
  Files                                    167      167           
  Lines                                  18876    18914   +38     
  Branches                                1766     1767    +1     
==================================================================
+ Hits                                   18317    18355   +38     
  Misses                                   415      415           
  Partials                                 144      144           

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

// CHECK: [[cb4:%.+]] = qecl.qec
// CHECK: scf.yield [[cb4]] : !qecl.codeblock<1>
%3 = scf.for %arg0 = %c0 to %c5 step %c1 iter_args(%arg1 = %2) -> (!quantum.bit) {
%out_qubits = quantum.custom "PauliX"() %arg1 : !quantum.bit
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks like the current implementation is bounded to k=1? Any implementation challenges you can foresee if we move to k > 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In fact, I think the control-flow conversion, as implemented here, should be generalizable to k > 1. The piece that would need to be updated is the conversion pattern for the quantum.extract op, which currently assumes k = 1. The tests still use k = 1 because the pass itself still raises an error when given k != 1 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But of course we'd need to verify once we start working on support for k > 1!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be lots of fun for k>1 , would like to know more about that later. Feel free to tag me to take a look

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.

2 participants