Conversation
_sum_transformation seeded its accumulation with arithmetics.evaluate(ScalarOperator.const(0)) and added every term to that, including the first. For the default evaluation arithmetics used by OperatorSum.evaluate(), that resolves to a plain `0 + term`, and the underlying C++ operator classes turn that into an explicit, zero-coefficient identity term with no degrees rather than simplifying it away. So evaluating a multi-term operator (e.g. an observable re-evaluated at each time step in cudaq.evolve) always grew one extra term, inflating term_count and yielding a term with an empty degrees list. Seed the accumulation with the first evaluated term instead, and only fall back to evaluating a scalar zero when the operator has no terms at all. Signed-off-by: Udaya Tejas <udayatejas2004@gmail.com>
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.
Fixes #5348
Bug
_sum_transformation(python/cudaq/operators/manipulation.py:154-188) seeds its accumulatorwith
arithmetics.evaluate(ScalarOperator.const(0))and adds every term to it, including thefirst. For the default arithmetics used by
OperatorSum.evaluate(), that first add is a plain0 + term1, and the underlying C++ operator classes turnscalar + operatorinto an explicitzero-coefficient identity term (
degrees == []) instead of simplifying it away. So evaluating amulti-term
SpinOperator/BosonOperator/FermionOperator/MatrixOperatoralways grows oneextra term that acts on no degrees of freedom, inflating
term_countby one every time. This isnot just cosmetic:
python/cudaq/dynamics/evolution.py:303,608calls.evaluate(**step_parameters)on every user-supplied observable at every time step of
cudaq.evolve().Fix
Seed the accumulation with the first evaluated term instead of a scalar zero, and only fall back
to evaluating
ScalarOperator.const(0)when the operator has no terms at all - soarithmetics.addis never called with a scalar zero as either operand.
Verification
Not a regression (introduced with the class in
da31e1b7a/ #2817, untouched since except acopyright-year bump - confirmed via
git log -S/git log --follow). No existing test checksterm_countor per-termdegreesafter.evaluate().Negative control: added
test_evaluate_no_spurious_zero_termtopython/tests/operator/test_matrix_op.py, ran it against the productionmanipulation.pybefore this fix (reverted to the upstream version) and after, using the CUDA-Q 0.15.1 wheel
(
aca5853a7, byte-identical to this file on currentmain- zero commits touchpython/cudaq/operatorsbetween the wheel commit andmain):Also ran the full existing
python/tests/operator/suite (test_matrix_op.py,test_spin_op.py,test_fermion_op.py,test_boson_op.py,test_scalar_op.py,test_conversions.py) with the fix applied: 59 passed (one pre-existing, unrelated nativetest_file_serializationcrash deselected - reproduces identically with or without thischange, in unrelated spin-operator file-deserialization code).
Small, net-positive diff; touches only the shared accumulation helper and adds one regression
test.
Signed-off-by: Udaya Tejas udayatejas2004@gmail.com