Skip to content

Commit

Permalink
Fix the grouping index bug in Estimator (#1839)
Browse files Browse the repository at this point in the history
* Fix the bug

* lint
  • Loading branch information
ikkoham authored Jun 6, 2023
1 parent 9ee3997 commit 91a60b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
17 changes: 11 additions & 6 deletions qiskit_aer/primitives/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def _run(
self._observable_ids[_observable_key(observable)] = len(self._observables)
self._observables.append(observable)
job = PrimitiveJob(
self._call, circuit_indices, observable_indices, parameter_values, **run_options
self._call,
circuit_indices,
observable_indices,
parameter_values,
**run_options,
)
job.submit()
return job
Expand Down Expand Up @@ -317,14 +321,12 @@ def _calculate_result_index(circ_ind, obs_ind, term_ind, param_val, obs_maps, ex

result_index = 0
for _circ_ind, basis_map in exp_map.items():
for _basis_ind, (_, param_vals) in basis_map.items():
for _basis_ind, (_, (_, param_vals)) in enumerate(basis_map.items()):
if circ_ind == _circ_ind and basis_ind == _basis_ind:
result_index += param_vals.index(param_val)
return result_index
result_index += len(param_vals)
raise AerError(
"Bug. Please report from isssue: https://github.com/Qiskit/qiskit-aer/issues"
)
raise AerError("Bug. Please report from issue: https://github.com/Qiskit/qiskit-aer/issues")

def _create_post_processing(
self, circuits, observables, parameter_values, obs_maps, exp_map
Expand Down Expand Up @@ -475,7 +477,10 @@ def _expval_with_variance(counts) -> tuple[float, float]:

class _PostProcessing:
def __init__(
self, result_indices: list[int], paulis: list[PauliList], coeffs: list[list[float]]
self,
result_indices: list[int],
paulis: list[PauliList],
coeffs: list[list[float]],
):
self._result_indices = result_indices
self._paulis = paulis
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
issues:
- |
Fix a bug that returns wrong expectation values in :class:`~Estimator` when
``abelian_grouping=True``.
15 changes: 15 additions & 0 deletions test/terra/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ def test_estimator(self, abelian_grouping):
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1.728515625])

with self.subTest("SparsePauliOp with another grouping"):
observable = SparsePauliOp.from_list(
[
("YZ", 0.39793742484318045),
("ZI", -0.39793742484318045),
("ZZ", -0.01128010425623538),
("XX", 0.18093119978423156),
]
)
ansatz = RealAmplitudes(num_qubits=2, reps=2)
est = Estimator(abelian_grouping=abelian_grouping)
result = est.run(ansatz, observable, parameter_values=[[0] * 6], seed=15).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [-0.4], rtol=0.02)

@data(True, False)
def test_init_observable_from_operator(self, abelian_grouping):
"""test for evaluate without parameters"""
Expand Down

0 comments on commit 91a60b7

Please sign in to comment.