diff --git a/qiskit/algorithms/gradients/lin_comb_sampler_gradient.py b/qiskit/algorithms/gradients/lin_comb_sampler_gradient.py index 0f4cf0631e97..2a2af2dd8df6 100644 --- a/qiskit/algorithms/gradients/lin_comb_sampler_gradient.py +++ b/qiskit/algorithms/gradients/lin_comb_sampler_gradient.py @@ -114,8 +114,10 @@ def _run( n = 2 ** circuits[i].num_qubits grad_dists = np.zeros((len(metadata_[i]["parameters"]), n)) for idx, coeff, dist in zip(result_indices_all[i], coeffs_all[i], result.quasi_dists): - grad_dists[idx][list(dist.keys())[:n]] += np.array(list(dist.values())[:n]) * coeff - grad_dists[idx][list(dist.keys())[:n]] -= np.array(list(dist.values())[n:]) * coeff + plus = {key: val for key, val in dist.items() if key < n} + minus = {key - n: val for key, val in dist.items() if key >= n} + grad_dists[idx][list(plus.keys())] += np.fromiter(plus.values(), dtype=float) * coeff + grad_dists[idx][list(minus.keys())] -= np.fromiter(minus.values(), dtype=float) * coeff gradient_ = [] for grad_dist in grad_dists: