Skip to content

Commit 7a1ed53

Browse files
authored
Change onehot to oneelement (#26)
1 parent e9f1be2 commit 7a1ed53

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorBase"
22
uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.1.13"
4+
version = "0.1.14"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
@@ -16,9 +16,11 @@ VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"
1616

1717
[weakdeps]
1818
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
19+
SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
1920

2021
[extensions]
2122
ITensorBaseDiagonalArraysExt = "DiagonalArrays"
23+
ITensorBaseSparseArraysBaseExt = ["NamedDimsArrays", "SparseArraysBase"]
2224

2325
[compat]
2426
Accessors = "0.1.39"
@@ -28,6 +30,7 @@ FillArrays = "1.13.0"
2830
LinearAlgebra = "1.10"
2931
MapBroadcast = "0.1.5"
3032
NamedDimsArrays = "0.4"
33+
SparseArraysBase = "0.2.11"
3134
UnallocatedArrays = "0.1.1"
3235
UnspecifiedTypes = "0.1.1"
3336
VectorInterface = "0.5.0"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ITensorBaseSparseArraysBaseExt
2+
3+
using ITensorBase: ITensor, Index
4+
using NamedDimsArrays: dename
5+
using SparseArraysBase: SparseArraysBase, oneelement
6+
7+
function SparseArraysBase.oneelement(
8+
value, index::NTuple{N,Int}, ax::NTuple{N,Index}
9+
) where {N}
10+
return ITensor(oneelement(value, index, only.(axes.(dename.(ax)))), ax)
11+
end
12+
13+
end

src/quirks.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
# TODO: Define this properly.
2+
# TODO: Rename this to `dual`.
23
dag(i::Index) = i
34
# TODO: Define this properly.
5+
# TODO: Rename this to `dual`.
46
dag(a::ITensor) = a
5-
# TODO: Deprecate.
7+
8+
# TODO: Deprecate, just use `Int(length(i))` or
9+
# `unname(length(i))` directly.
610
# Conversion to `Int` is used in case the output is named.
711
dim(i::Index) = Int(length(i))
812
# TODO: Deprecate.
913
# Conversion to `Int` is used in case the output is named.
14+
# TODO: Deprecate, just use `Int(length(i))` or
15+
# `unname(length(i))` directly.
1016
dim(a::AbstractITensor) = Int(length(a))
17+
1118
# TODO: Define this properly.
12-
hasqns(i::Index) = false
19+
# TODO: Maybe rename to `isgraded(i::Index) = isgraded(dename(i))`.
20+
hasqns(::Index) = false
1321
# TODO: Define this properly.
14-
hasqns(i::AbstractITensor) = false
22+
# TODO: Maybe rename to `isgraded(a) = all(isgraded, axes(a))`.
23+
hasqns(::AbstractITensor) = false
1524

1625
# This seems to be needed to get broadcasting working.
1726
# TODO: Investigate this and see if we can get rid of it.
1827
Base.Broadcast.extrude(a::AbstractITensor) = a
1928

20-
# TODO: Generalize this.
21-
# Maybe define it as `oneelement`, and base it on
22-
# `FillArrays.OneElement` (https://juliaarrays.github.io/FillArrays.jl/stable/#FillArrays.OneElement).
23-
function onehot(elt::Type{<:Number}, iv::Pair{<:Index,<:Int})
24-
a = ITensor(first(iv))
25-
a[last(iv)] = one(elt)
26-
return a
27-
end
28-
onehot(iv::Pair{<:Index,<:Int}) = onehot(Bool, iv)
29-
3029
# TODO: This is just a stand-in for truncated SVD
3130
# that only makes use of `maxdim`, just to get some
3231
# functionality running in `ITensorMPS.jl`.

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ITensorBase = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
55
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
66
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
77
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
8+
SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
89
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
910
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1011

test/test_basics.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using ITensorBase:
22
ITensorBase, ITensor, Index, gettag, hastag, inds, plev, prime, settag, tags, unsettag
33
using DiagonalArrays: δ, delta, diagview
44
using NamedDimsArrays: dename, name, named
5+
using SparseArraysBase: oneelement
56
using Test: @test, @test_broken, @testset
67

78
@testset "ITensorBase" begin
@@ -53,4 +54,25 @@ using Test: @test, @test_broken, @testset
5354
@test diagview(dename(a)) == ones(2)
5455
end
5556
end
57+
@testset "oneelement" begin
58+
i = Index(3)
59+
a = oneelement(i => 2)
60+
@test a isa ITensor
61+
@test ndims(a) == 1
62+
@test issetequal(inds(a), (i,))
63+
@test eltype(a) === Bool
64+
@test a[1] == 0
65+
@test a[2] == 1
66+
@test a[3] == 0
67+
68+
i = Index(3)
69+
a = oneelement(Float32, i => 2)
70+
@test a isa ITensor
71+
@test ndims(a) == 1
72+
@test issetequal(inds(a), (i,))
73+
@test eltype(a) === Float32
74+
@test a[1] == 0
75+
@test a[2] == 1
76+
@test a[3] == 0
77+
end
5678
end

0 commit comments

Comments
 (0)