Skip to content

Store Hamiltonian-term representatives in concretely typed vectors - #557

Open
hz-xiaxz wants to merge 6 commits into
QuantumKitHub:mainfrom
hz-xiaxz:typed-representatives
Open

hz-xiaxz wants to merge 6 commits into
QuantumKitHub:mainfrom
hz-xiaxz:typed-representatives

Conversation

@hz-xiaxz

@hz-xiaxz hz-xiaxz commented Nov 11, 2026 •

Copy link
Copy Markdown

This builds on #556 and contains its commits, so it should be merged after #556. Only the commit "Store the representatives in concretely typed vectors" is new here.

#556 puts inference barriers around the calls that use the representatives of _instantiate_operators. This PR fixes the cause instead: the representatives are grouped by operator type, so each group is stored in a concretely typed vector, and the barriers are removed. FiniteMPO(O) already infers concretely, so Base.promote_op(FiniteMPO, typeof(O)) gives the element type of each group.

Behaviour change: proportional operators are now only merged when they have the same type. For example, a real and a complex copy of the same operator no longer share a decomposition, and so no longer share virtual channels. Both are still built correctly; the bond dimension can just be larger in that case. No existing test covers it. This is why the change is in a separate PR.

On Julia 1.13.3, building the SU(2) Heisenberg InfiniteMPOHamiltonian in a fresh process, from a single A/B run:

#556 (barriers) this PR (typed, no barriers)
first call 15.6 s, 1.93 GiB 16.5 s, 2.21 GiB
inference frames on abstract signatures 1,551 (0.5 s) 1,547 (0.7 s)
first unsorted term (swap!) 2.9 s 2.7 s
warm median 0.063 ms 0.062 ms

Both remove the same abstract inference. The remaining difference of about a second is within what one run can resolve. Keeping only the outer barrier on instantiate_operator gave 15.8 s, so that barrier is not needed.

Locally, the mpo/finite, hamiltonian, operators and algorithms/changebonds test files pass.

🤖 Generated with Claude Code

KristofferC and others added 6 commits November 11, 2026 12:16
`_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>
Put inference barriers around the calls in `_instantiate_operators` that
receive the untyped representatives, and reorder the inverse `swap!`
contraction so that `τ'` is not the first `@plansor` tensor. That ordering
threw `UndefVarError: τ` (QuantumKitHub/TensorKit.jl#622), and since it was
an undefined global it also made the whole method infer abstractly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Group the representatives in `_instantiate_operators` by operator type, so
that each group is a concretely typed vector. This removes the abstract
inference that the inference barriers worked around, so those barriers are
removed. Proportional operators of different types (e.g. a real and a
complex copy of the same operator) no longer share a decomposition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# 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/mpo.jl 61.63% <ø> (ø)
src/operators/mpohamiltonian.jl 59.59% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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