|
| 1 | +module TensorNetworkBenchmarks |
| 2 | + |
| 3 | +include(joinpath(@__DIR__, "..", "utils", "BenchUtils.jl")) |
| 4 | + |
| 5 | +using .BenchUtils |
| 6 | +using BenchmarkTools |
| 7 | +using TensorKit |
| 8 | +using TOML |
| 9 | + |
| 10 | +const SUITE = BenchmarkGroup() |
| 11 | +const all_parameters = TOML.parsefile(joinpath(@__DIR__, "benchparams.toml")) |
| 12 | + |
| 13 | +# mpo contraction |
| 14 | +# --------------- |
| 15 | +function init_mpo_tensors(T, (Vmps, Vmpo, Vphys)) |
| 16 | + A = Tensor(randn, T, Vmps ⊗ Vphys ⊗ Vmps') |
| 17 | + M = Tensor(randn, T, Vmpo ⊗ Vphys ⊗ Vphys' ⊗ Vmpo') |
| 18 | + FL = Tensor(randn, T, Vmps ⊗ Vmpo' ⊗ Vmps') |
| 19 | + FR = Tensor(randn, T, Vmps ⊗ Vmpo ⊗ Vmps') |
| 20 | + return A, M, FL, FR |
| 21 | +end |
| 22 | + |
| 23 | +function benchmark_mpo(A, M, FL, FR) |
| 24 | + return @tensor FL[4, 2, 1] * A[1, 3, 6] * M[2, 5, 3, 7] * conj(A[4, 5, 8]) * FR[6, 7, 8] |
| 25 | +end |
| 26 | + |
| 27 | +function benchmark_mpo!(benchgroup, params::Dict) |
| 28 | + haskey(benchgroup, "mpo") || addgroup!(benchgroup, "mpo") |
| 29 | + bench = benchgroup["mpo"] |
| 30 | + for kwargs in expand_kwargs(params) |
| 31 | + benchmark_mpo!(bench; kwargs...) |
| 32 | + end |
| 33 | + return nothing |
| 34 | +end |
| 35 | +function benchmark_mpo!(bench; sigmas=nothing, T="Float64", I="Trivial", dims) |
| 36 | + T_ = parse_type(T) |
| 37 | + I_ = parse_type(I) |
| 38 | + |
| 39 | + Vs = generate_space.(I_, dims, sigmas) |
| 40 | + init() = init_mpo_tensors(T_, Vs) |
| 41 | + |
| 42 | + bench[T, I, dims, sigmas] = @benchmarkable benchmark_mpo(A, M, FL, FR) setup = ((A, M, FL, FR) = $init()) |
| 43 | + |
| 44 | + return nothing |
| 45 | +end |
| 46 | + |
| 47 | +if haskey(all_parameters, "mpo") |
| 48 | + g = addgroup!(SUITE, "mpo") |
| 49 | + for params in all_parameters["mpo"] |
| 50 | + benchmark_mpo!(g, params) |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +# pepo contraction |
| 55 | +# ---------------- |
| 56 | +function init_pepo_tensors(T, (Vpeps, Vpepo, Vphys, Venv)) |
| 57 | + A = Tensor(randn, T, Vpeps ⊗ Vpeps ⊗ Vphys ⊗ Vpeps' ⊗ Vpeps') |
| 58 | + P = Tensor(randn, T, Vpepo ⊗ Vpepo ⊗ Vphys ⊗ Vphys' ⊗ Vpepo' ⊗ Vpepo') |
| 59 | + FL = Tensor(randn, T, Venv ⊗ Vpeps ⊗ Vpepo' ⊗ Vpeps' ⊗ Venv') |
| 60 | + FD = Tensor(randn, T, Venv ⊗ Vpeps ⊗ Vpepo' ⊗ Vpeps' ⊗ Venv') |
| 61 | + FR = Tensor(randn, T, Venv ⊗ Vpeps ⊗ Vpepo ⊗ Vpeps' ⊗ Venv') |
| 62 | + FU = Tensor(randn, T, Venv ⊗ Vpeps ⊗ Vpepo ⊗ Vpeps' ⊗ Venv') |
| 63 | + return A, P, FL, FD, FR, FU |
| 64 | +end |
| 65 | + |
| 66 | +function benchmark_pepo(A, P, FL, FD, FR, FU) |
| 67 | + return @tensor FL[18, 7, 4, 2, 1] * FU[1, 3, 6, 9, 10] * A[2, 17, 5, 3, 11] * |
| 68 | + P[4, 16, 8, 5, 6, 12] * conj(A[7, 15, 8, 9, 13]) * |
| 69 | + FR[10, 11, 12, 13, 14] * FD[14, 15, 16, 17, 18] |
| 70 | +end |
| 71 | + |
| 72 | +function benchmark_pepo!(benchgroup, params::Dict) |
| 73 | + haskey(benchgroup, "pepo") || addgroup!(benchgroup, "pepo") |
| 74 | + bench = benchgroup["pepo"] |
| 75 | + for kwargs in expand_kwargs(params) |
| 76 | + benchmark_pepo!(bench; kwargs...) |
| 77 | + end |
| 78 | + return nothing |
| 79 | +end |
| 80 | +function benchmark_pepo!(bench; sigmas=nothing, T="Float64", I="Trivial", dims) |
| 81 | + T_ = parse_type(T) |
| 82 | + I_ = parse_type(I) |
| 83 | + |
| 84 | + Vs = generate_space.(I_, dims, sigmas) |
| 85 | + init() = init_pepo_tensors(T_, Vs) |
| 86 | + |
| 87 | + bench[T, I, dims, sigmas] = @benchmarkable benchmark_pepo(A, P, FL, FD, FR, FU) setup = ((A, P, FL, FD, FR, FU) = $init()) |
| 88 | + |
| 89 | + return nothing |
| 90 | +end |
| 91 | + |
| 92 | +if haskey(all_parameters, "pepo") |
| 93 | + g = addgroup!(SUITE, "pepo") |
| 94 | + for params in all_parameters["pepo"] |
| 95 | + benchmark_pepo!(g, params) |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +# mera contraction |
| 100 | +# ---------------- |
| 101 | +function init_mera_tensors(T, V) |
| 102 | + u = Tensor(randn, T, V ⊗ V ⊗ V' ⊗ V') |
| 103 | + w = Tensor(randn, T, V ⊗ V ⊗ V') |
| 104 | + ρ = Tensor(randn, T, V ⊗ V ⊗ V ⊗ V' ⊗ V' ⊗ V') |
| 105 | + h = Tensor(randn, T, V ⊗ V ⊗ V ⊗ V' ⊗ V' ⊗ V') |
| 106 | + return u, w, ρ, h |
| 107 | +end |
| 108 | + |
| 109 | +function benchmark_mera(u, w, ρ, h) |
| 110 | + return @tensor (((((((h[9, 3, 4, 5, 1, 2] * u[1, 2, 7, 12]) * conj(u[3, 4, 11, 13])) * |
| 111 | + (u[8, 5, 15, 6] * w[6, 7, 19])) * |
| 112 | + (conj(u[8, 9, 17, 10]) * conj(w[10, 11, 22]))) * |
| 113 | + ((w[12, 14, 20] * conj(w[13, 14, 23])) * ρ[18, 19, 20, 21, 22, 23])) * |
| 114 | + w[16, 15, 18]) * conj(w[16, 17, 21])) |
| 115 | +end |
| 116 | + |
| 117 | +function benchmark_mera!(benchgroup, params::Dict) |
| 118 | + haskey(benchgroup, "mera") || addgroup!(benchgroup, "mera") |
| 119 | + bench = benchgroup["mera"] |
| 120 | + for kwargs in expand_kwargs(params) |
| 121 | + benchmark_mera!(bench; kwargs...) |
| 122 | + end |
| 123 | + return nothing |
| 124 | +end |
| 125 | + |
| 126 | +function benchmark_mera!(bench; sigmas=nothing, T="Float64", I="Trivial", dims) |
| 127 | + T_ = parse_type(T) |
| 128 | + I_ = parse_type(I) |
| 129 | + |
| 130 | + Vs = generate_space.(I_, dims, sigmas) |
| 131 | + init() = init_mera_tensors(T_, Vs) |
| 132 | + |
| 133 | + bench[T, I, dims, sigmas] = @benchmarkable benchmark_mera(u, w, ρ, h) setup = ((u, w, ρ, h) = $init()) |
| 134 | + |
| 135 | + return nothing |
| 136 | +end |
| 137 | + |
| 138 | +if haskey(all_parameters, "mera") |
| 139 | + g = addgroup!(SUITE, "mera") |
| 140 | + for params in all_parameters["mera"] |
| 141 | + benchmark_mera!(g, params) |
| 142 | + end |
| 143 | +end |
| 144 | + |
| 145 | +end |
0 commit comments