Skip to content

Statevector._expectation_value_pauli returns incorrect result in certain cases #13029

@yjh-bill

Description

@yjh-bill

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.

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


The whole function is attached below to provide context.

def _expectation_value_pauli(self, pauli, qargs=None):
"""Compute the expectation value of a Pauli.
Args:
pauli (Pauli): a Pauli operator to evaluate expval of.
qargs (None or list): subsystems to apply operator on.
Returns:
complex: the expectation value.
"""
n_pauli = len(pauli)
if qargs is None:
qubits = np.arange(n_pauli)
else:
qubits = np.array(qargs)
x_mask = np.dot(1 << qubits, pauli.x)
z_mask = np.dot(1 << qubits, pauli.z)
pauli_phase = (-1j) ** pauli.phase if pauli.phase else 1
if x_mask + z_mask == 0:
return pauli_phase * np.linalg.norm(self.data)
if x_mask == 0:
return pauli_phase * expval_pauli_no_x(self.data, self.num_qubits, z_mask)
x_max = qubits[pauli.x][-1]
y_phase = (-1j) ** pauli._count_y()
y_phase = y_phase[0]
return pauli_phase * expval_pauli_with_x(
self.data, self.num_qubits, z_mask, x_mask, y_phase, x_max
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions