Skip to content

Use a comprehension in _instantiate_operators - #554

Merged
lkdvos merged 1 commit into
QuantumKitHub:mainfrom
KristofferC:kc/instantiate-operators-comprehension
Nov 11, 2026
Merged

lkdvos merged 1 commit into
QuantumKitHub:mainfrom
KristofferC:kc/instantiate-operators-comprehension

Conversation

@KristofferC

@KristofferC KristofferC commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

_instantiate_operators ran all of the work of the MPOHamiltonian constructors inside a map(...) do block. Below a map call, type inference applies its recursion limit to nested map calls over vectors whose element type is not concrete (for example map(dual, S.spaces) in BlockTensorKit), and since limited results are not cached, every function in between is inferred again at every call site. A comprehension gives the same result without the map frame at the root. The analysis is in JuliaLang/julia#64862; QuantumKitHub/BlockTensorKit.jl#90 fixes the same thing at the other end, and either one alone is enough for this case.

First call of InfiniteMPOHamiltonian([P, P], (1, 2) => h, (2, 3) => h) from that issue:

Julia main this PR
1.10.13 10.7 s, 4.5 GiB 9.4 s, 3.5 GiB
1.13.3 36.0 s, 16.7 GiB 16.8 s, 7.6 GiB
1.14.0-alpha2 29.5 s, 16.4 GiB 14.7 s, 7.9 GiB

Checklist

  • Tests pass locally (finite.jl, infinite.jl and mixed.jl in test/hamiltonian)
  • Documentation updated, if this PR changes public API (docstrings, docs/src/): no API change
  • Runic formatter is run
  • Changelog entry added under [Unreleased] in docs/src/changelog.md, if this PR is user-facing (new feature, behavior change, bug fix, deprecation, or removal)

🤖 Generated with Claude Code

`_instantiate_operators` ran all of the work of the `MPOHamiltonian` constructors inside
`map(collect(local_operators)) do term ... end`. Below that `map` call, type inference applies
its recursion limit to every nested `map` over a vector whose element type is not concrete,
such as `map(dual, S.spaces)` in `dual(::SumSpace)`. The limited results are not cached, so
each function between the two `map` calls is inferred again at every call site
(JuliaLang/julia#64862).

A comprehension gives the same result without a `map` frame at the root. First call of
`InfiniteMPOHamiltonian([P, P], (1, 2) => h, (2, 3) => h)` from that issue:

| Julia         | before           | after            |
|---------------|------------------|------------------|
| 1.10.13       | 10.7 s, 4.5 GiB  | 9.4 s, 3.5 GiB   |
| 1.13.3        | 36.0 s, 16.7 GiB | 16.8 s, 7.6 GiB  |
| 1.14.0-alpha2 | 29.5 s, 16.4 GiB | 14.7 s, 7.9 GiB  |

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@lkdvos

lkdvos commented Nov 11, 2026

Copy link
Copy Markdown
Member

Thanks for opening the PR, this is definitely not something I was aware of. Am I reading this correctly, that the recommended pattern for non-concrete types is a comprehension instead of a map? Is that generally true, and if so, maybe would be useful to add this to the performance section of the docs, I always struggle to motivate map vs comprehensions.

Much appreciated though!

@KristofferC

Copy link
Copy Markdown
Contributor Author

I wouldn't say there is any recommended pattern for this. The PR here just works around an existing deficiency in the julia compiler. Ideally, that should be fixed, but in the meantime it seems reasonable to get the inference numbers down here for a fairly low cost (the small rewrite in this PR).

@lkdvos

lkdvos commented Nov 11, 2026

Copy link
Copy Markdown
Member

Great, thanks. I'll merge once CI passes, really appreciate the help

@lkdvos
lkdvos merged commit 914cd35 into QuantumKitHub:main Nov 11, 2026
27 of 28 checks passed
hz-xiaxz added a commit to hz-xiaxz/MPSKit.jl that referenced this pull request Nov 11, 2026
# Conflicts:
#	src/operators/mpohamiltonian.jl
@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/operators/mpohamiltonian.jl 94.15% <100.00%> (-0.01%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants