Skip to content

Commit 93742b8

Browse files
authored
Add sweep_contract_dangling
Added sweep_contract_dangling (and associated comments, tests, and examples) which allows for networks with dangling lets to be contracted.
1 parent f8369af commit 93742b8

File tree

6 files changed

+164
-5
lines changed

6 files changed

+164
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SweepContractor"
22
uuid = "75a5deae-e917-4509-af32-a989148c8d5f"
33
authors = ["Christopher Chubb <[email protected]>"]
4-
version = "0.1.5"
4+
version = "0.1.6"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

examples/example_ABCD.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SweepContractor
1+
using Main.SweepContractor
22

33
LTN = LabelledTensorNetwork{Char}()
44
LTN['A'] = Tensor(['D','B'], [i^2-2j for i=0:2, j=0:2], 0, 1)
@@ -14,6 +14,13 @@ end
1414

1515
sweep = sweep_contract(LTN, 2, 4; fast=true)
1616

17+
(mps,dangexp) = sweep_contract_dangling(LTN, 2, 4; fast=true)
18+
dang=0.0
19+
for i1=1:2, i2=1:2, da=1:3, db=1:3, dc=1:3
20+
global dang+=mps[1][1,da,i1]*mps[2][i1,db,i2]*mps[3][i2,dc,1]*LTN['D'].arr[da,db,dc]
21+
end
22+
1723
println("example_ABCD")
1824
println("brute = $brute")
1925
println("sweep = $(ldexp(sweep...))")
26+
println("sweep_dangling = $(ldexp(dang,dangexp))")

src/SweepContractor.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module SweepContractor
1313
include("tn_utilities.jl")
1414

1515
export sweep_contract!, sweep_contract
16+
export sweep_contract_dangling, sweep_contract_dangling!
1617
# The sweep contraction algorithm
1718
include("sweep_contract.jl")
19+
include("sweep_contract_dangling.jl")
1820
end

