Skip to content

fix: LQ pullback modifies input cotangents#226

Merged
lkdvos merged 4 commits into
mainfrom
ld-LQfix
May 11, 2026
Merged

fix: LQ pullback modifies input cotangents#226
lkdvos merged 4 commits into
mainfrom
ld-LQfix

Conversation

@lkdvos
Copy link
Copy Markdown
Member

@lkdvos lkdvos commented May 10, 2026

Bumped into this to resolve QuantumKitHub/TensorKit.jl#419.

TLDR: the QR pullback correctly did not modify the input cotangents, using a getindex, while the LQ pullback uses a view.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/pullbacks/lq.jl 96.62% <100.00%> (ø)
src/pullbacks/qr.jl 95.55% <ø> (-0.05%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos lkdvos requested a review from Jutho May 10, 2026 22:34
@kshyatt
Copy link
Copy Markdown
Member

kshyatt commented May 11, 2026

Could we add an explicit test to make sure the cotangents are not touched?

@Jutho
Copy link
Copy Markdown
Member

Jutho commented May 11, 2026

Thanks this looks good.

This makes me however wonder to what extent we can rely on getindex returning a copy. If we ever switch over the TensorKit blocks to return a StridedView instead of a reshape(view(...)), we might run into problem, because getindex producing a view on StridedView objects has been a (possibly misinformed) design principle from early on. Maybe we should be more explicit with copy(view(...)) ?

@lkdvos
Copy link
Copy Markdown
Member Author

lkdvos commented May 11, 2026

@Jutho I have switched everything to copy(view(...)), but I think this is more a case of Strided performing piracy on a Base method by altering its semantics than MatrixAlgebraKit not being able to rely on this. It is a good comment though, this would be an annoying thing to debug in the future...

@kshyatt Good point, should be done now :).
Interestingly, my first idea was that this should be caught by the AD testing frameworks, but that just isn't the case because of different requirements: Mooncake actually requires us to zero out these output tangents, so that would never catch this (or be affected by this), and adding this to ChainRulesTestUtils seems like it will be hard to find someone to merge this...
This also brings up the point that if we end up dropping chainrules in the end, it would actually be more efficient to assume we can modify these inputs, but I guess we can keep that for the future 😉

@lkdvos lkdvos enabled auto-merge (squash) May 11, 2026 13:21
@lkdvos lkdvos disabled auto-merge May 11, 2026 21:41
@lkdvos lkdvos merged commit 978effe into main May 11, 2026
34 of 35 checks passed
@lkdvos lkdvos deleted the ld-LQfix branch May 11, 2026 21:41
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.

3 participants