diff --git a/src/FormNetworks/abstractformnetwork.jl b/src/FormNetworks/abstractformnetwork.jl index a2749335..e23424ba 100644 --- a/src/FormNetworks/abstractformnetwork.jl +++ b/src/FormNetworks/abstractformnetwork.jl @@ -13,15 +13,14 @@ 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)))) -function operator(f::AbstractFormNetwork) - return induced_subgraph(f, collect(values(operator_vertex_map(f)))) +function operator_vertices(f::AbstractFormNetwork) + return filter(v -> last(v) == operator_vertex_suffix(f), vertices(f)) end - -function derivative(f::AbstractFormNetwork, state_vertices::Vector; kwargs...) - tn_vertices = derivative_vertices(f, state_vertices) - return derivative(tensornetwork(f), tn_vertices; kwargs...) +function bra_vertices(f::AbstractFormNetwork) + return filter(v -> last(v) == bra_vertex_suffix(f), vertices(f)) +end +function ket_vertices(f::AbstractFormNetwork) + return filter(v -> last(v) == ket_vertex_suffix(f), vertices(f)) end function bra_vertices(f::AbstractFormNetwork, state_vertices::Vector) @@ -32,6 +31,26 @@ function ket_vertices(f::AbstractFormNetwork, state_vertices::Vector) return [ket_vertex_map(f)(sv) for sv in state_vertices] end +function Graphs.induced_subgraph(f::AbstractFormNetwork, vertices::Vector) + return induced_subgraph(tensornetwork(f), vertices) +end +function bra(f::AbstractFormNetwork) + return rename_vertices(inv_vertex_map(f), first(induced_subgraph(f, bra_vertices(f)))) +end +function ket(f::AbstractFormNetwork) + return rename_vertices(inv_vertex_map(f), first(induced_subgraph(f, ket_vertices(f)))) +end +function operator(f::AbstractFormNetwork) + return rename_vertices( + inv_vertex_map(f), first(induced_subgraph(f, operator_vertices(f))) + ) +end + +function derivative(f::AbstractFormNetwork, state_vertices::Vector; kwargs...) + tn_vertices = derivative_vertices(f, state_vertices) + return derivative(tensornetwork(f), tn_vertices; kwargs...) +end + function derivative_vertices(f::AbstractFormNetwork, state_vertices::Vector; kwargs...) return setdiff( vertices(f), vcat(bra_vertices(f, state_vertices), ket_vertices(f, state_vertices)) @@ -41,3 +60,4 @@ 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)) +inv_vertex_map(f::AbstractFormNetwork) = v -> first(v) diff --git a/src/FormNetworks/bilinearformnetwork.jl b/src/FormNetworks/bilinearformnetwork.jl index 5622fcb2..ecbbe01b 100644 --- a/src/FormNetworks/bilinearformnetwork.jl +++ b/src/FormNetworks/bilinearformnetwork.jl @@ -15,9 +15,9 @@ function BilinearFormNetwork( operator::AbstractITensorNetwork, bra::AbstractITensorNetwork, ket::AbstractITensorNetwork; - operator_vertex_suffix=default_operator_vertex_suffix, - bra_vertex_suffix=default_bra_vertex_suffix, - ket_vertex_suffix=default_ket_vertex_suffix, + operator_vertex_suffix=default_operator_vertex_suffix(), + bra_vertex_suffix=default_bra_vertex_suffix(), + ket_vertex_suffix=default_ket_vertex_suffix(), ) tn = disjoint_union( operator_vertex_suffix => operator, bra_vertex_suffix => bra, ket_vertex_suffix => ket diff --git a/test/test_forms.jl b/test/test_forms.jl index aa533ebd..c391bd53 100644 --- a/test/test_forms.jl +++ b/test/test_forms.jl @@ -3,7 +3,15 @@ using Graphs using NamedGraphs using ITensorNetworks using ITensorNetworks: - delta_network, update, tensornetwork, bra_vertex_map, ket_vertex_map, dual_index_map + delta_network, + update, + tensornetwork, + bra_vertex_map, + ket_vertex_map, + dual_index_map, + bra, + ket, + operator using Test using Random @@ -12,16 +20,20 @@ using Random s_ket = siteinds("S=1/2", g) s_bra = prime(s_ket; links=[]) s_operator = union_all_inds(s_bra, s_ket) - χ = 2 + χ, D = 2, 3 Random.seed!(1234) ψket = randomITensorNetwork(s_ket; link_space=χ) ψbra = randomITensorNetwork(s_bra; link_space=χ) - A = delta_network(s_operator) + A = randomITensorNetwork(s_operator; link_space=D) blf = BilinearFormNetwork(A, ψbra, ψket) @test nv(blf) == nv(ψket) + nv(ψbra) + nv(A) @test isempty(externalinds(blf)) + @test underlying_graph(ket(blf)) == underlying_graph(ψket) + @test underlying_graph(operator(blf)) == underlying_graph(A) + @test underlying_graph(bra(blf)) == underlying_graph(ψbra) + qf = QuadraticFormNetwork(A, ψket) @test nv(qf) == 2 * nv(ψbra) + nv(A) @test isempty(externalinds(qf)) @@ -33,4 +45,7 @@ using Random @test tensornetwork(qf_updated)[bra_vertex_map(qf_updated)(v)] ≈ dual_index_map(qf_updated)(dag(new_tensor)) @test tensornetwork(qf_updated)[ket_vertex_map(qf_updated)(v)] ≈ new_tensor + + @test underlying_graph(ket(qf)) == underlying_graph(ψket) + @test underlying_graph(operator(qf)) == underlying_graph(A) end