Skip to content

Use nalgebra matrices for uncommon paths in consolidate blocks#15881

Merged
Cryoris merged 1 commit intoQiskit:mainfrom
mtreinish:more-nalgebra-consolidation
Mar 27, 2026
Merged

Use nalgebra matrices for uncommon paths in consolidate blocks#15881
Cryoris merged 1 commit intoQiskit:mainfrom
mtreinish:more-nalgebra-consolidation

Conversation

@mtreinish
Copy link
Copy Markdown
Member

Summary

In the recently merged #15871 we updated the consolidate blocks pass to use Matrix4 in the common path of a 2q block being consolidated. This is like > 90% of what the pass does when run in the preset pass manager. However, there were uncommon cases in the pass around the handling of blocks of a single gate that are outside of the target which were not updated to use nalgebra arrays if it's a fixed size 1q or 2q gate. This commit updates these uncommon paths so that we're always returning an nalgebra matrix in the output UnitaryGate if the block being consolidated is a single qubit or two qubits.

Details and comments

In the recently merged Qiskit#15871 we updated the consolidate blocks pass to
use Matrix4 in the common path of a 2q block being consolidated. This is
like > 90% of what the pass does when run in the preset pass manager.
However, there were uncommon cases in the pass around the handling of
blocks of a single gate that are outside of the target which were not
updated to use nalgebra arrays if it's a fixed size 1q or 2q gate. This
commit updates these uncommon paths so that we're always returning an
nalgebra matrix in the output UnitaryGate if the block being consolidated
is a single qubit or two qubits.
@mtreinish mtreinish requested a review from a team as a code owner March 26, 2026 10:13
@mtreinish mtreinish added Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository labels Mar 26, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

Comment on lines +472 to +475
// Runs are necessarily single qubit gates so if it has a matrix then it
// must have a single qubit matrix or it's not a run and the blocks handling above
// would have covered it
let matrix = match get_1q_matrix_from_inst(first_inst) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it properly documented somewhere that runs are 1-q runs only? I can see it from digging in the code, but from looking at this function only it wasn't obvious -- in case it's not documented, could we add this to this function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's behavior from the passes that populate the fields in the property sets. Collect1qRuns collects "runs" which are 1q, while Collect2qBlocks collected "blocks" which are 2q. Functionally in practice we don't use either of these passes anymore as if neither is set ConsolidateBlocks just finds the 2q "blocks" itself and we don't run a separate analysis pass. This code path is just for backwards compatibility for people running the analysis passes first in a custom pass manager.

I expect it's not documented explicitly though. I can push up a separate documentation PR for this, but I don't view it as in scope for this PR which is only touching the pass's deep internals.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

#15891 🙂

Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Just one comment on documentation, otherwise LGTM 🙂

@Cryoris Cryoris added this pull request to the merge queue Mar 27, 2026
Merged via the queue into Qiskit:main with commit 9c6baac Mar 27, 2026
27 checks passed
gadial pushed a commit to gadial/qiskit that referenced this pull request Mar 29, 2026
…t#15881)

In the recently merged Qiskit#15871 we updated the consolidate blocks pass to
use Matrix4 in the common path of a 2q block being consolidated. This is
like > 90% of what the pass does when run in the preset pass manager.
However, there were uncommon cases in the pass around the handling of
blocks of a single gate that are outside of the target which were not
updated to use nalgebra arrays if it's a fixed size 1q or 2q gate. This
commit updates these uncommon paths so that we're always returning an
nalgebra matrix in the output UnitaryGate if the block being consolidated
is a single qubit or two qubits.
@github-project-automation github-project-automation Bot moved this from Ready to Done in Qiskit 2.5 Apr 25, 2026
@ShellyGarion ShellyGarion added this to the 2.5.0 milestone Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants