Skip to content

Commit

Permalink
Suffix naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Feb 12, 2024
1 parent c6592f7 commit 89f33c1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 74 deletions.
15 changes: 11 additions & 4 deletions src/FormNetworks/abstractformnetwork.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
default_bra_vertex_suffix() = "bra"
default_ket_vertex_suffix() = "ket"
default_operator_vertex_suffix() = "operator"

abstract type AbstractFormNetwork{V} <: AbstractITensorNetwork{V} end

#Needed for interface
bra_vertex_map(f::AbstractFormNetwork) = not_implemented()
ket_vertex_map(f::AbstractFormNetwork) = not_implemented()
operator_vertex_map(f::AbstractFormNetwork) = not_implemented()
dual_index_map(f::AbstractFormNetwork) = not_implemented()
tensornetwork(f::AbstractFormNetwork) = not_implemented()
copy(f::AbstractFormNetwork) = not_implemented()
derivative_vertices(f::AbstractFormNetwork) = not_implemented()
bra_ket_vertices(f::AbstractFormNetwork, state_vertices::Vector) = not_implemented()
operator_vertex_suffix(f::AbstractFormNetwork) = not_implemented()
bra_vertex_suffix(f::AbstractFormNetwork) = not_implemented()
ket_vertex_suffix(f::AbstractFormNetwork) = not_implemented()

bra(f::AbstractFormNetwork) = induced_subgraph(f, collect(values(bra_vertex_map(f))))
ket(f::AbstractFormNetwork) = induced_subgraph(f, collect(values(ket_vertex_map(f))))
Expand All @@ -34,3 +37,7 @@ function derivative_vertices(f::AbstractFormNetwork, state_vertices::Vector; kwa
vertices(f), vcat(bra_vertices(f, state_vertices), ket_vertices(f, state_vertices))
)
end

operator_vertex_map(f::AbstractFormNetwork) = v -> (v, operator_vertex_suffix(f))
bra_vertex_map(f::AbstractFormNetwork) = v -> (v, bra_vertex_suffix(f))
ket_vertex_map(f::AbstractFormNetwork) = v -> (v, ket_vertex_suffix(f))
61 changes: 26 additions & 35 deletions src/FormNetworks/bilinearformnetwork.jl
Original file line number Diff line number Diff line change
@@ -1,61 +1,52 @@
default_bra_vertex_map(v) = (v, "bra")
default_ket_vertex_map(v) = (v, "ket")
default_operator_vertex_map(v) = (v, "operator")
default_operator_constructor(s::IndsNetwork) = delta_network(s)

struct BilinearFormNetwork{
V,TensorNetwork<:AbstractITensorNetwork{V},BraMap,KetMap,OperatorMap
V,
TensorNetwork<:AbstractITensorNetwork{V},
OperatorVertexSuffix,
BraVertexSuffix,
KetVertexSuffix,
} <: AbstractFormNetwork{V}
tensornetwork::TensorNetwork
operator_vertex_map::OperatorMap
bra_vertex_map::BraMap
ket_vertex_map::KetMap
operator_vertex_suffix::OperatorVertexSuffix
bra_vertex_suffix::BraVertexSuffix
ket_vertex_suffix::KetVertexSuffix
end

function BilinearFormNetwork(
operator::AbstractITensorNetwork,
bra::AbstractITensorNetwork,
ket::AbstractITensorNetwork;
operator_vertex_map=default_operator_vertex_map,
bra_vertex_map=default_bra_vertex_map,
ket_vertex_map=default_ket_vertex_map,
operator_vertex_suffix=default_operator_vertex_suffix,
bra_vertex_suffix=default_bra_vertex_suffix,
ket_vertex_suffix=default_ket_vertex_suffix,
)

# TODO: Reminder to fix `rename_vertices(::AbstractITensorNetwork)`.
bra_renamed = rename_vertices_itn(bra, bra_vertex_map)
ket_renamed = rename_vertices_itn(ket, ket_vertex_map)
operator_renamed = rename_vertices_itn(operator, operator_vertex_map)

# TODO: Reminder to fix `union` so that `union(bra_renamed, operator_renamed, ket_renamed)` works.
tn = union(union(bra_renamed, operator_renamed), ket_renamed)

