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 phase of pauli_list.insert(..., qubit=True) for length-1 pauli_list #13624

Merged
merged 8 commits into from
Jan 8, 2025

Conversation

aeddins-ibm
Copy link
Contributor

@aeddins-ibm aeddins-ibm commented Jan 8, 2025

Summary

Fix #13623.

Details and comments

Fixes bug in PauliList.insert(..., qubit=True) method, where the output PauliList would have a 2D phase attribute (should be 1D).

The bug was in a line of code manually broadcasting the phase of the PauliList being inserted to match the shape of the phase of the original PauliList. This PR removes this line of code, and lets numpy handle the broadcasting of phase.

This PR also adds a check in PauliList.from_symplectic(..., phase) to reject phase if it is a multi-dimensional array, and a test for issue #13623.

(For the record, I messed up the commit message in 92161ec. The simpler if/elif case is indeed moved to be first, but in the commit message I mixed up the two cases).

`len(value) == 1` is the simplest case, and is also the case where the problematic clause `len(value) == size` fails.

This commit switches the order, so we check for `len(value) == 1` first.

This ensures that when the `len(value) == size` clause runs, we know that `size != 1`, avoiding the bug in Qiskit#13623.
Here, `phase` should be a 1D array. `np.vstack()` docs say explicitly output will be at least 2D, so we should not use that to create `phase`.

The intent of using `np.vstack()` was essentially to broadcast `phase` to properly add to `self.phase`. But, this happens automatically and correctly if we just add as-is. So this commit simplifies the code accordingly.
Verifying the input arg `phase`, since otherwise `from_symplectic()` will silently create PauliLists with malformed `phase` attributes (i.e. not 1D).

Zero-dimensional is OK (e.g. `phase` defaults to `0`) since that can broadcast per the shape of the `z` and `x` arrays.
@aeddins-ibm aeddins-ibm requested a review from a team as a code owner January 8, 2025 06:16
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jan 8, 2025
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Jan 8, 2025

Pull Request Test Coverage Report for Build 12675008274

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

  • 6 of 7 (85.71%) changed or added relevant lines in 1 file are covered.
  • 111 unchanged lines in 8 files lost coverage.
  • Overall coverage decreased (-0.04%) to 88.909%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/quantum_info/operators/symplectic/pauli_list.py 6 7 85.71%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py 2 98.28%
qiskit/circuit/library/standard_gates/rz.py 3 95.71%
qiskit/circuit/library/standard_gates/rx.py 3 95.95%
qiskit/circuit/library/standard_gates/ry.py 3 95.83%
crates/qasm2/src/lex.rs 4 91.23%
crates/qasm2/src/parse.rs 18 96.23%
qiskit/circuit/quantumcircuit.py 77 93.66%
Totals Coverage Status
Change from base Build 12655273978: -0.04%
Covered Lines: 79414
Relevant Lines: 89321

💛 - Coveralls

@ShellyGarion ShellyGarion added mod: quantum info Related to the Quantum Info module (States & Operators) Changelog: Bugfix Include in the "Fixed" section of the changelog and removed Community PR PRs from contributors that are not 'members' of the Qiskit repo labels Jan 8, 2025
jakelishman
jakelishman previously approved these changes Jan 8, 2025
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for this fix - it looks sensible to me (though I'll admit I'm not at all familiar with the code here).

@jakelishman jakelishman enabled auto-merge January 8, 2025 16:43
@jakelishman jakelishman added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jan 8, 2025
@jakelishman jakelishman added this to the 1.3.2 milestone Jan 8, 2025
@jakelishman jakelishman added this pull request to the merge queue Jan 8, 2025
Merged via the queue into Qiskit:main with commit 408741c Jan 8, 2025
18 checks passed
mergify bot pushed a commit that referenced this pull request Jan 8, 2025
…_list` (#13624)

* switch order of if-clauses

`len(value) == 1` is the simplest case, and is also the case where the problematic clause `len(value) == size` fails.

This commit switches the order, so we check for `len(value) == 1` first.

This ensures that when the `len(value) == size` clause runs, we know that `size != 1`, avoiding the bug in #13623.

* add test

* Simplify `value.phase` broadcasting

Here, `phase` should be a 1D array. `np.vstack()` docs say explicitly output will be at least 2D, so we should not use that to create `phase`.

The intent of using `np.vstack()` was essentially to broadcast `phase` to properly add to `self.phase`. But, this happens automatically and correctly if we just add as-is. So this commit simplifies the code accordingly.

* Verify that `phase` is 1D in `from_symplectic()`

Verifying the input arg `phase`, since otherwise `from_symplectic()` will silently create PauliLists with malformed `phase` attributes (i.e. not 1D).

Zero-dimensional is OK (e.g. `phase` defaults to `0`) since that can broadcast per the shape of the `z` and `x` arrays.

* remove vestigial line

* lint

* release note

* Update releasenotes/notes/fix-paulilist-length1-phase-688d0e3a64ec9a9f.yaml

Co-authored-by: Jake Lishman <[email protected]>

---------

Co-authored-by: Jake Lishman <[email protected]>
(cherry picked from commit 408741c)
@aeddins-ibm aeddins-ibm deleted the fix-paulilist-length1-phase branch January 8, 2025 19:47
github-merge-queue bot pushed a commit that referenced this pull request Jan 8, 2025
…_list` (#13624) (#13637)

* switch order of if-clauses

`len(value) == 1` is the simplest case, and is also the case where the problematic clause `len(value) == size` fails.

This commit switches the order, so we check for `len(value) == 1` first.

This ensures that when the `len(value) == size` clause runs, we know that `size != 1`, avoiding the bug in #13623.

* add test

* Simplify `value.phase` broadcasting

Here, `phase` should be a 1D array. `np.vstack()` docs say explicitly output will be at least 2D, so we should not use that to create `phase`.

The intent of using `np.vstack()` was essentially to broadcast `phase` to properly add to `self.phase`. But, this happens automatically and correctly if we just add as-is. So this commit simplifies the code accordingly.

* Verify that `phase` is 1D in `from_symplectic()`

Verifying the input arg `phase`, since otherwise `from_symplectic()` will silently create PauliLists with malformed `phase` attributes (i.e. not 1D).

Zero-dimensional is OK (e.g. `phase` defaults to `0`) since that can broadcast per the shape of the `z` and `x` arrays.

* remove vestigial line

* lint

* release note

* Update releasenotes/notes/fix-paulilist-length1-phase-688d0e3a64ec9a9f.yaml

Co-authored-by: Jake Lishman <[email protected]>

---------

Co-authored-by: Jake Lishman <[email protected]>
(cherry picked from commit 408741c)

Co-authored-by: aeddins-ibm <[email protected]>
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 mod: quantum info Related to the Quantum Info module (States & Operators) 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.

pauli_list.insert(..., qubit=True) with length-1 pauli_list sets phase to wrong shape
5 participants