Skip to content

Commit

Permalink
IndsNetworkMap to wrap IndsNetwork and IndexMap
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Apr 16, 2024
1 parent 2690e29 commit c0cdda1
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 380 deletions.
7 changes: 3 additions & 4 deletions examples/2d_laplace_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ seed!(1234)
L = 12
g = NamedGraph(SimpleGraph(uniform_tree(L)))

s = continuous_siteinds(g)
index_map = IndexMap(s; map_dimension=2)
s = continuous_siteinds(g; map_dimension=2)

ψ_fxy = 0.1 * rand_itn(s, index_map; link_space=2)
= laplacian_operator(s, index_map; scale=false, cutoff=1e-8)
ψ_fxy = 0.1 * rand_itn(s; link_space=2)
= laplacian_operator(s; scale=false, cutoff=1e-8)
println("2D Laplacian constructed for this tree, bond dimension is $(maxlinkdim(∇))")

init_energy =
Expand Down
9 changes: 4 additions & 5 deletions examples/construct_multi_dimensional_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ using Random: Random
L = 12
Random.seed!(1234)
g = NamedGraph(SimpleGraph(uniform_tree(L)))
s = continuous_siteinds(g)
index_map = IndexMap(s; map_dimension=3)
s = continuous_siteinds(g; map_dimension=3)

println(
"Constructing the 3D function f(x,y,z) = x³(y + y²) + cosh(πz) as a tensor network on a randomly chosen tree with $L vertices",
)
ψ_fx = poly_itn(s, index_map, [0.0, 0.0, 0.0, 1.0]; dimension=1)
ψ_fy = poly_itn(s, index_map, [0.0, 1.0, 1.0, 0.0]; dimension=2)
ψ_fz = cosh_itn(s, index_map; k=Float64(pi), dimension=3)
ψ_fx = poly_itn(s, [0.0, 0.0, 0.0, 1.0]; dimension=1)
ψ_fy = poly_itn(s, [0.0, 1.0, 1.0, 0.0]; dimension=2)
ψ_fz = cosh_itn(s; k=Float64(pi), dimension=3)
ψxyz = ψ_fx * ψ_fy + ψ_fz

ψxyz = truncate(ψxyz; cutoff=1e-12)
Expand Down
5 changes: 4 additions & 1 deletion src/ITensorNumericalAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ITensorNumericalAnalysis

include("utils.jl")
include("indexmap.jl")
include("indsnetworkmap.jl")
include("polynomialutils.jl")
include("itensornetworkfunction.jl")
include("elementary_functions.jl")
Expand All @@ -11,13 +12,15 @@ export continuous_siteinds
export ITensorNetworkFunction, itensornetwork, dimension_vertices
export IndexMap,
default_dimension_map,
dimension_inds,
calculate_xyz,
calculate_x,
calculate_ind_values,
dimension,
dimensions,
grid_points
export IndsNetworkMap,
continuous_siteinds,
indsnetwork,
indexmap,
vertex_dimension,
Expand All @@ -33,7 +36,7 @@ export const_itensornetwork,
sin_itensornetwork,
get_edge_toward_root,
polynomial_itensornetwork,
random_itensornetworkfunction,
random_itensornetwork,
laplacian_operator,
first_derivative_operator,
second_derivative_operator,
Expand Down
80 changes: 34 additions & 46 deletions src/elementary_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,32 @@ default_dimension() = 1

"""Build a representation of the function f(x,y,z,...) = c, with flexible choice of linkdim"""
function const_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
c::Union{Float64,ComplexF64}=default_c_value(),
linkdim::Int64=1,
s::IndsNetworkMap; c::Union{Float64,ComplexF64}=default_c_value(), linkdim::Int64=1
)
ψ = random_tensornetwork(s; link_space=linkdim)
ψ = random_itensornetwork(s; link_space=linkdim)
inv_L = Float64(1.0 / nv(s))
for v in vertices(ψ)
sinds = inds(s, v)
virt_inds = setdiff(inds(ψ[v]), sinds)
ψ[v] = c^inv_L * c_tensor(only(sinds), virt_inds)
end

return ITensorNetworkFunction(ψ, indexmap)
return ψ
end

