-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] Define random_unitary
constructor
#39
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
Draft
mtfishman
wants to merge
12
commits into
main
Choose a base branch
from
random_unitary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d132d0b
Define random_unitary constructor
mtfishman 8a1b555
Use MatrixAlgebraKit
mtfishman 6a849a7
Refactor in terms of in-place random_unitary
mtfishman 219ea49
Fix import
mtfishman 2e0e672
Simplify
mtfishman e47a544
Reorganize extensions
mtfishman bab22ac
Delete code duplication
mtfishman eb86d46
Format
mtfishman 0678785
Check square
mtfishman 9440534
Format
mtfishman 30999f2
Merge branch 'main' into random_unitary
mtfishman 879ef7d
Properly pass along RNG
mtfishman 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,13 +1,14 @@ | ||
name = "TensorAlgebra" | ||
uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" | ||
authors = ["ITensor developers <[email protected]> and contributors"] | ||
version = "0.2.1" | ||
version = "0.2.2" | ||
|
||
[deps] | ||
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" | ||
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" | ||
EllipsisNotation = "da5c29d0-fa7d-589e-88eb-ea29b0a81949" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6" | ||
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" | ||
|
||
|
@@ -22,7 +23,8 @@ ArrayLayouts = "1.10.4" | |
BlockArrays = "1.2.0" | ||
EllipsisNotation = "1.8.0" | ||
GradedUnitRanges = "0.1.0" | ||
LinearAlgebra = "1.10" | ||
LinearAlgebra = "1.10.0" | ||
Random = "1.10.0" | ||
TupleTools = "1.6.0" | ||
TypeParameterAccessors = "0.2.1, 0.3" | ||
TypeParameterAccessors = "0.2.1, 0.3.0" | ||
julia = "1.10" |
25 changes: 23 additions & 2 deletions
25
ext/TensorAlgebraGradedUnitRangesExt/TensorAlgebraGradedUnitRangesExt.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,8 +1,29 @@ | ||
module TensorAlgebraGradedUnitRangesExt | ||
using GradedUnitRanges: AbstractGradedUnitRange, tensor_product | ||
using TensorAlgebra: TensorAlgebra | ||
|
||
using GradedUnitRanges: AbstractGradedUnitRange, dual, tensor_product | ||
using GradedUnitRanges.BlockArrays: Block, blocklengths, blocksize | ||
using Random: AbstractRNG | ||
using TensorAlgebra: TensorAlgebra, random_unitary | ||
|
||
function TensorAlgebra.:⊗(a1::AbstractGradedUnitRange, a2::AbstractGradedUnitRange) | ||
return tensor_product(a1, a2) | ||
end | ||
|
||
function TensorAlgebra.dual(a::AbstractGradedUnitRange) | ||
return dual(a) | ||
end | ||
|
||
function TensorAlgebra.random_unitary( | ||
rng::AbstractRNG, | ||
elt::Type, | ||
ax::Tuple{AbstractGradedUnitRange}, | ||
) | ||
a = zeros(elt, dual.(ax)..., ax...) | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO: Define `blockdiagindices`. | ||
for i in 1:minimum(blocksize(a)) | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
a[Block(i, i)] = random_unitary(rng, elt, Int(blocklengths(only(ax))[i])) | ||
end | ||
return a | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Version of `sign` that returns one | ||
# if `x == 0`. | ||
function nonzero_sign(x) | ||
iszero(x) && return one(x) | ||
return sign(x) | ||
end | ||
|
||
using LinearAlgebra: LinearAlgebra, Diagonal, diag | ||
function qr_positive(M::AbstractMatrix) | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Q, R = LinearAlgebra.qr(M) | ||
Q′ = typeof(R)(Q) | ||
signs = nonzero_sign.(diag(R)) | ||
Q′ = Q′ * Diagonal(signs) | ||
R = Diagonal(conj.(signs)) * R | ||
return Q′, R | ||
end | ||
|
||
using Random: Random, AbstractRNG | ||
|
||
dual(x) = x | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function random_unitary( | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rng::AbstractRNG, | ||
elt::Type, | ||
ax::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, | ||
) | ||
ax_fused = ⊗(ax...) | ||
a_fused = random_unitary(rng, elt, ax_fused) | ||
return splitdims(a_fused, dual.(ax), ax) | ||
end | ||
|
||
# Copy of `Base.to_dim`: | ||
# https://github.com/JuliaLang/julia/blob/1431bec1bcd205f181ca2a3f1c314247b64076df/base/array.jl#L439-L440 | ||
to_dim(d::Integer) = d | ||
to_dim(d::Base.OneTo) = last(d) | ||
|
||
# Matrix version. | ||
function random_unitary( | ||
rng::AbstractRNG, | ||
elt::Type, | ||
ax::Tuple{AbstractUnitRange}, | ||
) | ||
return random_unitary(rng, elt, map(to_dim, ax)) | ||
end | ||
|
||
function random_unitary(rng::AbstractRNG, elt::Type, dims::Tuple{Integer}) | ||
Q, _ = qr_positive(randn(rng, elt, (dims..., dims...))) | ||
return Q | ||
end | ||
|
||
# Canonicalizing other kinds of inputs. | ||
function random_unitary(rng::AbstractRNG, elt::Type, dims::Tuple{Vararg{Union{AbstractUnitRange,Integer}}}) | ||
return random_unitary(Random.default_rng(), elt, map(to_axis, dims)) | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
function random_unitary(elt::Type, dims::Tuple{Vararg{Union{AbstractUnitRange,Integer}}}) | ||
return random_unitary(Random.default_rng(), elt, dims) | ||
end | ||
function random_unitary(rng::AbstractRNG, elt::Type, dims::Union{AbstractUnitRange,Integer}...) | ||
return random_unitary(rng, elt, dims) | ||
end | ||
function random_unitary(elt::Type, dims::Union{AbstractUnitRange,Integer}...) | ||
return random_unitary(Random.default_rng(), elt, dims) | ||
end | ||
function random_unitary(rng::AbstractRNG, dims::Union{AbstractUnitRange,Integer}...) | ||
return random_unitary(rng, Float64, dims) | ||
end | ||
function random_unitary(dims::Union{AbstractUnitRange,Integer}...) | ||
return random_unitary(Random.default_rng(), Float64, dims) | ||
end |
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.