Skip to content

Commit 93a0cdc

Browse files
authored
Smaller cleanups (#12)
* remove TK from project.toml * add TK as weakdep * fix bounds in constructor * clearer domain errors * iterate over sector values row-major to match isless * proper Nsymbol column check * correctly search for units, allow to error on time * fix ranges of dual calculations * add unitstyle, make allunits allocate less and have isunit not build allunits in the fallback * return number of objects matrix in dual cache, allowing easier counting and not hard-coding size * remove duplicate convert * fix not knowing size at compile time * fix seed for test reproducibility, add constructor test * be exhaustive with unitarity checks * test duals more rigorously * clean up readme where possible * remove allocations in Fsymbol call * final test cleanup * oopsie * make random sampling random at least * change pkgeval link (doesn't matter for now) * column check for Fsymbol.txt as well * tiny unit label refactor * remove overloading size, replace with rank * remove TK weakdep * format check through runic
1 parent 883f749 commit 93a0cdc

7 files changed

Lines changed: 126 additions & 175 deletions

File tree

‎.JuliaFormatter.toml‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎.github/workflows/FormatCheck.yml‎

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
1-
name: FormatCheck
1+
name: 'Format'
22

33
on:
4-
push:
5-
branches:
6-
- 'master'
7-
tags: '*'
84
pull_request:
9-
branches:
10-
- 'master'
11-
jobs:
12-
build:
13-
runs-on: ${{ matrix.os }}
14-
strategy:
15-
matrix:
16-
version:
17-
- '1' # automatically expands to the latest stable 1.x release of Julia
18-
os:
19-
- ubuntu-latest
20-
arch:
21-
- x64
22-
steps:
23-
- uses: julia-actions/setup-julia@latest
24-
with:
25-
version: ${{ matrix.version }}
26-
arch: ${{ matrix.arch }}
5+
paths: ['**/*.jl']
6+
types: [opened, synchronize, reopened, ready_for_review]
7+
8+
permissions:
9+
contents: read
2710

28-
- uses: actions/checkout@v6
29-
- name: Install JuliaFormatter and format
30-
run: |
31-
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
32-
julia -e 'using JuliaFormatter; format(".", verbose=true)'
11+
jobs:
12+
runic:
13+
uses: "QuantumKitHub/QuantumKitHubActions/.github/workflows/FormatCheck.yml@main"

‎Project.toml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MultiTensorKit"
22
uuid = "f0555a46-f681-4ef1-bed5-d64870568636"
3-
authors = ["Boris De Vos, Laurens Lootens and Lukas Devos"]
43
version = "0.1.0"
4+
authors = ["Boris De Vos, Laurens Lootens and Lukas Devos"]
55

66
[deps]
77
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
@@ -15,7 +15,6 @@ LinearAlgebra = "1"
1515
Pkg = "1"
1616
Random = "1"
1717
SafeTestsets = "0.1"
18-
TensorKit = "0.16, 0.17"
1918
TensorKitSectors = "0.3.7"
2019
TensorOperations = "5"
2120
Test = "1.10"
@@ -27,10 +26,9 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
2726
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
2827
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2928
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
30-
TensorKit = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec"
3129
TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2"
3230
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3331
TestExtras = "5ed8adda-3752-4e41-b88a-e8b09835ee3a"
3432

3533
[targets]
36-
test = ["Aqua", "LinearAlgebra", "Random", "SafeTestsets", "TensorKit", "TensorOperations", "Test", "TestExtras"]
34+
test = ["Aqua", "LinearAlgebra", "Random", "SafeTestsets", "TensorOperations", "Test", "TestExtras"]

‎README.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# MultiTensorKit.jl
22

3-
[TensorKit.jl](https://github.com/jutho/TensorKit.jl) extension for multi-fusion categories.
3+
[TensorKit.jl](https://github.com/QuantumKitHub/TensorKit.jl) extension for multi-fusion categories.
44

55

66
| **Documentation** | **Digital Object Identifier** |
77
|:-----------------:|:-----------------------------:|
88
| [![][docs-stable-img]][docs-stable-url] [![][docs-dev-img]][docs-dev-url] | [![DOI][doi-img]][doi-url] |
99

10-
| **Build Status** | **PkgEval** | **Coverage** |
11-
|:----------------:|:------------:|:------------:|
12-
| [![CI][ci-img]][ci-url] | [![PkgEval][pkgeval-img]][pkgeval-url] | [![Codecov][codecov-img]][codecov-url] |
10+
| **Build Status** | **PkgEval** | **Coverage** | **Quality assurance** |
11+
|:----------------:|:------------:|:------------:|:---------------------:|
12+
| [![CI][ci-img]][ci-url] | [![PkgEval][pkgeval-img]][pkgeval-url] | [![Codecov][codecov-img]][codecov-url] | [![Aqua QA][aqua-img]][aqua-url] |
1313

1414
[docs-stable-img]: https://img.shields.io/badge/docs-stable-blue.svg
1515
[docs-stable-url]: https://QuantumKitHub.github.io/MultiTensorKit.jl/stable
@@ -26,6 +26,8 @@
2626
[ci-img]: https://github.com/QuantumKitHub/MultiTensorKit.jl/actions/workflows/Tests.yml/badge.svg
2727
[ci-url]: https://github.com/QuantumKitHub/MultiTensorKit.jl/actions/workflows/Tests.yml
2828

29-
[pkgeval-img]: https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/T/MPSKit.svg
30-
[pkgeval-url]: https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/T/MPSKit.html
29+
[pkgeval-img]: https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/M/MultiTensorKit.svg
30+
[pkgeval-url]: https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/M/MultiTensorKit.html
3131

32+
[aqua-img]: https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg
33+
[aqua-url]: https://github.com/JuliaTesting/Aqua.jl

‎src/bimodulesector.jl‎

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ImplementedBimoduleSectors = [:A4]
1+
const ImplementedBimoduleSectors = (:A4,)
22

33
struct BimoduleSector{Name} <: Sector
44
i::Int
@@ -7,19 +7,18 @@ struct BimoduleSector{Name} <: Sector
77
function BimoduleSector{Name}(i::Int, j::Int, label::Int) where {Name}
88
Name ∈ ImplementedBimoduleSectors ||
99
throw(ArgumentError("BimoduleSector $Name not implemented"))
10-
i <= size(BimoduleSector{Name}) && j <= size(BimoduleSector{Name}) ||
11-
throw(DomainError("object outside the matrix $Name"))
12-
return label <= _numlabels(BimoduleSector{Name}, i, j) ? new{Name}(i, j, label) :
13-
throw(DomainError("label outside category $Name($i, $j)"))
10+
r = rank(BimoduleSector{Name})
11+
(1 <= i <= r && 1 <= j <= r) ||
12+
throw(DomainError((i, j), "subcategory outside the $r×$r matrix $Name"))
13+
return 1 <= label <= _numlabels(BimoduleSector{Name}, i, j) ? new{Name}(i, j, label) :
14+
throw(DomainError(label, "label outside category $Name($i, $j)"))
1415
end
1516
end
1617

1718
BimoduleSector{Name}(data::NTuple{3, Int}) where {Name} = BimoduleSector{Name}(data...)
1819
BimoduleSectorName(::Type{BimoduleSector{Name}}) where {Name} = Name
1920
const A4Object = BimoduleSector{:A4}
2021

21-
Base.convert(::Type{<:BimoduleSector{Name}}, labels::NTuple{3, Int}) where {Name} = BimoduleSector{Name}(labels...)
22-
2322
function Base.show(io::IO, a::BimoduleSector{Name}) where {Name}
2423
if get(io, :typeinfo, nothing) === typeof(a)
2524
print(io, (a.i, a.j, a.label))
@@ -39,27 +38,24 @@ function Base.convert(::Type{BimoduleSector{Name}}, d::NTuple{3, Int}) where {Na
3938
return BimoduleSector{Name}(d...)
4039
end
4140

42-
Base.size(::Type{A4Object}) = 7
41+
rank(I::Type{<:BimoduleSector}) = size(_get_dual_cache(I)[3], 1)
4342

4443
Base.IteratorSize(::Type{<:SectorValues{<:BimoduleSector}}) = Base.SizeUnknown()
4544

46-
function Base.iterate(iter::SectorValues{<:BimoduleSector}, (I, label) = (1, 1))
45+
function Base.iterate(iter::SectorValues{<:BimoduleSector}, (n, label) = (1, 1))
4746
A = eltype(iter)
48-
s = size(A)
49-
I > s * s && return nothing
50-
i, j = CartesianIndices((s, s))[I].I
47+
s = rank(A)
48+
n > s * s && return nothing
49+
i, j = fldmod1(n, s)
5150
maxlabel = _numlabels(A, i, j)
5251
return if label > maxlabel
53-
iterate(iter, (I + 1, 1))
52+
iterate(iter, (n + 1, 1))
5453
else
55-
A(i, j, label), (I, label + 1)
54+
A(i, j, label), (n, label + 1)
5655
end
5756
end
5857

59-
function Base.length(::SectorValues{I}) where {I <: BimoduleSector}
60-
s = size(I)
61-
return sum(_numlabels(I, i, j) for i in 1:s, j in 1:s)
62-
end
58+
Base.length(::SectorValues{I}) where {I <: BimoduleSector} = sum(_get_dual_cache(I)[3])
6359

6460
TensorKitSectors.FusionStyle(::Type{A4Object}) = GenericFusion()
6561
TensorKitSectors.BraidingStyle(::Type{<:BimoduleSector}) = NoBraiding()
@@ -76,8 +72,9 @@ function TensorKitSectors.:⊗(a::I, b::I) where {I <: BimoduleSector}
7672
end
7773

7874
function _numlabels(::Type{T}, i, j) where {T <: BimoduleSector}
79-
return length(_get_dual_cache(T)[2][i, j])
75+
return _get_dual_cache(T)[3][i, j]
8076
end
77+
_unitlabel(::Type{I}, i::Int) where {I <: BimoduleSector} = _get_dual_cache(I)[1][i]
8178

8279
# User-friendly functions
8380
# -------------------
@@ -92,10 +89,12 @@ function extract_Nsymbol(::Type{I}) where {I <: BimoduleSector}
9289
filename = joinpath(artifact_path, name, "Nsymbol.txt")
9390
isfile(filename) || throw(LoadError(filename, 0, "Nsymbol file not found for $name"))
9491
Narray = readdlm(filename) # matrix with 7 columns
92+
size(Narray, 2) == 7 ||
93+
throw(ArgumentError("Nsymbol file for $name has wrong number of columns: $(size(Narray, 2)) != 7"))
9594

9695
data_dict = Dict{NTuple{3, Int}, Dict{NTuple{3, Int}, Int}}()
9796
for row in eachrow(Narray)
98-
i, j, k, a, b, c, N = Int.(@view(row[1:size(I)]))
97+
i, j, k, a, b, c, N = Int.(@view(row[1:7]))
9998
colordict = get!(data_dict, (i, j, k), Dict{NTuple{3, Int}, Int}())
10099
push!(colordict, (a, b, c) => N)
101100
end
@@ -120,7 +119,7 @@ function TensorKitSectors.Nsymbol(a::I, b::I, c::I) where {I <: BimoduleSector}
120119
return get(_get_Ncache(I)[i, j, k], (a.label, b.label, c.label), 0)
121120
end
122121

123-
const Dualcache = IdDict{Type{<:BimoduleSector}, Tuple{Vector{Int64}, Matrix{Vector{Int64}}}}()
122+
const Dualcache = IdDict{Type{<:BimoduleSector}, Tuple{Vector{Int64}, Matrix{Vector{Int64}}, Matrix{Int64}}}()
124123

125124
function _get_dual_cache(::Type{T}) where {T <: BimoduleSector}
126125
global Dualcache
@@ -131,80 +130,72 @@ end
131130

132131
function extract_dual(::Type{I}) where {I <: BimoduleSector}
133132
N = _get_Ncache(I)
134-
ncats = size(I)
135-
Is = zeros(Int, ncats)
133+
ncats = maximum(maximum, keys(N))
134+
nobj = [maximum(first, keys(N[i, j, j])) for i in 1:ncats, j in 1:ncats]
136135

137-
map(1:ncats) do i
136+
Is = map(1:ncats) do i
138137
Niii = N[i, i, i]
139-
nobji = maximum(first, keys(N[i, i, i]))
140138
# want to return a leftunit and rightunit for each entry in multifusion cat
141139
# leftunit/rightunit needs to at least be the unit object within a fusion cat
142-
Is[i] = findfirst(1:nobji) do a
140+
u = findfirst(1:nobj[i, i]) do a
143141
get(Niii, (a, a, a), 0) == 1 || return false # I x I -> I
144-
for othera in 1:nobji
145-
get(Niii, (othera, a, othera), 0) == 1 || return false # a x I -> a
146-
get(Niii, (a, othera, othera), 0) == 1 || return false # I x a -> a
147-
end
148142

149-
# check leftunit
150-
map(1:ncats) do j
151-
nobjj = maximum(first, keys(N[j, j, j]))
152-
for b in 1:nobjj
153-
get(N[i, j, j], (a, b, b), 0) == 1 || return false # I = leftunit(b)
154-
end
143+
# I = leftunit(b) for every b in C_ij, I x b -> b in C_ii x C_ij -> C_ij
144+
for j in 1:ncats, b in 1:nobj[i, j]
145+
get(N[i, i, j], (a, b, b), 0) == 1 || return false
155146
end
156147

157-
# check rightunit
158-
map(1:ncats) do k
159-
nobjk = maximum(first, keys(N[k, k, k]))
160-
for c in 1:nobjk
161-
get(N[k, i, k], (c, a, c), 0) == 1 || return false # I = rightunit(c)
162-
end
148+
# I = rightunit(c) for every c in C_ki, c x I -> c in C_ki x C_ii -> C_ki
149+
for k in 1:ncats, c in 1:nobj[k, i]
150+
get(N[k, i, i], (c, a, c), 0) == 1 || return false
163151
end
164152
return true
165153
end
154+
isnothing(u) && throw(ArgumentError("no unit object found in subcategory ($i, $i) of $I"))
155+
return u
166156
end
167157

168158
allduals = Matrix{Vector{Int}}(undef, ncats, ncats) # ncats square matrix of vectors
169-
for i in 1:ncats
170-
nobji = maximum(first, keys(N[i, i, i]))
171-
for j in 1:ncats
172-
allduals[i, j] = Int[]
173-
174-
nobjj = maximum(first, keys(N[j, j, j]))
175-
# the nested vectors contain the duals of the objects in 𝒞_ij, which are in C_ji
176-
Niji = N[i, j, i] # 𝒞_ij x 𝒞_ji -> C_ii
177-
Njij = N[j, i, j] # 𝒞_ji x 𝒞_ij -> C_jj
178-
for i_ob in 1:nobji, j_ob in 1:nobjj
179-
get(Niji, (i_ob, j_ob, Is[i]), 0) == 1 || continue # leftunit(c_ij) ∈ c_ij x c_ji
180-
get(Njij, (j_ob, i_ob, Is[j]), 0) == 1 || continue # rightunit(c_ij) ∈ c_ji x c_ij
181-
push!(allduals[i, j], j_ob)
159+
for i in 1:ncats, j in 1:ncats
160+
Niji = N[i, j, i] # 𝒞_ij x 𝒞_ji -> 𝒞_ii
161+
Njij = N[j, i, j] # 𝒞_ji x 𝒞_ij -> 𝒞_jj
162+
allduals[i, j] = map(1:nobj[i, j]) do m
163+
mbar = findfirst(1:nobj[j, i]) do n
164+
get(Niji, (m, n, Is[i]), 0) == 1 && # leftunit(m) in m x mbar
165+
get(Njij, (n, m, Is[j]), 0) == 1 # rightunit(m) in mbar x m
182166
end
167+
isnothing(mbar) && throw(ArgumentError("no dual found for $I($i, $j, $m)"))
168+
return mbar
183169
end
184170
end
185-
return Is, allduals
171+
return Is, allduals, nobj
186172
end
187173

188174
function TensorKitSectors.unit(a::BimoduleSector)
189175
a.i == a.j || throw(DomainError("unit of module category ($(a.i), $(a.j)) of $(typeof(a)) is ill-defined"))
190-
return typeof(a)(a.i, a.i, _get_dual_cache(typeof(a))[1][a.i])
176+
return typeof(a)(a.i, a.i, _unitlabel(typeof(a), a.i))
191177
end
192178

179+
TensorKitSectors.UnitStyle(::Type{<:BimoduleSector}) = GenericUnit()
180+
193181
function TensorKitSectors.allunits(::Type{I}) where {I <: BimoduleSector}
194-
s = size(I)
195-
return I[I(i, i, _get_dual_cache(I)[1][i]) for i in 1:s]
182+
return I[I(i, i, u) for (i, u) in enumerate(_get_dual_cache(I)[1])]
183+
end
184+
185+
function TensorKitSectors.isunit(a::BimoduleSector)
186+
return a.i == a.j && a.label == _unitlabel(typeof(a), a.i)
196187
end
197188

198189
function TensorKitSectors.unit(::Type{<:BimoduleSector})
199190
throw(ArgumentError("unit of Type BimoduleSector doesn't exist"))
200191
end
201192

202193
function TensorKitSectors.leftunit(a::BimoduleSector)
203-
return typeof(a)(a.i, a.i, _get_dual_cache(typeof(a))[1][a.i])
194+
return typeof(a)(a.i, a.i, _unitlabel(typeof(a), a.i))
204195
end
205196

206197
function TensorKitSectors.rightunit(a::BimoduleSector)
207-
return typeof(a)(a.j, a.j, _get_dual_cache(typeof(a))[1][a.j])
198+
return typeof(a)(a.j, a.j, _unitlabel(typeof(a), a.j))
208199
end
209200

210201
function TensorKitSectors.dual(a::BimoduleSector)
@@ -218,6 +209,7 @@ function extract_Fsymbol(::Type{I}) where {I <: BimoduleSector}
218209
@assert isfile(filename) "cannot find $filename"
219210

220211
Farray = readdlm(filename)
212+
size(Farray, 2) == 16 || throw(ArgumentError("Fsymbol file for $name has wrong number of columns: $(size(Farray, 2)) != 16"))
221213
for ((i, j, k, l), colordict) in convert_Fs(Farray)
222214
result[(i, j, k, l)] = Dict{NTuple{6, Int}, Array{ComplexF64, 4}}()
223215
for ((a, b, c, d, e, f), Fvals) in colordict
@@ -272,11 +264,14 @@ function TensorKitSectors.Fsymbol(a::I, b::I, c::I, d::I, e::I, f::I) where {I <
272264
Nbcf = Nsymbol(b, c, f)
273265
Nafd = Nsymbol(a, f, d)
274266

275-
zero_array = zeros(sectorscalartype(I), Nabe, Necd, Nbcf, Nafd)
267+
T = sectorscalartype(I)
276268
Nabe > 0 && Necd > 0 && Nbcf > 0 && Nafd > 0 ||
277-
return zero_array
269+
return zeros(T, Nabe, Necd, Nbcf, Nafd)
278270

279271
i, j, k, l = a.i, a.j, b.j, c.j
280272
colordict = _get_Fcache(I)[i, j, k, l]
281-
return get!(colordict, (a.label, b.label, c.label, d.label, e.label, f.label), zero_array)
273+
return get!(
274+
() -> zeros(T, Nabe, Necd, Nbcf, Nafd),
275+
colordict, (a.label, b.label, c.label, d.label, e.label, f.label)
276+
)
282277
end

‎test/runtests.jl‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ using SafeTestsets: @safetestset
44
# either via `--group=ALL` or through ENV["GROUP"]
55
const pat = r"(?:--group=)(\w+)"
66
arg_id = findfirst(contains(pat), ARGS)
7-
const GROUP = uppercase(if isnothing(arg_id)
8-
get(ENV, "GROUP", "ALL")
9-
else
10-
only(match(pat, ARGS[arg_id]).captures)
11-
end)
7+
const GROUP = uppercase(
8+
if isnothing(arg_id)
9+
get(ENV, "GROUP", "ALL")
10+
else
11+
only(match(pat, ARGS[arg_id]).captures)
12+
end
13+
)
1214

1315
"match files of the form `test_*.jl`, but exclude `*setup*.jl`"
1416
istestfile(fn) = endswith(fn, ".jl") && startswith(basename(fn), "test_") &&
15-
!contains(fn, "setup")
17+
!contains(fn, "setup")
1618

1719
@time begin
1820
# tests in groups based on folder structure

0 commit comments

Comments
 (0)