Skip to content

Commit

Permalink
Fix PauliSumOp.permute to not change the original object (#7339)
Browse files Browse the repository at this point in the history
* fix PauliSumOp.permute

* black!

* extend fix to general PauliSumOp

* Revert "extend fix to general PauliSumOp"

This reverts commit 6c4fa32.

Co-authored-by: Ikko Hamamura <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 1, 2022
1 parent 299d03b commit 497811a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion qiskit/opflow/primitive_ops/pauli_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def permute(self, permutation: List[int]) -> "PauliSumOp":
if length > self.num_qubits:
spop = self.primitive.tensor(SparsePauliOp(Pauli("I" * (length - self.num_qubits))))
else:
spop = self.primitive
spop = self.primitive.copy()

permutation = [i for i in range(length) if i not in permutation] + permutation
permu_arr = np.arange(length)[np.argsort(permutation)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fix a bug in :meth:`qiskit.opflow.PauliSumOp.permute` where the original object on which
the method is called is permuted, instead of only returning a new, permuted copy.
This bug only occured for permutations that left the number of qubits in the operator
unchanged.
32 changes: 20 additions & 12 deletions test/python/opflow/test_pauli_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

""" Test PauliSumOp """
"""Test PauliSumOp."""

import unittest
from itertools import product
from test.python.opflow import QiskitOpflowTestCase
from ddt import ddt, data, unpack

import numpy as np
from scipy.sparse import csr_matrix
Expand All @@ -41,6 +42,7 @@
from qiskit.quantum_info import Pauli, PauliTable, SparsePauliOp


@ddt
class TestPauliSumOp(QiskitOpflowTestCase):
"""PauliSumOp tests."""

Expand Down Expand Up @@ -163,22 +165,28 @@ def test_tensor(self):
expected = (Z ^ Z) + (Z ^ I)
self.assertEqual(pauli_sum, expected)

def test_permute(self):
"""permute test"""
pauli_sum = PauliSumOp(SparsePauliOp((X ^ Y ^ Z).primitive))
expected = PauliSumOp(SparsePauliOp((X ^ I ^ Y ^ Z ^ I).primitive))
@data(([1, 2, 4], "XIYZI"), ([2, 1, 0], "ZYX"))
@unpack
def test_permute(self, permutation, expected_pauli):
"""Test the permute method."""
pauli_sum = PauliSumOp(SparsePauliOp.from_list([("XYZ", 1)]))
expected = PauliSumOp(SparsePauliOp.from_list([(expected_pauli, 1)]))
permuted = pauli_sum.permute(permutation)

self.assertEqual(pauli_sum.permute([1, 2, 4]), expected)
with self.subTest(msg="test permutated object"):
self.assertEqual(permuted, expected)

pauli_sum = PauliSumOp(SparsePauliOp((X ^ Y ^ Z).primitive))
expected = PauliSumOp(SparsePauliOp((Z ^ Y ^ X).primitive))
self.assertEqual(pauli_sum.permute([2, 1, 0]), expected)
with self.subTest(msg="test original object is unchanged"):
original = PauliSumOp(SparsePauliOp.from_list([("XYZ", 1)]))
self.assertEqual(pauli_sum, original)

with self.assertRaises(OpflowError):
pauli_sum.permute([1, 2, 1])
@data([1, 2, 1], [1, 2, -1])
def test_permute_invalid(self, permutation):
"""Test the permute method raises an error on invalid permutations."""
pauli_sum = PauliSumOp(SparsePauliOp((X ^ Y ^ Z).primitive))

with self.assertRaises(OpflowError):
pauli_sum.permute([1, 2, -1])
pauli_sum.permute(permutation)

def test_compose(self):
"""compose test"""
Expand Down

0 comments on commit 497811a

Please sign in to comment.