Skip to content

Commit

Permalink
Fix induced_subgraph errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Feb 12, 2024
1 parent 89f33c1 commit b0a46f0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
36 changes: 28 additions & 8 deletions src/FormNetworks/abstractformnetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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)
6 changes: 3 additions & 3 deletions src/FormNetworks/bilinearformnetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions test/test_forms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand All @@ -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

0 comments on commit b0a46f0

Please sign in to comment.