|
| 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 |
0 commit comments