Skip to content

Commit

Permalink
Fix typo, rename states to spin_states, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zmorrell committed Feb 10, 2022
1 parent 1fae3b2 commit d63f7bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ising.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}}()
Expand All @@ -52,4 +52,4 @@ function print_state_enrgies(ising_model::Dict)
end
end

end
end
28 changes: 28 additions & 0 deletions test/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d63f7bf

Please sign in to comment.