return BilinearFormNetwork(tn, bra_vertex_map, ket_vertex_map, operator_vertex_map)
tn = disjoint_union(
operator_vertex_suffix => operator, bra_vertex_suffix => bra, ket_vertex_suffix => ket
)
return BilinearFormNetwork(
tn, operator_vertex_suffix, bra_vertex_suffix, ket_vertex_suffix
)
end

#Needed for implementation
bra_vertex_map(blf::BilinearFormNetwork) = blf.bra_vertex_map
ket_vertex_map(blf::BilinearFormNetwork) = blf.ket_vertex_map
operator_vertex_map(blf::BilinearFormNetwork) = blf.operator_vertex_map
operator_vertex_suffix(blf::BilinearFormNetwork) = blf.operator_vertex_suffix
bra_vertex_suffix(blf::BilinearFormNetwork) = blf.bra_vertex_suffix
ket_vertex_suffix(blf::BilinearFormNetwork) = blf.ket_vertex_suffix
tensornetwork(blf::BilinearFormNetwork) = blf.tensornetwork
data_graph_type(::Type{<:BilinearFormNetwork}) = data_graph_type(tensornetwork(blf))
data_graph(blf::BilinearFormNetwork) = data_graph(tensornetwork(blf))

function copy(blf::BilinearFormNetwork)
return BilinearFormNetwork(
copy(tensornetwork(blf)),
bra_vertex_map(blf),
ket_vertex_map(blf),
operator_vertex_map(blf),
operator_vertex_suffix(blf),
bra_vertex_suffix(blf),
ket_vertex_suffix(blf),
)
end

function BilinearFormNetwork(
bra::AbstractITensorNetwork,
ket::AbstractITensorNetwork;
operator_constructor=default_operator_constructor,
kwargs...,
bra::AbstractITensorNetwork, ket::AbstractITensorNetwork; kwargs...
)
operator_space = union_all_inds(siteinds(bra), siteinds(ket))
O = tno_constructor(operator_space)
operator_inds = union_all_inds(siteinds(bra), siteinds(ket))
O = delta_network(operator_inds)
return BilinearFormNetwork(bra, O, ket; kwargs...)
end
16 changes: 7 additions & 9 deletions src/FormNetworks/quadraticformnetwork.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
default_index_map = prime
default_inv_index_map = noprime

struct QuadraticFormNetwork{V,FormNetwork<:BilinearFormNetwork{V},IndexMap} <:
AbstractFormNetwork{V}
Expand All @@ -21,9 +22,9 @@ end

#Needed for implementation, forward from bilinear form
for f in [
:bra_vertex_map,
:ket_vertex_map,
:operator_vertex_map,
:operator_vertex_suffix,
:bra_vertex_suffix,
:ket_vertex_suffix,
:tensornetwork,
:data_graph,
:data_graph_type,
Expand Down Expand Up @@ -51,14 +52,11 @@ function QuadraticFormNetwork(
end

function QuadraticFormNetwork(
ket::AbstractITensorNetwork;
dual_index_map=default_index_map,
operator_constructor=default_operator_constructor,
kwargs...,
ket::AbstractITensorNetwork; dual_index_map=default_index_map, kwargs...
)
s = siteinds(ket)
operator_space = union_all_inds(s, dual_index_map(s; links=[]))
operator = operator_constructor(operator_space)
operator_inds = union_all_inds(s, dual_index_map(s; links=[]))
operator = delta_network(operator_inds)
return QuadraticFormNetwork(operator, ket; kwargs...)
end

Expand Down
1 change: 0 additions & 1 deletion src/ITensorNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ include(joinpath("approx_itensornetwork", "binary_tree_partition.jl"))
include("contract.jl")
include("utility.jl")
include("specialitensornetworks.jl")
include("renameitensornetwork.jl")
include("boundarymps.jl")
include(joinpath("beliefpropagation", "beliefpropagation.jl"))
include(joinpath("beliefpropagation", "beliefpropagation_schedule.jl"))
Expand Down
25 changes: 0 additions & 25 deletions src/renameitensornetwork.jl

This file was deleted.

9 changes: 9 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ function line_to_tree(line::Vector)
end
return [line_to_tree(line[1:(end - 1)]), line[end]]
end

function Base.union(
graph1::AbstractNamedGraph,
graph2::AbstractNamedGraph,
graph3::AbstractNamedGraph,
graph_rest::AbstractNamedGraph...,
)
return union(union(graph1, graph2), graph3, graph_rest...)
end

0 comments on commit 89f33c1

Please sign in to comment.