From e65f9f7fb26d33c38353dc40f710c8bd186f1fa5 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 17 May 2024 00:29:16 -0400 Subject: [PATCH] Use `random_itensor`, `random_mps`, etc. (#183) --- Project.toml | 6 +++--- .../ITensorNetworksOMEinsumContractionOrdersExt.jl | 2 +- src/lib/ModelNetworks/src/ModelNetworks.jl | 4 ++-- src/mpo_mps_compatibility.jl | 4 ++-- test/test_belief_propagation.jl | 4 ++-- test/test_binary_tree_partition.jl | 10 +++++----- test/test_contract_deltas.jl | 4 ++-- test/test_forms.jl | 4 ++-- test/test_gauging.jl | 4 ++-- test/test_itensornetwork.jl | 2 +- test/test_itensorsextensions.jl | 4 ++-- .../test_solvers/test_contract.jl | 6 +++--- test/test_treetensornetworks/test_solvers/test_dmrg.jl | 6 +++--- test/test_ttno.jl | 4 ++-- test/test_ttns.jl | 4 ++-- 15 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Project.toml b/Project.toml index 57d2753c..1aa4351d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ITensorNetworks" uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7" authors = ["Matthew Fishman , Joseph Tindall and contributors"] -version = "0.11.8" +version = "0.11.9" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" @@ -58,8 +58,8 @@ DocStringExtensions = "0.9" EinExprs = "0.6.4" Graphs = "1.8" GraphsFlows = "0.1.1" -ITensorMPS = "0.1" -ITensors = "0.4, 0.5, 0.6" +ITensorMPS = "0.2.2" +ITensors = "0.6.8" IsApprox = "0.1" IterTools = "1.4.0" KrylovKit = "0.6, 0.7" diff --git a/ext/ITensorNetworksOMEinsumContractionOrdersExt/ITensorNetworksOMEinsumContractionOrdersExt.jl b/ext/ITensorNetworksOMEinsumContractionOrdersExt/ITensorNetworksOMEinsumContractionOrdersExt.jl index 6511327f..feee0d15 100644 --- a/ext/ITensorNetworksOMEinsumContractionOrdersExt/ITensorNetworksOMEinsumContractionOrdersExt.jl +++ b/ext/ITensorNetworksOMEinsumContractionOrdersExt/ITensorNetworksOMEinsumContractionOrdersExt.jl @@ -47,7 +47,7 @@ Returns a [`NestedEinsum`](@ref) instance. ```jldoctest julia> using ITensors, ITensorContractionOrders julia> i, j, k, l = Index(4), Index(5), Index(6), Index(7); -julia> x, y, z = randomITensor(i, j), randomITensor(j, k), randomITensor(k, l); +julia> x, y, z = random_itensor(i, j), random_itensor(j, k), random_itensor(k, l); julia> net = optimize_contraction([x, y, z]; optimizer=TreeSA()); ``` """ diff --git a/src/lib/ModelNetworks/src/ModelNetworks.jl b/src/lib/ModelNetworks/src/ModelNetworks.jl index 41fde3b1..88fee784 100644 --- a/src/lib/ModelNetworks/src/ModelNetworks.jl +++ b/src/lib/ModelNetworks/src/ModelNetworks.jl @@ -1,7 +1,7 @@ module ModelNetworks using Graphs: degree, dst, edges, src using ..ITensorNetworks: IndsNetwork, delta_network, insert_linkinds, itensor -using ITensors: commoninds, diagITensor, inds, noprime +using ITensors: commoninds, diag_itensor, inds, noprime using LinearAlgebra: Diagonal, eigen using NamedGraphs: NamedGraph @@ -21,7 +21,7 @@ function ising_network( tn = delta_network(eltype, s) if (szverts != nothing) for v in szverts - tn[v] = diagITensor(eltype[1, -1], inds(tn[v])) + tn[v] = diag_itensor(eltype[1, -1], inds(tn[v])) end end for edge in edges(tn) diff --git a/src/mpo_mps_compatibility.jl b/src/mpo_mps_compatibility.jl index a0c0b228..ac6d43f5 100644 --- a/src/mpo_mps_compatibility.jl +++ b/src/mpo_mps_compatibility.jl @@ -9,9 +9,9 @@ function ITensorMPS.MPO(opsum_sum::Sum{<:OpSum}, s::IndsNetwork) return ITensorMPS.MPO(sum(Ops.terms(opsum_sum)), s) end -function ITensorMPS.randomMPS(s::IndsNetwork, args...; kwargs...) +function ITensorMPS.random_mps(s::IndsNetwork, args...; kwargs...) s_linear = [only(s[v]) for v in 1:nv(s)] - return ITensorMPS.randomMPS(s_linear, args...; kwargs...) + return ITensorMPS.random_mps(s_linear, args...; kwargs...) end function ITensorMPS.MPS(s::IndsNetwork, args...; kwargs...) diff --git a/test/test_belief_propagation.jl b/test/test_belief_propagation.jl index ff8aa2e9..f960bb8b 100644 --- a/test/test_belief_propagation.jl +++ b/test/test_belief_propagation.jl @@ -24,7 +24,7 @@ using ITensorNetworks: update, update_factor, update_message -using ITensors: ITensors, ITensor, combiner, dag, inds, inner, op, prime, randomITensor +using ITensors: ITensors, ITensor, combiner, dag, inds, inner, op, prime, random_itensor using ITensorNetworks.ModelNetworks: ModelNetworks using ITensors.NDTensors: array using LinearAlgebra: eigvals, tr @@ -55,7 +55,7 @@ using Test: @test, @testset #Test updating the underlying tensornetwork in the cache v = first(vertices(ψψ)) - new_tensor = randomITensor(inds(ψψ[v])) + new_tensor = random_itensor(inds(ψψ[v])) bpc_updated = update_factor(bpc, v, new_tensor) ψψ_updated = tensornetwork(bpc_updated) @test ψψ_updated[v] == new_tensor diff --git a/test/test_binary_tree_partition.jl b/test/test_binary_tree_partition.jl index 970d7f1b..7b2b8050 100644 --- a/test/test_binary_tree_partition.jl +++ b/test/test_binary_tree_partition.jl @@ -3,7 +3,7 @@ using DataGraphs: DataGraph, underlying_graph, vertex_data using Graphs: add_vertex!, vertices # Trigger package extension. using GraphsFlows: GraphsFlows -using ITensors: Index, ITensor, contract, noncommoninds, randomITensor +using ITensors: Index, ITensor, contract, noncommoninds, random_itensor using ITensorMPS: MPS using ITensorNetworks: _DensityMartrixAlgGraph, @@ -35,7 +35,7 @@ using Test: @test, @testset o = Index(2, "o") p = Index(2, "p") - T = randomITensor(i, j, k, l, m, n, o, p) + T = random_itensor(i, j, k, l, m, n, o, p) M = MPS(T, (i, j, k, l, m, n, o, p); cutoff=1e-5, maxdim=500) tn = ITensorNetwork(M[:]) for out in [binary_tree_structure(tn), path_graph_structure(tn)] @@ -75,7 +75,7 @@ end @testset "test partition with mincut_recursive_bisection alg of disconnected tn" begin inds = [Index(2, "$i") for i in 1:5] - tn = ITensorNetwork([randomITensor(i) for i in inds]) + tn = ITensorNetwork([random_itensor(i) for i in inds]) par = _partition(tn, binary_tree_structure(tn); alg="mincut_recursive_bisection") network = mapreduce(v -> collect(eachtensor(par[v])), vcat, vertices(par)) @test isapprox(contract(tn), contract(network)) @@ -88,7 +88,7 @@ end l = Index(2, "l") m = Index(2, "m") for dtype in (Float64, Complex{Float64}) - T = randomITensor(dtype, i, j, k, l, m) + T = random_itensor(dtype, i, j, k, l, m) M = MPS(T, (i, j, k, l, m); cutoff=1e-5, maxdim=5) network = M[:] out1 = contract(network...) @@ -126,7 +126,7 @@ end k = Index(2, "k") l = Index(2, "l") m = Index(2, "m") - T = randomITensor(i, j, k, l, m) + T = random_itensor(i, j, k, l, m) M = MPS(T, (i, j, k, l, m); cutoff=1e-5, maxdim=5) tn = ITensorNetwork(M[:]) out_tree = path_graph_structure(tn) diff --git a/test/test_contract_deltas.jl b/test/test_contract_deltas.jl index fa6ade3b..75e92870 100644 --- a/test/test_contract_deltas.jl +++ b/test/test_contract_deltas.jl @@ -2,7 +2,7 @@ using Graphs: dfs_tree, nv, vertices # Trigger package extension. using GraphsFlows: GraphsFlows -using ITensors: Index, ITensor, delta, noncommoninds, randomITensor +using ITensors: Index, ITensor, delta, noncommoninds, random_itensor using ITensorNetworks: IndsNetwork, ITensorNetwork, @@ -21,7 +21,7 @@ using Test: @test, @testset @testset "test _contract_deltas with no deltas" begin i = Index(2, "i") - t = randomITensor(i) + t = random_itensor(i) tn = _contract_deltas(ITensorNetwork([t])) @test tn[1] == t end diff --git a/test/test_forms.jl b/test/test_forms.jl index be0b52a3..41561f0b 100644 --- a/test/test_forms.jl +++ b/test/test_forms.jl @@ -20,7 +20,7 @@ using ITensorNetworks: tensornetwork, union_all_inds, update -using ITensors: contract, dag, inds, prime, randomITensor +using ITensors: contract, dag, inds, prime, random_itensor using LinearAlgebra: norm using Test: @test, @testset using Random: Random @@ -52,7 +52,7 @@ using Random: Random @test isempty(flatten_siteinds(qf)) v = (1, 1) - new_tensor = randomITensor(inds(ψket[v])) + new_tensor = random_itensor(inds(ψket[v])) qf_updated = update(qf, v, copy(new_tensor)) @test tensornetwork(qf_updated)[bra_vertex(qf_updated, v)] ≈ diff --git a/test/test_gauging.jl b/test/test_gauging.jl index 2c8b6f8a..8b328a69 100644 --- a/test/test_gauging.jl +++ b/test/test_gauging.jl @@ -9,7 +9,7 @@ using ITensorNetworks: random_tensornetwork, siteinds, update -using ITensors: diagITensor, inds, inner +using ITensors: diag_itensor, inds, inner using ITensors.NDTensors: vector using LinearAlgebra: diag using NamedGraphs.NamedGraphGenerators: named_grid @@ -43,7 +43,7 @@ using Test: @test, @testset #Test all message tensors are approximately diagonal even when we keep running BP bp_cache = update(bp_cache; maxiter=10) for m_e in values(messages(bp_cache)) - @test diagITensor(vector(diag(only(m_e))), inds(only(m_e))) ≈ only(m_e) atol = 1e-8 + @test diag_itensor(vector(diag(only(m_e))), inds(only(m_e))) ≈ only(m_e) atol = 1e-8 end end end diff --git a/test/test_itensornetwork.jl b/test/test_itensornetwork.jl index 36d29657..ed4864b8 100644 --- a/test/test_itensornetwork.jl +++ b/test/test_itensornetwork.jl @@ -30,7 +30,7 @@ using ITensors: itensor, onehot, order, - randomITensor, + random_itensor, scalartype, sim, uniqueinds diff --git a/test/test_itensorsextensions.jl b/test/test_itensorsextensions.jl index b2438780..1a60b247 100644 --- a/test/test_itensorsextensions.jl +++ b/test/test_itensorsextensions.jl @@ -10,7 +10,7 @@ using ITensors: noprime, op, prime, - randomITensor, + random_itensor, replaceind, replaceinds, sim @@ -53,7 +53,7 @@ Random.seed!(1234) n in (2, 3, 5, 10) i, j = Index.(([QN() => n], [QN() => n])) - A = randomITensor(elt, i, j) + A = random_itensor(elt, i, j) P = A * prime(dag(A), i) sqrtP = map_eigvals(sqrt, P, i, i'; ishermitian=true) inv_P = dag(map_eigvals(inv, P, i, i'; ishermitian=true)) diff --git a/test/test_treetensornetworks/test_solvers/test_contract.jl b/test/test_treetensornetworks/test_solvers/test_contract.jl index 6c2c7e48..cc7dfaaf 100644 --- a/test/test_treetensornetworks/test_solvers/test_contract.jl +++ b/test/test_treetensornetworks/test_solvers/test_contract.jl @@ -150,11 +150,11 @@ end nbit = 3 sites = siteinds("Qubit", nbit) - # randomMPO does not support linkdims keyword. + # random_mpo does not support linkdims keyword. M1 = replaceprime( - ITensorMPS.randomMPO(sites) + ITensorMPS.randomMPO(sites), 1 => 2, 0 => 1 + ITensorMPS.random_mpo(sites) + ITensorMPS.random_mpo(sites), 1 => 2, 0 => 1 ) - M2 = ITensorMPS.randomMPO(sites) + ITensorMPS.randomMPO(sites) + M2 = ITensorMPS.random_mpo(sites) + ITensorMPS.random_mpo(sites) M12_ref = contract(M1, M2; alg="naive") t12_ref = ttn([M12_ref[v] for v in eachindex(M12_ref)]) diff --git a/test/test_treetensornetworks/test_solvers/test_dmrg.jl b/test/test_treetensornetworks/test_solvers/test_dmrg.jl index 881ffc5b..2f638e90 100644 --- a/test/test_treetensornetworks/test_solvers/test_dmrg.jl +++ b/test/test_treetensornetworks/test_solvers/test_dmrg.jl @@ -207,7 +207,7 @@ end vmap = Dictionary(collect(vertices(s))[linear_order], 1:length(linear_order)) sline = only.(collect(vertex_data(s)))[linear_order] Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], os), sline) - psiline = ITensorMPS.randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20) + psiline = ITensorMPS.random_mps(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20) e2, psi2 = dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0) @test inner(psi', H, psi) ≈ inner(psi2', Hline, psi2) atol = 1e-5 @@ -242,7 +242,7 @@ end # get MPS / MPO with JW string result ITensors.disable_auto_fermion() Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], os), sline) - psiline = ITensorMPS.randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20) + psiline = ITensorMPS.random_mps(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20) e_jw, psi_jw = dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0) ITensors.enable_auto_fermion() @@ -261,7 +261,7 @@ end # Compare to `ITensors.MPO` version of `dmrg` Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], os), sline) - psiline = ITensorMPS.randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20) + psiline = ITensorMPS.random_mps(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20) e2, psi2 = dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0) @test inner(psi', H, psi) ≈ inner(psi2', Hline, psi2) atol = 1e-5 diff --git a/test/test_ttno.jl b/test/test_ttno.jl index 95118ba1..3a5933ef 100644 --- a/test/test_ttno.jl +++ b/test/test_ttno.jl @@ -1,7 +1,7 @@ @eval module $(gensym()) using Graphs: vertices using ITensorNetworks: ttn, contract, ortho_region, siteinds, union_all_inds -using ITensors: @disable_warn_order, prime, randomITensor +using ITensors: @disable_warn_order, prime, random_itensor using LinearAlgebra: norm using NamedGraphs.NamedGraphGenerators: named_comb_tree using Random: shuffle @@ -24,7 +24,7 @@ using Test: @test, @testset cutoff = 1e-10 sites_o = [is_isp[v] for v in vertex_order] # create random ITensor with these indices - O = randomITensor(sites_o...) + O = random_itensor(sites_o...) # dense TTN constructor from IndsNetwork @disable_warn_order o1 = ttn(O, is_isp; cutoff) root_vertex = only(ortho_region(o1)) diff --git a/test/test_ttns.jl b/test/test_ttns.jl index 0a06e6e8..efe1edcc 100644 --- a/test/test_ttns.jl +++ b/test/test_ttns.jl @@ -2,7 +2,7 @@ using DataGraphs: vertex_data using Graphs: vertices using ITensorNetworks: ttn, contract, ortho_region, siteinds -using ITensors: @disable_warn_order, randomITensor +using ITensors: @disable_warn_order, random_itensor using LinearAlgebra: norm using NamedGraphs.NamedGraphGenerators: named_comb_tree using Random: shuffle @@ -22,7 +22,7 @@ using Test: @test, @testset @testset "Construct TTN from ITensor or Array" begin cutoff = 1e-10 # create random ITensor with these indices - S = randomITensor(vertex_data(is)...) + S = random_itensor(vertex_data(is)...) # dense TTN constructor from IndsNetwork @disable_warn_order s1 = ttn(S, is; cutoff) root_vertex = only(ortho_region(s1))