Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.1k stars 2.34k forks source link

Statevector._expectation_value_pauli returns incorrect result in certain cases #13029

Open yjh-bill opened 1 month ago

yjh-bill commented 1 month ago

The calculation for the expectation value of the identity operator seems incorrect when the statevector is not normalized to 1.

I think the reason is that one should use the norm squared rather than the norm in the following lines. https://github.com/Qiskit/qiskit/blob/1be2c5aa103f1991f2fdc75a77a09f82635b8431/qiskit/quantum_info/states/statevector.py#L478-L479


The whole function is attached below to provide context.

https://github.com/Qiskit/qiskit/blob/1be2c5aa103f1991f2fdc75a77a09f82635b8431/qiskit/quantum_info/states/statevector.py#L458-L491

jakelishman commented 1 month ago

Please can you show a reproducing code block following the bug report template, and the output you expected?

yjh-bill commented 1 month ago

Sure. Here it is

from qiskit.quantum_info import Statevector, SparsePauliOp, Pauli

state_normed = Statevector.from_label("0")
state_not_normed = 2 * state_normed

identity_op = SparsePauliOp.from_list([("I", 1)])

# using the public interface (expectation_value)
print(state_not_normed.expectation_value(identity_op))

# using _expectation_value_pauli (the backend)
print(state_not_normed._expectation_value_pauli(Pauli('I')))

Environment

Python 3.11.6 qiskit 0.45.3

Judging from the source code on github, the issue should be platform-independent and exists across different versions.

What is the expected result

The two print statement should give 4+0j and 4.0 respectively.

What is the actual result

The two print statement gave 2+0j and 2.0 respectively.