using TensorKit 
using statmech_tm_solver
using TensorOperations
using LinearAlgebra

io = open("result_triangular_ising_fulldiag.txt", "w")
close(io)
T = mpo_triangular_AF_ising()

T5 = ncon([T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 1]])
T5 = permute(T5, (1, 3, 5, 7, 9), (2, 4, 6, 8, 10))

T6 = ncon([T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 1]])
T6 = permute(T6, (1, 3, 5, 7, 9, 11), (2, 4, 6, 8, 10, 12))

T7 = ncon([T, T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 7], [7, -13, -14, 1]])
T7 = permute(T7, (1, 3, 5, 7, 9, 11, 13), (2, 4, 6, 8, 10, 12, 14))

T8 = ncon([T, T, T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 7], [7, -13, -14, 8], [8, -15, -16, 1]])
T8 = permute(T8, (1, 3, 5, 7, 9, 11, 13, 15), (2, 4, 6, 8, 10, 12, 14, 16))

T9 = ncon([T, T, T, T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 7], [7, -13, -14, 8], [8, -15, -16, 9], [9, -17, -18, 1]])
T9 = permute(T9, (1, 3, 5, 7, 9, 11, 13, 15, 17), (2, 4, 6, 8, 10, 12, 14, 16, 18))

T10 = ncon([T, T, T, T, T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 7], [7, -13, -14, 8], [8, -15, -16, 9], [9, -17, -18, 10], [10, -19, -20, 1]])
T10 = permute(T10, (1, 3, 5, 7, 9, 11, 13, 15, 17, 19), (2, 4, 6, 8, 10, 12, 14, 16, 18, 20))

T11 = ncon([T, T, T, T, T, T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 7], [7, -13, -14, 8], [8, -15, -16, 9], [9, -17, -18, 10], [10, -19, -20, 11], [11, -21, -22, 1]])
T11 = permute(T11, (1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21), (2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22))

T12 = ncon([T, T, T, T, T, T, T, T, T, T, T, T], [[1, -1, -2, 2], [2, -3, -4, 3], [3, -5, -6, 4], [4, -7, -8, 5], [5, -9, -10, 6], [6, -11, -12, 7], [7, -13, -14, 8], [8, -15, -16, 9], [9, -17, -18, 10], [10, -19, -20, 11], [11, -21, -22, 12], [12, -23, -24, 1]])
T12 = permute(T12, (1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23), (2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24))

for (n, Tn) in zip([5, 6, 7, 8, 9, 10, 11, 12], [T5, T6, T7, T8, T9, T10, T11, T12])
    w, _ = eig(Tn)
    w = diag(w.data)
    w_reals = real.(w)
    w_imags = imag.(w)

    io = open("result_triangular_ising_fulldiag.txt", "a+")
    for (w_real, w_imag) in zip(w_reals, w_imags)
        println(io, "$(n)   $(w_real)   $(w_imag) ")
    end
    close(io)
end

w, v = eig(T12);
space(v)
v0 = v.data[:, 4096]

w.data[4096, 4096]
λs=diag(w.data)

findmax(imag.(λs))

v11 = v.data[:, 11]

findmax(norm.(v0))

findall(>(0.03), norm.(v11))

v11[220]

