Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
02d33db
Working ContractNetwork
JoeyT1994 Oct 2, 2025
4d43eb5
Better tolerance check on test
JoeyT1994 Oct 3, 2025
5dcecfb
Contract Network Stuff
JoeyT1994 Oct 5, 2025
b56b752
Weak Dependencies
JoeyT1994 Oct 5, 2025
6543ad4
External Dependency
JoeyT1994 Oct 5, 2025
87647df
Fix imports
JoeyT1994 Oct 5, 2025
9f218d7
Force specification of contract alg
JoeyT1994 Oct 5, 2025
433face
Merge branch 'main' into Contract
JoeyT1994 Oct 5, 2025
6c5ac55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2025
4c16d56
Runic Formatting
JoeyT1994 Oct 5, 2025
d5cdf3f
Merge branch 'Contract' of github.com:JoeyT1994/ITensorNetworksNext.j…
JoeyT1994 Oct 5, 2025
edf78b7
Revert Project toml slightly
JoeyT1994 Oct 5, 2025
f345f6c
Remove extras from Project.toml
JoeyT1994 Oct 6, 2025
074e347
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
8187ca7
Add to weakdeps
JoeyT1994 Oct 6, 2025
7a2a24c
Merge branch 'Contract' of github.com:JoeyT1994/ITensorNetworksNext.j…
JoeyT1994 Oct 6, 2025
23cca48
Merge remote-tracking branch 'origin/main' into Contract
JoeyT1994 Oct 7, 2025
37d9a76
Merge
JoeyT1994 Oct 21, 2025
89d383d
Getting lazy to work
JoeyT1994 Oct 23, 2025
c73d264
Working contract
JoeyT1994 Oct 23, 2025
6d7d2d5
Merge latest changes
JoeyT1994 Oct 23, 2025
aa0a997
Attempt to fix the toml
JoeyT1994 Oct 23, 2025
1517613
Attempt to fix the toml
JoeyT1994 Oct 23, 2025
2cabf12
Attempt to fix the toml
JoeyT1994 Oct 23, 2025
1fb1ea7
Update ext/ITensorNetworksNextTensorOperationsExt/ITensorNetworksNext…
JoeyT1994 Oct 23, 2025
e026190
ITensorBase back to weak dep
JoeyT1994 Oct 23, 2025
3ddc3c2
Update src/lazynameddimsarrays.jl
JoeyT1994 Oct 23, 2025
f3be6c5
Updates
JoeyT1994 Oct 23, 2025
744b283
Updates
JoeyT1994 Oct 23, 2025
28b33ff
Updates
JoeyT1994 Oct 23, 2025
8c95173
Fix Bug
JoeyT1994 Oct 24, 2025
3ff8f30
Update src/contractnetwork.jl
JoeyT1994 Oct 24, 2025
25e8b1f
Update src/contractnetwork.jl
JoeyT1994 Oct 24, 2025
a4b0224
Update src/contractnetwork.jl
JoeyT1994 Oct 24, 2025
bbe0bc0
Have sequence alg vs sequence
JoeyT1994 Oct 24, 2025
39c4bc6
Have sequence alg vs sequence
JoeyT1994 Oct 24, 2025
fd1c7e1
Fix typo
JoeyT1994 Oct 24, 2025
25b2cf6
Fix typo
JoeyT1994 Oct 24, 2025
7f27cdb
Fix default
JoeyT1994 Oct 24, 2025
5a7dc37
Contractnetwork -> contract_network
JoeyT1994 Oct 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Project.toml
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"
1 change: 1 addition & 0 deletions src/ITensorNetworksNext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module ITensorNetworksNext

include("abstracttensornetwork.jl")
include("tensornetwork.jl")
include("contractnetwork.jl")

end
39 changes: 39 additions & 0 deletions src/contractnetwork.jl
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
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Expand All @@ -20,4 +21,5 @@ NamedDimsArrays = "0.7.14"
NamedGraphs = "0.6.8"
SafeTestsets = "0.1"
Suppressor = "0.2.8"
TensorOperations = "5.3.1"
Test = "1.10"
38 changes: 38 additions & 0 deletions test/test_contractnetwork.jl
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
Loading