-
Notifications
You must be signed in to change notification settings - Fork 1
Define TensorAlgebra.matricize
#13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
06b4847
WIP trivial_axis
ogauthe b5c9ecd
mostly working
ogauthe fcc862b
bump BlockArrays compat
ogauthe ea7f0aa
test GradedArray
ogauthe 3f8b709
matricize_axes and sectormergesort
ogauthe 448079e
fix trivial_axis
ogauthe 8b292c1
uncomment test
ogauthe 163c715
row/col axis -> domain
ogauthe 750ce3f
remove @test
ogauthe 1c372d1
simpler definitions
ogauthe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "GradedArrays" | ||
uuid = "bc96ca6e-b7c8-4bb6-888e-c93f838762c2" | ||
authors = ["ITensor developers <[email protected]> and contributors"] | ||
version = "0.2.3" | ||
version = "0.2.4" | ||
|
||
[deps] | ||
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" | ||
|
@@ -23,16 +23,16 @@ TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" | |
GradedArraysTensorAlgebraExt = "TensorAlgebra" | ||
|
||
[compat] | ||
BlockArrays = "1.5.0" | ||
BlockSparseArrays = "0.4.0" | ||
BlockArrays = "1.6.0" | ||
BlockSparseArrays = "0.4.2" | ||
Compat = "4.16.0" | ||
DerivableInterfaces = "0.4.4" | ||
FillArrays = "1.13.0" | ||
HalfIntegers = "1.6.0" | ||
LinearAlgebra = "1.10.0" | ||
Random = "1.10.0" | ||
SplitApplyCombine = "1.2.3" | ||
TensorAlgebra = "0.2.7" | ||
TensorAlgebra = "0.3.2" | ||
TensorProducts = "0.1.3" | ||
TypeParameterAccessors = "0.3.9" | ||
julia = "1.10" |
125 changes: 46 additions & 79 deletions
125
ext/GradedArraysTensorAlgebraExt/GradedArraysTensorAlgebraExt.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,76 @@ | ||
module GradedArraysTensorAlgebraExt | ||
|
||
using BlockArrays: Block, BlockIndexRange, blockedrange, blocks | ||
using BlockSparseArrays: | ||
BlockSparseArrays, | ||
AbstractBlockSparseArray, | ||
AbstractBlockSparseArrayInterface, | ||
BlockSparseArray, | ||
BlockSparseArrayInterface, | ||
BlockSparseMatrix, | ||
BlockSparseVector, | ||
block_merge | ||
using DerivableInterfaces: @interface | ||
using BlockArrays: blocks | ||
using BlockSparseArrays: BlockSparseArray, blockreshape | ||
using GradedArrays: GradedArray | ||
using GradedArrays.GradedUnitRanges: | ||
GradedUnitRanges, | ||
AbstractGradedUnitRange, | ||
blockmergesortperm, | ||
blocksortperm, | ||
dual, | ||
flip, | ||
invblockperm, | ||
nondual, | ||
unmerged_tensor_product | ||
using LinearAlgebra: Adjoint, Transpose | ||
using GradedArrays.SymmetrySectors: trivial | ||
using TensorAlgebra: | ||
TensorAlgebra, FusionStyle, BlockReshapeFusion, SectorFusion, fusedims, splitdims | ||
using TensorProducts: OneToOne | ||
TensorAlgebra, | ||
AbstractBlockPermutation, | ||
BlockedTuple, | ||
FusionStyle, | ||
trivial_axis, | ||
unmatricize | ||
|
||
#= | ||
reducewhile(f, op, collection, state) | ||
struct SectorFusion <: FusionStyle end | ||
|
||
reducewhile(x -> length(x) < 3, vcat, ["a", "b", "c", "d"], 2; init=String[]) == | ||
(["b", "c"], 4) | ||
=# | ||
function reducewhile(f, op, collection, state; init) | ||
prev_result = init | ||
prev_state = state | ||
result = prev_result | ||
while f(result) | ||
prev_result = result | ||
prev_state = state | ||
value_and_state = iterate(collection, state) | ||
isnothing(value_and_state) && break | ||
value, state = value_and_state | ||
result = op(result, value) | ||
end | ||
return prev_result, prev_state | ||
end | ||
|
||
#= | ||
groupreducewhile(f, op, collection, ngroups) | ||
|
||
groupreducewhile((i, x) -> length(x) ≤ i, vcat, ["a", "b", "c", "d", "e", "f"], 3; init=String[]) == | ||
(["a"], ["b", "c"], ["d", "e", "f"]) | ||
=# | ||
function groupreducewhile(f, op, collection, ngroups; init) | ||
state = firstindex(collection) | ||
return ntuple(ngroups) do group_number | ||
result, state = reducewhile(x -> f(group_number, x), op, collection, state; init) | ||
return result | ||
end | ||
end | ||
TensorAlgebra.FusionStyle(::Type{<:GradedArray}) = SectorFusion() | ||
|
||
TensorAlgebra.FusionStyle(::AbstractGradedUnitRange) = SectorFusion() | ||
# TBD consider heterogeneous sectors? | ||
TensorAlgebra.trivial_axis(t::Tuple{Vararg{AbstractGradedUnitRange}}) = trivial(first(t)) | ||
|
||
# Sort the blocks by sector and then merge the common sectors. | ||
function block_mergesort(a::AbstractArray) | ||
I = blockmergesortperm.(axes(a)) | ||
return a[I...] | ||
function matricize_axes( | ||
blocked_axes::BlockedTuple{2,<:Any,<:Tuple{Vararg{AbstractUnitRange}}} | ||
) | ||
@assert !isempty(blocked_axes) | ||
default_axis = trivial_axis(Tuple(blocked_axes)) | ||
codomain_axes, domain_axes = blocks(blocked_axes) | ||
codomain_axis = unmerged_tensor_product(default_axis, codomain_axes...) | ||
unflipped_domain_axis = unmerged_tensor_product(default_axis, domain_axes...) | ||
return codomain_axis, flip(unflipped_domain_axis) | ||
end | ||
|
||
function TensorAlgebra.fusedims( | ||
::SectorFusion, a::AbstractArray, merged_axes::AbstractUnitRange... | ||
function TensorAlgebra.matricize( | ||
::SectorFusion, a::AbstractArray, biperm::AbstractBlockPermutation{2} | ||
) | ||
# First perform a fusion using a block reshape. | ||
# TODO avoid groupreducewhile. Require refactor of fusedims. | ||
unmerged_axes = groupreducewhile( | ||
unmerged_tensor_product, axes(a), length(merged_axes); init=OneToOne() | ||
) do i, axis | ||
return length(axis) ≤ length(merged_axes[i]) | ||
end | ||
|
||
a_reshaped = fusedims(BlockReshapeFusion(), a, unmerged_axes...) | ||
a_perm = permutedims(a, Tuple(biperm)) | ||
codomain_axis, domain_axis = matricize_axes(axes(a)[biperm]) | ||
a_reshaped = blockreshape(a_perm, (codomain_axis, domain_axis)) | ||
# Sort the blocks by sector and merge the equivalent sectors. | ||
return block_mergesort(a_reshaped) | ||
return sectormergesort(a_reshaped) | ||
end | ||
|
||
function TensorAlgebra.splitdims( | ||
::SectorFusion, a::AbstractArray, split_axes::AbstractUnitRange... | ||
function TensorAlgebra.unmatricize( | ||
::SectorFusion, | ||
m::AbstractMatrix, | ||
blocked_axes::BlockedTuple{2,<:Any,<:Tuple{Vararg{AbstractUnitRange}}}, | ||
) | ||
# First, fuse axes to get `blockmergesortperm`. | ||
# Then unpermute the blocks. | ||
axes_prod = groupreducewhile( | ||
unmerged_tensor_product, split_axes, ndims(a); init=OneToOne() | ||
) do i, axis | ||
return length(axis) ≤ length(axes(a, i)) | ||
end | ||
blockperms = blocksortperm.(axes_prod) | ||
sorted_axes = map((r, I) -> only(axes(r[I])), axes_prod, blockperms) | ||
fused_axes = matricize_axes(blocked_axes) | ||
|
||
blockperms = blocksortperm.(fused_axes) | ||
sorted_axes = map((r, I) -> only(axes(r[I])), fused_axes, blockperms) | ||
|
||
# TODO: This is doing extra copies of the blocks, | ||
# use `@view a[axes_prod...]` instead. | ||
# That will require implementing some reindexing logic | ||
# for this combination of slicing. | ||
a_unblocked = a[sorted_axes...] | ||
a_blockpermed = a_unblocked[invblockperm.(blockperms)...] | ||
return splitdims(BlockReshapeFusion(), a_blockpermed, split_axes...) | ||
m_unblocked = m[sorted_axes...] | ||
m_blockpermed = m_unblocked[invblockperm.(blockperms)...] | ||
return unmatricize(FusionStyle(BlockSparseArray), m_blockpermed, blocked_axes) | ||
end | ||
|
||
# Sort the blocks by sector and then merge the common sectors. | ||
function sectormergesort(a::AbstractArray) | ||
I = blockmergesortperm.(axes(a)) | ||
return a[I...] | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.