Skip to content

Possible micro-optimization in linear_qubit_operator.py #1410

Description

@mhucka

Is your feature request related to a use case or problem?

Gemini Code Assist reported the following for linear_qubit_operator.py. This looks simple enough to do, but it should be checked for correctness, and some kind of unit tests should be added to go along with this.

Performance/Memory Overhead in Single-Process Path (Lines 189-190):

Using functools.reduce(numpy.add, ...) creates $N-1$ intermediate numpy arrays, where $N$ is the number of operators. For large state vectors, this causes significant memory churn and garbage collection overhead.

Fix:

Initialize a single zero array and accumulate the results in-place using += as suggested below.

        if self.options.processes <= 1:
            res = numpy.zeros(x.shape, dtype=complex)
            for operator in self.linear_operators:
                res += operator * x
            return res

Originally posted by @gemini-code-assist[bot] in #1404 (comment)

What solution or approach do you envision?

GCA's suggestion should be implemented and tested.

How urgent is this for you?

P3 – not blocked; it's an idea

Metadata

Metadata

Assignees

No one assigned

    Fields

    No fields configured for Enhancement.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions