Skip to content

Implement KrylovKit.eigsolve for Tensors #23

Open
jofrevalles wants to merge 3 commits intomasterfrom
feature/KrylovKit_ext
Open

Implement KrylovKit.eigsolve for Tensors #23
jofrevalles wants to merge 3 commits intomasterfrom
feature/KrylovKit_ext

Conversation

@jofrevalles
Copy link
Member

@jofrevalles jofrevalles commented Jun 27, 2025

Summary

This PR extends the KrylovKit.eigsolve function for Tensors. We added some tests to cover this function too.

Example

julia> using KrylovKit

julia> using Muscle; using LinearAlgebra; using Test

julia> A = rand(ComplexF64, 4, 4)
4×4 Matrix{ComplexF64}: ...

julia> data = (A + A') / 2 # Make it Hermitian

julia> tensor = Tensor(data, [Index(:i), Index(:j)])
4×4 Tensor{ComplexF64, 2, Matrix{ComplexF64}}: ..

julia> vals, vecs, info = eigsolve(tensor, Tensor(rand(ComplexF64, 4), [Index(:i)]), 4, :SR, Lanczos(; krylovdim=4, tol=1e-16); left_inds=[Index(:i)], right_inds=[Index(:j)]) # Perform eigensolve
...

julia> V_matrix = hcat([reshape(parent(vec), :) for vec in vecs]...) # Convert vecs to matrix form for reconstruction
4×4 Matrix{ComplexF64}: ...

julia> D_matrix = Diagonal(vals)
4×4 Diagonal{Float64, Vector{Float64}}:
 -0.573245   ⋅          ⋅         ⋅ 
   ⋅        0.0709279   ⋅         ⋅ 
   ⋅         ⋅         0.430396   ⋅ 
   ⋅         ⋅          ⋅        2.32354

julia> reconstructed_matrix = V_matrix * D_matrix * inv(V_matrix)
4×4 Matrix{ComplexF64}: ...

julia> @test isapprox(reconstructed_matrix, parent(tensor))
Test Passed

@jofrevalles jofrevalles requested review from Copilot, mofeing and starsfordummies and removed request for mofeing and starsfordummies June 27, 2025 10:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR extends the KrylovKit.eigsolve functionality to support Tensor types and adds integration tests for this new feature.

  • Implement Tensor reshaping and permutation logic before eigendecomposition.
  • Add integration tests covering both default and Lanczos algorithm usage.
  • Include the new Tensor functionality in the test suite.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/runtests.jl Added integration test inclusion for KrylovKit.
test/integration/KrylovKit_test.jl Added integration tests for eigsolve with various Tensor scenarios.
ext/MuscleKrylovKitExt.jl Introduced Tensor reshaping utility and eigsolve overloads for Tensors.

"The initial guess must have the same left indices as the tensor, but got $(inds(x₀)) and $left_inds."
),
)
prod(size.((x₀,), left_inds)) != prod_left_sizes && throw(
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message references '$prod_x₀_sizes' which is not defined. Consider computing prod_x₀_sizes (e.g., prod_x₀_sizes = prod(size.((x₀,), left_inds))) before using it in the error message.

Suggested change
prod(size.((x₀,), left_inds)) != prod_left_sizes && throw(
prod_x₀_sizes = prod(size.((x₀,), left_inds))
prod_x₀_sizes != prod_left_sizes && throw(

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hola copilot, sí que està definit... Les IA encara no esteu preparades per ser reviewers...

@starsfordummies
Copy link
Contributor

nice! next step would be to get it to work with sergio's dmrg example (using a function to compute H\psi instead of storing H, that should be al least a cost cut chi^4 -> chi^3

@mofeing
Copy link
Member

mofeing commented Jun 30, 2025

After reading the documentation of KrylovKit.jl, this package extension is not needed and actually goes against the recommended usage. Instead, I made Tensor implement the "Vector interface" from VectorInterface.jl in f3f78d6 and works flawlessly, also for cases where the effective Hamiltonian is factorized.

I'm not yet closing this PR because the tests are still useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants