Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the calculation for identity operator's expectation value #13345

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qiskit/quantum_info/states/statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _expectation_value_pauli(self, pauli, qargs=None):
pauli_phase = (-1j) ** pauli.phase if pauli.phase else 1

if x_mask + z_mask == 0:
return pauli_phase * np.linalg.norm(self.data)
return pauli_phase * np.linalg.norm(self.data) ** 2

if x_mask == 0:
return pauli_phase * expval_pauli_no_x(self.data, self.num_qubits, z_mask)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed a bug that caused :meth:`Statevector.expectation_value` to yield incorrect results
Cryoris marked this conversation as resolved.
Show resolved Hide resolved
for the identity operator when the statevector was not normalized.
25 changes: 25 additions & 0 deletions test/python/quantum_info/states/test_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,31 @@ def test_expval_pauli_qargs(self, qubits):
expval = state.expectation_value(op, qubits)
self.assertAlmostEqual(expval, target)

def test_expval_identity(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you need to run the formatter here, you can do that e.g. by calling make black (you might want to make sure the right version is installed before doing so, by running pip install -r requirements-dev.txt -c constraints.txt before)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks for the instruction.

"""Test whether the calculation for identity operator has been fixed"""

# 1 qubit case test
state_1 = Statevector.from_label("0")
state_1_n1 = 2 * state_1 # test the same state with different norms
state_1_n2 = (1 + 2j) * state_1
identity_op_1 = SparsePauliOp.from_list([("I", 1)])
expval_state_1 = state_1.expectation_value(identity_op_1)
expval_state_1_n1 = state_1_n1.expectation_value(identity_op_1)
expval_state_1_n2 = state_1_n2.expectation_value(identity_op_1)
self.assertAlmostEqual(expval_state_1, 1.0 + 0j)
self.assertAlmostEqual(expval_state_1_n1, 4 + 0j)
self.assertAlmostEqual(expval_state_1_n2, 5 + 0j)

# Let's try a multi-qubit case
n_qubits = 3
state_coeff = 3 - 4j
op_coeff = 2 - 2j
state_test = state_coeff * Statevector.from_label("0" * n_qubits)
op_test = SparsePauliOp.from_list([("I" * n_qubits, op_coeff)])
expval = state_test.expectation_value(op_test)
target = op_coeff * np.abs(state_coeff) ** 2
self.assertAlmostEqual(expval, target)

@data(*(qargs for i in range(4) for qargs in permutations(range(4), r=i + 1)))
def test_probabilities_qargs(self, qargs):
"""Test probabilities method with qargs"""
Expand Down