diff --git a/qiskit/algorithms/eigensolvers/numpy_eigensolver.py b/qiskit/algorithms/eigensolvers/numpy_eigensolver.py index ae4a45d88cea..41af1708ae07 100755 --- a/qiskit/algorithms/eigensolvers/numpy_eigensolver.py +++ b/qiskit/algorithms/eigensolvers/numpy_eigensolver.py @@ -120,10 +120,10 @@ def _solve(self, operator: BaseOperator | PauliSumOp) -> tuple[np.ndarray, np.nd "Trying dense computation", type(operator), ) - try: - op_matrix = operator.to_matrix() - except AttributeError as ex: - raise AlgorithmError(f"Unsupported operator type `{type(operator)}`.") from ex + try: + op_matrix = operator.to_matrix() + except AttributeError as ex: + raise AlgorithmError(f"Unsupported operator type `{type(operator)}`.") from ex if isinstance(op_matrix, scisparse.csr_matrix): # If matrix is diagonal, the elements on the diagonal are the eigenvalues. Solve by sorting. diff --git a/releasenotes/notes/fix-numpy-eigensolver-sparse-0e255d7b13b5e43b.yaml b/releasenotes/notes/fix-numpy-eigensolver-sparse-0e255d7b13b5e43b.yaml new file mode 100644 index 000000000000..2440e729c00e --- /dev/null +++ b/releasenotes/notes/fix-numpy-eigensolver-sparse-0e255d7b13b5e43b.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a bug in :class:`~.minimum_eigensolvers.NumPyMinimumEigensolver` and + :class:`~.eigensolvers.NumPyEigensolver` where operators that support conversion + to sparse matrices, such as :class:`.SparsePauliOp`, were converted to dense matrices anyways.