Repository: Qiskit/qiskit

Issue:
PauliExpectation generates incorrect decomposition
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: 0.19
- **Python version**: 3.9
- **Operating system**: Windows 10

### What is the current behavior?

The `PauliExpectation` with option `group_paulis= True` generates an incorrect decomposition for the `Y` operator.

### Steps to reproduce the problem

A minimal example is:
```
rom qiskit.opflow import PauliBasisChange
from qiskit.opflow.primitive_ops import PauliSumOp
from qiskit.quantum_info import Pauli, SparsePauliOp
from qiskit.opflow import StateFn, PauliExpectation, Zero, One
from qiskit.circuit import QuantumCircuit

# define obervable
term = SparsePauliOp(Pauli("Y"), coeffs=[1])       
qubit_op =PauliSumOp(term).reduce()
observable = StateFn(qubit_op).adjoint()

print(qubit_op)
print(observable)

# decompose the obervable in two different ways
pb=PauliExpectation(group_paulis= False)
pauli_operator=pb.convert(observable)

pb=PauliExpectation(group_paulis= True)
pauli_operator2=pb.convert(observable)

# are different
print(pauli_operator)
print(pauli_operator2)

# check expectation values for a state
qc=QuantumCircuit(1); qc.h(0); qc.sdg(0)
state=StateFn(qc)
print(state)

(pauli_operator @ state).eval(), (pauli_operator2 @ state).eval() # different!
```

### What is the expected behavior?

The decompositions with either `group_paulis= True` or `group_paulis= False` should be equal (or equivalent). The output of `print(pauli_operator2)` for qiskit 0.18.1 is 
```
ComposedOp([
  OperatorMeasurement(1.0 * Z),
       ┌─────┐┌───┐
  q_0: ┤ Sdg ├┤ H ├
       └─────┘└───┘
])
```
But for current `dev` it is.
```
ComposedOp([
  OperatorMeasurement(1.0 * -iZ),
       ┌─────┐┌───┐
  q_0: ┤ Sdg ├┤ H ├
       └─────┘└───┘
])
```

### Suggested solutions

The problem is introduced in #6826, it is likely the problem is related to the code changes there.





Discussion:
@ikkoham This refers to a prior PR that you were involved with and says the issue arose from changes there.
Thank you. I'll check it.

Resolving patch:
diff --git a/qiskit/opflow/converters/pauli_basis_change.py b/qiskit/opflow/converters/pauli_basis_change.py
--- a/qiskit/opflow/converters/pauli_basis_change.py
+++ b/qiskit/opflow/converters/pauli_basis_change.py
@@ -144,6 +144,10 @@ def convert(self, operator: OperatorBase) -> OperatorBase:
             cob_instr_op, _ = self.get_cob_circuit(origin_pauli)
             primitive.paulis.z = np.logical_or(primitive.paulis.x, primitive.paulis.z)
             primitive.paulis.x = False
+            # The following line is because the deprecated PauliTable did not have a phase
+            # and did not track it, so phase=0 was always guaranteed.
+            # But the new PauliList may change phase.
+            primitive.paulis.phase = 0
             dest_pauli_sum_op = PauliSumOp(primitive, coeff=operator.coeff, grouping_type="TPB")
             return self._replacement_fn(cob_instr_op, dest_pauli_sum_op)
 


Test patch:
diff --git a/test/python/opflow/test_pauli_basis_change.py b/test/python/opflow/test_pauli_basis_change.py
--- a/test/python/opflow/test_pauli_basis_change.py
+++ b/test/python/opflow/test_pauli_basis_change.py
@@ -12,15 +12,26 @@
 
 """ Test Pauli Change of Basis Converter """
 
-import unittest
-from test.python.opflow import QiskitOpflowTestCase
 import itertools
+import unittest
 from functools import reduce
+from test.python.opflow import QiskitOpflowTestCase
+
 import numpy as np
 
-from qiskit.quantum_info import Pauli
-from qiskit.opflow import X, Y, Z, I, SummedOp, ComposedOp, PauliSumOp
+from qiskit import QuantumCircuit
+from qiskit.opflow import (
+    ComposedOp,
+    I,
+    OperatorStateFn,
+    PauliSumOp,
+    SummedOp,
+    X,
+    Y,
+    Z,
+)
 from qiskit.opflow.converters import PauliBasisChange
+from qiskit.quantum_info import Pauli, SparsePauliOp
 
 
 class TestPauliCoB(QiskitOpflowTestCase):
@@ -125,6 +136,21 @@ def test_grouped_pauli(self):
         )
         np.testing.assert_array_almost_equal(inst.to_matrix(), expected_inst)
 
+    def test_grouped_pauli_statefn(self):
+        """grouped pauli test with statefn"""
+        grouped_pauli = PauliSumOp(SparsePauliOp(["Y"]), grouping_type="TPB")
+        observable = OperatorStateFn(grouped_pauli, is_measurement=True)
+
+        converter = PauliBasisChange(replacement_fn=PauliBasisChange.measurement_replacement_fn)
+        cob = converter.convert(observable)
+
+        expected = PauliSumOp(SparsePauliOp(["Z"]), grouping_type="TPB")
+        self.assertEqual(cob[0].primitive, expected)
+        circuit = QuantumCircuit(1)
+        circuit.sdg(0)
+        circuit.h(0)
+        self.assertEqual(cob[1].primitive, circuit)
+
 
 if __name__ == "__main__":
     unittest.main()