forked from QuantumKitHub/MPSKit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiline.jl
More file actions
107 lines (85 loc) · 3.6 KB
/
multiline.jl
File metadata and controls
107 lines (85 loc) · 3.6 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
"""
struct Multiline{T}
Object that represents multiple lines of objects of type `T`. Typically used to represent
multiple lines of `InfiniteMPS` (`MultilineMPS`) or MPO (`Multiline{<:AbstractMPO}`).
# Fields
- `data::PeriodicArray{T,1}`: the data of the multiline object
See also: [`MultilineMPS`](@ref) and [`MultilineMPO`](@ref)
"""
struct Multiline{T}
data::PeriodicArray{T, 1}
function Multiline{T}(data::AbstractVector{T}) where {T}
@assert allequal(length.(data)) "All lines must have the same length"
return new{T}(data)
end
end
Multiline(data::AbstractVector{T}) where {T} = Multiline{T}(data)
# AbstractArray interface
# -----------------------
Base.parent(m::Multiline) = m.data
Base.size(m::Multiline) = (length(parent(m)), length(parent(m)[1]))
Base.size(m::Multiline, i::Int) = getindex(size(m), i)
Base.length(m::Multiline) = prod(size(m))
function Base.axes(m::Multiline, i::Int)
return i == 1 ? axes(parent(m), 1) :
i == 2 ? axes(parent(m)[1], 1) : throw(ArgumentError("Invalid index $i"))
end
Base.eachindex(m::Multiline) = CartesianIndices(size(m))
eachsite(m::Multiline) = eachsite(first(parent(m)))
Base.getindex(m::Multiline, i::Int) = getindex(parent(m), i)
Base.setindex!(m::Multiline, v, i::Int) = (setindex!(parent(m), v, i); m)
Base.copy(m::Multiline) = Multiline(map(copy, parent(m)))
Base.iterate(m::Multiline, args...) = iterate(parent(m), args...)
# Utility functions
# -----------------
Base.circshift(A::Multiline, n::Int) = Multiline(circshift(parent(A), n))
function Base.circshift(A::Multiline, shifts::Tuple{Int, Int})
data′ = circshift.(parent(A), shifts[2])
return Multiline(circshift!(data′, shifts[1]))
end
Base.reverse(A::Multiline) = Multiline(reverse(parent(A)))
Base.only(A::Multiline) = only(parent(A))
function Base.repeat(A::Multiline, rows::Int, cols::Int)
inner = map(Base.Fix2(repeat, cols), A.data)
outer = repeat(inner, rows)
return Multiline(outer)
end
# Style
# ----------------
OperatorStyle(::Type{Multiline{T}}) where {T} = OperatorStyle(T)
GeometryStyle(::Type{Multiline{T}}) where {T} = GeometryStyle(T)
# VectorInterface
# ---------------
VectorInterface.scalartype(::Type{Multiline{T}}) where {T} = scalartype(T)
function VectorInterface.zerovector(x::Multiline, ::Type{S}) where {S <: Number}
return Multiline(zerovector.(parent(x), S))
end
VectorInterface.zerovector!(x::Multiline) = (zerovector!.(parent(x)); x)
function VectorInterface.scale(x::Multiline, α::Number)
return scale!(zerovector(x, VectorInterface.promote_scale(x, α)), x, α)
end
function VectorInterface.scale!(x::Multiline, α::Number)
scale!.(parent(x), α)
return x
end
VectorInterface.scale!!(x::Multiline, α::Number) = scale!(x, α)
function VectorInterface.scale!(x::Multiline, x′::Multiline, α::Number)
scale!.(parent(x), parent(x′), α)
return x
end
VectorInterface.scale!!(x::Multiline, x′::Multiline, α::Number) = scale!(x, x′, α)
function VectorInterface.add(x::Multiline, y::Multiline, α::Number, β::Number)
z = zerovector(x, VectorInterface.promote_add(x, y, α, β))
return add!(scale!(z, x, β), y, α)
end
function VectorInterface.add!(x::Multiline, y::Multiline, α::Number, β::Number)
add!.(parent(x), parent(y), α, β)
return x
end
VectorInterface.add!!(x::Multiline, y::Multiline, α::Number, β::Number) = add!(x, y, α, β)
function VectorInterface.inner(x::Multiline, y::Multiline)
T = VectorInterface.promote_inner(x, y)
init = zero(T)
return sum(splat(inner), zip(parent(parent(x)), parent(parent(y))); init)
end
LinearAlgebra.norm(x::Multiline) = sqrt(real(inner(x, x)))