Skip to content

Commit

Permalink
fix LinCombSamplerGradient
Browse files Browse the repository at this point in the history
  • Loading branch information
t-imamichi committed Dec 7, 2022
1 parent 0df0d29 commit db67cb9
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions qiskit/algorithms/gradients/lin_comb_sampler_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit db67cb9

Please sign in to comment.