Skip to content

Commit

Permalink
Performance improvement of _DiagonalEstimator (#9229)
Browse files Browse the repository at this point in the history
* performance improvement of _DiagonalEstimator

* optimize

Co-authored-by: Jake Lishman <[email protected]>

* add reno

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 0df0d29)
  • Loading branch information
t-imamichi authored and mergify[bot] committed Dec 5, 2022
1 parent 778f840 commit 214b028
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 7 additions & 12 deletions qiskit/algorithms/minimum_eigensolvers/diagonal_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,14 @@ def aggregate(measurements):
return aggregate


_PARITY = np.array([-1 if bin(i).count("1") % 2 else 1 for i in range(256)], dtype=np.complex128)


def _evaluate_sparsepauli(state: int, observable: SparsePauliOp) -> complex:
return sum(
coeff * _evaluate_bitstring(state, paulistring)
for paulistring, coeff in observable.label_iter()
)


def _evaluate_bitstring(state: int, paulistring: str) -> float:
"""Evaluate a bitstring on a Pauli label."""
n = len(paulistring) - 1
return np.prod(
[-1 if state & (1 << (n - i)) else 1 for i, pauli in enumerate(paulistring) if pauli == "Z"]
)
packed_uint8 = np.packbits(observable.paulis.z, axis=1, bitorder="little")
state_bytes = np.frombuffer(state.to_bytes(packed_uint8.shape[1], "little"), dtype=np.uint8)
reduced = np.bitwise_xor.reduce(packed_uint8 & state_bytes, axis=1)
return np.sum(observable.coeffs * _PARITY[reduced])


def _check_observable_is_diagonal(observable: SparsePauliOp) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed a performance bug that :class:`.SamplingVQE` evaluated the energies of eigenstates
in a slow way.

0 comments on commit 214b028

Please sign in to comment.