From 5331823ac10e13500212afbc57757a29261d6bc4 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sun, 2 Feb 2025 10:55:41 -0500 Subject: [PATCH 1/5] Change onehot to oneelement --- Project.toml | 2 +- src/abstractitensor.jl | 18 ++++++++++++++++++ src/quirks.jl | 25 ++++++++++++------------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Project.toml b/Project.toml index b1326a1..feab04f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ITensorBase" uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7" authors = ["ITensor developers and contributors"] -version = "0.1.13" +version = "0.1.14" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/src/abstractitensor.jl b/src/abstractitensor.jl index e6a825f..c2eac47 100644 --- a/src/abstractitensor.jl +++ b/src/abstractitensor.jl @@ -91,6 +91,24 @@ function ITensor(parent::AbstractArray) return ITensor(parent, ()) end +# TODO: +# 1. Generalize this to arbitrary dimensions. +# 2. Define a basic non-ITensor version in `SparseArraysBase.jl`, +# and this constructor can wrap that one. It could construct +# a `OneElement` sparse object like `FillArrays.OneElement` +# (https://juliaarrays.github.io/FillArrays.jl/stable/#FillArrays.OneElement). +# 3. Define `oneelement(value, index::Tuple{Vargarg{Int}}, axes::Tuple{Vararg{Index}})`, +# where the pair version calls out to that one. +function oneelement(elt::Type{<:Number}, iv::Pair{<:Index,<:Int}) + a = ITensor(first(iv)) + a[last(iv)] = one(elt) + return a +end +# TODO: The non-ITensor version should default to `Float64`, +# like `FillArrays.OneElement`. +# TODO: Make the element type `UnspecifiedOne`. +oneelement(iv::Pair{<:Index,<:Int}) = oneelement(Bool, iv) + using Accessors: @set setdenamed(a::ITensor, denamed) = (@set a.parent = denamed) setdenamed!(a::ITensor, denamed) = (a.parent = denamed) diff --git a/src/quirks.jl b/src/quirks.jl index 7cd48e5..e290280 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -1,32 +1,31 @@ # TODO: Define this properly. +# TODO: Rename this to `dual`. dag(i::Index) = i # TODO: Define this properly. +# TODO: Rename this to `dual`. dag(a::ITensor) = a -# TODO: Deprecate. + +# TODO: Deprecate, just use `Int(length(i))` or +# `unname(length(i))` directly. # Conversion to `Int` is used in case the output is named. dim(i::Index) = Int(length(i)) # TODO: Deprecate. # Conversion to `Int` is used in case the output is named. +# TODO: Deprecate, just use `Int(length(i))` or +# `unname(length(i))` directly. dim(a::AbstractITensor) = Int(length(a)) + # TODO: Define this properly. -hasqns(i::Index) = false +# TODO: Maybe rename to `isgraded(i::Index) = isgraded(dename(i))`. +hasqns(::Index) = false # TODO: Define this properly. -hasqns(i::AbstractITensor) = false +# TODO: Maybe rename to `isgraded(a) = all(isgraded, axes(a))`. +hasqns(::AbstractITensor) = false # This seems to be needed to get broadcasting working. # TODO: Investigate this and see if we can get rid of it. Base.Broadcast.extrude(a::AbstractITensor) = a -# TODO: Generalize this. -# Maybe define it as `oneelement`, and base it on -# `FillArrays.OneElement` (https://juliaarrays.github.io/FillArrays.jl/stable/#FillArrays.OneElement). -function onehot(elt::Type{<:Number}, iv::Pair{<:Index,<:Int}) - a = ITensor(first(iv)) - a[last(iv)] = one(elt) - return a -end -onehot(iv::Pair{<:Index,<:Int}) = onehot(Bool, iv) - # TODO: This is just a stand-in for truncated SVD # that only makes use of `maxdim`, just to get some # functionality running in `ITensorMPS.jl`. From 5576de16c9e8dc9ed6f3d5fec0196aa477a71c51 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sun, 2 Feb 2025 10:59:11 -0500 Subject: [PATCH 2/5] Add tests --- test/test_basics.jl | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/test_basics.jl b/test/test_basics.jl index c2c0e1b..fd3c214 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1,5 +1,16 @@ using ITensorBase: - ITensorBase, ITensor, Index, gettag, hastag, inds, plev, prime, settag, tags, unsettag + ITensorBase, + ITensor, + Index, + gettag, + hastag, + inds, + oneelement, + plev, + prime, + settag, + tags, + unsettag using DiagonalArrays: δ, delta, diagview using NamedDimsArrays: dename, name, named using Test: @test, @test_broken, @testset @@ -35,6 +46,27 @@ using Test: @test, @test_broken, @testset @test plev(i) == 0 @test length(tags(i)) == 0 end + @testset "oneelement" begin + i = Index(3) + a = oneelement(i => 2) + @test a isa ITensor + @test ndims(a) == 1 + @test issetequal(inds(a), (i,)) + @test eltype(a) === Bool + @test a[1] == 0 + @test a[2] == 1 + @test a[3] == 0 + + i = Index(3) + a = oneelement(Float32, i => 2) + @test a isa ITensor + @test ndims(a) == 1 + @test issetequal(inds(a), (i,)) + @test eltype(a) === Float32 + @test a[1] == 0 + @test a[2] == 1 + @test a[3] == 0 + end @testset "delta" begin i, j = Index.((2, 2)) for a in ( From baf0d2f464c3869d6e7c4c008051d17cdbae525c Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sun, 2 Feb 2025 21:48:27 -0500 Subject: [PATCH 3/5] Implement `oneelement` in SparseArraysBaseExt --- Project.toml | 3 ++ .../ITensorBaseSparseArraysBaseExt.jl | 13 +++++++ src/abstractitensor.jl | 18 --------- test/Project.toml | 1 + test/test_basics.jl | 38 +++++++++---------- 5 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 ext/ITensorBaseSparseArraysBaseExt/ITensorBaseSparseArraysBaseExt.jl diff --git a/Project.toml b/Project.toml index feab04f..c6e2c31 100644 --- a/Project.toml +++ b/Project.toml @@ -16,9 +16,11 @@ VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8" [weakdeps] DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77" +SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" [extensions] ITensorBaseDiagonalArraysExt = "DiagonalArrays" +ITensorBaseSparseArraysBaseExt = ["NamedDimsArrays", "SparseArraysBase"] [compat] Accessors = "0.1.39" @@ -28,6 +30,7 @@ FillArrays = "1.13.0" LinearAlgebra = "1.10" MapBroadcast = "0.1.5" NamedDimsArrays = "0.4" +SparseArraysBase = "0.2.10" UnallocatedArrays = "0.1.1" UnspecifiedTypes = "0.1.1" VectorInterface = "0.5.0" diff --git a/ext/ITensorBaseSparseArraysBaseExt/ITensorBaseSparseArraysBaseExt.jl b/ext/ITensorBaseSparseArraysBaseExt/ITensorBaseSparseArraysBaseExt.jl new file mode 100644 index 0000000..e8eb709 --- /dev/null +++ b/ext/ITensorBaseSparseArraysBaseExt/ITensorBaseSparseArraysBaseExt.jl @@ -0,0 +1,13 @@ +module ITensorBaseSparseArraysBaseExt + +using ITensorBase: ITensor, Index +using NamedDimsArrays: dename +using SparseArraysBase: SparseArraysBase, oneelement + +function SparseArraysBase.oneelement( + value, index::NTuple{N,Int}, ax::NTuple{N,Index} +) where {N} + return ITensor(oneelement(value, index, only.(axes.(dename.(ax)))), ax) +end + +end diff --git a/src/abstractitensor.jl b/src/abstractitensor.jl index c2eac47..e6a825f 100644 --- a/src/abstractitensor.jl +++ b/src/abstractitensor.jl @@ -91,24 +91,6 @@ function ITensor(parent::AbstractArray) return ITensor(parent, ()) end -# TODO: -# 1. Generalize this to arbitrary dimensions. -# 2. Define a basic non-ITensor version in `SparseArraysBase.jl`, -# and this constructor can wrap that one. It could construct -# a `OneElement` sparse object like `FillArrays.OneElement` -# (https://juliaarrays.github.io/FillArrays.jl/stable/#FillArrays.OneElement). -# 3. Define `oneelement(value, index::Tuple{Vargarg{Int}}, axes::Tuple{Vararg{Index}})`, -# where the pair version calls out to that one. -function oneelement(elt::Type{<:Number}, iv::Pair{<:Index,<:Int}) - a = ITensor(first(iv)) - a[last(iv)] = one(elt) - return a -end -# TODO: The non-ITensor version should default to `Float64`, -# like `FillArrays.OneElement`. -# TODO: Make the element type `UnspecifiedOne`. -oneelement(iv::Pair{<:Index,<:Int}) = oneelement(Bool, iv) - using Accessors: @set setdenamed(a::ITensor, denamed) = (@set a.parent = denamed) setdenamed!(a::ITensor, denamed) = (a.parent = denamed) diff --git a/test/Project.toml b/test/Project.toml index 355bb50..fce2cd3 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -5,6 +5,7 @@ ITensorBase = "4795dd04-0d67-49bb-8f44-b89c448a1dc7" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" +SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/test_basics.jl b/test/test_basics.jl index fd3c214..1683a47 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -5,7 +5,6 @@ using ITensorBase: gettag, hastag, inds, - oneelement, plev, prime, settag, @@ -13,6 +12,7 @@ using ITensorBase: unsettag using DiagonalArrays: δ, delta, diagview using NamedDimsArrays: dename, name, named +using SparseArraysBase: oneelement using Test: @test, @test_broken, @testset @testset "ITensorBase" begin @@ -46,6 +46,24 @@ using Test: @test, @test_broken, @testset @test plev(i) == 0 @test length(tags(i)) == 0 end + @testset "delta" begin + i, j = Index.((2, 2)) + for a in ( + delta(i, j), + delta(Bool, i, j), + delta((i, j)), + delta(Bool, (i, j)), + δ(i, j), + δ(Bool, i, j), + δ((i, j)), + δ(Bool, (i, j)), + ) + @test eltype(a) === Bool + # TODO: Fix this. + @test_broken diagview(a) + @test diagview(dename(a)) == ones(2) + end + end @testset "oneelement" begin i = Index(3) a = oneelement(i => 2) @@ -67,22 +85,4 @@ using Test: @test, @test_broken, @testset @test a[2] == 1 @test a[3] == 0 end - @testset "delta" begin - i, j = Index.((2, 2)) - for a in ( - delta(i, j), - delta(Bool, i, j), - delta((i, j)), - delta(Bool, (i, j)), - δ(i, j), - δ(Bool, i, j), - δ((i, j)), - δ(Bool, (i, j)), - ) - @test eltype(a) === Bool - # TODO: Fix this. - @test_broken diagview(a) - @test diagview(dename(a)) == ones(2) - end - end end From 387bb9eb5c362355ca79ad51be22162c2a0d2805 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sun, 2 Feb 2025 21:53:15 -0500 Subject: [PATCH 4/5] Bump requires SparseArraysBase version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c6e2c31..028d514 100644 --- a/Project.toml +++ b/Project.toml @@ -30,7 +30,7 @@ FillArrays = "1.13.0" LinearAlgebra = "1.10" MapBroadcast = "0.1.5" NamedDimsArrays = "0.4" -SparseArraysBase = "0.2.10" +SparseArraysBase = "0.2.11" UnallocatedArrays = "0.1.1" UnspecifiedTypes = "0.1.1" VectorInterface = "0.5.0" From 917cf559cf4aaaed9c9e950aee4c3af9e650f80f Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sun, 2 Feb 2025 21:57:19 -0500 Subject: [PATCH 5/5] Format --- test/test_basics.jl | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/test/test_basics.jl b/test/test_basics.jl index 1683a47..b19c892 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1,15 +1,5 @@ using ITensorBase: - ITensorBase, - ITensor, - Index, - gettag, - hastag, - inds, - plev, - prime, - settag, - tags, - unsettag + ITensorBase, ITensor, Index, gettag, hastag, inds, plev, prime, settag, tags, unsettag using DiagonalArrays: δ, delta, diagview using NamedDimsArrays: dename, name, named using SparseArraysBase: oneelement