From 54d5d10c7b94b3e41807feb8d1f042a52ec262e6 Mon Sep 17 00:00:00 2001 From: sanderdemeyer Date: Fri, 26 Jun 2026 17:36:20 +0200 Subject: [PATCH 1/4] Exponential via MatrixAlgebraKit --- src/tensors/linalg.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tensors/linalg.jl b/src/tensors/linalg.jl index 40a0e0024..c45c9f4f6 100644 --- a/src/tensors/linalg.jl +++ b/src/tensors/linalg.jl @@ -421,7 +421,16 @@ function exp!(t::TensorMap) domain(t) == codomain(t) || error("Exponential of a tensor only exist when domain == codomain.") for (c, b) in blocks(t) - copy!(b, LinearAlgebra.exp!(b)) + MatrixAlgebraKit.exponential!(b, b) + end + return t +end + +function exp!((τ, t)::Tuple{Number, TensorMap}) + domain(t) == codomain(t) || + error("Exponential of a tensor only exist when domain == codomain.") + for (c, b) in blocks(t) + MatrixAlgebraKit.exponential!((τ, b), b) end return t end From caab0e48fc066979c915035c3ac9991d4db4caf9 Mon Sep 17 00:00:00 2001 From: sanderdemeyer Date: Fri, 26 Jun 2026 17:58:15 +0200 Subject: [PATCH 2/4] include case where t is real and tau is complex --- src/tensors/linalg.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tensors/linalg.jl b/src/tensors/linalg.jl index c45c9f4f6..f07f1b174 100644 --- a/src/tensors/linalg.jl +++ b/src/tensors/linalg.jl @@ -429,6 +429,13 @@ end function exp!((τ, t)::Tuple{Number, TensorMap}) domain(t) == codomain(t) || error("Exponential of a tensor only exist when domain == codomain.") + if eltype(t) <: Real && eltype(τ) <: Complex + t_complex = complex(t) + for ((cr, br), (cc, bc)) in zip(blocks(t), blocks(t_complex)) + MatrixAlgebraKit.exponential!((τ, br), bc) + end + return t_complex + end for (c, b) in blocks(t) MatrixAlgebraKit.exponential!((τ, b), b) end From 863ca6d28b709f16caafa96ab866b652d53c8214 Mon Sep 17 00:00:00 2001 From: sanderdemeyer Date: Fri, 26 Jun 2026 18:43:27 +0200 Subject: [PATCH 3/4] Add test --- test/tensors/exponential.jl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/tensors/exponential.jl diff --git a/test/tensors/exponential.jl b/test/tensors/exponential.jl new file mode 100644 index 000000000..cb5befb4e --- /dev/null +++ b/test/tensors/exponential.jl @@ -0,0 +1,25 @@ +using Test, TestExtras +using TensorKit +using Random + +spaces = [ℂ^4, Vect[U1Irrep](0 => 1, 1 => 2), Vect[SU2Irrep](0 => 1, 1//2 => 1)] +scalartypes = [Float64, ComplexF64] + +@timedtestset "exp!(τ,A) for $space, scalartype(A) = $st1, scalartype(τ) = $st2" for space = spaces, st1 = scalartypes, st2 = scalartypes + A = randn(st1, space, space) + τ = rand(st2) + + @test exp!(copy(A)) == exp!((1.0, copy(A))) + + A2 = exp!((τ,A)) + if st1 <: Real && st2 <: Complex + @test objectid(A2) != objectid(A) + else + @test objectid(A2) == objectid(A) + end + + expτA = exp!((τ,copy(A))) + expminτA = exp!((-τ,copy(A))) + @test expτA * expminτA ≈ id(scalartype(expτA), space) + @test expτA ≈ inv(expminτA) +end From bf8c3af6a3c254d21dbd599456c3d730668b00fc Mon Sep 17 00:00:00 2001 From: sanderdemeyer Date: Fri, 26 Jun 2026 18:44:06 +0200 Subject: [PATCH 4/4] Fix formatting --- test/tensors/exponential.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/tensors/exponential.jl b/test/tensors/exponential.jl index cb5befb4e..51fe1072b 100644 --- a/test/tensors/exponential.jl +++ b/test/tensors/exponential.jl @@ -2,24 +2,24 @@ using Test, TestExtras using TensorKit using Random -spaces = [ℂ^4, Vect[U1Irrep](0 => 1, 1 => 2), Vect[SU2Irrep](0 => 1, 1//2 => 1)] +spaces = [ℂ^4, Vect[U1Irrep](0 => 1, 1 => 2), Vect[SU2Irrep](0 => 1, 1 // 2 => 1)] scalartypes = [Float64, ComplexF64] -@timedtestset "exp!(τ,A) for $space, scalartype(A) = $st1, scalartype(τ) = $st2" for space = spaces, st1 = scalartypes, st2 = scalartypes +@timedtestset "exp!(τ,A) for $space, scalartype(A) = $st1, scalartype(τ) = $st2" for space in spaces, st1 in scalartypes, st2 in scalartypes A = randn(st1, space, space) τ = rand(st2) @test exp!(copy(A)) == exp!((1.0, copy(A))) - A2 = exp!((τ,A)) + A2 = exp!((τ, A)) if st1 <: Real && st2 <: Complex @test objectid(A2) != objectid(A) else @test objectid(A2) == objectid(A) end - expτA = exp!((τ,copy(A))) - expminτA = exp!((-τ,copy(A))) + expτA = exp!((τ, copy(A))) + expminτA = exp!((-τ, copy(A))) @test expτA * expminτA ≈ id(scalartype(expτA), space) @test expτA ≈ inv(expminτA) end