Skip to content

Restore instantiate_operator as the extension point for custom terms - #564

Open
lkdvos wants to merge 2 commits into
mainfrom
instantiate-operator-fallback
Open

lkdvos wants to merge 2 commits into
mainfrom
instantiate-operator-fallback

Conversation

@lkdvos

@lkdvos lkdvos commented Dec 6, 2026

Copy link
Copy Markdown
Member

Follow-up to #557, which broke MPSKitModels: the Hamiltonian constructors started destructuring every term as inds => O, so terms of other types were no longer handed to instantiate_operator(lattice, term). MPSKitModels overloads that function for its LocalOperator, so @mpoham and every model built on it threw a MethodError (reported in #557 (comment)).

This restores the old behaviour without giving up the compile-time improvements:

  • Every term first goes through the new internal _local_term(lattice, term) -> indices => operator.
    • A pair only has its indices converted to linear indices.
    • Any other term goes through instantiate_operator(lattice, term), and the identities padding the sites in between are dropped again. The constructors add them back later.
  • instantiate_operator(lattice, inds => O) returns the padded sites => local_mpo again, exactly as in v0.14.1. It is now built from _local_term and the internal _local_mpo, which is the former three-argument method that Store Hamiltonian-term representatives in concretely typed vectors #557 had added.
  • The front end calls _local_term through an inference barrier. Otherwise inference walks the generic method into the tensor code with abstract types, which doubles allocations on the first construction.

Checks:

With this, QuantumKitHub/MPSKitModels.jl#90 is no longer required for compatibility.

Assisted by Claude Code

#557 made the Hamiltonian constructors destructure every term as
`inds => O`, so terms of other types, such as MPSKitModels' `LocalOperator`,
were no longer handed to `instantiate_operator(lattice, term)`.

Terms now first go through the internal `_local_term`, which turns a pair
into linear indices and the operator, and hands any other term to
`instantiate_operator`, dropping the identity padding again.
`instantiate_operator(lattice, ::Pair)` keeps returning the padded local
MPO, as in v0.14.1.

Assisted-by: Claude Code
- share the channel graph and unit space between the finite and infinite case
- check the operator types once, through `_mpo_tensortype`
- find the bucket of an operator through its element type
- shorten the comments to the non-obvious parts

Assisted-by: Claude Code
@lkdvos

lkdvos commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

@hz-xiaxz I would love to request a review from you for this one as well. The idea is to make the MPSKitModels PR an optional performance improvement, such that we can release a non-breaking version here, separately from that issue (which I think we should also do in any case, but that can thus be handled separately).
I also instructed claude to attempt to simplify some of the code we introduced, which at least cut down slightly on the line numbers 🤷

@lkdvos lkdvos mentioned this pull request Dec 6, 2026

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.

1 participant