Skip to content

Commit

Permalink
performance improvement of _DiagonalEstimator
Browse files Browse the repository at this point in the history
  • Loading branch information
t-imamichi committed Dec 2, 2022
1 parent 8bac5d4 commit 36f7f85
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions qiskit/algorithms/minimum_eigensolvers/diagonal_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,11 @@ def aggregate(measurements):


def _evaluate_sparsepauli(state: int, observable: SparsePauliOp) -> complex:
packed_uint8 = np.packbits(observable.paulis.z, axis=1, bitorder="little").astype(object)
power_uint8 = 1 << (8 * np.arange(packed_uint8.shape[1], dtype=object))
packed_int = (packed_uint8 @ power_uint8) & state
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"]
coeff * (-1) ** bin(bit).count("1") for coeff, bit in zip(observable.coeffs, packed_int)
)


Expand Down

0 comments on commit 36f7f85

Please sign in to comment.