Skip to content

[qref 2.5] Subroutines take in extracted quantum.bit values during semantics conversion#2642

Merged
paul0403 merged 26 commits intomainfrom
paul0403/convert_qref_subroutines
Apr 14, 2026
Merged

[qref 2.5] Subroutines take in extracted quantum.bit values during semantics conversion#2642
paul0403 merged 26 commits intomainfrom
paul0403/convert_qref_subroutines

Conversation

@paul0403
Copy link
Copy Markdown
Member

@paul0403 paul0403 commented Mar 31, 2026

Context:
When converting subroutines from reference semantics to value semantics, currently we take in whole registers as-is:

func.func @subroutine(%r: !qref.reg<1>) {
    %q = qref.get %r[0] : !qref.reg<1> -> !qref.bit
    qref.custom "PauliX"() %q : !qref.bit
    return
}

func.func @main() attributes {quantum.node} {
    %r = qref.alloc(1) : !qref.reg<1>
    func.call @subroutine(%r) : (!qref.reg<1>) -> ()
    qref.dealloc %r : !qref.reg<1>
    return
}
  func.func @subroutine(%arg0: !quantum.reg) -> !quantum.reg {
    %0 = quantum.extract %arg0[ 0] : !quantum.reg -> !quantum.bit
    %out_qubits = quantum.custom "PauliX"() %0 : !quantum.bit
    %1 = quantum.insert %arg0[ 0], %out_qubits : !quantum.reg, !quantum.bit
    return %1 : !quantum.reg
  }
  func.func @main() attributes {quantum.node} {
    %0 = quantum.alloc( 1) : !quantum.reg
    %1 = call @subroutine(%0) : (!quantum.reg) -> !quantum.reg
    quantum.dealloc %1 : !quantum.reg
    return
  }

However, this is obviously not optimal, since the subroutine call, although only on a subset of qubits, will interject into the data flow of all qubit values in that register.

If we can statically know which qubits the subroutine needs, we can just extract before the call:

  func.func @subroutine(%arg0: !quantum.bit) -> !quantum.bit {
    %out_qubits = quantum.custom "PauliX"() %arg0 : !quantum.bit
    return %out_qubits : !quantum.bit
  }
  func.func @main() attributes {quantum.node} {
    %0 = quantum.alloc( 1) : !quantum.reg
    %1 = quantum.extract %0[ 0] : !quantum.reg -> !quantum.bit
    %2 = call @subroutine(%1) : (!quantum.bit) -> !quantum.bit
    %3 = quantum.insert %0[ 0], %2 : !quantum.reg, !quantum.bit
    quantum.dealloc %3 : !quantum.reg
    return
  }

Description of the Change:
Extract qubits from static indices before the callsites of the subroutine, and use the extracted qubits instead.

Benefits:
More optimized quantum dataflow; unified treatment of how we handle subroutines vs other regions (control flow and adjoint) during the qref-to-quantum semantics conversion.

Possible Drawbacks:
None

Related GitHub Issues:
[sc-115119]

@paul0403 paul0403 marked this pull request as ready for review April 7, 2026 14:56
@paul0403 paul0403 requested review from dime10, mehrdad2m and rniczh April 7, 2026 14:56
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.12%. Comparing base (1959863) to head (e6128c2).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2642      +/-   ##
==========================================
- Coverage   97.15%   97.12%   -0.04%     
==========================================
  Files          36      157     +121     
  Lines        4295    17920   +13625     
  Branches        0     1709    +1709     
==========================================
+ Hits         4173    17405   +13232     
- Misses        122      378     +256     
- Partials        0      137     +137     

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

Copy link
Copy Markdown
Member

@rniczh rniczh left a comment

Choose a reason for hiding this comment

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

🚀 @paul0403 , just some minor comments from my side. Overall looks good to me.

Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
Comment thread mlir/lib/QRef/Transforms/value_semantics_conversion.cpp Outdated
Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Copy link
Copy Markdown
Contributor

@mehrdad2m mehrdad2m left a comment

Choose a reason for hiding this comment

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

Thanks @paul0403, Just a few minor comments

Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Comment thread mlir/lib/QRef/Transforms/value_semantics_conversion.cpp
Comment thread mlir/test/QRef/SemanticConversion/TestSubroutines.mlir
Comment thread mlir/test/QRef/SemanticConversion/TestSubroutines.mlir Outdated
Copy link
Copy Markdown
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

I'm a bit confused about the organization of the code into the two headers and the source file (i.e. why code is one place or another), but C++ can be confusing that way I suppose 😅

Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/subroutine_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
Comment thread mlir/lib/QRef/Transforms/value_semantics_conversion.cpp Outdated
Comment thread mlir/include/QRef/Transforms/value_semantics_conversion.hpp Outdated
@paul0403 paul0403 merged commit 69c7ed3 into main Apr 14, 2026
38 checks passed
@paul0403 paul0403 deleted the paul0403/convert_qref_subroutines branch April 14, 2026 21:09
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.

4 participants