Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Be more memory efficient converting SummedOp to MatrixOp #1129

Merged
merged 4 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 9 additions & 0 deletions qiskit/aqua/operators/list_ops/summed_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ def reduce(self) -> OperatorBase:
else:
return cast(OperatorBase, reduced_ops)

def to_matrix_op(self, massive: bool = False) -> OperatorBase:
""" Returns an equivalent Operator composed of only NumPy-based primitives, such as
``MatrixOp`` and ``VectorStateFn``. """
accum = self.oplist[0].to_matrix_op(massive=massive) # type: ignore
for i in range(1, len(self.oplist)):
accum += self.oplist[i].to_matrix_op(massive=massive) # type: ignore

return accum * self.coeff

def to_legacy_op(self, massive: bool = False) -> LegacyBaseOperator:
# We do this recursively in case there are SummedOps of PauliOps in oplist.
legacy_ops = [op.to_legacy_op(massive=massive) for op in self.oplist]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
Previously, SummedOp.to_matrix_op built a list MatrixOp's (with numpy
matrices) and then summed them, returning a single MatrixOp. Some
algorithms (for example vqe) require summing thousands of matrices, which
exhausts memory when building the list of matrices. With this change,
no list is constructed. Rather, each operand in the sum is converted to
a matrix, added to an accumulator, and discarded.