Skip to content

Commit

Permalink
Merge d6d73fe into 6223a7a
Browse files Browse the repository at this point in the history
  • Loading branch information
dexter2206 committed Apr 28, 2021
2 parents 6223a7a + d6d73fe commit 3ae42e6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/factor.jl
@@ -1,4 +1,4 @@
export factor_graph, rank_reveal, projectors, split_into_clusters
export factor_graph, rank_reveal, projectors, split_into_clusters, decode_factor_graph_state


function split_into_clusters(ig::LabelledGraph{S, T}, assignment_rule) where {S, T}
Expand Down Expand Up @@ -91,3 +91,13 @@ function rank_reveal(energy, order=:PE)

order == :PE ? (P, E) : (E, P)
end


function decode_factor_graph_state(fg, state::Vector{Int})
all_spins = vcat([vertices(get_prop(fg, v, :cluster)) for v vertices(fg)]...)
spin_perm = sortperm(all_spins)

vcat(
[get_prop(fg, v, :spectrum).states[i] for (i, v) zip(state, vertices(fg))]...
)[spin_perm]
end
27 changes: 27 additions & 0 deletions test/factor.jl
Expand Up @@ -174,3 +174,30 @@ end
@test E * Pr E_old
@test Pl * E * Pr energy
end


function create_example_factor_graph()
J12 = -1.0
h1 = 0.5
h2 = 0.75


D = Dict((1, 2) => J12, (1, 1) => h1, (2, 2) => h2)
ig = ising_graph(D)

factor_graph(
ig,
Dict((1, 1) => 2, (1, 2) => 2),
spectrum = full_spectrum,
cluster_assignment_rule = Dict(1 => (1, 1), 2 => (1, 2)),
)
end

const fg_state_to_spin = [
([1, 1], [-1, -1]), ([1, 2], [-1, 1]), ([2, 1], [1, -1]), ([2, 2], [1, 1])
]

@testset "Test decoding solution $(state) to spins gives $(spin_values) results" for (state, spin_values) fg_state_to_spin
fg = create_example_factor_graph()
@test decode_factor_graph_state(fg, state) == spin_values
end

0 comments on commit 3ae42e6

Please sign in to comment.