Open yjh-bill opened 1 month ago
Please can you show a reproducing code block following the bug report template, and the output you expected?
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')))
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.
The two print statement should give 4+0j
and 4.0
respectively.
The two print statement gave 2+0j
and 2.0
respectively.
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