GenericTensorNetworks.jl icon indicating copy to clipboard operation
GenericTensorNetworks.jl copied to clipboard

[BUG] Unexpected behavior of option `ConfigsMax` in solving MIS

Open fliingelephant opened this issue 10 months ago • 2 comments

The following code solves MIS on a 10x10 king's graph

using GenericTensorNetworks
using Graphs
using Yao.YaoBlocks

Lx = Ly = 10
N = Lx * Ly
unit = 3.0
atoms = [(unit * ((i - 1) % Lx), unit * ((i - 1) ÷ Lx)) for i in 1:Lx*Ly]

g = SimpleGraph(N)
for i=1:N, j=i+1:N
    if sum(abs2, atoms[i] .- atoms[j]) <= unit ^ 2
        add_edge!(g, i, j)
    end
end
mis_problem = IndependentSet(g, ones(N));
configs_mapped = GenericTensorNetworks.solve(GenericTensorNetwork(mis_problem;), ConfigsMax())[];
MIS_config = YaoBlocks.BitStr{length(atoms)}(configs_mapped.c[1].data[1])
for i in 1:Ly
    println(MIS_config[(i-1)*Lx+1:i*Lx])
end

with an unreasonable output (the last few rows are all zero):

[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

It seems that the ConfigsMax() option does not operate correctly.

fliingelephant avatar Jun 12 '25 11:06 fliingelephant

I think the issue is probably related to YaoBlocks.BitStr, it uses Int64 as the default bit storage. The result itself looks fine.

julia> configs_mapped
(50.0, {0101010101101010101001010101011010101010010101010110101010100101010101101010101001010101011010101010, 1010101010010101010110101010100101010101101010101001010101011010101010010101010110101010100101010101})ₜ

GiggleLiu avatar Jun 16 '25 09:06 GiggleLiu

To fix your issue, you can use:

@testset "issue #99" begin
    Lx = Ly = 10
    N = Lx * Ly
    unit = 3.0
    atoms = [(unit * ((i - 1) % Lx), unit * ((i - 1) ÷ Lx)) for i in 1:Lx*Ly]

    g = SimpleGraph(N)
    for i=1:N, j=i+1:N
        if sum(abs2, atoms[i] .- atoms[j]) <= unit ^ 2
            add_edge!(g, i, j)
        end
    end
    mis_problem = IndependentSet(g, ones(N));
    configs_mapped = GenericTensorNetworks.solve(GenericTensorNetwork(mis_problem;), ConfigsMax())[];
    TYPE = YaoBlocks.BitBasis.longinttype(100, 2)
    mis_config = YaoBlocks.BitStr{length(atoms)}(TYPE(configs_mapped.c[1].data))
    for i in 1:Ly
        println(mis_config[(i-1)*Lx+1:i*Lx])
    end
end

The longinttype will return you a proper long integer type for storing 100 2-level system configurations:

julia> YaoBlocks.BitBasis.longinttype(100, 2)
LongLongUInt{2}

GiggleLiu avatar Jun 16 '25 09:06 GiggleLiu