Skip to content

Commit 0ac8b57

Browse files
committed
Add back ITensorMPS to some tests
1 parent 1632a70 commit 0ac8b57

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
1010
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
1111
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1212
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
13+
ITensorMPS = "0d1a4710-d33b-49a5-8f18-73bdf49b47e2"
1314
ITensorNetworks = "2919e153-833c-4bdc-8836-1ea460a35fc7"
1415
ITensorUnicodePlots = "73163f41-4a9e-479f-8353-73bf94dbd758"
1516
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
@@ -43,6 +44,7 @@ EinExprs = "0.6.8"
4344
Glob = "1.3.1"
4445
Graphs = "1.12.0"
4546
GraphsFlows = "0.1.1"
47+
ITensorMPS = "0.3.6"
4648
ITensorNetworks = "0.13.0"
4749
ITensorUnicodePlots = "0.1.6"
4850
ITensors = "0.7, 0.8, 0.9"

test/test_opsum_to_ttn.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using ITensors:
1313
dag,
1414
inds,
1515
removeqns
16+
using ITensorMPS: ITensorMPS
1617
using ITensors.NDTensors: matrix
1718
using ITensorNetworks: ITensorNetworks, OpSum, ttn, siteinds
1819
using ITensorNetworks.ITensorsExtensions: replace_vertices
@@ -59,6 +60,26 @@ end
5960
# root_vertex = (1, 2)
6061
# println(leaf_vertices(is))
6162

63+
@testset "Svd approach" for root_vertex in leaf_vertices(is)
64+
# get TTN Hamiltonian directly
65+
Hsvd = ttn(H, is; root_vertex, cutoff=1e-10)
66+
# get corresponding MPO Hamiltonian
67+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], H), sites)
68+
# compare resulting dense Hamiltonians
69+
@disable_warn_order begin
70+
Tttno = prod(Hline)
71+
Tmpo = contract(Hsvd)
72+
end
73+
@test Tttno Tmpo rtol = 1e-6
74+
75+
Hsvd_lr = ttn(Hlr, is; root_vertex, cutoff=1e-10)
76+
Hline_lr = ITensorMPS.MPO(replace_vertices(v -> vmap[v], Hlr), sites)
77+
@disable_warn_order begin
78+
Tttno_lr = prod(Hline_lr)
79+
Tmpo_lr = contract(Hsvd_lr)
80+
end
81+
@test Tttno_lr Tmpo_lr rtol = 1e-6
82+
end
6283
if auto_fermion_enabled
6384
ITensors.enable_auto_fermion()
6485
end
@@ -116,6 +137,28 @@ end
116137

117138
# root_vertex = (1, 2)
118139
# println(leaf_vertices(is))
140+
141+
@testset "Svd approach" for root_vertex in leaf_vertices(is)
142+
# get TTN Hamiltonian directly
143+
Hsvd = ttn(H, is; root_vertex, cutoff=1e-10)
144+
# get corresponding MPO Hamiltonian
145+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], H), sites)
146+
# compare resulting sparse Hamiltonians
147+
148+
@disable_warn_order begin
149+
Tmpo = prod(Hline)
150+
Tttno = contract(Hsvd)
151+
end
152+
@test Tttno Tmpo rtol = 1e-6
153+
154+
Hsvd_lr = ttn(Hlr, is; root_vertex, cutoff=1e-10)
155+
Hline_lr = ITensorMPS.MPO(replace_vertices(v -> vmap[v], Hlr), sites)
156+
@disable_warn_order begin
157+
Tttno_lr = prod(Hline_lr)
158+
Tmpo_lr = contract(Hsvd_lr)
159+
end
160+
@test Tttno_lr Tmpo_lr rtol = 1e-6
161+
end
119162
end
120163

121164
@testset "OpSum to TTN Fermions" begin
@@ -138,6 +181,30 @@ end
138181
# add combination of longer range interactions
139182
Hlr = copy(H)
140183

