Skip to content

Commit 2bf400b

Browse files
committed
various fixes
1 parent 1cba3f5 commit 2bf400b

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

src/states/abstractmps.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ This might be useful to construct "charged" MPS, or to work with [`WindowMPS`](@
223223
struct FiniteMPSManifold{S <: ElementarySpace, S′ <: TensorSpace{S}} <: AbstractMPSManifold{S}
224224
pspaces::Vector{S′}
225225
vspaces::Vector{S}
226+
227+
# disable default constructor
228+
function FiniteMPSManifold{S, S′}(
229+
pspaces::Vector{S′}, vspaces::Vector{S}
230+
) where {S <: ElementarySpace, S′ <: TensorSpace{S}}
231+
return new{S, S′}(pspaces, vspaces)
232+
end
226233
end
227234

228235
function FiniteMPSManifold(
@@ -245,7 +252,7 @@ end
245252
function FiniteMPSManifold(
246253
physicalspaces::AbstractVector{S′}, max_virtualspace::S; kwargs...
247254
) where {S <: ElementarySpace, S′ <: TensorSpace{S}}
248-
return FiniteMPSManifold(physicalspaces, fill(max_virtualspace, length(physicalspaces) - 1))
255+
return FiniteMPSManifold(physicalspaces, fill(max_virtualspace, length(physicalspaces) - 1); kwargs...)
249256
end
250257
function FiniteMPSManifold(mps_tensors::AbstractVector{A}) where {A <: GenericMPSTensor}
251258
numin(A) == 1 || throw(ArgumentError("Not a valid MPS tensor space"))
@@ -302,9 +309,9 @@ function InfiniteMPSManifold(
302309
# ensure all spaces are full rank -- use vspaces as maximum
303310
return makefullrank!(manifold)
304311
end
305-
function InfiniteMPSManifold(mps_tensors::PeriodicVector{A}) where {A <: GenericMPSTensor}
306-
pspaces = map(physicalspace, mps_tensors)
307-
vspaces = map(left_virtualspace, mps_tensors)
312+
function InfiniteMPSManifold(mps_tensors::AbstractVector{A}) where {A <: GenericMPSTensor}
313+
pspaces = PeriodicVector(map(physicalspace, mps_tensors))
314+
vspaces = PeriodicVector(map(left_virtualspace, mps_tensors))
308315
for i in eachindex(vspaces)
309316
vspaces[i] == right_virtualspace(mps_tensors[i - 1]) ||
310317
throw(SpaceMismatch("incompatible spaces between site $(i - 1) and $i"))
@@ -423,14 +430,14 @@ function makefullrank!(manifold::FiniteMPSManifold)
423430
# left-to-right sweep
424431
for site in 1:length(manifold)
425432
if !isfullrank(manifold[site]; side = :right)
426-
maxspace = fuse(left_virtualspace(manifold, i), fuse(physicalspace(manifold, site)))
433+
maxspace = fuse(left_virtualspace(manifold, site), fuse(physicalspace(manifold, site)))
427434
manifold.vspaces[site + 1] = infimum(right_virtualspace(manifold, site), maxspace)
428435
end
429436
end
430437
# right-to-left sweep
431438
for site in reverse(1:length(manifold))
432439
if !isfullrank(manifold[site]; side = :left)
433-
maxspace = fuse(right_virtualspace(manifold, i), dual(fuse(physicalspace(manifold, site))))
440+
maxspace = fuse(right_virtualspace(manifold, site), dual(fuse(physicalspace(manifold, site))))
434441
manifold.vspaces[site] = infimum(left_virtualspace(manifold, site), maxspace)
435442
end
436443
end
@@ -443,15 +450,15 @@ function makefullrank!(manifold::InfiniteMPSManifold)
443450
# left-to-right sweep
444451
for site in 1:length(manifold)
445452
if !isfullrank(manifold[site]; side = :right)
446-
maxspace = fuse(left_virtualspace(manifold, i), fuse(physicalspace(manifold, site)))
453+
maxspace = fuse(left_virtualspace(manifold, site), fuse(physicalspace(manifold, site)))
447454
manifold.vspaces[site + 1] = infimum(right_virtualspace(manifold, site), maxspace)
448455
haschanged = true
449456
end
450457
end
451458
# right-to-left sweep
452459
for site in reverse(1:length(manifold))
453460
if !isfullrank(manifold[site]; side = :left)
454-
maxspace = fuse(right_virtualspace(manifold, i), dual(fuse(physicalspace(manifold, site))))
461+
maxspace = fuse(right_virtualspace(manifold, site), dual(fuse(physicalspace(manifold, site))))
455462
manifold.vspaces[site] = infimum(left_virtualspace(manifold, site), maxspace)
456463
haschanged = true
457464
end

src/states/finitemps.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ function FiniteMPS(
9292
# determine the MPS manifold from the input and instantiate the MPS
9393
mps_tensors = map(ALs, ARs, ACs) do AL, AR, AC
9494
mps_tensor = coalesce(AL, AR, AC)
95-
ismissing(sitetensor) && throw(ArgumentError("missing site tensor at site $i"))
95+
ismissing(mps_tensor) && throw(ArgumentError("missing site tensor at site $i"))
9696
return mps_tensor
9797
end
9898
manifold = FiniteMPSManifold(mps_tensors)
9999
A = _not_missing_type(MA)
100100
B = _not_missing_type(MB)
101-
mps = FiniteMPS{A, B}(undef, manifold)
101+
mps = FiniteMPS{A, B}(undef, length(manifold))
102102

103103
# populate the non-missing tensors into the MPS
104104
for i in 1:L
@@ -116,13 +116,15 @@ function FiniteMPS(
116116
getfield(mps, :ACs)[i] = ACs[i]
117117
end
118118
if !ismissing(Cs[i])
119-
space(Cs[i]) == left_virtualspace(V_mps) left_virtualspace(V_mps) ||
119+
V = left_virtualspace(manifold, i)
120+
space(Cs[i]) == (V V) ||
120121
throw(SpaceMismatch("incompatible space for C[$i]"))
121122
getfield(mps, :Cs)[i] = Cs[i]
122123
end
123124
end
124125
if !ismissing(Cs[end])
125-
space(Cs[end]) == right_virtualspace(manifold, L) right_virtualspace(manifold, L) ||
126+
V = right_virtualspace(manifold, L)
127+
space(Cs[end]) == (V V) ||
126128
throw(SpaceMismatch("incompatible space for C[$(L + 1)]"))
127129
getfield(mps, :Cs)[L + 1] = Cs[L + 1]
128130
end
@@ -198,7 +200,7 @@ function FiniteMPS(As::Vector{<:GenericMPSTensor}; normalize::Bool = false)
198200
mps = FiniteMPS
199201

200202
# start with first in case eltype changes
201-
AL1, C = qr_compact(As[i])
203+
AL1, C = qr_compact(first(As))
202204
normalize && normalize!(C)
203205

204206
# instantiate the destination
@@ -225,7 +227,7 @@ function FiniteMPS(ψ::AbstractTensor)
225227
A = _transpose_front(
226228
U * transpose* U', ((), reverse(ntuple(identity, numind(ψ) + 1))))
227229
)
228-
return FiniteMPS(decompose_localmps(A); normalize = false, overwrite = true)
230+
return FiniteMPS(decompose_localmps(A); normalize = false)
229231
end
230232

231233
for f in (:zeros, :ones)
@@ -239,12 +241,12 @@ for f in (:zeros, :ones)
239241
end
240242

241243
for randfun in (:rand, :randn)
242-
randfun! = Symbol(randf, :!)
243-
@eval function $randfun(rng::Random.AbstractRNG, ::Type{T}, manifold::FiniteMPSManifold) where {T}
244+
randfun! = Symbol(randfun, :!)
245+
@eval function Random.$randfun(rng::Random.AbstractRNG, ::Type{T}, manifold::FiniteMPSManifold) where {T}
244246
As = map(i -> $randfun(rng, T, manifold[i]), 1:length(manifold))
245247
return normalize!(FiniteMPS(As))
246248
end
247-
@eval function $randfun!(rng::Random.AbstractRNG, mps::FiniteMPS)
249+
@eval function Random.$randfun!(rng::Random.AbstractRNG, mps::FiniteMPS)
248250
for i in 1:length(mps)
249251
mps.AC[i] = $randfun!(rng, mps.AC[i])
250252
end
@@ -264,7 +266,7 @@ Base.@deprecate(
264266
FiniteMPS(
265267
f, elt, Pspaces::Vector{<:TensorSpace{S}}, maxVspace::S; left::S = oneunit(S), right::S = oneunit(S)
266268
) where {S <: ElementarySpace},
267-
f(elt, FiniteMPSManifold(Pspaces, maxVspaces; left_virtualspace = left, right_virtualspace = right))
269+
f(elt, FiniteMPSManifold(Pspaces, maxVspace; left_virtualspace = left, right_virtualspace = right))
268270
)
269271
Base.@deprecate(
270272
FiniteMPS(
@@ -274,13 +276,13 @@ Base.@deprecate(
274276
)
275277
Base.@deprecate(
276278
FiniteMPS(
277-
f, elt, N::Int, V::VectorSpace, args...; left::S = oneunit(S), right::S = oneunit(S)
279+
f, elt, N::Int, V::TensorSpace{S}, args...; left::S = oneunit(S), right::S = oneunit(S)
278280
) where {S <: ElementarySpace},
279281
f(elt, FiniteMPSManifold(fill(V, N), args...; left_virtualspace = left, right_virtualspace = right))
280282
)
281283
Base.@deprecate(
282284
FiniteMPS(
283-
N::Int, V::VectorSpace, args...; left::S = oneunit(S), right::S = oneunit(S)
285+
N::Int, V::TensorSpace{S}, args...; left::S = oneunit(S), right::S = oneunit(S)
284286
) where {S <: ElementarySpace},
285287
rand(FiniteMPSManifold(fill(V, N), args...; left_virtualspace = left, right_virtualspace = right))
286288
)

src/states/infinitemps.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function InfiniteMPS(A::AbstractVector{<:GenericMPSTensor}; kwargs...)
153153
@warn "Constructing an MPS from tensors that are not full rank"
154154
makefullrank!(A_copy)
155155

156-
manifold = InfiniteMPSManifold(A)
156+
manifold = InfiniteMPSManifold(A_copy)
157157
ψ = InfiniteMPS{eltype(A)}(undef, manifold)
158158

159159
# gaugefix the MPS
@@ -170,6 +170,7 @@ function InfiniteMPS(AL::AbstractVector{<:GenericMPSTensor}, C₀::MPSBondTensor
170170

171171
all(isfullrank, AL) || @error "Constructing an MPS from tensors that are not full rank"
172172
ψ = InfiniteMPS{eltype(AL)}(undef, InfiniteMPSManifold(AL))
173+
ψ.AL .= AL
173174

174175
# gaugefix the MPS
175176
gaugefix!(ψ, AL, C₀; order = :R, kwargs...)
@@ -183,6 +184,7 @@ function InfiniteMPS(C₀::MPSBondTensor, AR::AbstractVector{<:GenericMPSTensor}
183184

184185
all(isfullrank, AR) || @error "Constructing an MPS from tensors that are not full rank"
185186
ψ = InfiniteMPS{eltype(AR)}(undef, InfiniteMPSManifold(AR))
187+
ψ.AR .= AR
186188

187189
# gaugefix the MPS
188190
gaugefix!(ψ, AR, C₀; order = :L, kwargs...)
@@ -192,12 +194,12 @@ function InfiniteMPS(C₀::MPSBondTensor, AR::AbstractVector{<:GenericMPSTensor}
192194
end
193195

194196
for randfun in (:rand, :randn)
195-
randfun! = Symbol(randf, :!)
196-
@eval function $randfun(rng::Random.AbstractRNG, T::Type, manifold::InfiniteMPSManifold; kwargs...)
197+
randfun! = Symbol(randfun, :!)
198+
@eval function Random.$randfun(rng::Random.AbstractRNG, T::Type, manifold::InfiniteMPSManifold; kwargs...)
197199
As = map(i -> $randfun(rng, T, manifold[i]), 1:length(manifold))
198200
return InfiniteMPS(As; kwargs...)
199201
end
200-
@eval function $randfun!(rng::Random.AbstractRNG, mps::InfiniteMPS)
202+
@eval function Random.$randfun!(rng::Random.AbstractRNG, mps::InfiniteMPS)
201203
foreach(Base.Fix1(rng, $randfun!), mps.AC)
202204
C₀ = $randfun!(rng, mps.C[0])
203205
gaugefix!(mps, mps.AC, C₀)

0 commit comments

Comments
 (0)