-
Notifications
You must be signed in to change notification settings - Fork 2
Fix bipermutations in contract
#75
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "TensorAlgebra" | ||
uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" | ||
authors = ["ITensor developers <[email protected]> and contributors"] | ||
version = "0.3.11" | ||
version = "0.3.12" | ||
|
||
[deps] | ||
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,16 @@ | ||||||
using .BaseExtensions: BaseExtensions | ||||||
using BlockArrays: blocklengths | ||||||
|
||||||
# default: if no bipartion is specified, all axes to domain | ||||||
invbiperm(perm, ::Any) = invbiperm(perm, Val(0)) | ||||||
invbiperm(perm, t::Tuple{Tuple,Tuple}) = invbiperm(perm, tuplemortar(t)) | ||||||
invbiperm(perm, t::AbstractBlockTuple{2}) = invbiperm(perm, Val(first(blocklength(t)))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
function invbiperm(perm, ::Val{N1}) where {N1} | ||||||
perm_out = invperm(Tuple(perm)) | ||||||
length(perm) <= N1 && return blockedpermvcat(perm_out, ()) | ||||||
return blockedpermvcat(perm_out[begin:N1], (perm_out[(N1 + 1):end])) | ||||||
end | ||||||
|
||||||
function blockedperms( | ||||||
f::typeof(contract), alg::Algorithm, dimnames_dest, dimnames1, dimnames2 | ||||||
|
@@ -19,18 +31,18 @@ function blockedperms(::typeof(contract), dimnames_dest, dimnames1, dimnames2) | |||||
|
||||||
perm_codomain_dest = BaseExtensions.indexin(codomain, dimnames_dest) | ||||||
perm_domain_dest = BaseExtensions.indexin(domain, dimnames_dest) | ||||||
biperm_dest_to_a12 = (perm_codomain_dest..., perm_domain_dest...) | ||||||
biperm_a12_to_dest = invbiperm(biperm_dest_to_a12, dimnames_dest) | ||||||
|
||||||
perm_codomain1 = BaseExtensions.indexin(codomain, dimnames1) | ||||||
perm_domain1 = BaseExtensions.indexin(contracted, dimnames1) | ||||||
|
||||||
perm_codomain2 = BaseExtensions.indexin(contracted, dimnames2) | ||||||
perm_domain2 = BaseExtensions.indexin(domain, dimnames2) | ||||||
|
||||||
permblocks_dest = (perm_codomain_dest, perm_domain_dest) | ||||||
biperm_dest = blockedpermvcat(permblocks_dest...) | ||||||
permblocks1 = (perm_codomain1, perm_domain1) | ||||||
biperm1 = blockedpermvcat(permblocks1...) | ||||||
permblocks2 = (perm_codomain2, perm_domain2) | ||||||
biperm2 = blockedpermvcat(permblocks2...) | ||||||
return biperm_dest, biperm1, biperm2 | ||||||
return biperm_a12_to_dest, biperm1, biperm2 | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,17 +74,24 @@ function matricize(a::AbstractArray, permblock1::Tuple, permblock2::Tuple) | |
end | ||
|
||
# ==================================== unmatricize ======================================= | ||
function unmatricize(m::AbstractMatrix, axes, biperm::AbstractBlockPermutation{2}) | ||
length(axes) == length(biperm) || throw(ArgumentError("axes do not match permutation")) | ||
return unmatricize(FusionStyle(m), m, axes, biperm) | ||
function unmatricize( | ||
m::AbstractMatrix, axes_dest, biperm_dest_to_a12::AbstractBlockPermutation{2} | ||
) | ||
length(axes_dest) == length(biperm_dest_to_a12) || | ||
throw(ArgumentError("axes do not match permutation")) | ||
return unmatricize(FusionStyle(m), m, axes_dest, biperm_dest_to_a12) | ||
Comment on lines
+77
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in the context of this function, the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing with the name |
||
end | ||
|
||
function unmatricize( | ||
::FusionStyle, m::AbstractMatrix, axes, biperm::AbstractBlockPermutation{2} | ||
::FusionStyle, | ||
m::AbstractMatrix, | ||
axes_dest, | ||
biperm_dest_to_a12::AbstractBlockPermutation{2}, | ||
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above about the naming. |
||
) | ||
blocked_axes = axes[biperm] | ||
a_perm = unmatricize(m, blocked_axes) | ||
return permuteblockeddims(a_perm, invperm(biperm)) | ||
blocked_axes = axes_dest[biperm_dest_to_a12] | ||
a12 = unmatricize(m, blocked_axes) | ||
biperm_a12_to_dest = invbiperm(biperm_dest_to_a12, axes_dest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I lost track of the discussions we had about the conventions we want to use in this PR, I thought we had discussed that we would change the convention of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe as an alternative, we could change the convention of function unmatricize(
style::FusionStyle,
m::AbstractMatrix,
ax,
biperm::AbstractBlockPermutation{2},
)
a = unmatricize(style, m, ax)
return permutedims(a, biperm)
end |
||
return permuteblockeddims(a12, biperm_a12_to_dest) | ||
end | ||
|
||
function unmatricize( | ||
|
@@ -108,10 +115,19 @@ function unmatricize( | |
return unmatricize(m, blocked_axes) | ||
end | ||
|
||
function unmatricize!(a, m::AbstractMatrix, biperm::AbstractBlockPermutation{2}) | ||
ndims(a) == length(biperm) || | ||
function unmatricize!( | ||
a_dest, m::AbstractMatrix, biperm_dest_to_a12::AbstractBlockPermutation{2} | ||
) | ||
ndims(a_dest) == length(biperm_dest_to_a12) || | ||
throw(ArgumentError("destination does not match permutation")) | ||
blocked_axes = axes(a)[biperm] | ||
blocked_axes = axes(a_dest)[biperm_dest_to_a12] | ||
a_perm = unmatricize(m, blocked_axes) | ||
return permuteblockeddims!(a, a_perm, invperm(biperm)) | ||
biperm_a12_to_dest = invbiperm(biperm_dest_to_a12, axes(a_dest)) | ||
return permuteblockeddims!(a_dest, a_perm, biperm_a12_to_dest) | ||
end | ||
|
||
function unmatricize_add!(a_dest, a_dest_mat, biperm_dest_to_a12, α, β) | ||
a12 = unmatricize(a_dest_mat, axes(a_dest), biperm_dest_to_a12) | ||
a_dest .= α .* a12 .+ β .* a_dest | ||
return a_dest | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit strange to me that it just allows anything as the second argument. Maybe this should be
invbiperm(perm)
? Is this used anywhere right now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the rest of the code, I see that it is being used in calls like
biperm_a12_to_dest = invbiperm(biperm_dest_to_a12, axes(a_dest))
, whereaxes(a_dest)
might output a blocked tuple or a flat tuple.I think the
invbiperm
function is trying to do too much and therefore makes the code harder to understand. Instead, maybe we could introduce new functionsbiperm
andlength_codomain
:and the use a combination of
invperm
,biperm
, andlength_codomain
in the contract code, for example: