Add embed_lazy for lazy operator embedding#228
Closed
Temitope15 wants to merge 1 commit into
Closed
Conversation
Add a structure-preserving lazy embedding API as an alternative to the eager embed: single-subsystem operators become a LazyTensor, LazySum and TimeDependentSum keep their structure, and a LazyTensor is re-embedded into the larger basis. Incompatible bases and unsupported monolithic multi-site operators fail with clear errors. Includes tests and an api.md entry. Implements qojulia/QuantumOptics.jl#523.
Member
|
we do not accept LLM-generated PRs for bounties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a lazy operator-embedding API,
embed_lazy, as requested in the unitaryHACK bounty issue qojulia/QuantumOptics.jl#523.Where
embedmaterializes the full embedded matrix (e.g. a2^n × 2^nsparse matrix to embed a one-site operator into ann-site basis),embed_lazypreserves the local tensor-product structure and returns a structure-preserving lazy operator. The result can be multiplied by kets, used as a term in aLazySum, and converted withdense/sparseonly when explicitly requested.What it does
DataOperator/ any single-subsystemAbstractOperatorat a single index →LazyTensorLazyTensorre-embedded into the larger basis, preserving suboperators and factor (the suboperator indices are remapped throughindices, so partialLazyTensors embed correctly)LazySumpreserved as aLazySumof lazily embedded terms, factors carried overTimeDependentSumpreserved, keeping its coefficients while lazily embedding the static termsembed_lazy(basis_l, basis_r, indices, op)signatureIncompatibleBases; unsortedindicesraiseArgumentErrorAPI decisions (the points raised in the issue)
embed_lazyfrom PR Draft: Embedding with lazy semantics #88, exported fromQuantumOpticsBase.embed_lazy(basis_l, basis_r, indices, op)is supported alongside the single-basis form.ArgumentErrorpointing the user toembedor to passing aLazyTensor/LazySum. (LazyProductis likewise left unsupported for now.)Note on the issue's example
The issue's minimal example writes
local_h = sx + 0.5*szand expectsembed_lazy(b, 3, local_h) isa LazySum. In QuantumOpticsBase,+on two operators sharing a basis returns a single eagerOperator, not aLazySum, soembed_lazycorrectly returns aLazyTensorfor that input.embed_lazypreserves aLazySumonly when actually given one (e.g.LazySum(sx, 0.5*sz)); the tests and docstring use an explicitLazySumto reflect this.Tests & docs
test/test_embed_lazy.jl: single-index dense/sparse, the two-basis signature,LazySumpreservation + factor carry-over, multi-indexLazyTensorre-embedding (including a partialLazyTensorand sparse suboperators),TimeDependentSum, incompatible-basis and unsupported-operator failures, and an allocation check confirming the lazy path does not materialize the full matrix.jldoctestand an@docsentry indocs/src/api.mdnext toembed.test_embed_lazytest set, the Aqua checks (no new ambiguities), the doctests, and JET on the new methods all pass.AI disclosure
I used Claude Code as an assistant to help me find my way around this large codebase and to help brainstorm and draft parts of the implementation, tests, and docstring. I drove the design decisions, reviewed and refined the code, and manually verified everything on Julia 1.11.5 — running the new
test_embed_lazyset, the Aqua checks, the doctests, and JET. The clarification about+returning an eager operator (soembed_lazyreturns aLazyTensor, not aLazySum, for the issue's example) and the choice to fail fast on unsortedindicesboth came from problems I hit and worked through while testing locally.