Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ext/TensorKitChainRulesCoreExt/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
return D, DiagonalTensorMap_pullback
end

function ChainRulesCore.rrule(::Type{DiagonalTensorMap}, t::AbstractTensorMap)
d = DiagonalTensorMap(t)
function DiagonalTensorMap_pullback(Δd_)
Δt = similar(t) # no projector needed
for (c, b) in blocks(unthunk(Δd_))
copy!(block(Δt, c), Diagonal(b))
end
return NoTangent(), Δt

Check warning on line 57 in ext/TensorKitChainRulesCoreExt/constructors.jl

View check run for this annotation

Codecov / codecov/patch

ext/TensorKitChainRulesCoreExt/constructors.jl#L50-L57

Added lines #L50 - L57 were not covered by tests
end
return d, DiagonalTensorMap_pullback

Check warning on line 59 in ext/TensorKitChainRulesCoreExt/constructors.jl

View check run for this annotation

Codecov / codecov/patch

ext/TensorKitChainRulesCoreExt/constructors.jl#L59

Added line #L59 was not covered by tests
end

function ChainRulesCore.rrule(::typeof(Base.getproperty), t::TensorMap, prop::Symbol)
if prop === :data
function getdata_pullback(Δdata)
Expand Down
3 changes: 3 additions & 0 deletions test/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ Vlist = ((ℂ^2, (ℂ^3)', ℂ^3, ℂ^2, (ℂ^2)'),
test_rrule(DiagonalTensorMap, D.data, D.domain)
test_rrule(Base.getproperty, D, :data)
test_rrule(Base.getproperty, D1, :data)

test_rrule(DiagonalTensorMap, rand!(T1))
test_rrule(DiagonalTensorMap, randn!(T))
end
end

Expand Down