"""Construct the product state representation of the exp(kx+a)
function for x ∈ [0,1] as an ITensorNetworkFunction, along the specified dim"""
function exp_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
s::IndsNetworkMap;
k::Union{Float64,ComplexF64}=default_k_value(),
a::Union{Float64,ComplexF64}=default_a_value(),
dimension::Int64=default_dimension(),
)
ψ = const_itensornetwork(s, indexmap)
Lx = length(inds(indexmap, dimension))
ψ = const_itensornetwork(s)
Lx = length(dimension_vertices, dimension))
for v in dimension_vertices(ψ, dimension)
sind = only(inds(s, v))
ψ[v] = ITensor(
exp(a / Lx) * exp.(k * index_values_to_scalars(indexmap, sind)), inds(ψ[v])
)
ψ[v] = ITensor(exp(a / Lx) * exp.(k * index_values_to_scalars(s, sind)), inds(ψ[v]))
end

return ψ
Expand All @@ -59,14 +53,13 @@ end
"""Construct the bond dim 2 representation of the cosh(kx+a) function for x ∈ [0,1] as an ITensorNetwork, using an IndsNetwork which
defines the network geometry. Vertex map provides the ordering of the sites as bits"""
function cosh_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
s::IndsNetworkMap;
k::Union{Float64,ComplexF64}=default_k_value(),
a::Union{Float64,ComplexF64}=default_a_value(),
dimension::Int64=default_dimension(),
)
ψ1 = exp_itensornetwork(s, indexmap; a, k, dimension)
ψ2 = exp_itensornetwork(s, indexmap; a=-a, k=-k, dimension)
ψ1 = exp_itensornetwork(s; a, k, dimension)
ψ2 = exp_itensornetwork(s; a=-a, k=-k, dimension)

ψ1[first(vertices(ψ1))] *= 0.5
ψ2[first(vertices(ψ1))] *= 0.5
Expand All @@ -77,14 +70,13 @@ end
"""Construct the bond dim 2 representation of the sinh(kx+a) function for x ∈ [0,1] as an ITensorNetwork, using an IndsNetwork which
defines the network geometry. Vertex map provides the ordering of the sites as bits"""
function sinh_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
s::IndsNetworkMap;
k::Union{Float64,ComplexF64}=default_k_value(),
a::Union{Float64,ComplexF64}=default_a_value(),
dimension::Int64=default_dimension(),
)
ψ1 = exp_itensornetwork(s, indexmap; a, k, dimension)
ψ2 = exp_itensornetwork(s, indexmap; a=-a, k=-k, dimension)
ψ1 = exp_itensornetwork(s; a, k, dimension)
ψ2 = exp_itensornetwork(s; a=-a, k=-k, dimension)

ψ1[first(vertices(ψ1))] *= 0.5
ψ2[first(vertices(ψ1))] *= -0.5
Expand All @@ -95,16 +87,15 @@ end
"""Construct the bond dim n representation of the tanh(kx+a) function for x ∈ [0,1] as an ITensorNetwork, using an IndsNetwork which
defines the network geometry. Vertex map provides the ordering of the sites as bits"""
function tanh_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
s::IndsNetworkMap;
k::Union{Float64,ComplexF64}=default_k_value(),
a::Union{Float64,ComplexF64}=default_a_value(),
nterms::Int64=default_nterms(),
dimension::Int64=default_dimension(),
)
ψ = const_itensornetwork(s, indexmap)
ψ = const_itensornetwork(s)
for n in 1:nterms
ψt = exp_itensornetwork(s, indexmap; a=-2 * n * a, k=-2 * k * n, dimension)
ψt = exp_itensornetwork(s; a=-2 * n * a, k=-2 * k * n, dimension)
ψt[first(vertices(ψt))] *= 2 * ((-1)^n)
ψ = ψ + ψt
end
Expand All @@ -115,14 +106,13 @@ end
"""Construct the bond dim 2 representation of the cos(kx+a) function for x ∈ [0,1] as an ITensorNetwork, using an IndsNetwork which
defines the network geometry. Vertex map provides the ordering of the sites as bits"""
function cos_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
s::IndsNetworkMap;
k::Union{Float64,ComplexF64}=default_k_value(),
a::Union{Float64,ComplexF64}=default_a_value(),
dimension::Int64=default_dimension(),
)
ψ1 = exp_itensornetwork(s, indexmap; a=a * im, k=k * im, dimension)
ψ2 = exp_itensornetwork(s, indexmap; a=-a * im, k=-k * im, dimension)
ψ1 = exp_itensornetwork(s; a=a * im, k=k * im, dimension)
ψ2 = exp_itensornetwork(s; a=-a * im, k=-k * im, dimension)

