Skip to content

TimeEvolver-owned allocator - #484

Merged
Yue-Zhengyuan merged 3 commits into
QuantumKitHub:mainfrom
Yue-Zhengyuan:timeevolver-allocator
Nov 4, 2026
Merged

Yue-Zhengyuan merged 3 commits into
QuantumKitHub:mainfrom
Yue-Zhengyuan:timeevolver-allocator

Conversation

@Yue-Zhengyuan

Copy link
Copy Markdown
Member

Previous work on simple update (#459, #483) introduced reusable allocators to handle contractions during a single dt step. This PR extends the lifespan of the allocator by creating it with TimeEvolver, so it can be reused across all dt steps.

The additional benefit for simple update is quite small (less than 5% allocation reduction), but can be much more helpful for NTU #144.

@Yue-Zhengyuan

Copy link
Copy Markdown
Member Author

Need professional opinions since I'm not familiar with TensorOperations.jl infrastructure.

@Yue-Zhengyuan
Yue-Zhengyuan requested a review from lkdvos November 4, 2026 14:55

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks perfect, I left a small comment on the use of finally, although I do think that in principle you might be right there, it might just be overkill.

The one thing I would recommend to try and keep is that indeed, reusing the buffer helps, but I'd try and avoid as much as possible having it leak out of the public-interface functions, and have this as an automatic behind-the-scenes approach as much as possible. That is in line with this PR I think, since I'd say manually constructing the evolver is expert-use, but I just wanted to mention that here again, as I do think it is quite important, because it is very easy to mis-use

(iter == it.nstep) && return nothing
psi, env, ϵ = su_iter(state.psi, it.circuit, it.alg, state.env)
cp = allocator_checkpoint!(it.allocator)
psi, env, ϵ = try

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why the try - finally is here, but I think in principle this is not really necessary/consistent with the rest.
We more or less made the decision that indeed, if there would be an error, the internal state of the allocators might be corrupted and you might end up with memory leaks. I'm not saying this is the best decision, and in principle we might want to avoid this, but since this is already the case in the TensorOperations implementation itself, the finally here would only help for a very small amount of cases, so probably it's not really worth it.

Specifically here, I think the allocator will go out of scope as soon as the time evolver does, so that would already fix the bumper, while actually using a manual allocator would leak memory anyways, even with the finally clause.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the checkpoint and the try-finally block.

@Yue-Zhengyuan
Yue-Zhengyuan merged commit 9823568 into QuantumKitHub:main Nov 4, 2026
31 of 32 checks passed
@Yue-Zhengyuan
Yue-Zhengyuan deleted the timeevolver-allocator branch November 4, 2026 23:49
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