src/sweep_contract.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ function sweep_contract!(TN::TensorNetwork, χ::Int, τ::Int;
129129

130130
sort!(TN)
131131

132-
N = length(TN)
133-
134132
resexp = 0
135133
count = 0
136134

src/sweep_contract_dangling.jl

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""
2+
sweep_contract_dangling(LTN::LabelledTensorNetwork, χ, τ;
3+
fast=false, valid=false, planar=false, connected=false, report=false)
4+
sweep_contract_dangling(TN::TensorNetwork, χ, τ;
5+
fast=false, valid=false, planar=false, connected=false, report=false)
6+
7+
A modified version of `sweep_contract` which can be used to evaluate open tensor networks
8+
which possess dangling legs. This is done by halting the sweep procedure before the final
9+
tensor is contracted and returning the MPS approximating the network so far. To do this a
10+
dummy tensor must be present in the network which is above the rest of the network, i.e. has
11+
a higher y coordinate.
12+
13+
Similar to sweep_contract underflow/overflow issues are avoided by return the MPS in a
14+
floating point format. The output is returned as a tuple `(mps::MPS, i::Int64)` where `mps`
15+
has a 2-norm in [1,2), representing the MPS `mps*2^i`.
16+
17+
For other details see the documentation of `sweep_contract`.
18+
"""
19+
sweep_contract_dangling(LTN::LabelledTensorNetwork, χ::Int, τ::Int;
20+
fast=false, valid=false, planar=false, connected=false, report=false) =
21+
sweep_contract_dangling!(deepcopy(LTN), χ, τ;
22+
fast=fast, planar=planar, connected=connected, report=report)
23+
24+
sweep_contract_dangling(TN::TensorNetwork, χ::Int, τ::Int;
25+
fast=false, valid=false, planar=false, connected=false, report=false) =
26+
sweep_contract_dangling!(deepcopy(TN), χ, τ;
27+
fast=fast, planar=planar, connected=connected, report=report)
28+
29+
"""
30+
sweep_contract_dangling!(LTN::LabelledTensorNetwork, χ, τ;
31+
fast=false, valid=false, planar=false, connected=false, report=false)
32+
sweep_contract_dangling!(TN::TensorNetwork, χ, τ;
33+
fast=false, valid=false, planar=false, connected=false, report=false)
34+
35+
The mutating form of `sweep_contract_dangling`.
36+
"""
37+
sweep_contract_dangling!(LTN::LabelledTensorNetwork, χ::Int, τ::Int;
38+
fast=false, valid=false, planar=false, connected=false, report=false) =
39+
sweep_contract_dangling!(delabel(LTN), χ, τ;
40+
fast=fast, planar=planar, connected=connected, report=report)
41+
42+
function sweep_contract_dangling!(TN::TensorNetwork, χ::Int, τ::Int;
43+
fast=false,valid=false,planar=false,connected=false,report=false)::Tuple{MPS,Int}
44+
if !fast
45+
valid || checkvalid(TN)
46+
planar || planarise!(TN)
47+
connected || connect!(hull!(TN))
48+
end
49+
50+
sort!(TN)
51+
52+
resexp = 0
53+
count = 0
54+
55+
MPS_t = [ones(1,1,1)]
56+
MPS_i = Int[]
57+
58+
for (i,t) enumerate(TN)
59+
if i==length(TN)
60+
break;
61+
end
62+
ind_up = Int[]
63+
ind_do = Int[]
64+
for n t.adj
65+
if TN[n]>t
66+
push!(ind_up, n)
67+
elseif TN[n]<t
68+
push!(ind_do, n)
69+
else
70+
throw(InvalidTNError("Overlapping tensors"))
71+
end
72+
end
73+
sort!(ind_up, by=λ->atan(TN[λ].x-t.x,TN[λ].y-t.y))
74+
sort!(ind_do, by=λ->atan(TN[λ].x-t.x,t.y-TN[λ].y))
75+
σ = permutebetween(t.adj, [ind_do; ind_up])
76+
t.arr = permutedims(t.arr, σ)
77+
s = size(t.arr)
78+
t.arr = reshape(t.arr,(prod(s[1:length(ind_do)]),s[length(ind_do)+1:end]...))
79+
80+
if isempty(MPS_i)
81+
MPS_t = splitMPStensor(MPS_t[1][1]*reshape(t.arr,(size(t.arr)...,1)))
82+
MPS_i = ind_up
83+
else
84+
lo = findfirst(isequal(i), MPS_i)
85+
hi = findlast(isequal(i), MPS_i)
86+
87+
isnothing(lo) && throw(InvalidTNError("Disconnected TN"))
88+
89+
X::Array{Float64} = MPS_t[lo]
90+
for j lo+1:hi
91+
finalsize = (size(X,1),size(X,2)*size(MPS_t[j],2),size(MPS_t[j],3))
92+
X = reshape(X,(size(X,1)*size(X,2),size(X,3)))*
93+
reshape(MPS_t[j],(size(MPS_t[j],1),size(MPS_t[j],2)*size(MPS_t[j],3)))
94+
X = reshape(X,finalsize)
95+
end
96+
X = permutedims(X,[1,3,2])
97+
M = reshape(t.arr,(size(t.arr,1),prod(size(t.arr)[2:end])))
98+
X = reshape(
99+
reshape(X,(size(X,1)*size(X,2),size(X,3)))*M,
100+
(size(X,1),size(X,2),size(M,2))
101+
)
102+
X = permutedims(X,[1,3,2])
103+
X = reshape(X,(size(X,1),size(t.arr)[2:end]...,size(X,3)))
104+
105+
MPS_i = [MPS_i[1:lo-1]; ind_up; MPS_i[hi+1:end]]
106+
if ndims(X)!=2
107+
MPS_t = [MPS_t[1:lo-1]; splitMPStensor(X); MPS_t[hi+1:end]]
108+
elseif isempty(MPS_i)
109+
MPS_t=[reshape([X[1]],(1,1,1))]
110+
elseif lo>1
111+
s = size(MPS_t[lo-1])
112+
MPS_t[lo-1] = reshape(
113+
reshape(MPS_t[lo-1],(s[1]*s[2],s[3]))*X,
114+
(s[1],s[2],size(X,2))
115+
)
116+
MPS_t = [MPS_t[1:lo-1]; MPS_t[hi+1:end]]
117+
else
118+
s = size(MPS_t[hi+1])
119+
MPS_t[hi+1] = reshape(
120+
X*reshape(MPS_t[hi+1],(s[1],s[2]*s[3])),
121+
(size(X,1),s[2],s[3])
122+
)
123+
MPS_t = [MPS_t[1:lo-1]; MPS_t[hi+1:end]]
124+
end
125+
126+
if any(size.(MPS_t,3).>τ)
127+
count += 1
128+
truncMPS!(MPS_t, χ)
129+
h = Int(floor(log2(LinearAlgebra.norm(MPS_t[1]))))
130+
resexp += h
131+
MPS_t[1] /= exp2(h)
132+
end
133+
end
134+
end
135+
136+
report && println("Number of truncations: $count")
137+
138+
truncMPS!(MPS_t, χ)
139+
h = Int(floor(log2(LinearAlgebra.norm(MPS_t[1]))))
140+
resexp += h
141+
MPS_t[1] /= exp2(h)
142+
143+
return (MPS_t,resexp);
144+
end

test/runtests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ using Test
1010

1111
brute = 0.0
1212
for ab=1:3, ad=1:3, bc=1:3, bd=1:3, cd=1:3
13-
brute += LTN['A'].arr[ad,ab] * LTN['B'].arr[ab,bd,bc] * LTN['C'].arr[bc,cd] *
13+
global brute += LTN['A'].arr[ad,ab] * LTN['B'].arr[ab,bd,bc] * LTN['C'].arr[bc,cd] *
1414
LTN['D'].arr[ad,bd,cd]
1515
end
1616

1717
sweep = sweep_contract(LTN, 2, 4; fast=true)
18+
19+
(mps,dangexp) = sweep_contract_dangling(LTN, 2, 4; fast=true)
20+
dang=0.0
21+
for i1=1:2, i2=1:2, da=1:3, db=1:3, dc=1:3
22+
global dang+=mps[1][1,da,i1]*mps[2][i1,db,i2]*mps[3][i2,dc,1]*LTN['D'].arr[da,db,dc]
23+
end
24+
1825
@test ldexp(sweep...) == brute
26+
@test ldexp(sweep...) ldexp(dang,dangexp)
1927
end
2028

2129

0 commit comments

Comments
 (0)