Fix weighted_pauli_operator for Aer 0.4
Fixes change in the complex format of snapshot values in Aer 0.4 from a list [re, im] to a type complex.
@@ -753,8 +753,11 @@ class WeightedPauliOperator(BaseOperator):
if use_simulator_snapshot_mode:
snapshot_data = result.data(
circuit_name_prefix + 'snapshot_mode')['snapshots']
- expval = snapshot_data['expectation_value']['expval'][0]['value']
- avg = expval[0] + 1j * expval[1]
+ avg = snapshot_data['expectation_value']['expval'][0]['value']
+ if isinstance(avg, (list, tuple)):
+ # Aer versions before 0.4 use a list snapshot format
+ # which must be converted to a complex value.
+ avg = avg[0] + 1j * avg[1]
elif statevector_mode:
quantum_state = np.asarray(result.get_statevector(circuit_name_prefix + 'psi'))
for weight, pauli in self._paulis:
