From a788148a595a916d81499691719ed5189d854404 Mon Sep 17 00:00:00 2001 From: GiggleLiu Date: Sat, 14 Dec 2024 02:17:17 +0800 Subject: [PATCH] fix tests --- test/dragondrop.jl | 14 ++++++++------ test/reduceto.jl | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/test/dragondrop.jl b/test/dragondrop.jl index 0bc00bb..0fd30d8 100644 --- a/test/dragondrop.jl +++ b/test/dragondrop.jl @@ -14,11 +14,12 @@ using Random r1 = solve(GenericTensorNetwork(IndependentSet(graph, weights)), SingleConfigMax())[] J2 = vcat([Float64[J[i,j] for j=i+1:n] for i=1:n]...) # note the different sign convention of J - r2 = solve(GenericTensorNetwork(SpinGlass(complete_graph(n), -J2, H)), SingleConfigMax())[] + sg = SpinGlass(complete_graph(n), -J2, H) + r2 = solve(GenericTensorNetwork(sg), SingleConfigMax())[] @test r1.n - qubo.mis_overhead ≈ r2.n @test r1.n % 1 ≈ r2.n % 1 c1 = map_config_back(qubo, r1.c.data) - @test spinglass_energy(complete_graph(n), c1; J=-J2, h=H) ≈ spinglass_energy(complete_graph(n), r2.c.data; J=-J2, h=H) + @test GenericTensorNetworks.energy(sg, 1 .- 2 .* Int.(c1)) ≈ GenericTensorNetworks.energy(sg, 1 .- 2 .* Int.(r2.c.data)) #display(MappingGrid(UnitDiskMapping.CopyLine[], 0, qubo)) end @@ -56,7 +57,7 @@ end add_edge!(g2, (i-1)*n+j, (i2-1)*n+j2) push!(weights, J) end - r2 = solve(GenericTensorNetwork(SpinGlass(g2, -weights)), SingleConfigMax())[] + r2 = solve(GenericTensorNetwork(SpinGlass(g2, -weights, zeros(Int, n*n))), SingleConfigMax())[] @show r1, r2 end @@ -88,9 +89,10 @@ end for (i,j,h) in onsite hs[i+(j-1)*m] = h end - r2 = solve(GenericTensorNetwork(SpinGlass(g2, -Js, hs)), SingleConfigMax())[] + sg = SpinGlass(g2, -Js, hs) + r2 = solve(GenericTensorNetwork(sg), SingleConfigMax())[] @test r1.n - qubo.mis_overhead ≈ r2.n c1 = map_config_back(qubo, collect(Int,r1.c.data)) c2 = collect(r2.c.data) - @test spinglass_energy(g2, c1; J=-Js, h=hs) ≈ spinglass_energy(g2, c2; J=-Js, h=hs) -end \ No newline at end of file + @test GenericTensorNetworks.energy(sg, 1 .- 2 .* Int.(c1)) ≈ GenericTensorNetworks.energy(sg, 1 .- 2 .* Int.(c2)) +end diff --git a/test/reduceto.jl b/test/reduceto.jl index 319ef6c..c3b35a8 100644 --- a/test/reduceto.jl +++ b/test/reduceto.jl @@ -26,14 +26,14 @@ end ] @info "Testing reduction from $(typeof(source)) to $(target_type)" # directly solve - best_source = ProblemReductions.findbest(source, ProblemReductions.BruteForce()) + solver = GTNSolver(optimizer=TreeSA(ntrials=1)) + best_source = ProblemReductions.findbest(source, source isa ConstraintSatisfactionProblem ? solver : ProblemReductions.BruteForce()) # reduce and solve result = ProblemReductions.reduceto(target_type, source) target = ProblemReductions.target_problem(result) @test target isa target_type - #best_target = findbest(target, BruteForce()) - best_target = GenericTensorNetworks.solve(GenericTensorNetwork(GenericTensorNetworks.IndependentSet(SimpleGraph(target.graph), collect(target.weights))), ConfigsMax())[].c.data + best_target = ProblemReductions.findbest(IndependentSet(SimpleGraph(target.graph), collect(target.weights)), solver) # extract the solution best_source_extracted_single = unique( ProblemReductions.extract_solution.(Ref(result), best_target) )