From 7171e83939e9b533fd2193a8dc66638e84322ec9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 24 Jan 2023 17:36:00 +0000 Subject: [PATCH] Fix `PauliOp.adjoint()` (#9434) (#9443) * 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) Co-authored-by: Kazuki Tsuoka Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- 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):