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 (backport #12385) #12386

Merged
merged 1 commit into from
May 10, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot 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


This is an automatic backport of pull request #12385 done by [Mergify](https://mergify.com).

…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 <jake@binhbar.com>
(cherry picked from commit fe275a0)
@mergify mergify bot requested a review from a team as a code owner May 10, 2024 12:18
@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 the following people are requested to review this:

  • @Qiskit/terra-core

@github-actions github-actions bot added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: quantum info Related to the Quantum Info module (States & Operators) labels May 10, 2024
@github-actions github-actions bot added this to the 1.1.0 milestone May 10, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 9032100787

Details

  • 8 of 8 (100.0%) changed or added relevant lines in 2 files are covered.
  • 6 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.641%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.03%
crates/qasm2/src/lex.rs 5 92.11%
Totals Coverage Status
Change from base Build 9031944671: 0.02%
Covered Lines: 62263
Relevant Lines: 69458

💛 - Coveralls

@jakelishman jakelishman added this pull request to the merge queue May 10, 2024
Merged via the queue into stable/1.1 with commit 70eae3c May 10, 2024
18 checks passed
@mergify mergify bot deleted the mergify/bp/stable/1.1/pr-12385 branch May 10, 2024 14:03
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)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants