-
Notifications
You must be signed in to change notification settings - Fork 2
Working ContractNetwork #7
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
Open
JoeyT1994
wants to merge
16
commits into
ITensor:main
Choose a base branch
from
JoeyT1994:Contract
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.
Open
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
02d33db
Working ContractNetwork
JoeyT1994 4d43eb5
Better tolerance check on test
JoeyT1994 5dcecfb
Contract Network Stuff
JoeyT1994 b56b752
Weak Dependencies
JoeyT1994 6543ad4
External Dependency
JoeyT1994 87647df
Fix imports
JoeyT1994 9f218d7
Force specification of contract alg
JoeyT1994 433face
Merge branch 'main' into Contract
JoeyT1994 6c5ac55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4c16d56
Runic Formatting
JoeyT1994 d5cdf3f
Merge branch 'Contract' of github.com:JoeyT1994/ITensorNetworksNext.j…
JoeyT1994 edf78b7
Revert Project toml slightly
JoeyT1994 f345f6c
Remove extras from Project.toml
JoeyT1994 074e347
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8187ca7
Add to weakdeps
JoeyT1994 7a2a24c
Merge branch 'Contract' of github.com:JoeyT1994/ITensorNetworksNext.j…
JoeyT1994 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,31 +1,35 @@ | ||
name = "ITensorNetworksNext" | ||
uuid = "302f2e75-49f0-4526-aef7-d8ba550cb06c" | ||
authors = ["ITensor developers <support@itensor.org> and contributors"] | ||
version = "0.1.2" | ||
version = "0.1.3" | ||
|
||
[deps] | ||
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" | ||
BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5" | ||
DataGraphs = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" | ||
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" | ||
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" | ||
ITensorBase = "4795dd04-0d67-49bb-8f44-b89c448a1dc7" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" | ||
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" | ||
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19" | ||
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" | ||
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66" | ||
TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2" | ||
|
||
[compat] | ||
Adapt = "4.3.0" | ||
BackendSelection = "0.1.6" | ||
DataGraphs = "0.2.7" | ||
Dictionaries = "0.4.5" | ||
Graphs = "1.13.1" | ||
ITensorBase = "0.2.13" | ||
LinearAlgebra = "1.10" | ||
MacroTools = "0.5.16" | ||
NamedDimsArrays = "0.7.13" | ||
NamedGraphs = "0.6.9" | ||
SimpleTraits = "0.9.5" | ||
SplitApplyCombine = "1.2.3" | ||
TensorOperations = "5.3.1" | ||
julia = "1.10" |
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,39 @@ | ||
using TensorOperations: TensorOperations, optimaltree | ||
using ITensorBase: inds, dim | ||
|
||
default_sequence_alg = "optimal" | ||
|
||
function contraction_sequence(::Algorithm"optimal", tn::Vector{<:AbstractArray}) | ||
network = collect.(inds.(tn)) | ||
#Converting dims to Float64 to minimize overflow issues | ||
inds_to_dims = Dict(i => Float64(dim(i)) for i in unique(reduce(vcat, network))) | ||
seq, _ = optimaltree(network, inds_to_dims) | ||
return seq | ||
end | ||
|
||
function contraction_sequence(::Algorithm"leftassociative", tn::Vector{<:AbstractArray}) | ||
return Any[i for i in 1:length(tn)] | ||
end | ||
|
||
function contraction_sequence(tn::Vector{<:AbstractArray}; alg=default_sequence_alg) | ||
contraction_sequence(Algorithm(alg), tn) | ||
end | ||
|
||
# Internal recursive worker | ||
function recursive_contractnetwork(tn::Union{AbstractVector,AbstractArray}) | ||
tn isa AbstractVector && return reduce(*, map(recursive_contractnetwork, tn)) | ||
return tn | ||
end | ||
|
||
# Recursive worker for ordering the tensors according to the sequence | ||
rearrange(tn::Vector{<:AbstractArray}, i::Integer) = tn[i] | ||
rearrange(tn::Vector{<:AbstractArray}, v::AbstractVector) = [rearrange(tn, s) for s in v] | ||
|
||
function contractnetwork(tn::Vector{<:AbstractArray}; sequence_alg=default_sequence_alg) | ||
sequence = contraction_sequence(tn; alg=sequence_alg) | ||
return recursive_contractnetwork(rearrange(tn, sequence)) | ||
end | ||
|
||
function contractnetwork(tn::AbstractTensorNetwork; sequence_alg=default_sequence_alg) | ||
return contractnetwork([tn[v] for v in vertices(tn)]; sequence_alg) | ||
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,38 @@ | ||
using Graphs: edges | ||
using NamedGraphs.GraphsExtensions: arranged_edges, incident_edges | ||
using NamedGraphs.NamedGraphGenerators: named_grid | ||
using ITensorBase: Index, ITensor | ||
using ITensorNetworksNext: | ||
TensorNetwork, linkinds, siteinds, contractnetwork, contraction_sequence | ||
using Test: @test, @testset | ||
|
||
@testset "ContractNetwork" begin | ||
@testset "Contract Vectors of ITensors" begin | ||
i, j, k = Index(2), Index(2), Index(5) | ||
A = ITensor([1.0 1.0; 0.5 1.0], i, j) | ||
B = ITensor([2.0, 1.0], i) | ||
C = ITensor([5.0, 1.0], j) | ||
D = ITensor([-2.0, 3.0, 4.0, 5.0, 1.0], k) | ||
|
||
ABCD_1 = contractnetwork([A, B, C, D]; sequence_alg="leftassociative") | ||
ABCD_2 = contractnetwork([A, B, C, D]; sequence_alg="optimal") | ||
|
||
@test ABCD_1 == ABCD_2 | ||
end | ||
|
||
@testset "Contract One Dimensional Network" begin | ||
dims = (4, 4) | ||
g = named_grid(dims) | ||
l = Dict(e => Index(2) for e in edges(g)) | ||
l = merge(l, Dict(reverse(e) => l[e] for e in edges(g))) | ||
tn = TensorNetwork(g) do v | ||
is = map(e -> l[e], incident_edges(g, v)) | ||
return randn(Tuple(is)) | ||
end | ||
|
||
z1 = contractnetwork(tn; sequence_alg="optimal")[] | ||
z2 = contractnetwork(tn; sequence_alg="leftassociative")[] | ||
|
||
@test abs(z1 - z2) / abs(z1) <= 1e-14 | ||
end | ||
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.