Skip to content

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

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

hz-xiaxz wants to merge 7 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 commits "Store the representatives in concretely typed vectors" and "Restore the inference barrier on instantiate_operator" are 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. FiniteMPO(O) already infers concretely, so Base.promote_op(FiniteMPO, typeof(O)) gives the element type of each group. With that, the barriers on _proportionality and on rescaling the last tensor are removed. The barrier on instantiate_operator stays, because the finite case needs it (see below).

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 (the second column is without any barrier):

#556 (barriers) 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

With the barrier on instantiate_operator kept, the infinite first call was 15.8 s.

That barrier matters for finite chains. First FiniteMPOHamiltonian(fill(P, 12), (i, i + 1) => h for i in 1:11) for the same SU(2) Heisenberg term, with TensorKit 0.17.4, in a fresh process, both variants run at the same time:

Julia typed, no barriers this PR (typed + barrier on instantiate_operator)
1.12.7 302 s, 2.96 G allocations 29 s, 97 M allocations
1.13.3 41 s, 215 M allocations 20 s, 52 M allocations

For the infinite Hamiltonian both variants took 19–27 s. On main, the same finite construction takes 320 s on 1.12 (see #556).

Locally, the mpo/finite, hamiltonian and operators test files pass with the barrier restored, and algorithms/changebonds passed before.

🤖 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.

Without it, `instantiate_operator` is inferred inside the comprehension of
`_instantiate_operators`. For a nearest-neighbour `FiniteMPOHamiltonian` that
takes minutes on Julia 1.12 (SU(2) Heisenberg chain of 12 sites: 302 s, with
the barrier 29 s) and about twice as long on 1.13.3 (41 s vs 20 s). The
infinite case is unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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