Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement of _DiagonalEstimator #9229

Merged
merged 4 commits into from
Dec 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions qiskit/algorithms/minimum_eigensolvers/diagonal_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,10 @@ def aggregate(measurements):


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").astype(object)
power_uint8 = 1 << (8 * np.arange(packed_uint8.shape[1], dtype=object))
bits = (packed_uint8 @ power_uint8) & state
return sum(coeff * (-1) ** bin(bit).count("1") for coeff, bit in zip(observable.coeffs, bits))


def _check_observable_is_diagonal(observable: SparsePauliOp) -> bool:
Expand Down