Skip to content

Commit 8446a73

Browse files
authored
Start adding support for automatic allocation (#2)
1 parent cb5d932 commit 8446a73

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

Project.toml

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
name = "ITensorBase"
22
uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
7+
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
78
BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2"
9+
Derive = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a"
10+
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
811
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
12+
UnallocatedArrays = "43c9e47c-e622-40fb-bf18-a09fc8c466b6"
13+
UnspecifiedTypes = "42b3faec-625b-4613-8ddc-352bf9672b8d"
914

1015
[compat]
16+
Accessors = "0.1.39"
1117
BroadcastMapConversion = "0.1.2"
18+
Derive = "0.3.6"
19+
FillArrays = "1.13.0"
1220
NamedDimsArrays = "0.3.0"
21+
UnallocatedArrays = "0.1.1"
22+
UnspecifiedTypes = "0.1.1"
1323
julia = "1.10"

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ q, r = qr(a, (i,))
4444
@test q * r a
4545
````
4646

47+
Automatic allocation
48+
49+
````julia
50+
a = ITensor(i, j)
51+
````
52+
53+
Broken, need to fix:
54+
a[j[1], i[2]] = 1 + 2im
55+
56+
````julia
57+
a[2, 1] = 1 + 2im
58+
eltype(a) == Complex{Int}
59+
@test a[i[2], j[1]] == 1 + 2im
60+
````
61+
4762
---
4863

4964
*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*

examples/README.jl

+8
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ d = a + a′
3939
@test a aligndims(a, (j, i))
4040
q, r = qr(a, (i,))
4141
@test q * r a
42+
43+
# Automatic allocation
44+
a = ITensor(i, j)
45+
# Broken, need to fix:
46+
# a[j[1], i[2]] = 1 + 2im
47+
a[2, 1] = 1 + 2im
48+
eltype(a) == Complex{Int}
49+
@test a[i[2], j[1]] == 1 + 2im

src/ITensorBase.jl

+57-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ using NamedDimsArrays:
88
AbstractNamedInteger,
99
AbstractNamedUnitRange,
1010
AbstractNamedVector,
11+
NamedDimsArray,
1112
dename,
1213
dimnames,
1314
name,
1415
named,
16+
nameddimsindices,
1517
unname
1618

1719
@kwdef struct IndexName <: AbstractName
@@ -69,11 +71,65 @@ NamedDimsArrays.nameddimsarraytype(::Type{<:IndexName}) = ITensor
6971

7072
Base.ndims(::Type{<:AbstractITensor}) = Any
7173

72-
struct ITensor <: AbstractITensor
74+
using FillArrays: Zeros
75+
using UnallocatedArrays: UnallocatedZeros, allocate
76+
using UnspecifiedTypes: UnspecifiedZero
77+
78+
# TODO: Make this more general, maybe with traits `is_unallocated`
79+
# and `is_eltype_unspecified`.
80+
function specify_eltype(a::Zeros{UnspecifiedZero}, elt::Type)
81+
return Zeros{elt}(axes(a))
82+
end
83+
84+
# TODO: Use `adapt` to reach down into the storage.
85+
function specify_eltype!(a::AbstractITensor, elt::Type)
86+
setdenamed!(a, specify_eltype(dename(a), elt))
87+
return a
88+
end
89+
90+
# Assume it is allocated.
91+
allocate!(a::AbstractArray) = a
92+
93+
# TODO: Use `adapt` to reach down into the storage.
94+
function allocate!(a::AbstractITensor)
95+
setdenamed!(a, allocate(dename(a)))
96+
return a
97+
end
98+
99+
using Derive: @derive, @interface, AbstractArrayInterface
100+
101+
abstract type AbstractAllocatableArrayInterface <: AbstractArrayInterface end
102+
struct AllocatableArrayInterface <: AbstractAllocatableArrayInterface end
103+
104+
unallocatable(a::AbstractITensor) = NamedDimsArray(a)
105+
106+
@interface ::AbstractAllocatableArrayInterface function Base.setindex!(
107+
a::AbstractArray, value, I::Int...
108+
)
109+
allocate!(specify_eltype!(a, typeof(value)))
110+
# TODO: Maybe use `@interface interface(a) a[I...] = value`?
111+
unallocatable(a)[I...] = value
112+
return a
113+
end
114+
115+
@derive AllocatableArrayInterface() (T=AbstractITensor,) begin
116+
Base.setindex!(::T, ::Any, ::Int...)
117+
end
118+
119+
mutable struct ITensor <: AbstractITensor
73120
parent::AbstractArray
74121
nameddimsindices
75122
end
76123
Base.parent(a::ITensor) = a.parent
77124
NamedDimsArrays.nameddimsindices(a::ITensor) = a.nameddimsindices
78125

126+
using Accessors: @set
127+
setdenamed(a::ITensor, denamed) = (@set a.parent = denamed)
128+
setdenamed!(a::ITensor, denamed) = (a.parent = denamed)
129+
130+
function ITensor(I1::Index, I_rest::Index...)
131+
I = (I1, I_rest...)
132+
return ITensor(Zeros{UnspecifiedZero}(length.(dename.(I))...), I)
133+
end
134+
79135
end

test/test_aqua.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ using Aqua: Aqua
33
using Test: @testset
44

55
@testset "Code quality (Aqua.jl)" begin
6-
Aqua.test_all(ITensorBase)
6+
Aqua.test_all(ITensorBase; ambiguities=false, persistent_tasks=false)
77
end

0 commit comments

Comments
 (0)