Skip to content

Commit de7d4dd

Browse files
authored
Add support for specifying RNGs in tensor network constructors, use StableRNGs.jl in tests (#184)
1 parent e65f9f7 commit de7d4dd

27 files changed

+208
-225
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorNetworks"
22
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
33
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
4-
version = "0.11.9"
4+
version = "0.11.10"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

TODO.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/specialitensornetworks.jl

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using ITensors: delta
22
using ITensors.NDTensors: dim
33
using DataGraphs: IsUnderlyingGraph
44
using Distributions: Distribution
5+
using Random: Random, AbstractRNG
56

67
"""
78
RETURN A TENSOR NETWORK WITH COPY TENSORS ON EACH VERTEX.
@@ -28,37 +29,67 @@ end
2829
"""
2930
Build an ITensor network on a graph specified by the inds network s. Bond_dim is given by link_space and entries are randomised (normal distribution, mean 0 std 1)
3031
"""
31-
function random_tensornetwork(eltype::Type, s::IndsNetwork; kwargs...)
32+
function random_tensornetwork(rng::AbstractRNG, eltype::Type, s::IndsNetwork; kwargs...)
3233
return ITensorNetwork(s; kwargs...) do v
33-
return inds -> itensor(randn(eltype, dim.(inds)...), inds)
34+
return inds -> itensor(randn(rng, eltype, dim.(inds)...), inds)
3435
end
3536
end
3637

38+
function random_tensornetwork(eltype::Type, s::IndsNetwork; kwargs...)
39+
return random_tensornetwork(Random.default_rng(), eltype, s; kwargs...)
40+
end
41+
42+
function random_tensornetwork(rng::AbstractRNG, s::IndsNetwork; kwargs...)
43+
return random_tensornetwork(rng, Float64, s; kwargs...)
44+
end
45+
3746
function random_tensornetwork(s::IndsNetwork; kwargs...)
38-
return random_tensornetwork(Float64, s; kwargs...)
47+
return random_tensornetwork(Random.default_rng(), s; kwargs...)
48+
end
49+
50+
@traitfn function random_tensornetwork(
51+
rng::AbstractRNG, eltype::Type, g::::IsUnderlyingGraph; kwargs...
52+
)
53+
return random_tensornetwork(rng, eltype, IndsNetwork(g); kwargs...)
3954
end
4055

4156
@traitfn function random_tensornetwork(eltype::Type, g::::IsUnderlyingGraph; kwargs...)
42-
return random_tensornetwork(eltype, IndsNetwork(g); kwargs...)
57+
return random_tensornetwork(Random.default_rng(), eltype, g; kwargs...)
58+
end
59+
60+
@traitfn function random_tensornetwork(rng::AbstractRNG, g::::IsUnderlyingGraph; kwargs...)
61+
return random_tensornetwork(rng, Float64, g; kwargs...)
4362
end
4463

4564
@traitfn function random_tensornetwork(g::::IsUnderlyingGraph; kwargs...)
46-
return random_tensornetwork(Float64, IndsNetwork(g); kwargs...)
65+
return random_tensornetwork(Random.default_rng(), g; kwargs...)
4766
end
4867

4968
"""
5069
Build an ITensor network on a graph specified by the inds network s.
5170
Bond_dim is given by link_space and entries are randomized.
5271
The random distribution is based on the input argument `distribution`.
5372
"""
54-
function random_tensornetwork(distribution::Distribution, s::IndsNetwork; kwargs...)
73+
function random_tensornetwork(
74+
rng::AbstractRNG, distribution::Distribution, s::IndsNetwork; kwargs...
75+
)
5576
return ITensorNetwork(s; kwargs...) do v
56-
return inds -> itensor(rand(distribution, dim.(inds)...), inds)
77+
return inds -> itensor(rand(rng, distribution, dim.(inds)...), inds)
5778
end
5879
end
5980

81+
function random_tensornetwork(distribution::Distribution, s::IndsNetwork; kwargs...)
82+
return random_tensornetwork(Random.default_rng(), distribution, s; kwargs...)
83+
end
84+
85+
@traitfn function random_tensornetwork(
86+
rng::AbstractRNG, distribution::Distribution, g::::IsUnderlyingGraph; kwargs...
87+
)
88+
return random_tensornetwork(rng, distribution, IndsNetwork(g); kwargs...)
89+
end
90+
6091
@traitfn function random_tensornetwork(
6192
distribution::Distribution, g::::IsUnderlyingGraph; kwargs...
6293
)
63-
return random_tensornetwork(distribution, IndsNetwork(g); kwargs...)
94+
return random_tensornetwork(Random.default_rng(), distribution, g; kwargs...)
6495
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
2424
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
2525
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2626
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
27+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
2728
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
2829
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2930
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

test/test_additensornetworks.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ using NamedGraphs.NamedGraphGenerators: named_grid
55
using ITensorNetworks: ITensorNetwork, inner_network, random_tensornetwork, siteinds
66
using ITensors: ITensors, apply, op, scalar, inner
77
using LinearAlgebra: norm_sqr
8-
using Random: Random
8+
using StableRNGs: StableRNG
99
using Test: @test, @testset
10-
1110
@testset "add_itensornetworks" begin
12-
Random.seed!(5623)
1311
g = named_grid((2, 2))
1412
s = siteinds("S=1/2", g)
1513
ψ1 = ITensorNetwork(v -> "", s)
@@ -32,8 +30,9 @@ using Test: @test, @testset
3230
rem_edge!(s2, NamedEdge((1, 1) => (1, 2)))
3331

3432
v = rand(vertices(g))
35-
ψ1 = random_tensornetwork(s1; link_space=χ)
36-
ψ2 = random_tensornetwork(s2; link_space=χ)
33+
rng = StableRNG(1234)
34+
ψ1 = random_tensornetwork(rng, s1; link_space=χ)
35+
ψ2 = random_tensornetwork(rng, s2; link_space=χ)
3736

3837
ψ12 = ψ1 + ψ2
3938

test/test_apply.jl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,32 @@ using ITensorNetworks:
1414
using ITensors: ITensors, inner, op
1515
using NamedGraphs.NamedGraphGenerators: named_grid
1616
using NamedGraphs.PartitionedGraphs: PartitionVertex
17-
using Random: Random
1817
using SplitApplyCombine: group
18+
using StableRNGs: StableRNG
1919
using Test: @test, @testset
20-
2120
@testset "apply" begin
22-
Random.seed!(5623)
2321
g_dims = (2, 2)
2422
n = prod(g_dims)
2523
g = named_grid(g_dims)
2624
s = siteinds("S=1/2", g)
2725
χ = 2
28-
ψ = random_tensornetwork(s; link_space=χ)
26+
rng = StableRNG(1234)
27+
ψ = random_tensornetwork(rng, s; link_space=χ)
2928
v1, v2 = (2, 2), (1, 2)
3029
ψψ = norm_sqr_network(ψ)
3130
#Simple Belief Propagation Grouping
3231
bp_cache = BeliefPropagationCache(ψψ, group(v -> v[1], vertices(ψψ)))
3332
bp_cache = update(bp_cache; maxiter=20)
3433
envsSBP = environment(bp_cache, PartitionVertex.([v1, v2]))
35-
3634
ψv = VidalITensorNetwork(ψ)
37-
3835
#This grouping will correspond to calculating the environments exactly (each column of the grid is a partition)
3936
bp_cache = BeliefPropagationCache(ψψ, group(v -> v[1][1], vertices(ψψ)))
4037
bp_cache = update(bp_cache; maxiter=20)
4138
envsGBP = environment(bp_cache, [(v1, "bra"), (v1, "ket"), (v2, "bra"), (v2, "ket")])
42-
4339
inner_alg = "exact"
44-
4540
ngates = 5
46-
4741
for i in 1:ngates
4842
o = op("RandomUnitary", s[v1]..., s[v2]...)
49-
5043
ψOexact = apply(o, ψ; cutoff=1e-16)
5144
ψOSBP = apply(
5245
o,
@@ -79,9 +72,7 @@ using Test: @test, @testset
7972
fGBP =
8073
inner(ψOGBP, ψOexact; alg=inner_alg) /
8174
sqrt(inner(ψOexact, ψOexact; alg=inner_alg) * inner(ψOGBP, ψOGBP; alg=inner_alg))
82-
8375
@test real(fGBP * conj(fGBP)) >= real(fSBP * conj(fSBP))
84-
8576
@test isapprox(real(fSBP * conj(fSBP)), real(fVidal * conj(fVidal)); atol=1e-3)
8677
end
8778
end

test/test_belief_propagation.jl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,27 @@ using LinearAlgebra: eigvals, tr
3131
using NamedGraphs: NamedEdge, NamedGraph
3232
using NamedGraphs.NamedGraphGenerators: named_comb_tree, named_grid
3333
using NamedGraphs.PartitionedGraphs: PartitionVertex, partitionedges
34-
using Random: Random
3534
using SplitApplyCombine: group
35+
using StableRNGs: StableRNG
3636
using Test: @test, @testset
37-
3837
@testset "belief_propagation" begin
3938
ITensors.disable_warn_order()
40-
4139
g = named_grid((3, 3))
4240
s = siteinds("S=1/2", g)
4341
χ = 2
44-
Random.seed!(1234)
45-
ψ = random_tensornetwork(s; link_space=χ)
42+
rng = StableRNG(1234)
43+
ψ = random_tensornetwork(rng, s; link_space=χ)
4644
ψψ = ψ prime(dag(ψ); sites=[])
47-
4845
bpc = BeliefPropagationCache(ψψ)
4946
bpc = update(bpc; maxiter=50, tol=1e-10)
50-
5147
#Test messages are converged
5248
for pe in partitionedges(partitioned_tensornetwork(bpc))
5349
@test update_message(bpc, pe) message(bpc, pe) atol = 1e-8
5450
end
55-
5651
#Test updating the underlying tensornetwork in the cache
5752
v = first(vertices(ψψ))
58-
new_tensor = random_itensor(inds(ψψ[v]))
53+
rng = StableRNG(1234)
54+
new_tensor = random_itensor(rng, inds(ψψ[v]))
5955
bpc_updated = update_factor(bpc, v, new_tensor)
6056
ψψ_updated = tensornetwork(bpc_updated)
6157
@test ψψ_updated[v] == new_tensor
@@ -79,7 +75,8 @@ using Test: @test, @testset
7975
#Test edge case of network which evalutes to 0
8076
χ = 2
8177
g = named_grid((3, 1))
82-
ψ = random_tensornetwork(ComplexF64, g; link_space=χ)
78+
rng = StableRNG(1234)
79+
ψ = random_tensornetwork(rng, ComplexF64, g; link_space=χ)
8380
ψ[(1, 1)] = 0.0 * ψ[(1, 1)]
8481
@test iszero(scalar(ψ; alg="bp"))
8582
end

test/test_binary_tree_partition.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using NamedGraphs.NamedGraphGenerators: named_grid
2323
using NamedGraphs.GraphsExtensions:
2424
is_binary_arborescence, post_order_dfs_vertices, root_vertex
2525
using OMEinsumContractionOrders: OMEinsumContractionOrders
26+
using StableRNGs: StableRNG
2627
using Test: @test, @testset
2728

2829
@testset "test mincut functions on top of MPS" begin
@@ -60,7 +61,8 @@ end
6061
@testset "test _binary_tree_partition_inds of a 2D network" begin
6162
N = (3, 3, 3)
6263
linkdim = 2
63-
network = random_tensornetwork(IndsNetwork(named_grid(N)); link_space=linkdim)
64+
rng = StableRNG(1234)
65+
network = random_tensornetwork(rng, IndsNetwork(named_grid(N)); link_space=linkdim)
6466
tn = Array{ITensor,length(N)}(undef, N...)
6567
for v in vertices(network)
6668
tn[v...] = network[v...]

test/test_contract_deltas.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ using ITensorNetworks:
1717
random_tensornetwork
1818
using NamedGraphs.GraphsExtensions: leaf_vertices, root_vertex
1919
using NamedGraphs.NamedGraphGenerators: named_grid
20+
using StableRNGs: StableRNG
2021
using Test: @test, @testset
2122

2223
@testset "test _contract_deltas with no deltas" begin
@@ -41,7 +42,8 @@ end
4142
@testset "test _contract_deltas over partition" begin
4243
N = (3, 3, 3)
4344
linkdim = 2
44-
network = random_tensornetwork(IndsNetwork(named_grid(N)); link_space=linkdim)
45+
rng = StableRNG(1234)
46+
network = random_tensornetwork(rng, IndsNetwork(named_grid(N)); link_space=linkdim)
4547
tn = Array{ITensor,length(N)}(undef, N...)
4648
for v in vertices(network)
4749
tn[v...] = network[v...]

test/test_contraction_sequence.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ using ITensorNetworks:
55
using ITensors: ITensors, contract
66
using NamedGraphs.NamedGraphGenerators: named_grid
77
using OMEinsumContractionOrders: OMEinsumContractionOrders
8-
using Random: Random
8+
using StableRNGs: StableRNG
99
using Test: @test, @testset
10-
11-
Random.seed!(1234)
1210
@testset "contraction_sequence" begin
1311
ITensors.@disable_warn_order begin
1412
dims = (2, 3)
1513
g = named_grid(dims)
1614
s = siteinds("S=1/2", g)
1715
χ = 10
18-
ψ = random_tensornetwork(s; link_space=χ)
16+
rng = StableRNG(1234)
17+
ψ = random_tensornetwork(rng, s; link_space=χ)
1918
tn = norm_sqr_network(ψ)
2019
seq_optimal = contraction_sequence(tn; alg="optimal")
2120
res_optimal = contract(tn; sequence=seq_optimal)[]

0 commit comments

Comments
 (0)