From d918f86bc98e457f37b4f25fce470fcdb74c0041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Tue, 25 Feb 2025 13:53:44 -0500 Subject: [PATCH 1/4] relax TensorAlgebra dep --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 96944c68..acfe01fe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.2.19" +version = "0.2.20" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" @@ -45,7 +45,7 @@ MacroTools = "0.5.13" MapBroadcast = "0.1.5" SparseArraysBase = "0.2.10" SplitApplyCombine = "1.2.3" -TensorAlgebra = "0.1.0" +TensorAlgebra = "0.1.0, 0.2" Test = "1.10" TypeParameterAccessors = "0.2.0, 0.3" julia = "1.10" From ba45d6bfb210fd68e34e2549c8c2dc2ab57a0129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Wed, 26 Feb 2025 16:31:04 -0500 Subject: [PATCH 2/4] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index acfe01fe..d4d58c8a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.2.20" +version = "0.2.21" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" From 0d5dd0a60f2d8841fded2939b1f81f9ae5085ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Thu, 27 Feb 2025 19:31:08 -0500 Subject: [PATCH 3/4] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d4d58c8a..be2fedac 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.2.21" +version = "0.2.22" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" From 1407021f91a9835a3d7b4a59afe6495cbf9a220e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Fri, 28 Feb 2025 17:17:08 -0500 Subject: [PATCH 4/4] move TensorAlgebra test here --- Project.toml | 2 +- test/test_tensoralgebraext.jl | 139 ++++++++++++++++++++++++++++++---- 2 files changed, 127 insertions(+), 14 deletions(-) diff --git a/Project.toml b/Project.toml index be2fedac..673742b4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.2.22" +version = "0.2.23" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/test/test_tensoralgebraext.jl b/test/test_tensoralgebraext.jl index 06dff458..470efaad 100644 --- a/test/test_tensoralgebraext.jl +++ b/test/test_tensoralgebraext.jl @@ -1,17 +1,130 @@ -@eval module $(gensym()) -using Test: @test, @testset +using Random: randn! +using Test: @test, @test_broken, @testset + +using BlockArrays: Block, BlockArray, BlockedArray, blockedrange, blocksize + using BlockSparseArrays: BlockSparseArray +using GradedUnitRanges: dual, gradedrange +using SymmetrySectors: U1 using TensorAlgebra: contract -using SparseArraysBase: densearray -@testset "BlockSparseArraysTensorAlgebraExt (eltype=$elt)" for elt in ( - Float32, Float64, Complex{Float32}, Complex{Float64} -) - a1 = BlockSparseArray{elt}([1, 2], [2, 3], [3, 2]) - a2 = BlockSparseArray{elt}([2, 2], [3, 2], [2, 3]) - a_dest, dimnames_dest = contract(a1, (1, -1, -2), a2, (2, -2, -1)) - a_dest_dense, dimnames_dest_dense = contract( - densearray(a1), (1, -1, -2), densearray(a2), (2, -2, -1) - ) - @test a_dest ≈ a_dest_dense + +function randn_blockdiagonal(elt::Type, axes::Tuple) + a = BlockSparseArray{elt}(axes) + blockdiaglength = minimum(blocksize(a)) + for i in 1:blockdiaglength + b = Block(ntuple(Returns(i), ndims(a))) + a[b] = randn!(a[b]) + end + return a end + +const elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) +@testset "`contract` `BlockSparseArray` (eltype=$elt)" for elt in elts + @testset "BlockedOneTo" begin + d = blockedrange([2, 3]) + a1 = randn_blockdiagonal(elt, (d, d, d, d)) + a2 = randn_blockdiagonal(elt, (d, d, d, d)) + a3 = randn_blockdiagonal(elt, (d, d)) + a1_dense = convert(Array, a1) + a2_dense = convert(Array, a2) + a3_dense = convert(Array, a3) + + # matrix matrix + a_dest, dimnames_dest = contract(a1, (1, -1, 2, -2), a2, (2, -3, 1, -4)) + a_dest_dense, dimnames_dest_dense = contract( + a1_dense, (1, -1, 2, -2), a2_dense, (2, -3, 1, -4) + ) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + + # matrix vector + @test_broken a_dest, dimnames_dest = contract(a1, (2, -1, -2, 1), a3, (1, 2)) + #= + a_dest_dense, dimnames_dest_dense = contract(a1_dense, (2, -1, -2, 1), a3_dense, (1, 2)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + =# + + # vector matrix + a_dest, dimnames_dest = contract(a3, (1, 2), a1, (2, -1, -2, 1)) + a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a1_dense, (2, -1, -2, 1)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + + # vector vector + a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (2, 1)) + a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray{elt,0} + @test a_dest ≈ a_dest_dense + + # outer product + a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (3, 4)) + a_dest, dimnames_dest = contract(a3, (1, 2), a3, (3, 4)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + end + + @testset "GradedOneTo with U(1)" begin + d = gradedrange([U1(0) => 2, U1(1) => 3]) + a1 = randn_blockdiagonal(elt, (d, d, dual(d), dual(d))) + a2 = randn_blockdiagonal(elt, (d, d, dual(d), dual(d))) + a3 = randn_blockdiagonal(elt, (d, dual(d))) + a1_dense = convert(Array, a1) + a2_dense = convert(Array, a2) + a3_dense = convert(Array, a3) + + # matrix matrix + a_dest, dimnames_dest = contract(a1, (1, -1, 2, -2), a2, (2, -3, 1, -4)) + a_dest_dense, dimnames_dest_dense = contract( + a1_dense, (1, -1, 2, -2), a2_dense, (2, -3, 1, -4) + ) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + + # matrix vector + @test_broken a_dest, dimnames_dest = contract(a1, (2, -1, -2, 1), a3, (1, 2)) + #= + a_dest_dense, dimnames_dest_dense = contract(a1_dense, (2, -1, -2, 1), a3_dense, (1, 2)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + =# + + # vector matrix + a_dest, dimnames_dest = contract(a3, (1, 2), a1, (2, -1, -2, 1)) + a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a1_dense, (2, -1, -2, 1)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + + # vector vector + a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1)) + a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (2, 1)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray{elt,0} + @test a_dest ≈ a_dest_dense + + # outer product + a_dest, dimnames_dest = contract(a3, (1, 2), a3, (3, 4)) + a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (3, 4)) + @test dimnames_dest == dimnames_dest_dense + @test size(a_dest) == size(a_dest_dense) + @test a_dest isa BlockSparseArray + @test a_dest ≈ a_dest_dense + end end