-
Notifications
You must be signed in to change notification settings - Fork 45
Changes for non-CPU array support #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,48 +15,52 @@ Base.unlock(envs::AbstractMPSEnvironments) = unlock(envs.lock); | |||||
| # ------------------ | ||||||
| function allocate_GL(bra::AbstractMPS, mpo::AbstractMPO, ket::AbstractMPS, i::Int) | ||||||
| T = Base.promote_type(scalartype(bra), scalartype(mpo), scalartype(ket)) | ||||||
| TA = similarstoragetype(storagetype(mpo), T) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is already a strict improvement, but to make this work in full generality I don't think only using the storagetype of the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can modify this to take into account the backing types of |
||||||
| V = left_virtualspace(bra, i) ⊗ left_virtualspace(mpo, i)' ← | ||||||
| left_virtualspace(ket, i) | ||||||
| if V isa BlockTensorKit.TensorMapSumSpace | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), T) | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), TA) | ||||||
| else | ||||||
| TT = TensorMap{T} | ||||||
| TT = TensorKit.TensorMapWithStorage{T, TA} | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| end | ||||||
| return TT(undef, V) | ||||||
| end | ||||||
|
|
||||||
| function allocate_GR(bra::AbstractMPS, mpo::AbstractMPO, ket::AbstractMPS, i::Int) | ||||||
| T = Base.promote_type(scalartype(bra), scalartype(mpo), scalartype(ket)) | ||||||
| TA = similarstoragetype(storagetype(mpo), T) | ||||||
| V = right_virtualspace(ket, i) ⊗ right_virtualspace(mpo, i) ← | ||||||
| right_virtualspace(bra, i) | ||||||
| if V isa BlockTensorKit.TensorMapSumSpace | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), T) | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), TA) | ||||||
| else | ||||||
| TT = TensorMap{T} | ||||||
| TT = TensorKit.TensorMapWithStorage{T, TA} | ||||||
| end | ||||||
| return TT(undef, V) | ||||||
| end | ||||||
|
|
||||||
| function allocate_GBL(bra::QP, mpo::AbstractMPO, ket::QP, i::Int) | ||||||
| T = Base.promote_type(scalartype(bra), scalartype(mpo), scalartype(ket)) | ||||||
| TA = similarstoragetype(storagetype(mpo), T) | ||||||
| V = left_virtualspace(bra.left_gs, i) ⊗ left_virtualspace(mpo, i)' ← | ||||||
| auxiliaryspace(ket)' ⊗ left_virtualspace(ket.right_gs, i) | ||||||
| if V isa BlockTensorKit.TensorMapSumSpace | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), T) | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), TA) | ||||||
| else | ||||||
| TT = TensorMap{T} | ||||||
| TT = TensorKit.TensorMapWithStorage{T, TA} | ||||||
| end | ||||||
| return TT(undef, V) | ||||||
| end | ||||||
|
|
||||||
| function allocate_GBR(bra::QP, mpo::AbstractMPO, ket::QP, i::Int) | ||||||
| T = Base.promote_type(scalartype(bra), scalartype(mpo), scalartype(ket)) | ||||||
| TA = similarstoragetype(storagetype(mpo), T) | ||||||
| V = right_virtualspace(ket.left_gs, i) ⊗ right_virtualspace(mpo, i) ← | ||||||
| auxiliaryspace(ket)' ⊗ right_virtualspace(bra.right_gs, i) | ||||||
| if V isa BlockTensorKit.TensorMapSumSpace | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), T) | ||||||
| TT = blocktensormaptype(spacetype(bra), numout(V), numin(V), TA) | ||||||
| else | ||||||
| TT = TensorMap{T} | ||||||
| TT = TensorKit.TensorMapWithStorage{T, TA} | ||||||
| end | ||||||
| return TT(undef, V) | ||||||
| end | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -824,7 +824,7 @@ function Base.:*(H::FiniteMPOHamiltonian, mps::FiniteMPS) | |
| ) | ||
| ) | ||
| # left to middle | ||
| U = ones(scalartype(H), left_virtualspace(H, 1)) | ||
| U = ones(storagetype(H), left_virtualspace(H, 1)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think here we should actually call |
||
| @plansor a[-1 -2; -3 -4] := A[1][-1 2; -3] * H[1][1 -2; 2 -4] * conj(U[1]) | ||
| Q, R = qr_compact!(a) | ||
| A′[1] = TensorMap(Q) | ||
|
|
@@ -836,7 +836,7 @@ function Base.:*(H::FiniteMPOHamiltonian, mps::FiniteMPS) | |
| end | ||
|
|
||
| # right to middle | ||
| U = ones(scalartype(H), right_virtualspace(H, N)) | ||
| U = ones(storagetype(H), right_virtualspace(H, N)) | ||
| @plansor a[-1 -2; -3 -4] := A[end][-1 2; -3] * H[end][-2 -4; 2 1] * U[1] | ||
| L, Q = lq_compact!(a) | ||
| A′[end] = transpose(TensorMap(Q), ((1, 3), (2,))) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ end | |
| MultilineMPO(mpos::AbstractVector{<:AbstractMPO}) = Multiline(mpos) | ||
| MultilineMPO(t::MPOTensor) = MultilineMPO(PeriodicMatrix(fill(t, 1, 1))) | ||
|
|
||
| TensorKit.storagetype(M::MultilineMPO) = storagetype(M.data) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work? I would expect you would need something like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does seem to work, using |
||
|
|
||
| # allow indexing with two indices | ||
| Base.getindex(t::MultilineMPO, ::Colon, j::Int) = Base.getindex.(t.data, j) | ||
| Base.getindex(t::MultilineMPO, i::Int, j) = Base.getindex(t[i], j) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,14 +33,14 @@ Construct an `MPSTensor` with given physical and virtual spaces. | |||||||||||||||||||||||
| - `right_D::Int`: right virtual dimension | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| function MPSTensor( | ||||||||||||||||||||||||
| ::UndefInitializer, eltype, P::Union{S, CompositeSpace{S}}, Vₗ::S, Vᵣ::S = Vₗ | ||||||||||||||||||||||||
| ) where {S <: ElementarySpace} | ||||||||||||||||||||||||
| return TensorMap{eltype}(undef, Vₗ ⊗ P ← Vᵣ) | ||||||||||||||||||||||||
| ::UndefInitializer, ::Type{TorA}, P::Union{S, CompositeSpace{S}}, Vₗ::S, Vᵣ::S = Vₗ | ||||||||||||||||||||||||
| ) where {S <: ElementarySpace, TorA} | ||||||||||||||||||||||||
| return TensorKit.TensorMapWithStorage{TorA}(undef, Vₗ ⊗ P ← Vᵣ) | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion to also use That being said, this constructor is the worst type piracy I have ever committed, and I would be happy to not have to feel the shame of its existence anymore... |
||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
| function MPSTensor( | ||||||||||||||||||||||||
| f, eltype, P::Union{S, CompositeSpace{S}}, Vₗ::S, Vᵣ::S = Vₗ | ||||||||||||||||||||||||
| ) where {S <: ElementarySpace} | ||||||||||||||||||||||||
| A = MPSTensor(undef, eltype, P, Vₗ, Vᵣ) | ||||||||||||||||||||||||
| f, ::Type{TorA}, P::Union{S, CompositeSpace{S}}, Vₗ::S, Vᵣ::S = Vₗ | ||||||||||||||||||||||||
| ) where {S <: ElementarySpace, TorA} | ||||||||||||||||||||||||
| A = MPSTensor(undef, TorA, P, Vₗ, Vᵣ) | ||||||||||||||||||||||||
| if f === rand | ||||||||||||||||||||||||
| return rand!(A) | ||||||||||||||||||||||||
| elseif f === randn | ||||||||||||||||||||||||
|
|
@@ -70,18 +70,18 @@ Construct an `MPSTensor` with given physical and virtual dimensions. | |||||||||||||||||||||||
| - `Dₗ::Int`: left virtual dimension | ||||||||||||||||||||||||
| - `Dᵣ::Int`: right virtual dimension | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| MPSTensor(f, eltype, d::Int, Dₗ::Int, Dᵣ::Int = Dₗ) = MPSTensor(f, eltype, ℂ^d, ℂ^Dₗ, ℂ^Dᵣ) | ||||||||||||||||||||||||
| MPSTensor(f, ::Type{TorA}, d::Int, Dₗ::Int, Dᵣ::Int = Dₗ) where {TorA} = MPSTensor(f, TorA, ℂ^d, ℂ^Dₗ, ℂ^Dᵣ) | ||||||||||||||||||||||||
| MPSTensor(d::Int, Dₗ::Int; Dᵣ::Int = Dₗ) = MPSTensor(ℂ^d, ℂ^Dₗ, ℂ^Dᵣ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| MPSTensor(A::AbstractArray) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Convert an array to an `MPSTensor`. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| function MPSTensor(A::AbstractArray{T}) where {T <: Number} | ||||||||||||||||||||||||
| function MPSTensor(A::AA) where {T <: Number, AA <: AbstractArray{T}} | ||||||||||||||||||||||||
| @assert ndims(A) > 2 "MPSTensor should have at least 3 dims, but has $ndims(A)" | ||||||||||||||||||||||||
| sz = size(A) | ||||||||||||||||||||||||
| t = TensorMap(undef, T, foldl(⊗, ComplexSpace.(sz[1:(end - 1)])) ← ℂ^sz[end]) | ||||||||||||||||||||||||
| t = TensorKit.TensorMapWithStorage{T, AA}(undef, foldl(⊗, ComplexSpace.(sz[1:(end - 1)])) ← ℂ^sz[end]) | ||||||||||||||||||||||||
| t[] .= A | ||||||||||||||||||||||||
|
Comment on lines
+81
to
85
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this might be slightly cleaner, simply making use of TensorKit functionality? |
||||||||||||||||||||||||
| return t | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,8 @@ function PeriodicArray{T, N}(initializer, args...) where {T, N} | |
| return PeriodicArray(Array{T, N}(initializer, args...)) | ||
| end | ||
|
|
||
| TensorKit.storagetype(PA::PeriodicArray{T, N}) where {T, N} = storagetype(T) | ||
|
|
||
|
Comment on lines
+41
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little hesitant to adopt |
||
| """ | ||
| PeriodicVector{T} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a bit strange to me, would we not expect this to have to be on the CPU anyways, since it originates from a call to
dotwhich produces a scalar?If you prefer, I am also happy with writing this entire expression as a single call to
sumto avoid the intermediate allocation altogether