-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathconstructors.jl
More file actions
140 lines (129 loc) · 5.24 KB
/
constructors.jl
File metadata and controls
140 lines (129 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@non_differentiable TensorKit.TensorMap(f::Function, storagetype, cod, dom)
@non_differentiable TensorKit.id(args...)
@non_differentiable TensorKit.isomorphism(args...)
@non_differentiable TensorKit.isometry(args...)
@non_differentiable TensorKit.unitary(args...)
function ChainRulesCore.rrule(::Type{TensorMap}, d::DenseArray, args...; kwargs...)
function TensorMap_pullback(Δt)
∂d = convert(Array, unthunk(Δt))
return NoTangent(), ∂d, ntuple(_ -> NoTangent(), length(args))...
end
return TensorMap(d, args...; kwargs...), TensorMap_pullback
end
# these are not the conversion to/from array, but actually take in data parameters
# -- as a result, requires quantum dimensions to keep inner product the same:
# ⟨Δdata, ∂data⟩ = ⟨Δtensor, ∂tensor⟩ = ∑_c d_c ⟨Δtensor_c, ∂tensor_c⟩
# ⟹ Δdata = d_c Δtensor_c
function ChainRulesCore.rrule(::Type{TensorMap{T}}, data::DenseVector, V::TensorMapSpace) where {T}
t = TensorMap{T}(data, V)
P = ProjectTo(data)
function TensorMap_pullback(Δt_)
Δt = copy(unthunk(Δt_))
for (c, b) in blocks(Δt)
scale!(b, dim(c))
end
∂data = P(Δt.data)
return NoTangent(), ∂data, NoTangent()
end
return t, TensorMap_pullback
end
function ChainRulesCore.rrule(::Type{<:DiagonalTensorMap}, data::DenseVector, args...; kwargs...)
D = DiagonalTensorMap(data, args...; kwargs...)
P = ProjectTo(data)
function DiagonalTensorMap_pullback(Δt_)
# unclear if we're allowed to modify/take ownership of the input
Δt = copy(unthunk(Δt_))
for (c, b) in blocks(Δt)
scale!(b, dim(c))
end
∂data = P(Δt.data)
return NoTangent(), ∂data, NoTangent()
end
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
end
return d, DiagonalTensorMap_pullback
end
function ChainRulesCore.rrule(::typeof(Base.getproperty), t::TensorMap, prop::Symbol)
if prop === :data
function getdata_pullback(Δdata)
# unclear if we're allowed to modify/take ownership of the input
t′ = typeof(t)(copy(unthunk(Δdata)), t.space)
for (c, b) in blocks(t′)
scale!(b, inv(dim(c)))
end
return NoTangent(), t′, NoTangent()
end
return t.data, getdata_pullback
elseif prop === :space
return t.space, Returns((NoTangent(), ZeroTangent(), NoTangent()))
else
throw(ArgumentError("unknown property $prop"))
end
end
function ChainRulesCore.rrule(::typeof(Base.getproperty), t::DiagonalTensorMap, prop::Symbol)
if prop === :data
function getdata_pullback(Δdata)
# unclear if we're allowed to modify/take ownership of the input
t′ = typeof(t)(copy(unthunk(Δdata)), t.domain)
for (c, b) in blocks(t′)
scale!(b, inv(dim(c)))
end
return NoTangent(), t′, NoTangent()
end
return t.data, getdata_pullback
elseif prop === :domain
return t.domain, Returns((NoTangent(), ZeroTangent(), NoTangent()))
else
throw(ArgumentError("unknown property $prop"))
end
end
function ChainRulesCore.rrule(::typeof(Base.copy), t::AbstractTensorMap)
copy_pullback(Δt) = NoTangent(), Δt
return copy(t), copy_pullback
end
function ChainRulesCore.rrule(
::typeof(TensorKit.copy_oftype), t::AbstractTensorMap, T::Type{<:Number}
)
project = ProjectTo(t)
copy_oftype_pullback(Δt) = NoTangent(), project(unthunk(Δt)), NoTangent()
return TensorKit.copy_oftype(t, T), copy_oftype_pullback
end
function ChainRulesCore.rrule(::typeof(Base.convert), T::Type{<:Array}, t::AbstractTensorMap)
A = convert(T, t)
function convert_pullback(ΔA)
# use constructor to (unconditionally) project back onto symmetric subspace
∂t = TensorMap(unthunk(ΔA), codomain(t), domain(t); tol = Inf)
return NoTangent(), NoTangent(), ∂t
end
return A, convert_pullback
end
function ChainRulesCore.rrule(::typeof(Base.convert), ::Type{Dict}, t::AbstractTensorMap)
out = convert(Dict, t)
function convert_pullback(c′)
c = unthunk(c′)
if haskey(c, :data) # :data is the only thing for which this dual makes sense
dual = copy(out)
dual[:data] = c[:data]
return (NoTangent(), NoTangent(), convert(TensorMap, dual))
else
# instead of zero(t) you can also return ZeroTangent(), which is type unstable
return (NoTangent(), NoTangent(), zero(t))
end
end
return out, convert_pullback
end
function ChainRulesCore.rrule(::typeof(Base.convert), ::Type{TensorMap}, t::Dict{Symbol, Any})
return convert(TensorMap, t), v -> (NoTangent(), NoTangent(), convert(Dict, v))
end
function ChainRulesCore.rrule(T::Type{<:TensorKit.AdjointTensorMap}, t::AbstractTensorMap)
return T(t), Δt -> (NoTangent(), adjoint(unthunk(Δt)))
end