From feda4b1abe24bcb89460a84e4d5ec7d06d467a58 Mon Sep 17 00:00:00 2001 From: Kazuki Tsuoka Date: Wed, 25 Jan 2023 00:39:21 +0900 Subject: [PATCH] Fix `PauliOp.adjoint()` (#9434) * bug fix * reno mod * reno mod * Update releasenotes/notes/fix-PauliOp-adjoint-a275876185df989f.yaml Co-authored-by: Julien Gacon Co-authored-by: Julien Gacon (cherry picked from commit 85c30a2ffd47bd60944ed9820e878cd4eb538b21) --- qiskit/opflow/primitive_ops/pauli_op.py | 2 +- .../notes/fix-PauliOp-adjoint-a275876185df989f.yaml | 6 ++++++ test/python/opflow/test_op_construction.py | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-PauliOp-adjoint-a275876185df989f.yaml diff --git a/qiskit/opflow/primitive_ops/pauli_op.py b/qiskit/opflow/primitive_ops/pauli_op.py index ff1c20f19924..1699275ef52a 100644 --- a/qiskit/opflow/primitive_ops/pauli_op.py +++ b/qiskit/opflow/primitive_ops/pauli_op.py @@ -84,7 +84,7 @@ def add(self, other: OperatorBase) -> OperatorBase: return SummedOp([self, other]) def adjoint(self) -> "PauliOp": - return PauliOp(self.primitive, coeff=self.coeff.conjugate()) + return PauliOp(self.primitive.adjoint(), coeff=self.coeff.conjugate()) def equals(self, other: OperatorBase) -> bool: if isinstance(other, PauliOp) and self.coeff == other.coeff: diff --git a/releasenotes/notes/fix-PauliOp-adjoint-a275876185df989f.yaml b/releasenotes/notes/fix-PauliOp-adjoint-a275876185df989f.yaml new file mode 100644 index 000000000000..d9860f702700 --- /dev/null +++ b/releasenotes/notes/fix-PauliOp-adjoint-a275876185df989f.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a bug where :meth:`.PauliOp.adjoint` did not return a correct value for Paulis + with complex coefficients, like ``PauliOp(Pauli("iX"))``. + Fixed `#9433 `. diff --git a/test/python/opflow/test_op_construction.py b/test/python/opflow/test_op_construction.py index d797c566b9a4..fd474f3942ef 100644 --- a/test/python/opflow/test_op_construction.py +++ b/test/python/opflow/test_op_construction.py @@ -1288,6 +1288,10 @@ def test_adjoint(self): expected = PauliOp(Pauli("XYZX"), coeff=2 - 3j) self.assertEqual(~pauli_op, expected) + pauli_op = PauliOp(Pauli("iXYZX"), coeff=2 + 3j) + expected = PauliOp(Pauli("-iXYZX"), coeff=2 - 3j) + self.assertEqual(~pauli_op, expected) + @data(*itertools.product(pauli_group_labels(2, full_group=True), repeat=2)) @unpack def test_compose(self, label1, label2):