Skip to content

Correctly enforce matrix size check in CommutationChecker#15933

Merged
alexanderivrii merged 2 commits intoQiskit:mainfrom
Cryoris:fix-cc-matrix-size
Apr 2, 2026
Merged

Correctly enforce matrix size check in CommutationChecker#15933
alexanderivrii merged 2 commits intoQiskit:mainfrom
Cryoris:fix-cc-matrix-size

Conversation

@Cryoris
Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris commented Apr 1, 2026

Fixed the matrix size check for non-cachable gates in the commutation checker.

A typo in the matrix size check allowed to sneak into a matrix-based check even if the operations exceeded the maximum matrix size. The operations first_.. and second_.. are sorted, but ..1 and ..2 are not! The wrong one was used here.

… checker.

A typo in the matrix size check allowed to sneak into a matrix-based check even if the operations exceeded the maximum matrix size. The operations `first_..` and `second_..` are sorted, but `..1` and `..2` are not! The wrong one was used here.
@Cryoris Cryoris requested a review from a team as a code owner April 1, 2026 12:43
@Cryoris Cryoris added stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. labels Apr 1, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

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

  • @Qiskit/terra-core


def test_large_custom_gate(self):
"""Test a large custom gate is caught by the qubit threshold."""
big = UnitaryGate(np.eye(2**12))
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.

This may be a little unnecessarily large for the test - I think it ends up allocating 384MiB in matrices lol. Can we just use 6 qubits or so, or is 12 the threshold?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It still panics with 9 qubits, that should be 384/8 so 48ish MiB? Maybe still a bit large? 😅

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.

We can pass matrix_max_num_qubits to scc.commute, thus allowing to reduce the test size.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I was testing a path that would panic in the existing code, which was happening because the matrix sizes get too large. I can also reduce it and ensure the commutation evaluates to False (since it should not enter the check) when it actually comes out to True (if you were doing the check, which you shouldn't).

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.

I'd use the smallest size that still tests what you want. 9 qubits is actually 64x smaller - you're thinking about statevectors, but using operators.

I mean, even the big big matrices probably wouldn't be a problem, they were just a bit unnecessary haha

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I changed to the True/False setting I described above, which still tests the right thing but doesn't rely on triggering a panic anymore 🙂 So that only needs a 4-dim identity now 😄

This test now doesn't rely on the internal path panicking, but still tests the right behavior since the results with and without a matrix check differ.
Copy link
Copy Markdown
Member

@alexanderivrii alexanderivrii left a comment

Choose a reason for hiding this comment

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

Thanks Julien, good to have this fixed.

@alexanderivrii alexanderivrii added this pull request to the merge queue Apr 2, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 2, 2026
@alexanderivrii alexanderivrii added this pull request to the merge queue Apr 2, 2026
Merged via the queue into Qiskit:main with commit b43a2f3 Apr 2, 2026
25 checks passed
mergify bot pushed a commit that referenced this pull request Apr 2, 2026
* Fixed the matrix size check for non-cachable gates in the commutation checker.

A typo in the matrix size check allowed to sneak into a matrix-based check even if the operations exceeded the maximum matrix size. The operations `first_..` and `second_..` are sorted, but `..1` and `..2` are not! The wrong one was used here.

* Reduce test size

This test now doesn't rely on the internal path panicking, but still tests the right behavior since the results with and without a matrix check differ.

(cherry picked from commit b43a2f3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants