Skip to content

Conversation

rniczh
Copy link
Contributor

@rniczh rniczh commented Oct 1, 2025

Context:

Currently, the decomposition pass assumes all qubits involved in a gate operation come from the same quantum register (qreg). However, gates may operate on qubits from different registers, which causes decomposition failures or incorrect behaviour!

Description of the Change:

This PR introduces dynamic qreg allocation logic that automatically created when qubits from different qreg are involved in a gate operation. When such cross-qreg operations are detected:

  1. Allocates a new unified quantum register with sufficient capacity
  2. Inserts all involved qubits into the new register with sequential indices (0, 1, 2, ...)
  3. After the decomposed ops are finished, extracts the results to qubits and deallocates the temporary register. It will run into an error if we deallocate this tmp register

The implementation adds a needAllocQreg flag to the OpSignature struct and implements the allocation logic in prepareCallOperands() and prepareCallResultForQreg()

Exampe:

%reg1 = quantum.alloc(1)
%reg2 = quantum.alloc(1) 
%q1 = quantum.extract %reg1[0]
%q2 = quantum.extract %reg2[0]
%result:2 = quantum.custom "CNOT"() %q1, %q2 

decmopse to

%reg1 = quantum.alloc(1)
%reg2 = quantum.alloc(1)
%q1 = quantum.extract %reg1[0] 
%q2 = quantum.extract %reg2[0]

// new qreg
%tmp = quantum.alloc(2) 
%tmp = quantum.insert %tmp[0], %q1
%tmp = quantum.insert %tmp[1], %q2

// Decomposition happens on tmp qreg:
call @cnot_decomp(%tmp, [0,1]) -> %result_qreg

// Results extracted and cleaned up:
%result1 = quantum.extract %result_qreg[0]
%result2 = quantum.extract %result_qreg[1] 
quantum.dealloc %result_qreg

Benefits:

Possible Drawbacks:

There is a underlying issue if we deallocate the tmp qreg, for running the circuit on lightning, I removed these line. I don't think it's the right way to resolve them.

// FIXME: Dealloc should be fine, but it will cause the error in lightning now
// if (signature.needAllocQreg) {
// rewriter.create<quantum::DeallocOp>(callOp.getLoc(), qreg);
// }

Related GitHub Issues:
[sc-100312]

@rniczh rniczh requested a review from maliasadi October 1, 2025 04:06
Copy link
Contributor

github-actions bot commented Oct 1, 2025

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md on your branch with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented Oct 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.48%. Comparing base (64015c1) to head (2e5d350).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2074   +/-   ##
=======================================
  Coverage   97.48%   97.48%           
=======================================
  Files          91       91           
  Lines       10594    10594           
  Branches      990      990           
=======================================
  Hits        10328    10328           
  Misses        211      211           
  Partials       55       55           

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

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.

1 participant