catalyst
catalyst copied to clipboard
Add dynamic qreg alloc for cross-qreg gate decomposition
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:
- Allocates a new unified quantum register with sufficient capacity
- Inserts all involved qubits into the new register with sequential indices (0, 1, 2, ...)
- 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. https://github.com/PennyLaneAI/catalyst/blob/2e5d3505c9e910a83a9700aabdde8c8697d9b0e9/mlir/lib/Quantum/Transforms/DecomposeLoweringPatterns.cpp#L284-L287
Related GitHub Issues: [sc-100312]
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.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 97.49%. Comparing base (526b58b) to head (ac5dd59).
:warning: Report is 2 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #2074 +/- ##
=======================================
Coverage 97.49% 97.49%
=======================================
Files 93 93
Lines 10890 10890
Branches 1038 1038
=======================================
Hits 10617 10617
Misses 211 211
Partials 62 62
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.