Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in isometry.rs #12469

Merged
merged 5 commits into from
May 28, 2024
Merged

Fix a bug in isometry.rs #12469

merged 5 commits into from
May 28, 2024

Conversation

ShellyGarion
Copy link
Member

@ShellyGarion ShellyGarion commented May 28, 2024

Summary

After PR #12197 was merged, there is a bug with the following code:

num_qubits = 3
num_controls = 4
u = random_unitary(2 ** num_qubits)
ug = UnitaryGate(u)
cug = ug.control(num_controls)
cug 

The code is working for num_qubits = 1 or 2 but not for 3 or 4 or 5, and raises the following error:

/qiskit/circuit/library/generalized_gates/isometry.py:221, in Isometry._disentangle(self, circuit, q, diag, remaining_isometry, column_index, s)
    217 diagonal_mcg = self._append_mcg_up_to_diagonal(
    218     circuit, q, gate, control_labels, target_label
    219 )
    220 # apply the MCG to the remaining isometry
--> 221 v = isometry_rs.apply_multi_controlled_gate(v, control_labels, target_label, gate)
    222 # correct for the implementation "up to diagonal"
    223 diag_mcg_inverse = np.conj(diagonal_mcg).astype(complex, copy=False)

PanicException: assertion failed: j <= 1

Details and comments

The solution is to remove the assertion assert!(j <= 1) in crates/accelerate/src/isometry.rs:215:13

@ShellyGarion ShellyGarion added Changelog: Bugfix Include in the "Fixed" section of the changelog Rust This PR or issue is related to Rust code in the repository labels May 28, 2024
@ShellyGarion ShellyGarion added this to the 1.2.0 milestone May 28, 2024
@ShellyGarion ShellyGarion requested a review from a team as a code owner May 28, 2024 13:26
@qiskit-bot
Copy link
Collaborator

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

@coveralls
Copy link

coveralls commented May 28, 2024

Pull Request Test Coverage Report for Build 9273316343

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 25 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.003%) to 89.586%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 7 92.37%
crates/qasm2/src/parse.rs 18 96.69%
Totals Coverage Status
Change from base Build 9224776217: -0.003%
Covered Lines: 62317
Relevant Lines: 69561

💛 - Coveralls

@mtreinish mtreinish added the stable backport potential The bug might be minimal and/or import enough to be port to stable label May 28, 2024
@mtreinish mtreinish modified the milestones: 1.2.0, 1.1.1 May 28, 2024
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

This LGTM thanks for catching and fixing this. TBH, at the time when I added that assert I can't remember what I thought I was guarding against, but do remember explicitly thinking it was needed. But looking at it in retrospect it's clearly not correct for more than 2 target qubits so I'm not sure what I was thinking (I probably had tunnel vision trying to get the tests to pass).

Just one minor nit in the release notes but otherwise this is ready to go.

releasenotes/notes/fix-isometry-rust-adf0eed09c6611f1.yaml Outdated Show resolved Hide resolved
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@mtreinish mtreinish enabled auto-merge May 28, 2024 17:08
@mtreinish mtreinish added this pull request to the merge queue May 28, 2024
Merged via the queue into Qiskit:main with commit 0f0a634 May 28, 2024
15 checks passed
mergify bot pushed a commit that referenced this pull request May 28, 2024
* remove assertion

* extend the test

* add release notes

* fix release notes

* Update releasenotes/notes/fix-isometry-rust-adf0eed09c6611f1.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
(cherry picked from commit 0f0a634)
github-merge-queue bot pushed a commit that referenced this pull request May 28, 2024
* remove assertion

* extend the test

* add release notes

* fix release notes

* Update releasenotes/notes/fix-isometry-rust-adf0eed09c6611f1.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
(cherry picked from commit 0f0a634)

Co-authored-by: Shelly Garion <46566946+ShellyGarion@users.noreply.github.com>
ElePT pushed a commit to ElePT/qiskit that referenced this pull request May 31, 2024
* remove assertion

* extend the test

* add release notes

* fix release notes

* Update releasenotes/notes/fix-isometry-rust-adf0eed09c6611f1.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog Rust This PR or issue is related to Rust code in the repository stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants