Skip to content

Commit 5c133d4

Browse files
committed
Use sparse matmul for non-Operator
1 parent c0902ef commit 5c133d4

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

qiskit/algorithms/eigensolvers/numpy_eigensolver.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,17 @@ def _eval_aux_operators(
174174
if operator is None:
175175
continue
176176
value = 0.0
177-
if isinstance(operator, PauliSumOp):
178-
if operator.coeff != 0:
179-
mat = operator.to_spmatrix()
180-
# Terra doesn't support sparse yet, so do the matmul directly if so
181-
# This is necessary for the particle_hole and other chemistry tests because the
182-
# pauli conversions are 2^12th large and will OOM error if not sparse.
183-
if isinstance(mat, scisparse.spmatrix):
184-
value = mat.dot(wavefn).dot(np.conj(wavefn))
185-
else:
186-
value = (
187-
Statevector(wavefn).expectation_value(operator.primitive)
188-
* operator.coeff
189-
)
190-
else:
177+
if isinstance(operator, Operator):
191178
value = Statevector(wavefn).expectation_value(operator)
179+
else:
180+
if isinstance(operator, PauliSumOp):
181+
if operator.coeff != 0:
182+
op_matrix = operator.to_spmatrix()
183+
value = op_matrix.dot(wavefn).dot(np.conj(wavefn))
184+
else:
185+
op_matrix = operator.to_matrix(sparse=True)
186+
value = op_matrix.dot(wavefn).dot(np.conj(wavefn))
187+
192188
value = value if np.abs(value) > threshold else 0.0
193189
# The value gets wrapped into a tuple: (mean, metadata).
194190
# The metadata includes variance (and, for other eigensolvers, shots).

0 commit comments

Comments
 (0)