ψ1[first(vertices(ψ1))] *= 0.5
ψ2[first(vertices(ψ1))] *= 0.5
Expand All @@ -133,14 +123,13 @@ end
"""Construct the bond dim 2 representation of the sin(kx+a) function for x ∈ [0,1] as an ITensorNetwork, using an IndsNetwork which
defines the network geometry. Vertex map provides the ordering of the sites as bits"""
function sin_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap;
s::IndsNetworkMap;
k::Union{Float64,ComplexF64}=default_k_value(),
a::Union{Float64,ComplexF64}=default_a_value(),
dimension::Int64=default_dimension(),
)
ψ1 = exp_itensornetwork(s, indexmap; a=a * im, k=k * im, dimension)
ψ2 = exp_itensornetwork(s, indexmap; a=-a * im, k=-k * im, dimension)
ψ1 = exp_itensornetwork(s; a=a * im, k=k * im, dimension)
ψ2 = exp_itensornetwork(s; a=-a * im, k=-k * im, dimension)

ψ1[first(vertices(ψ1))] *= -0.5 * im
ψ2[first(vertices(ψ1))] *= 0.5 * im
Expand All @@ -151,23 +140,22 @@ end
"""Build a representation of the function f(x) = sum_{i=0}^{n}coeffs[i+1]*(x)^{i} on the graph structure specified
by indsnetwork"""
function polynomial_itensornetwork(
s::IndsNetwork,
indexmap::IndexMap,
coeffs::Vector{Float64};
dimension::Int64=default_dimension(),
s::IndsNetworkMap, coeffs::Vector{Float64}; dimension::Int64=default_dimension()
)
n = length(coeffs)
#First treeify the index network (ignore edges that form loops)
g = underlying_graph(s)
_s = indsnetwork(s)
g = underlying_graph(_s)
g_tree = undirected_graph(random_bfs_tree(g, first(vertices(g))))
s_tree = add_edges(rem_edges(s, edges(g)), edges(g_tree))
s_tree = add_edges(rem_edges(_s, edges(g)), edges(g_tree))
s_tree = IndsNetworkMap(s_tree, indexmap(s))

ψ = const_itensornetwork(s_tree, indexmap; linkdim=n)
ψ = const_itensornetwork(s_tree; linkdim=n)
dim_vertices = dimension_vertices(ψ, dimension)
source_vertex = first(dim_vertices)

for v in dim_vertices
siteindex = only(s_tree[v])
siteindex = only(inds(s_tree, v))
if v != source_vertex
e = get_edge_toward_vertex(g_tree, v, source_vertex)
betaindex = only(commoninds(ψ, e))
Expand All @@ -177,7 +165,7 @@ function polynomial_itensornetwork(
siteindex,
alphas,
betaindex,
index_values_to_scalars(indexmap, siteindex),
index_values_to_scalars(s_tree, siteindex),
)
elseif v == source_vertex
betaindex = Index(n, "DummyInd")
Expand All @@ -187,7 +175,7 @@ function polynomial_itensornetwork(
siteindex,
alphas,
betaindex,
index_values_to_scalars(indexmap, siteindex),
index_values_to_scalars(s_tree, siteindex),
)
ψ[v] = ψv * ITensor(coeffs, betaindex)
end
Expand All @@ -196,7 +184,7 @@ function polynomial_itensornetwork(
#Put the transfer tensors in, these are special tensors that
# go on the digits (sites) that don't correspond to the desired dimension
for v in setdiff(vertices(ψ), dim_vertices)
siteindex = only(s_tree[v])
siteindex = only(inds(s_tree, v))
e = get_edge_toward_vertex(g_tree, v, source_vertex)
betaindex = only(commoninds(ψ, e))
alphas = setdiff(inds(ψ[v]), Index[siteindex, betaindex])
Expand All @@ -206,8 +194,8 @@ function polynomial_itensornetwork(
return ψ
end

function random_itensornetwork(s::IndsNetwork, bit_map; kwargs...)
return ITensorNetworkFunction(random_tensornetwork(s; kwargs...), bit_map)
function random_itensornetwork(s::IndsNetworkMap; kwargs...)
return ITensorNetworkFunction(random_tensornetwork(indsnetwork(s); kwargs...), s)
end

const const_itn = const_itensornetwork
Expand Down
Loading

0 comments on commit c0cdda1

Please sign in to comment.