QuantumKitHub / MPSKit.jl

A Julia package dedicated to simulating quantum many-body systems using Matrix Product States (MPS)
MIT License
136 stars 30 forks source link

Possible `MethodError` when running `excitations(H,alg::FiniteExcited, states)` #89

Closed DaanMaertens closed 11 months ago

DaanMaertens commented 11 months ago

When running finite excitation it is possible to get the following error

MethodError: no method matching expectation_value(::FiniteMPS{TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 1, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}}, ::MPSKit.LinearCombination{Tuple{MPOHamiltonian{GradedSpace{PlanarTrivial, Tuple{Int64}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 2, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}}, ComplexF64}, MPSKit.ProjectionOperator{FiniteMPS{TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 1, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}}}}, Tuple{Float64, Float64}}, ::MPSKit.LazyLincoCache{MPSKit.LinearCombination{Tuple{MPOHamiltonian{GradedSpace{PlanarTrivial, Tuple{Int64}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 2, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}}, ComplexF64}, MPSKit.ProjectionOperator{FiniteMPS{TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 1, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}}}}, Tuple{Float64, Float64}}, Tuple{MPSKit.FinEnv{Nothing, MPOHamiltonian{GradedSpace{PlanarTrivial, Tuple{Int64}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 2, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}}, ComplexF64}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}, Vector{TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}}}, MPSKit.FinEnv{FiniteMPS{TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 1, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}}, Vector{Nothing}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 2, 1, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 2, 0, 1, Nothing}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}}, TensorMap{GradedSpace{PlanarTrivial, Tuple{Int64}}, 1, 3, PlanarTrivial, TensorKit.SortedVectorDict{PlanarTrivial, Matrix{ComplexF64}}, FusionTree{PlanarTrivial, 1, 0, 0, Nothing}, FusionTree{PlanarTrivial, 3, 1, 2, Nothing}}}}})

The error details that there does not exist a method for expectation_value(::FiniteMPS,::MPSKit.LinearCombination,::MPSKit.LazyLincoCache). More precisely the error stems from this part inside dmrgexciation.jl

super_op = LinearCombination(
        tuple(H, ProjectionOperator.(states)...),
        tuple(1.0, broadcast(x -> alg.weight, states)...),
    )
    envs = environments(init, super_op)
    ne, _ = find_groundstate(init, super_op, alg.gsalg, envs)

When the maximum amount of iterations is reached find_groundstate warns the users with a message that contains expectation_value of that state with H. In this case H= super_op is a LinearCombination so it tries to calculate this but fails because this method is not defined. A possible solution is to define

function expectation_value(Ψ, H::LinearCombination, envs::LazyLincoCache=environments(Ψ,H))
    return return sum(((c, op, env),) -> c * expectation_value(Ψ, op, env), zip(H.coeffs,H.opps, envs.envs))
end

However this causes again an error when it tries to do expectation_value(Ψ, ::MPSKit.ProjectionOperator, ::MPSKit.FinEnv) for which there also doesn't exist any method. I don't know how expectation_value should be defined for a ProjectionOperator and at this point it seems easier to just suppress the warning that causes the error in the first place.

lkdvos commented 11 months ago

I think it is fine to define expectation_value(psi::FiniteMPS, H::ProjectionOperator), as H = |psi0><psi0| this should just be the overlap between the two states (squared?). I'll look into actually implementing it tomorrow or later this week. I would however have to investigate if this is a useful metric to print anyways, as this arises in the context of excitations and this is not necessarily the excitation energy (I think?)