184+
@testset "Svd approach" for root_vertex in leaf_vertices(is)
185+
# get TTN Hamiltonian directly
186+
Hsvd = ttn(H, is; root_vertex, cutoff=1e-10)
187+
# get corresponding MPO Hamiltonian
188+
sites = [only(is[v]) for v in reverse(post_order_dfs_vertices(c, root_vertex))]
189+
vmap = Dictionary(reverse(post_order_dfs_vertices(c, root_vertex)), 1:length(sites))
190+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], H), sites)
191+
@disable_warn_order begin
192+
Tmpo = prod(Hline)
193+
Tttno = contract(Hsvd)
194+
end
195+
196+
# verify that the norm isn't 0 and thus the same (which would indicate a problem with the autofermion system
197+
@test norm(Tmpo) > 0
198+
@test norm(Tttno) > 0
199+
@test norm(Tmpo) norm(Tttno) rtol = 1e-6
200+
201+
# TODO: fix comparison for fermionic tensors
202+
@test_broken Tmpo Tttno
203+
# In the meantime: matricize tensors and convert to dense Matrix to compare element by element
204+
dTmm = to_matrix(Tmpo)
205+
dTtm = to_matrix(Tttno)
206+
@test any(>(1e-14), dTmm - dTtm)
207+
end
141208
if !auto_fermion_enabled
142209
ITensors.disable_auto_fermion()
143210
end
@@ -177,6 +244,28 @@ end
177244
Hlr += -4, "Z", (1, 1), "Z", (2, 2)
178245
Hlr += 2.0, "Z", (2, 2), "Z", (3, 2)
179246
Hlr += -1.0, "Z", (1, 2), "Z", (3, 1)
247+
248+
@testset "Svd approach" for root_vertex in leaf_vertices(is)
249+
# get TTN Hamiltonian directly
250+
Hsvd = ttn(H, is_missing_site; root_vertex, cutoff=1e-10)
251+
# get corresponding MPO Hamiltonian
252+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], H), sites)
253+
254+
# compare resulting sparse Hamiltonians
255+
@disable_warn_order begin
256+
Tmpo = prod(Hline)
257+
Tttno = contract(Hsvd)
258+
end
259+
@test Tttno Tmpo rtol = 1e-6
260+
261+
Hsvd_lr = ttn(Hlr, is_missing_site; root_vertex, cutoff=1e-10)
262+
Hline_lr = ITensorMPS.MPO(replace_vertices(v -> vmap[v], Hlr), sites)
263+
@disable_warn_order begin
264+
Tttno_lr = prod(Hline_lr)
265+
Tmpo_lr = contract(Hsvd_lr)
266+
end
267+
@test Tttno_lr Tmpo_lr rtol = 1e-6
268+
end
180269
end
181270
end
182271
end

test/test_ttn_dmrg.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using DataGraphs: edge_data, vertex_data
33
using Dictionaries: Dictionary
44
using Graphs: nv, vertices, uniform_tree
5+
using ITensorMPS: ITensorMPS
56
using ITensorNetworks:
67
ITensorNetworks,
78
OpSum,
@@ -51,10 +52,16 @@ ITensors.disable_auto_fermion()
5152
nsweeps = 10
5253
maxdim = [10, 20, 40, 100]
5354

55+
# Compare to `ITensors.MPO` version of `dmrg`
56+
H_mpo = ITensorMPS.MPO([H[v] for v in 1:nv(H)])
57+
psi_mps = ITensorMPS.MPS([psi[v] for v in 1:nv(psi)])
58+
e2, psi2 = ITensorMPS.dmrg(H_mpo, psi_mps; nsweeps, maxdim, outputlevel=0)
59+
5460
e, psi = dmrg(
5561
H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1)
5662
)
5763
@test inner(psi', H, psi) e
64+
@test inner(psi', H, psi) inner(psi2', H_mpo, psi2)
5865

5966
# Alias for `ITensorNetworks.dmrg`
6067
e, psi = eigsolve(
@@ -228,6 +235,17 @@ end
228235
H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1)
229236
)
230237

238+
# Compare to `ITensors.MPO` version of `dmrg`
239+
linear_order = [4, 1, 2, 5, 3, 6]
240+
vmap = Dictionary(collect(vertices(s))[linear_order], 1:length(linear_order))
241+
sline = only.(collect(vertex_data(s)))[linear_order]
242+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], os), sline)
243+
rng = StableRNG(1234)
244+
psiline = ITensorMPS.random_mps(rng, sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
245+
e2, psi2 = ITensorMPS.dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0)
246+
247+
@test inner(psi', H, psi) ITensorMPS.inner(psi2', Hline, psi2) atol = 1e-5
248+
231249
if !auto_fermion_enabled
232250
ITensors.disable_auto_fermion()
233251
end
@@ -255,6 +273,14 @@ end
255273
vmap = Dictionary(collect(vertices(s))[linear_order], 1:length(linear_order))
256274
sline = only.(collect(vertex_data(s)))[linear_order]
257275

276+
# get MPS / MPO with JW string result
277+
ITensors.disable_auto_fermion()
278+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], os), sline)
279+
rng = StableRNG(1234)
280+
psiline = ITensorMPS.random_mps(rng, sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
281+
e_jw, psi_jw = ITensorMPS.dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0)
282+
ITensors.enable_auto_fermion()
283+
258284
# now get auto-fermion results
259285
H = ttn(os, s)
260286
# make init_state
@@ -268,6 +294,16 @@ end
268294
H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1)
269295
)
270296

297+
# Compare to `ITensors.MPO` version of `dmrg`
298+
Hline = ITensorMPS.MPO(replace_vertices(v -> vmap[v], os), sline)
299+
rng = StableRNG(1234)
300+
psiline = ITensorMPS.random_mps(rng, sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
301+
e2, psi2 = ITensorMPS.dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0)
302+
303+
@test inner(psi', H, psi) ITensorMPS.inner(psi2', Hline, psi2) atol = 1e-5
304+
@test e2 e_jw atol = 1e-5
305+
@test inner(psi2', Hline, psi2) e_jw atol = 1e-5
306+
271307
if !auto_fermion_enabled
272308
ITensors.disable_auto_fermion()
273309
end

0 commit comments

Comments
 (0)