|
| 1 | +using ITensorNetworks |
| 2 | +using ITensorNetworks: |
| 3 | + approx_network_region, |
| 4 | + belief_propagation, |
| 5 | + get_environment, |
| 6 | + contract_inner, |
| 7 | + find_subgraph, |
| 8 | + message_tensors, |
| 9 | + neighbor_vertices, |
| 10 | + nested_graph_leaf_vertices, |
| 11 | + symmetric_gauge, |
| 12 | + vidal_gauge, |
| 13 | + vidal_to_symmetric_gauge, |
| 14 | + norm_network |
| 15 | +using Test |
| 16 | +using Compat |
| 17 | +using Dictionaries |
| 18 | +using ITensors |
| 19 | +using Metis |
| 20 | +using NamedGraphs |
| 21 | +using Observers |
| 22 | +using Random |
| 23 | +using LinearAlgebra |
| 24 | +using SplitApplyCombine |
| 25 | +using OMEinsumContractionOrders |
| 26 | + |
| 27 | +function expect_bp(opname, v, ψ, mts) |
| 28 | + s = siteinds(ψ) |
| 29 | + ψψ = norm_network(ψ) |
| 30 | + numerator_network = approx_network_region( |
| 31 | + ψψ, mts, [(v, 1)]; verts_tn=ITensorNetwork(ITensor[apply(op(opname, s[v]), ψ[v])]) |
| 32 | + ) |
| 33 | + denominator_network = approx_network_region(ψψ, mts, [(v, 1)]) |
| 34 | + return contract(numerator_network)[] / contract(denominator_network)[] |
| 35 | +end |
| 36 | + |
| 37 | +function vertex_array(ψ, v, v⃗ⱼ) |
| 38 | + indsᵥ = unioninds((linkinds(ψ, v => vⱼ) for vⱼ in v⃗ⱼ)...) |
| 39 | + indsᵥ = unioninds(siteinds(ψ, v), indsᵥ) |
| 40 | + ψᵥ = ψ[v] |
| 41 | + ψᵥ /= norm(ψᵥ) |
| 42 | + return array(permute(ψᵥ, indsᵥ)) |
| 43 | +end |
| 44 | + |
| 45 | +function simple_update_bp( |
| 46 | + os, |
| 47 | + ψ::ITensorNetwork; |
| 48 | + maxdim, |
| 49 | + variational_optimization_only=false, |
| 50 | + regauge=false, |
| 51 | + reduced=true, |
| 52 | +) |
| 53 | + println("Simple update, BP") |
| 54 | + ψψ = norm_network(ψ) |
| 55 | + mts = message_tensors(partition(ψψ, group(v -> v[1], vertices(ψψ)))) |
| 56 | + mts = belief_propagation( |
| 57 | + ψψ, mts; contract_kwargs=(; alg="exact"), niters=50, target_precision=1e-5 |
| 58 | + ) |
| 59 | + for layer in eachindex(os) |
| 60 | + @show layer |
| 61 | + o⃗ = os[layer] |
| 62 | + for o in o⃗ |
| 63 | + v⃗ = neighbor_vertices(ψ, o) |
| 64 | + for e in edges(mts) |
| 65 | + @assert order(only(mts[e])) == 2 |
| 66 | + @assert order(only(mts[reverse(e)])) == 2 |
| 67 | + end |
| 68 | + |
| 69 | + @assert length(v⃗) == 2 |
| 70 | + v1, v2 = v⃗ |
| 71 | + |
| 72 | + s1 = find_subgraph((v1, 1), mts) |
| 73 | + s2 = find_subgraph((v2, 1), mts) |
| 74 | + envs = get_environment(ψψ, mts, [(v1, 1), (v1, 2), (v2, 1), (v2, 2)]) |
| 75 | + obs = observer() |
| 76 | + # TODO: Make a version of `apply` that accepts message tensors, |
| 77 | + # and computes the environment and does the message tensor update of the bond internally. |
| 78 | + ψ = apply( |
| 79 | + o, |
| 80 | + ψ; |
| 81 | + envs, |
| 82 | + (observer!)=obs, |
| 83 | + maxdim, |
| 84 | + normalize=true, |
| 85 | + variational_optimization_only, |
| 86 | + nfullupdatesweeps=20, |
| 87 | + symmetrize=true, |
| 88 | + reduced, |
| 89 | + ) |
| 90 | + S = only(obs.singular_values) |
| 91 | + S /= norm(S) |
| 92 | + |
| 93 | + # Update message tensor |
| 94 | + ψψ = norm_network(ψ) |
| 95 | + mts[s1] = ITensorNetwork(dictionary([(v1, 1) => ψψ[v1, 1], (v1, 2) => ψψ[v1, 2]])) |
| 96 | + mts[s2] = ITensorNetwork(dictionary([(v2, 1) => ψψ[v2, 1], (v2, 2) => ψψ[v2, 2]])) |
| 97 | + mts[s1 => s2] = ITensorNetwork(obs.singular_values) |
| 98 | + mts[s2 => s1] = ITensorNetwork(obs.singular_values) |
| 99 | + end |
| 100 | + if regauge |
| 101 | + println("regauge") |
| 102 | + mts = belief_propagation( |
| 103 | + ψψ, mts; contract_kwargs=(; alg="exact"), niters=50, target_precision=1e-5 |
| 104 | + ) |
| 105 | + end |
| 106 | + end |
| 107 | + return ψ, mts |
| 108 | +end |
| 109 | + |
| 110 | +function simple_update_vidal(os, ψ::ITensorNetwork; maxdim, regauge=false) |
| 111 | + println("Simple update, Vidal gauge") |
| 112 | + ψψ = norm_network(ψ) |
| 113 | + mts = message_tensors(partition(ψψ, group(v -> v[1], vertices(ψψ)))) |
| 114 | + mts = belief_propagation( |
| 115 | + ψψ, mts; contract_kwargs=(; alg="exact"), niters=50, target_precision=1e-5 |
| 116 | + ) |
| 117 | + ψ, bond_tensors = vidal_gauge(ψ, mts) |
| 118 | + for layer in eachindex(os) |
| 119 | + @show layer |
| 120 | + o⃗ = os[layer] |
| 121 | + for o in o⃗ |
| 122 | + v⃗ = neighbor_vertices(ψ, o) |
| 123 | + ψ, bond_tensors = apply(o, ψ, bond_tensors; maxdim, normalize=true) |
| 124 | + end |
| 125 | + if regauge |
| 126 | + println("regauge") |
| 127 | + ψ_symmetric, mts = vidal_to_symmetric_gauge(ψ, bond_tensors) |
| 128 | + ψψ = norm_network(ψ_symmetric) |
| 129 | + mts = belief_propagation( |
| 130 | + ψψ, mts; contract_kwargs=(; alg="exact"), niters=50, target_precision=1e-5 |
| 131 | + ) |
| 132 | + ψ, bond_tensors = vidal_gauge(ψ_symmetric, mts) |
| 133 | + end |
| 134 | + end |
| 135 | + return ψ, bond_tensors |
| 136 | +end |
| 137 | + |
| 138 | +function main(; |
| 139 | + seed=5623, |
| 140 | + graph, |
| 141 | + opname, |
| 142 | + dims, |
| 143 | + χ, |
| 144 | + nlayers, |
| 145 | + variational_optimization_only=false, |
| 146 | + regauge=false, |
| 147 | + reduced=true, |
| 148 | +) |
| 149 | + Random.seed!(seed) |
| 150 | + n = prod(dims) |
| 151 | + g = graph(dims) |
| 152 | + s = siteinds("S=1/2", g) |
| 153 | + ψ = randomITensorNetwork(s; link_space=χ) |
| 154 | + es = edges(g) |
| 155 | + os = [ |
| 156 | + [op(opname, s[src(e)]..., s[dst(e)]...; eltype=Float64) for e in es] for _ in 1:nlayers |
| 157 | + ] |
| 158 | + |
| 159 | + # BP SU |
| 160 | + ψ_bp, mts = simple_update_bp( |
| 161 | + os, ψ; maxdim=χ, variational_optimization_only, regauge, reduced |
| 162 | + ) |
| 163 | + # ψ_bp, mts = vidal_to_symmetric_gauge(vidal_gauge(ψ_bp, mts)...) |
| 164 | + |
| 165 | + # Vidal SU |
| 166 | + ψ_vidal, bond_tensors = simple_update_vidal(os, ψ; maxdim=χ, regauge) |
| 167 | + ψ_vidal, mts_vidal = vidal_to_symmetric_gauge(ψ_vidal, bond_tensors) |
| 168 | + |
| 169 | + return ψ_bp, mts, ψ_vidal, mts_vidal |
| 170 | +end |
0 commit comments