Avoid deepcopy in SymbolicOperator arithmetic#1415
Open
blazingphoenix7 wants to merge 2 commits into
Open
Conversation
__add__, __sub__, __mul__ and accumulate copied the operand with copy.deepcopy before mutating it in place. Term keys are tuples of tuples and coefficients are numbers or sympy expressions, all immutable, so copying the terms dictionary is enough to keep the result independent of the operand. Add a small _clone helper that does this and use it in place of deepcopy.
Contributor
There was a problem hiding this comment.
Code Review
This pull request replaces expensive copy.deepcopy calls with a custom _clone method in SymbolicOperator to improve performance. The review feedback points out that in the accumulate method, cloning cls.zero() when start is None is redundant and suggests an optimized ternary expression instead.
mhucka
approved these changes
Jul 16, 2026
mhucka
left a comment
Collaborator
There was a problem hiding this comment.
Thank you for doing this. Looks good!
Collaborator
|
@blazingphoenix7 This is approved, but we are about to release 1.8.1 to fix a backward compatibility problem in 1.8.0. We will merge this PR after the 1.8.1 release. |
Contributor
Author
|
Sounds good, thanks. Happy to wait for 1.8.1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the patch offered in #1407.
SymbolicOperator.__add__,__sub__,__mul__andaccumulateeach start bycopying the operand with
copy.deepcopyand then mutate the copy in place withthe matching
__iadd__/__isub__/__imul__. deepcopy walks the wholetermsdictionary and recursively copies every key and every value.None of that recursion buys anything here. A term key is a tuple of
(index, action)tuples, and a coefficient is an int, float, complex or sympyexpression. All of those are immutable, so the copy only needs to be independent
at the dictionary level: the in-place operators rebind
self.terms[term]and addor remove keys, they never mutate a key or a coefficient object in place. A plain
dict(self.terms)is therefore enough to make a result that is safe to mutatewithout touching the original.
This adds a small
_clonehelper that builds an empty instance and copies theterms dictionary into it, and uses it in the four spots that called deepcopy. For
operators with many terms it's a good deal cheaper than the deep copy, and the
out-of-place guarantee (
a + bleaves bothaandbunchanged) is the same asbefore.
Tests
The existing
symbolic_operator_test.pyalready covers this: the add, subtract,multiply and divide tests each assert the operation is done out of place, and
qubit_operator_test.pyexercisesaccumulate. I checked that every changed lineis hit by the current suite, and
ops/operators/,utils/andtransforms/opconversions/pass. black and mypy are clean on the file.Benchmarks
Random QubitOperators over 30 qubits, both operands the same size:
Measured on my laptop (Intel Core Ultra 5 225, Windows, Python 3.12, numpy 2.5).
Times are medians over 25 repetitions after a warmup. I ran the whole
base/patch comparison twice in alternation with fixed seeds and the speedups
agreed within about 10 percent, so the table pools both rounds.