qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Fix a corner case of `SparsePauliOp.apply_layout`

Open t-imamichi opened this issue 1 year ago • 3 comments

Summary

SparsePauliOp.apply_layout raises an error if a zero-qubit operator is given. This PR fixes the case.

from qiskit.quantum_info import SparsePauliOp

op = SparsePauliOp("")
print(op.apply_layout(None, 3))

main branch

SparsePauliOp([''],
              coeffs=[1.+0.j])
Traceback (most recent call last):
  File "/Users/ima/tasks/4_2024/qiskit/terra/tmp/layout.py", line 5, in <module>
    print(op.apply_layout(None, 3))
  File "/Users/ima/tasks/4_2024/qiskit/terra/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py", line 1167, in apply_layout
    return new_op.compose(self, qargs=layout)
  File "/Users/ima/tasks/4_2024/qiskit/terra/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py", line 349, in compose
    q = np.logical_and(z1[:, np.newaxis], x2).reshape((-1, num_qubits))
ValueError: cannot reshape array of size 0 into shape (0)

this PR

SparsePauliOp(['III'],
              coeffs=[1.+0.j])

Details and comments

t-imamichi avatar May 09 '24 03:05 t-imamichi

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

  • @Qiskit/terra-core

qiskit-bot avatar May 09 '24 03:05 qiskit-bot

Pull Request Test Coverage Report for Build 9081444739

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

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 21 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.651%

Files with Coverage Reduction New Missed Lines %
qiskit/dagcircuit/dagnode.py 8 88.12%
crates/circuit/src/circuit_instruction.rs 13 87.59%
<!-- Total: 21
Totals Coverage Status
Change from base Build 9037095581: 0.02%
Covered Lines: 62323
Relevant Lines: 69517

💛 - Coveralls

coveralls avatar May 09 '24 03:05 coveralls

I noticed that I have to fix Pauli as well.

t-imamichi avatar May 11 '24 00:05 t-imamichi

I confirmed that the current Pauli.apply_laytout works correctly. So, we don't have to update it. I just added tests to ensure it.

t-imamichi avatar May 13 '24 07:05 t-imamichi

Looks good although maybe could be made briefer using @data like test_zero_qubit_pauli_construction above.

ewinston avatar May 13 '24 20:05 ewinston

Thank you for your suggestion, Erick. I simplified this PR with combine. I also applied isort to remove pylint: disable=wrong-import-order

t-imamichi avatar May 14 '24 01:05 t-imamichi