-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from iitis/kd_graphic
Kd graphic
- Loading branch information
Showing
27 changed files
with
982 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,166 @@ | ||
export PepsTensor | ||
|
||
mutable struct PepsTensor | ||
nbrs::Dict{String, Int} | ||
left::AbstractArray | ||
right::AbstractArray | ||
up::AbstractArray | ||
down::AbstractArray | ||
loc::AbstractArray | ||
tensor::AbstractArray | ||
|
||
function PepsTensor(fg::MetaDiGraph, v::Int) | ||
pc = new() | ||
pc.nbrs = Dict() | ||
pc.loc = get_prop(fg, v, :local_exp) | ||
|
||
outgoing = outneighbors(fg, v) | ||
incoming = inneighbors(fg, v) | ||
|
||
for u ∈ outgoing | ||
e = SimpleEdge(v, u) | ||
if get_prop(fg, e, :orientation) == "horizontal" | ||
pc.right = first(get_prop(fg, e, :decomposition)) | ||
push!(pc.nbrs, "h_out" => u) | ||
else | ||
pc.down = first(get_prop(fg, e, :decomposition)) | ||
push!(pc.nbrs, "v_out" => u) | ||
end | ||
end | ||
export NetworkGraph, PepsNetwork | ||
export generate_tensor, MPO | ||
|
||
mutable struct NetworkGraph | ||
factor_graph::MetaDiGraph | ||
nbrs::Dict | ||
β::Number | ||
|
||
function NetworkGraph(factor_graph::MetaDiGraph, nbrs::Dict, β::Number) | ||
ng = new(factor_graph, nbrs, β) | ||
|
||
for u ∈ incoming | ||
e = SimpleEdge(u, v) | ||
if get_prop(fg, e, :orientation) == "horizontal" | ||
pc.left = last(get_prop(fg, e, :decomposition)) | ||
push!(pc.nbrs, "h_in" => u) | ||
else | ||
pc.up = last(get_prop(fg, e, :decomposition)) | ||
push!(pc.nbrs, "v_in" => u) | ||
end | ||
count = 0 | ||
for v ∈ vertices(ng.factor_graph), w ∈ ng.nbrs[v] | ||
if has_edge(ng.factor_graph, v, w) count += 1 end | ||
end | ||
# open boundary conditions | ||
if !isdefined(pc, :left) | ||
pc.left = ones(1, size(pc.right, 1)) | ||
|
||
mc = ne(ng.factor_graph) | ||
if count < mc | ||
error("Error: $(count) < $(mc)") | ||
end | ||
ng | ||
end | ||
end | ||
|
||
function generate_tensor(ng::NetworkGraph, v::Int) | ||
fg = ng.factor_graph | ||
loc_exp = exp.(-ng.β .* get_prop(fg, v, :loc_en)) | ||
|
||
dim = [] | ||
@cast tensor[_, i] := loc_exp[i] | ||
|
||
if !isdefined(pc, :right) | ||
pc.right = ones(size(pc.left, 2), 1) | ||
for w ∈ ng.nbrs[v] | ||
if has_edge(fg, w, v) | ||
_, _, pv = get_prop(fg, w, v, :split) | ||
pv = pv' | ||
elseif has_edge(fg, v, w) | ||
pv, _, _ = get_prop(fg, v, w, :split) | ||
else | ||
pv = ones(length(loc_exp), 1) | ||
end | ||
|
||
if !isdefined(pc, :up) | ||
pc.up = ones(1, size(pc.down, 1)) | ||
@cast tensor[(c, γ), σ] |= tensor[c, σ] * pv[σ, γ] | ||
push!(dim, size(pv, 2)) | ||
end | ||
|
||
reshape(tensor, dim..., :) | ||
end | ||
|
||
function _generate_tensor(ng::NetworkGraph, v::Int) | ||
fg = ng.factor_graph | ||
loc_exp = exp.(-ng.β .* get_prop(fg, v, :loc_en)) | ||
|
||
projs = Dict() | ||
for (i, w) ∈ enumerate(ng.nbrs[v]) | ||
if has_edge(fg, w, v) | ||
_, _, pv = get_prop(fg, w, v, :split) | ||
pv = pv' | ||
elseif has_edge(fg, v, w) | ||
pv, _, _ = get_prop(fg, v, w, :split) | ||
else | ||
pv = ones(length(loc_exp), 1) | ||
end | ||
push!(projs, i => pv) | ||
end | ||
|
||
L, U, R, D = projs[1], projs[2], projs[3], projs[4] | ||
@cast tensor[l, u, r, d, σ] |= L[σ, l] * U[σ, u] * R[σ, r] * D[σ, d] * loc_exp[σ] | ||
|
||
if !isdefined(pc, :down) | ||
pc.down = ones(size(pc.up, 2), 1) | ||
tensor | ||
end | ||
|
||
function generate_tensor(ng::NetworkGraph, v::Int, w::Int) | ||
fg = ng.factor_graph | ||
if has_edge(fg, w, v) | ||
_, e, _ = get_prop(fg, w, v, :split) | ||
tensor = exp.(-ng.β .* e') | ||
elseif has_edge(fg, v, w) | ||
_, e, _ = get_prop(fg, v, w, :split) | ||
tensor = exp.(-ng.β .* e) | ||
else | ||
tensor = ones(1, 1) | ||
end | ||
tensor | ||
end | ||
|
||
mutable struct PepsNetwork | ||
size::NTuple{2, Int} | ||
map::Dict | ||
network_graph::NetworkGraph | ||
origin::Symbol | ||
i_max::Int | ||
j_max::Int | ||
|
||
function PepsNetwork(m::Int, n::Int, fg::MetaDiGraph, β::Number, origin::Symbol) | ||
pn = new((m, n)) | ||
pn.map, pn.i_max, pn.j_max = LinearIndices(m, n, origin) | ||
|
||
nbrs = Dict() | ||
for i ∈ 1:pn.i_max, j ∈ 1:pn.j_max | ||
push!(nbrs, | ||
pn.map[i, j] => (pn.map[i, j-1], pn.map[i-1, j], | ||
pn.map[i, j+1], pn.map[i+1, j])) | ||
end | ||
pn.network_graph = NetworkGraph(fg, nbrs, β) | ||
pn | ||
end | ||
end | ||
|
||
generate_tensor(pn::PepsNetwork, m::NTuple{2,Int}) = generate_tensor(pn.network_graph, pn.map[m]) | ||
_generate_tensor(pn::PepsNetwork, m::NTuple{2,Int}) = _generate_tensor(pn.network_graph, pn.map[m]) | ||
generate_tensor(pn::PepsNetwork, m::NTuple{2,Int}, n::NTuple{2,Int}) = generate_tensor(pn.network_graph, pn.map[m], pn.map[n]) | ||
|
||
function MPO(::Type{T}, ψ::PEPSRow) where {T <: Number} | ||
n = length(ψ) | ||
ϕ = MPO(T, n) | ||
for i=1:n | ||
A = ψ[i] | ||
@reduce B[l, u, r, d] |= sum(σ) A[l, u, r, d, σ] | ||
ϕ[i] = B | ||
end | ||
ϕ | ||
end | ||
MPO(ψ::PEPSRow) = MPO(Float64, ψ) | ||
|
||
@cast pc.tensor[l, r, u, d, σ] |= pc.loc[σ] * pc.left[l, σ] * pc.right[σ, r] * pc.up[u, σ] * pc.down[σ, d] | ||
function PEPSRow(::Type{T}, peps::PepsNetwork, i::Int) where {T <: Number} | ||
n = peps.j_max | ||
ψ = PEPSRow(T, n) | ||
|
||
pc | ||
for j ∈ 1:n | ||
ψ[j] = generate_tensor(peps, (i, j)) | ||
end | ||
|
||
for j ∈ 1:n-1 | ||
ten = generate_tensor(peps, (i, j), (i, j+1)) | ||
A = ψ[j] | ||
@tensor B[l, u, r, d, σ] := A[l, u, r̃, d, σ] * ten[r̃, r] | ||
ψ[j] = B | ||
end | ||
ψ | ||
end | ||
PEPSRow(peps::PepsNetwork, i::Int) = PEPSRow(Float64, peps, i) | ||
|
||
function MPO(::Type{T}, peps::PepsNetwork, i::Int, k::Int) where {T <: Number} | ||
n = peps.j_max | ||
|
||
ψ = MPO(T, n) | ||
fg = peps.network_graph.factor_graph | ||
|
||
for j ∈ 1:n | ||
v, w = peps.map[i, j], peps.map[k, j] | ||
|
||
Base.size(A::PepsTensor) = size(A.tensor) | ||
if has_edge(fg, v, w) | ||
_, en, _ = get_prop(fg, v, w, :split) | ||
elseif has_edge(fg, w, v) | ||
_, en, _ = get_prop(fg, w, v, :split) | ||
else | ||
en = ones(1, 1) | ||
end | ||
|
||
#@cast A[_, σ, _, η] := en[σ, η] | ||
@cast A[u, _, d, _] := en[u, d] | ||
ψ[j] = A | ||
end | ||
ψ | ||
end | ||
MPO(peps::PepsNetwork, i::Int, k::Int) = MPO(Float64, peps, i, k) |
Oops, something went wrong.