expectation_value() rebuilds the entire evaluation functional on every call
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- cpp
- Domain
- performance
Research direction
Read cpp/monoprop/detail/monomial_propagator/MonomialPropagatorImpl.h, especially make_functional_ and the two expectation methods, then inspect the listed mutators and existing tests. Add a regression test that changes propagator state between calls and confirms the second result reflects it; benchmark with just bench serial and run just test and just test-mpi "1;2;4". Done means repeated unpared calls reuse the functional without returning stale results after mutations.
Written by the indexing model from the issue text.
Description
🤖 AI text below 🤖
What
expectation_value and expectation_value_and_gradient construct a fresh evaluation functional on
every single call.
cpp/monoprop/detail/monomial_propagator/MonomialPropagatorImpl.h:989-1004
auto MonomialPropagator<NumModes>::expectation_value(const VecD ¶meters) -> double {
...
return expectation_value_functional(std::nullopt)(parameters);
}
auto MonomialPropagator<NumModes>::expectation_value_and_gradient(const VecD ¶meters)
-> std::pair<double, VecD> {
...
return expectation_value_and_gradient_functional(std::nullopt)(parameters);
}
Why this is a problem
make_functional_ (same file, ~lines 885-954) is not cheap. Per call it:
- copies the full coefficient vector —
VecD op = mp_op_.get_operator();, onedoubleper operator
term; - snapshots the evaluation state (
EvalState::sparsecopies rows and values;EvalState::dense
copies the whole vector); - calls
build_cos_callbacks, which allocates aLayerCosper graph layer and constructs a
LazyFoldin each (make_lazy_fold→make_fold_mask+build_even_parity_generator_columns+
a heapstd::vector<size_t> columns).
That is O(terms + layers) of allocation before any evaluation work happens. An optimizer looping on
expectation_value pays it on every iteration — and on a partitioned propagator, once per partition,
since the facade fans out to s.expectation_value(parameters).
The documented workaround is to use expectation_value_functional and hold the callable, but the
convenience methods are the obvious entry point and are what the shorthand expval / expval_and_grad
wrap.
Suggested fix
Memoize the pare-free functional behind a revision counter, invalidated by every mutator. The mutation
surface is small and already funnelled:
build_graph,propagatecontract_partially(..., inplace=true)apply_initial_operator_/update_initial_operatorset_parameter_mappingupdate_setting_— which already covers thelower_atol,upper_atol,cutoff,cutoff_typeand
basis_changesetters
Also bump it from the non-const mp_op() and indexing() accessors. Both hand out mutable
references to the operator store, so a caller can mutate behind the cache's back; a conservative
invalidation on any non-const access is the cheap way to stay correct.
Worth noting what the existing safety net does and does not catch: the functional already calls
validate_expected_graph_layers(graph->layers(), expected_layers) on entry, which detects layer-count
drift from build_graph/contract_partially — but not an operator re-weight. So
update_initial_operator is the case the revision counter must not miss.
Only the pare_threshold == std::nullopt path should be cached; a pared functional is explicitly
per-threshold.
Verification
just bench serialbefore/after, comparingbenches/results/REPORT.md.- Add a regression test that mutates the propagator between two
expectation_valuecalls —
update_initial_operator, an atol setter,set_parameter_mapping— and asserts the second value
reflects the mutation. That is the bug the cache could introduce, so it should be written before the
cache. just test,just test-mpi "1;2;4".
Found by a code-reading review of the repository at 29a8050. No build tree was available, so the
analysis is from source inspection and should be confirmed by measurement.
- Dominant language
- C++
- Stars
- 52
- Forks
- 2
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 22
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Algorithmiq/monoprop
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Algorithmiq/monoprop#193 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Algorithmiq/monoprop#192 ·
Maintainers usually reply within 1 day
-
bug performance
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Algorithmiq/monoprop#191 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Algorithmiq/monoprop#189 ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 4/5 3-5 days Newbie friendliness 44/100
Algorithmiq/monoprop#442 ·
Maintainers usually reply within 1 day
All issues in Algorithmiq/monoprop
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
? - Needs Triage bot_watch bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NVIDIA/cudf-spark-jni#5594 ·
Maintainers usually reply within 1 day
-
Branch-master
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
TrinityCore/TrinityCore#32177 ·
Maintainers usually reply within 10 days
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
mw00/project-maya#1499 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 1 day