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 {Pauli,SparsePauliOp}.apply_layout to raise an error with negative or duplicate indices #12385

Merged
merged 4 commits into from
May 10, 2024

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented May 10, 2024

Summary

{Pauli,SparsePauliOp}.apply_layout currently allow negative or duplicate indices.
The behavior is not always comprehensive.

  1. If we give duplicate indices, some coefficients or phases may change.
  2. If we give negative indices that are out of bounds, it raises IndexError while positive indices raises QiskitError.

This PR raises QiskitError with duplicate indices or negative indices (even within bounds for simplicity).
I think duplicate indices are not valid as a layout. But, if we need to support the pattern, we might need to update the code not to change the coefficients.

I noticed this situation when I review #12221 (comment)

from qiskit.quantum_info import Pauli, SparsePauliOp

op = Pauli("XY")
op2 = SparsePauliOp("XY")

print("duplicate indices")
try:
    print(op.apply_layout([0, 0]))
except Exception as ex:
    print(ex)
try:
    print(op2.apply_layout([0, 0]))
except Exception as ex:
    print(ex)

print("\nnegative indices not out of bounds")
try:
    print(op.apply_layout([-1, 0]))
except Exception as ex:
    print(ex)
try:
    print(op2.apply_layout([-1, 0]))
except Exception as ex:
    print(ex)

print("\nnegative indices out of bounds")
try:
    print(op.apply_layout([-3, 0]))
except Exception as ex:
    print(ex)
try:
    print(op2.apply_layout([-3, 0]))
except Exception as ex:
    print(ex)

print("\npositive indices out of bounds")
try:
    print(op.apply_layout([3, 0]))
except Exception as ex:
    print(ex)
try:
    print(op2.apply_layout([3, 0]))
except Exception as ex:
    print(ex)

main

duplicate indices
-iIX
SparsePauliOp(['IX'],
              coeffs=[0.-1.j])

negative indices not out of bounds
YX
SparsePauliOp(['YX'],
              coeffs=[1.+0.j])

negative indices out of bounds
index -3 is out of bounds for axis 1 with size 2
index -3 is out of bounds for axis 1 with size 2

positive indices out of bounds
'Provided layout contains indices outside the number of qubits.'
'Provided layout contains indices outside the number of qubits.'

this PR

duplicate indices
'Provided layout contains duplicate indices.'
'Provided layout contains duplicate indices.'

negative indices not out of bounds
'Provided layout contains indices outside the number of qubits.'
'Provided layout contains indices outside the number of qubits.'

negative indices out of bounds
'Provided layout contains indices outside the number of qubits.'
'Provided layout contains indices outside the number of qubits.'

positive indices out of bounds
'Provided layout contains indices outside the number of qubits.'
'Provided layout contains indices outside the number of qubits.'

Details and comments

@t-imamichi t-imamichi added the mod: quantum info Related to the Quantum Info module (States & Operators) label May 10, 2024
@t-imamichi t-imamichi requested a review from a team as a code owner May 10, 2024 10:02
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

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, this seems like a good idea to me. Could you add a brief bug-fix release note mentioning the change?

@jakelishman jakelishman added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels May 10, 2024
@jakelishman jakelishman added this to the 1.1.0 milestone May 10, 2024
@coveralls
Copy link

coveralls commented May 10, 2024

Pull Request Test Coverage Report for Build 9030840979

Details

  • 8 of 8 (100.0%) changed or added relevant lines in 2 files are covered.
  • 23 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.03%) to 89.626%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.88%
crates/qasm2/src/parse.rs 18 96.69%
Totals Coverage Status
Change from base Build 9023909750: -0.03%
Covered Lines: 62201
Relevant Lines: 69401

💛 - Coveralls

jakelishman
jakelishman previously approved these changes May 10, 2024
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.

Looks good, thanks for the quick change and the fix!

@jakelishman jakelishman added this pull request to the merge queue May 10, 2024
Merged via the queue into Qiskit:main with commit fe275a0 May 10, 2024
15 checks passed
mergify bot pushed a commit that referenced this pull request May 10, 2024
…e or duplicate indices (#12385)

* fix apply_layout to raise an error with negative or duplicate indices

* reno

* Fix Sphinx syntax

* Fix my own Sphinx lookup problem

---------

Co-authored-by: Jake Lishman <[email protected]>
(cherry picked from commit fe275a0)
@t-imamichi t-imamichi deleted the fix-apply_layout branch May 10, 2024 13:47
github-merge-queue bot pushed a commit that referenced this pull request May 10, 2024
…e or duplicate indices (#12385) (#12386)

* fix apply_layout to raise an error with negative or duplicate indices

* reno

* Fix Sphinx syntax

* Fix my own Sphinx lookup problem

---------

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

Co-authored-by: Takashi Imamichi <[email protected]>
ElePT pushed a commit to ElePT/qiskit that referenced this pull request May 31, 2024
…e or duplicate indices (Qiskit#12385)

* fix apply_layout to raise an error with negative or duplicate indices

* reno

* Fix Sphinx syntax

* Fix my own Sphinx lookup problem

---------

Co-authored-by: Jake Lishman <[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.

4 participants