From d63f7bfa8c874cf11b6f5138bc172a880fafee9b Mon Sep 17 00:00:00 2001 From: zmorrell Date: Thu, 10 Feb 2022 14:10:12 -0700 Subject: [PATCH] Fix typo, rename states to spin_states, add tests --- src/ising.jl | 8 ++++---- test/base.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/ising.jl b/src/ising.jl index 306c69a..bbba560 100644 --- a/src/ising.jl +++ b/src/ising.jl @@ -1,12 +1,12 @@ ### Helper Functions for Classical Ising Models ### -function eval_ising_state_energy(state::Vector, ising_model::Dict) +function eval_ising_state_energy(spin_state::Vector, ising_model::Dict) energy = 0.0 for (ids,v) in ising_model val = v for qid in ids - val *= state[qid] + val *= spin_state[qid] end energy += val end @@ -26,7 +26,7 @@ function compute_state_energies(ising_model::Dict) return state_energy end -function print_state_enrgies(ising_model::Dict) +function print_state_energies(ising_model::Dict) state_energies = compute_state_energies(ising_model) energy_levels = Dict{Float64,Set{Int}}() @@ -52,4 +52,4 @@ function print_state_enrgies(ising_model::Dict) end end -end \ No newline at end of file +end diff --git a/test/base.jl b/test/base.jl index 88b3fc9..e6a3771 100644 --- a/test/base.jl +++ b/test/base.jl @@ -39,6 +39,34 @@ end +@testset "ising energy computations" begin + @testset "single qubit, single state" begin + @test isapprox(eval_ising_state_energy([1],Dict((1,) => 1)), 1) + @test isapprox(eval_ising_state_energy([-1],Dict((1,) => 1)), -1) + end + + @testset "single qubit, all states" begin + energies = compute_state_energies(Dict((1,) => 1)) + @test isapprox(energies[0], 1) + @test isapprox(energies[1], -1) + end + + @testset "double qubit, single state" begin + ising_model = Dict((1,) => 1, (2,) => 1, (1,2) => -1) + @test isapprox(eval_ising_state_energy([1, 1], ising_model), 1) + @test isapprox(eval_ising_state_energy([1, -1], ising_model), 1) + @test isapprox(eval_ising_state_energy([-1, 1], ising_model), 1) + @test isapprox(eval_ising_state_energy([-1, -1], ising_model), -3) + end + + @testset "double qubit, all states" begin + energies = compute_state_energies(Dict((1,) => 1, (2,) => 1, (1,2) => -1)) + @test isapprox(energies[0], 1) + @test isapprox(energies[1], 1) + @test isapprox(energies[2], 1) + @test isapprox(energies[3], -3) + end +end @testset "csv annealing schedules" begin s_100 = range(0, 1, length=100)