Skip to content

Support TensorKit v0.17.4 by introducing blockwise _braid! - #92

Merged
lkdvos merged 4 commits into
mainfrom
bd/permute-twist
Nov 27, 2026
Merged

lkdvos merged 4 commits into
mainfrom
bd/permute-twist

Conversation

@borisdevos

@borisdevos borisdevos commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

This PR adds permute_twist! for block tensors. I kind of just mimicked what's going on in the TO.tensoradd! methods as well as QuantumKitHub/TensorKit.jl#612, especially with the VI alphas and betas, so please check those. If I understand them correctly, they're probably correct 😄

Locally, this seemed to fix where it failed, namely a selfdual space like the cartesian one, so that's in the added test. Also added a case where permute_twist! actually has a twist mask. For cases where it already worked, I guess the fallback within TensorKit was just extremely slow, since I do see a speedup as well. This seemed to also fix the current failing test locally in MPSKit's clusterexpansion.jl:107 where cartesian spaces were used.

Edit: I now overload TK._braid! directly, which allows to remove the overloading of permute!, braid! and TO.tensoradd!, as well as support permute_twist!.

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.28571% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/tensors/indexmanipulations.jl 74.28% 9 Missing ⚠️
Files with missing lines Coverage Δ
src/tensors/tensoroperations.jl 95.04% <ø> (+4.06%) ⬆️
src/tensors/indexmanipulations.jl 55.26% <74.28%> (+1.83%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@borisdevos
borisdevos requested a review from lkdvos November 25, 2026 13:53
Comment thread src/tensors/tensoroperations.jl Outdated
Comment on lines +59 to +66
function TK.permute_twist!(
tdst::AbstractBlockTensorMap, tsrc::BlockTensorOrAdjoint, p::Index2Tuple,
twistmask, backend, allocator
)
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p)
tsrc′, p′, _, conjsrc = TK._unwrap_source(tsrc, p, nothing, false)
return _permute_twist_blocks!(tdst, tsrc′, p′, conjsrc, twistmask, backend, allocator)
end

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 might be reading this wrong so definitely double-check me here, but isn't this a direct copy of the TensorKit code? Would it be the same if we don't overload here, and overload TK._braid! on blocktensors instead, which I think is the same as the _permute_twist_blocks! function here?

Looking at the TensorKit state a bit more, can we just overload TK._braid! instead of the TK.braid! overloads in the indexmanipulations and that would cover the same thing?

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.

It's indeed a copy, I just intercept it here and send it directly to the _permute_twist_blocks! below. I could indeed do the blockwise loop through _braid!, again a dense and sparse version (and probably a TensorMap-BlockTensorMap version as well), so TensorKit's permute_twist! catches the individual blocks.

To your last point: correct me if I'm wrong, but I think I could remove more than TK.braid! when overloading TK._braid!. Just looking at the TK code, besides braid! there's also permute! and TO.tensoradd! (and of course permute_twist!) which are overloaded here, and I think those can be removed then? This is definitely not the minimal change needed to introduce the new internal entry point permute_twist! though.

The downside I think to doing one of the two you propose is that we're overwriting something internal that's allowed to change with patch releases, so this might break silently in the future. Worst case, things just start running slower by falling back to the generic case in TensorKit. I'm wondering if there's an easy way to test the dispatch directly.

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.

Unfortunately all of the options here are overloading or using internal functions from TensorKit. This is probably something we should look into improving in the future, I just never really found the time to do it. Ultimately, I'm definitely in favor of keeping the surface area of that minimal, so if we can just remove many overloads here and only end up needing to overload _braid!, that sounds great

@borisdevos borisdevos changed the title Support TensorKit v0.17.4 by introducing blockwise permute_twist! Support TensorKit v0.17.4 by introducing blockwise _braid! Nov 26, 2026
@borisdevos

Copy link
Copy Markdown
Member Author

This took a minute, the unwrapping confused me for a bit. There are some comments I left behind to clarify potential future confusions about this, it might save time for anyone (or me again) looking at this.

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

Love that this ends up cutting total lines of code in the end, looks great, thanks for fixing!

@lkdvos
lkdvos merged commit 28a59cd into main Nov 27, 2026
27 of 28 checks passed
@lkdvos
lkdvos deleted the bd/permute-twist branch November 27, 2026 03:51
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