Trajectree 0.0.0__py3-none-any.whl → 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trajectree/__init__.py +3 -0
- trajectree/fock_optics/devices.py +1 -1
- trajectree/fock_optics/light_sources.py +2 -2
- trajectree/fock_optics/measurement.py +3 -3
- trajectree/fock_optics/utils.py +6 -6
- trajectree/quimb/docs/_pygments/_pygments_dark.py +118 -0
- trajectree/quimb/docs/_pygments/_pygments_light.py +118 -0
- trajectree/quimb/docs/conf.py +158 -0
- trajectree/quimb/docs/examples/ex_mpi_expm_evo.py +62 -0
- trajectree/quimb/quimb/__init__.py +507 -0
- trajectree/quimb/quimb/calc.py +1491 -0
- trajectree/quimb/quimb/core.py +2279 -0
- trajectree/quimb/quimb/evo.py +712 -0
- trajectree/quimb/quimb/experimental/__init__.py +0 -0
- trajectree/quimb/quimb/experimental/autojittn.py +129 -0
- trajectree/quimb/quimb/experimental/belief_propagation/__init__.py +109 -0
- trajectree/quimb/quimb/experimental/belief_propagation/bp_common.py +397 -0
- trajectree/quimb/quimb/experimental/belief_propagation/d1bp.py +316 -0
- trajectree/quimb/quimb/experimental/belief_propagation/d2bp.py +653 -0
- trajectree/quimb/quimb/experimental/belief_propagation/hd1bp.py +571 -0
- trajectree/quimb/quimb/experimental/belief_propagation/hv1bp.py +775 -0
- trajectree/quimb/quimb/experimental/belief_propagation/l1bp.py +316 -0
- trajectree/quimb/quimb/experimental/belief_propagation/l2bp.py +537 -0
- trajectree/quimb/quimb/experimental/belief_propagation/regions.py +194 -0
- trajectree/quimb/quimb/experimental/cluster_update.py +286 -0
- trajectree/quimb/quimb/experimental/merabuilder.py +865 -0
- trajectree/quimb/quimb/experimental/operatorbuilder/__init__.py +15 -0
- trajectree/quimb/quimb/experimental/operatorbuilder/operatorbuilder.py +1631 -0
- trajectree/quimb/quimb/experimental/schematic.py +7 -0
- trajectree/quimb/quimb/experimental/tn_marginals.py +130 -0
- trajectree/quimb/quimb/experimental/tnvmc.py +1483 -0
- trajectree/quimb/quimb/gates.py +36 -0
- trajectree/quimb/quimb/gen/__init__.py +2 -0
- trajectree/quimb/quimb/gen/operators.py +1167 -0
- trajectree/quimb/quimb/gen/rand.py +713 -0
- trajectree/quimb/quimb/gen/states.py +479 -0
- trajectree/quimb/quimb/linalg/__init__.py +6 -0
- trajectree/quimb/quimb/linalg/approx_spectral.py +1109 -0
- trajectree/quimb/quimb/linalg/autoblock.py +258 -0
- trajectree/quimb/quimb/linalg/base_linalg.py +719 -0
- trajectree/quimb/quimb/linalg/mpi_launcher.py +397 -0
- trajectree/quimb/quimb/linalg/numpy_linalg.py +244 -0
- trajectree/quimb/quimb/linalg/rand_linalg.py +514 -0
- trajectree/quimb/quimb/linalg/scipy_linalg.py +293 -0
- trajectree/quimb/quimb/linalg/slepc_linalg.py +892 -0
- trajectree/quimb/quimb/schematic.py +1518 -0
- trajectree/quimb/quimb/tensor/__init__.py +401 -0
- trajectree/quimb/quimb/tensor/array_ops.py +610 -0
- trajectree/quimb/quimb/tensor/circuit.py +4824 -0
- trajectree/quimb/quimb/tensor/circuit_gen.py +411 -0
- trajectree/quimb/quimb/tensor/contraction.py +336 -0
- trajectree/quimb/quimb/tensor/decomp.py +1255 -0
- trajectree/quimb/quimb/tensor/drawing.py +1646 -0
- trajectree/quimb/quimb/tensor/fitting.py +385 -0
- trajectree/quimb/quimb/tensor/geometry.py +583 -0
- trajectree/quimb/quimb/tensor/interface.py +114 -0
- trajectree/quimb/quimb/tensor/networking.py +1058 -0
- trajectree/quimb/quimb/tensor/optimize.py +1818 -0
- trajectree/quimb/quimb/tensor/tensor_1d.py +4778 -0
- trajectree/quimb/quimb/tensor/tensor_1d_compress.py +1854 -0
- trajectree/quimb/quimb/tensor/tensor_1d_tebd.py +662 -0
- trajectree/quimb/quimb/tensor/tensor_2d.py +5954 -0
- trajectree/quimb/quimb/tensor/tensor_2d_compress.py +96 -0
- trajectree/quimb/quimb/tensor/tensor_2d_tebd.py +1230 -0
- trajectree/quimb/quimb/tensor/tensor_3d.py +2869 -0
- trajectree/quimb/quimb/tensor/tensor_3d_tebd.py +46 -0
- trajectree/quimb/quimb/tensor/tensor_approx_spectral.py +60 -0
- trajectree/quimb/quimb/tensor/tensor_arbgeom.py +3237 -0
- trajectree/quimb/quimb/tensor/tensor_arbgeom_compress.py +565 -0
- trajectree/quimb/quimb/tensor/tensor_arbgeom_tebd.py +1138 -0
- trajectree/quimb/quimb/tensor/tensor_builder.py +5411 -0
- trajectree/quimb/quimb/tensor/tensor_core.py +11179 -0
- trajectree/quimb/quimb/tensor/tensor_dmrg.py +1472 -0
- trajectree/quimb/quimb/tensor/tensor_mera.py +204 -0
- trajectree/quimb/quimb/utils.py +892 -0
- trajectree/quimb/tests/__init__.py +0 -0
- trajectree/quimb/tests/test_accel.py +501 -0
- trajectree/quimb/tests/test_calc.py +788 -0
- trajectree/quimb/tests/test_core.py +847 -0
- trajectree/quimb/tests/test_evo.py +565 -0
- trajectree/quimb/tests/test_gen/__init__.py +0 -0
- trajectree/quimb/tests/test_gen/test_operators.py +361 -0
- trajectree/quimb/tests/test_gen/test_rand.py +296 -0
- trajectree/quimb/tests/test_gen/test_states.py +261 -0
- trajectree/quimb/tests/test_linalg/__init__.py +0 -0
- trajectree/quimb/tests/test_linalg/test_approx_spectral.py +368 -0
- trajectree/quimb/tests/test_linalg/test_base_linalg.py +351 -0
- trajectree/quimb/tests/test_linalg/test_mpi_linalg.py +127 -0
- trajectree/quimb/tests/test_linalg/test_numpy_linalg.py +84 -0
- trajectree/quimb/tests/test_linalg/test_rand_linalg.py +134 -0
- trajectree/quimb/tests/test_linalg/test_slepc_linalg.py +283 -0
- trajectree/quimb/tests/test_tensor/__init__.py +0 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/__init__.py +0 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/test_d1bp.py +39 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/test_d2bp.py +67 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/test_hd1bp.py +64 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/test_hv1bp.py +51 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/test_l1bp.py +142 -0
- trajectree/quimb/tests/test_tensor/test_belief_propagation/test_l2bp.py +101 -0
- trajectree/quimb/tests/test_tensor/test_circuit.py +816 -0
- trajectree/quimb/tests/test_tensor/test_contract.py +67 -0
- trajectree/quimb/tests/test_tensor/test_decomp.py +40 -0
- trajectree/quimb/tests/test_tensor/test_mera.py +52 -0
- trajectree/quimb/tests/test_tensor/test_optimizers.py +488 -0
- trajectree/quimb/tests/test_tensor/test_tensor_1d.py +1171 -0
- trajectree/quimb/tests/test_tensor/test_tensor_2d.py +606 -0
- trajectree/quimb/tests/test_tensor/test_tensor_2d_tebd.py +144 -0
- trajectree/quimb/tests/test_tensor/test_tensor_3d.py +123 -0
- trajectree/quimb/tests/test_tensor/test_tensor_arbgeom.py +226 -0
- trajectree/quimb/tests/test_tensor/test_tensor_builder.py +441 -0
- trajectree/quimb/tests/test_tensor/test_tensor_core.py +2066 -0
- trajectree/quimb/tests/test_tensor/test_tensor_dmrg.py +388 -0
- trajectree/quimb/tests/test_tensor/test_tensor_spectral_approx.py +63 -0
- trajectree/quimb/tests/test_tensor/test_tensor_tebd.py +270 -0
- trajectree/quimb/tests/test_utils.py +85 -0
- trajectree/trajectory.py +2 -2
- {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/METADATA +2 -2
- trajectree-0.0.1.dist-info/RECORD +126 -0
- trajectree-0.0.0.dist-info/RECORD +0 -16
- {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/WHEEL +0 -0
- {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/licenses/LICENSE +0 -0
- {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
import autoray as ar
|
|
5
|
+
import numpy as np
|
|
6
|
+
from numpy.testing import assert_allclose
|
|
7
|
+
|
|
8
|
+
import quimb as qu
|
|
9
|
+
import quimb.tensor as qtn
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestPEPSConstruct:
|
|
13
|
+
@pytest.mark.parametrize("Lx", [3, 4, 5])
|
|
14
|
+
@pytest.mark.parametrize("Ly", [3, 4, 5])
|
|
15
|
+
def test_basic_rand(self, Lx, Ly):
|
|
16
|
+
psi = qtn.PEPS.rand(Lx, Ly, bond_dim=4)
|
|
17
|
+
|
|
18
|
+
assert psi.max_bond() == 4
|
|
19
|
+
assert psi.Lx == Lx
|
|
20
|
+
assert psi.Ly == Ly
|
|
21
|
+
assert len(psi.tensor_map) == Lx * Ly
|
|
22
|
+
assert psi.site_inds == tuple(
|
|
23
|
+
f"k{i},{j}" for i in range(Lx) for j in range(Ly)
|
|
24
|
+
)
|
|
25
|
+
assert psi.site_tags == tuple(
|
|
26
|
+
f"I{i},{j}" for i in range(Lx) for j in range(Ly)
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
assert psi.bond_size((1, 1), (1, 2)) == (4)
|
|
30
|
+
|
|
31
|
+
for i in range(Lx):
|
|
32
|
+
assert len(psi.select(f"X{i}").tensor_map) == Ly
|
|
33
|
+
for j in range(Ly):
|
|
34
|
+
assert len(psi.select(f"Y{j}").tensor_map) == Lx
|
|
35
|
+
|
|
36
|
+
for i in range(Lx):
|
|
37
|
+
for j in range(Ly):
|
|
38
|
+
assert psi.phys_dim(i, j) == 2
|
|
39
|
+
assert isinstance(psi[i, j], qtn.Tensor)
|
|
40
|
+
assert isinstance(psi[f"I{i},{j}"], qtn.Tensor)
|
|
41
|
+
|
|
42
|
+
if Lx == Ly == 3:
|
|
43
|
+
psi_dense = psi.to_qarray(optimize="auto-hq")
|
|
44
|
+
assert psi_dense.shape == (512, 1)
|
|
45
|
+
|
|
46
|
+
psi.show()
|
|
47
|
+
assert f"Lx={Lx}" in psi.__str__()
|
|
48
|
+
assert f"Lx={Lx}" in psi.__repr__()
|
|
49
|
+
|
|
50
|
+
def test_cyclic_edge_cases(self):
|
|
51
|
+
peps = qtn.PEPS.rand(3, 3, bond_dim=1, cyclic=True)
|
|
52
|
+
assert peps.is_cyclic_x()
|
|
53
|
+
assert peps.is_cyclic_y()
|
|
54
|
+
assert peps.num_indices == peps.num_tensors * 3
|
|
55
|
+
|
|
56
|
+
def test_zeros(self):
|
|
57
|
+
peps = qtn.PEPS.zeros(3, 3, cyclic=True, bond_dim=1)
|
|
58
|
+
assert peps.num_tensors == 9
|
|
59
|
+
assert peps.num_indices == 27
|
|
60
|
+
assert_allclose(peps.to_dense(), np.zeros([512, 1]))
|
|
61
|
+
|
|
62
|
+
def test_flatten(self):
|
|
63
|
+
psi = qtn.PEPS.rand(3, 5, 3, seed=42)
|
|
64
|
+
norm = psi.H & psi
|
|
65
|
+
assert len(norm.tensors) == 30
|
|
66
|
+
norm.flatten_()
|
|
67
|
+
assert len(norm.tensors) == 15
|
|
68
|
+
assert norm.max_bond() == 9
|
|
69
|
+
|
|
70
|
+
def test_add_peps(self):
|
|
71
|
+
pa = qtn.PEPS.rand(3, 4, 2)
|
|
72
|
+
pb = qtn.PEPS.rand(3, 4, 3)
|
|
73
|
+
pc = qtn.PEPS.rand(3, 4, 4)
|
|
74
|
+
pab = pa + pb
|
|
75
|
+
assert pab.max_bond() == 5
|
|
76
|
+
assert pab @ pc == pytest.approx(pa @ pc + pb @ pc)
|
|
77
|
+
|
|
78
|
+
@pytest.mark.parametrize("Lx", [3, 4, 5])
|
|
79
|
+
@pytest.mark.parametrize("Ly", [3, 4, 5])
|
|
80
|
+
def test_bond_coordinates(self, Lx, Ly):
|
|
81
|
+
psi = qtn.PEPS.rand(Lx, Ly, bond_dim=1)
|
|
82
|
+
all_bonds = tuple(psi.gen_bond_coos())
|
|
83
|
+
assert len(all_bonds) == 2 * Lx * Ly - Lx - Ly
|
|
84
|
+
he = tuple(psi.gen_horizontal_even_bond_coos())
|
|
85
|
+
ho = tuple(psi.gen_horizontal_odd_bond_coos())
|
|
86
|
+
ve = tuple(psi.gen_vertical_even_bond_coos())
|
|
87
|
+
vo = tuple(psi.gen_vertical_odd_bond_coos())
|
|
88
|
+
for p in (he, ho, ve, vo):
|
|
89
|
+
assert len(set(p)) == len(p)
|
|
90
|
+
# check there is no overlap at all
|
|
91
|
+
sites = tuple(itertools.chain.from_iterable(he))
|
|
92
|
+
assert len(set(sites)) == len(sites)
|
|
93
|
+
# check all coordinates are generated
|
|
94
|
+
assert set(itertools.chain(he, ho, ve, vo)) == set(all_bonds)
|
|
95
|
+
|
|
96
|
+
@pytest.mark.parametrize(
|
|
97
|
+
"where",
|
|
98
|
+
[
|
|
99
|
+
[(0, 0)],
|
|
100
|
+
[(0, 1)],
|
|
101
|
+
[(0, 2)],
|
|
102
|
+
[(2, 2)],
|
|
103
|
+
[(3, 2)],
|
|
104
|
+
[(3, 1)],
|
|
105
|
+
[(3, 0)],
|
|
106
|
+
[(2, 0)],
|
|
107
|
+
[(1, 1)],
|
|
108
|
+
],
|
|
109
|
+
)
|
|
110
|
+
@pytest.mark.parametrize("contract", [False, True])
|
|
111
|
+
def test_gate_2d_single_site(self, where, contract):
|
|
112
|
+
Lx = 4
|
|
113
|
+
Ly = 3
|
|
114
|
+
D = 2
|
|
115
|
+
|
|
116
|
+
psi = qtn.PEPS.rand(Lx, Ly, bond_dim=D, seed=42, dtype=complex)
|
|
117
|
+
psi_d = psi.to_qarray()
|
|
118
|
+
G = qu.rand_matrix(2)
|
|
119
|
+
|
|
120
|
+
# compute the exact dense reference
|
|
121
|
+
dims = [[2] * Ly] * Lx
|
|
122
|
+
IGI = qu.ikron(G, dims, where, sparse=True)
|
|
123
|
+
xe = (psi_d.H @ IGI @ psi_d).item()
|
|
124
|
+
|
|
125
|
+
tn = psi.H & psi.gate(G, where, contract=contract)
|
|
126
|
+
assert len(tn.tensors) == 2 * Lx * Ly + int(not contract)
|
|
127
|
+
|
|
128
|
+
assert tn ^ all == pytest.approx(xe)
|
|
129
|
+
|
|
130
|
+
@pytest.mark.parametrize(
|
|
131
|
+
"contract", [False, True, "split", "reduce-split"]
|
|
132
|
+
)
|
|
133
|
+
@pytest.mark.parametrize(
|
|
134
|
+
"where",
|
|
135
|
+
[
|
|
136
|
+
[(1, 1), (2, 1)],
|
|
137
|
+
[(3, 2), (2, 2)],
|
|
138
|
+
[(0, 0), (1, 1)],
|
|
139
|
+
[(3, 1), (1, 2)],
|
|
140
|
+
],
|
|
141
|
+
)
|
|
142
|
+
def test_gate_2d_two_site(self, where, contract):
|
|
143
|
+
Lx = 4
|
|
144
|
+
Ly = 3
|
|
145
|
+
D = 2
|
|
146
|
+
|
|
147
|
+
psi = qtn.PEPS.rand(Lx, Ly, bond_dim=D, seed=42, dtype=complex)
|
|
148
|
+
psi_d = psi.to_qarray()
|
|
149
|
+
|
|
150
|
+
# ikron can't tensor operators across non-adjacent subsytems
|
|
151
|
+
# so we explicitly construct the gate as a sum of tensor components
|
|
152
|
+
G_comps = [(qu.rand_matrix(2), qu.rand_matrix(2)) for _ in range(4)]
|
|
153
|
+
G = sum(A & B for A, B in G_comps)
|
|
154
|
+
|
|
155
|
+
# compute the exact dense reference
|
|
156
|
+
dims = [[2] * Ly] * Lx
|
|
157
|
+
IGI = sum(
|
|
158
|
+
qu.ikron([A, B], dims, where, sparse=True) for A, B in G_comps
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
xe = (psi_d.H @ IGI @ psi_d).item()
|
|
162
|
+
|
|
163
|
+
tn = psi.H & psi.gate(G, where, contract=contract)
|
|
164
|
+
change = {False: 1, True: -1, "split": 0, "reduce-split": 0}[contract]
|
|
165
|
+
assert len(tn.tensors) == 2 * Lx * Ly + change
|
|
166
|
+
|
|
167
|
+
assert tn ^ all == pytest.approx(xe)
|
|
168
|
+
|
|
169
|
+
@pytest.mark.parametrize(
|
|
170
|
+
"propagate_tags", [False, True, "sites", "register"]
|
|
171
|
+
)
|
|
172
|
+
def test_gate_propagate_tags(self, propagate_tags):
|
|
173
|
+
Lx = 4
|
|
174
|
+
Ly = 3
|
|
175
|
+
D = 1
|
|
176
|
+
psi = qtn.PEPS.rand(Lx, Ly, D, tags="PSI0")
|
|
177
|
+
psi.gate_(
|
|
178
|
+
qu.rand_uni(4),
|
|
179
|
+
[(1, 1), (1, 2)],
|
|
180
|
+
tags="G1",
|
|
181
|
+
propagate_tags=propagate_tags,
|
|
182
|
+
)
|
|
183
|
+
psi.gate_(
|
|
184
|
+
qu.rand_uni(4),
|
|
185
|
+
[(1, 2), (3, 2)],
|
|
186
|
+
tags="G2",
|
|
187
|
+
propagate_tags=propagate_tags,
|
|
188
|
+
)
|
|
189
|
+
if propagate_tags is False:
|
|
190
|
+
assert set(psi["G1"].tags) == {"G1"}
|
|
191
|
+
assert set(psi["G2"].tags) == {"G2"}
|
|
192
|
+
if propagate_tags is True:
|
|
193
|
+
tgs1 = {"I1,1", "I1,2", "G1", "PSI0", "Y1", "Y2", "X1"}
|
|
194
|
+
assert set(psi["G1"][0].tags) == tgs1
|
|
195
|
+
assert set(psi["G2"].tags) == tgs1 | {"G2", "I3,2", "X3", "Y2"}
|
|
196
|
+
if propagate_tags == "sites":
|
|
197
|
+
assert set(psi["G1"].tags) == {"G1", "I1,1", "I1,2"}
|
|
198
|
+
assert set(psi["G2"].tags) == {"G2", "I1,1", "I1,2", "I3,2"}
|
|
199
|
+
if propagate_tags == "register":
|
|
200
|
+
assert set(psi["G1"].tags) == {"G1", "I1,1", "I1,2"}
|
|
201
|
+
assert set(psi["G2"].tags) == {"G2", "I1,2", "I3,2"}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class Test2DContract:
|
|
205
|
+
@pytest.mark.parametrize("mode", ["mps", "projector", "full-bond"])
|
|
206
|
+
def test_contract_boundary(self, mode):
|
|
207
|
+
# make a large but cheap and easy (mostly positive) TN
|
|
208
|
+
rng = np.random.default_rng(42)
|
|
209
|
+
tn = qtn.TN2D_from_fill_fn(
|
|
210
|
+
lambda shape: rng.uniform(low=-0.1, size=shape),
|
|
211
|
+
Lx=8,
|
|
212
|
+
Ly=8,
|
|
213
|
+
D=2,
|
|
214
|
+
)
|
|
215
|
+
Zex = tn.contract(...)
|
|
216
|
+
Z = tn.contract_boundary(max_bond=4, mode=mode)
|
|
217
|
+
assert Z == pytest.approx(Zex, rel=1e-3)
|
|
218
|
+
|
|
219
|
+
def test_contract_2d_one_layer_boundary(self):
|
|
220
|
+
psi = qtn.PEPS.rand(4, 4, 3, seed=42)
|
|
221
|
+
norm = psi.make_norm()
|
|
222
|
+
xe = norm.contract(all, optimize="auto-hq")
|
|
223
|
+
xt = norm.contract_boundary(max_bond=9)
|
|
224
|
+
assert xt == pytest.approx(xe, rel=1e-2)
|
|
225
|
+
|
|
226
|
+
def test_contract_2d_two_layer_boundary(self):
|
|
227
|
+
psi = qtn.PEPS.rand(4, 4, 3, seed=42, tags="KET")
|
|
228
|
+
norm = psi.make_norm()
|
|
229
|
+
xe = norm.contract(all, optimize="auto-hq")
|
|
230
|
+
xt = norm.contract_boundary(max_bond=27, layer_tags=["KET", "BRA"])
|
|
231
|
+
assert xt == pytest.approx(xe, rel=1e-2)
|
|
232
|
+
|
|
233
|
+
def test_contract_2d_full_bond(self):
|
|
234
|
+
psi = qtn.PEPS.rand(4, 4, 3, seed=42, tags="KET")
|
|
235
|
+
norm = psi.make_norm()
|
|
236
|
+
xe = norm.contract(all, optimize="auto-hq")
|
|
237
|
+
xt = norm.contract_boundary(max_bond=27, mode="full-bond")
|
|
238
|
+
assert xt == pytest.approx(xe, rel=1e-2)
|
|
239
|
+
|
|
240
|
+
@pytest.mark.parametrize("dims", [(10, 4), (4, 10)])
|
|
241
|
+
def test_contract_boundary_stopping_criterion(self, dims):
|
|
242
|
+
tn = qtn.TN2D_from_fill_fn(
|
|
243
|
+
lambda shape: ar.lazy.Variable(shape=shape, backend="numpy"),
|
|
244
|
+
*dims,
|
|
245
|
+
D=2,
|
|
246
|
+
)
|
|
247
|
+
tn.contract_ctmrg_(4, cutoff=0.0, final_contract=False, progbar=True)
|
|
248
|
+
assert tn.max_bond() == 4
|
|
249
|
+
assert 16 <= tn.num_tensors <= 20
|
|
250
|
+
|
|
251
|
+
@pytest.mark.parametrize("lazy", [False, True])
|
|
252
|
+
def test_coarse_grain_basics(self, lazy):
|
|
253
|
+
tn = qtn.TN2D_from_fill_fn(
|
|
254
|
+
lambda shape: ar.lazy.Variable(shape, backend="numpy"),
|
|
255
|
+
Lx=6,
|
|
256
|
+
Ly=7,
|
|
257
|
+
D=2,
|
|
258
|
+
)
|
|
259
|
+
tncg = tn.coarse_grain_hotrg("x", max_bond=3, cutoff=0.0, lazy=lazy)
|
|
260
|
+
assert (tncg.Lx, tncg.Ly) == (3, 7)
|
|
261
|
+
assert not tncg.outer_inds()
|
|
262
|
+
assert tncg.max_bond() == 3
|
|
263
|
+
assert "I4,0" not in tncg.tag_map
|
|
264
|
+
assert "X5" not in tncg.tag_map
|
|
265
|
+
|
|
266
|
+
tncg = tn.coarse_grain_hotrg("y", max_bond=3, cutoff=0.0, lazy=lazy)
|
|
267
|
+
assert (tncg.Lx, tncg.Ly) == (6, 4)
|
|
268
|
+
assert not tncg.outer_inds()
|
|
269
|
+
assert tncg.max_bond() == 3
|
|
270
|
+
assert "I0,5" not in tncg.tag_map
|
|
271
|
+
assert "Y6" not in tncg.tag_map
|
|
272
|
+
|
|
273
|
+
def test_contract_hotrg(self):
|
|
274
|
+
tn = qtn.TN2D_classical_ising_partition_function(16, 16, 0.44)
|
|
275
|
+
tn.contract_hotrg_(max_bond=5, progbar=True, equalize_norms=1.0)
|
|
276
|
+
Zap = tn.item() * 10**tn.exponent
|
|
277
|
+
assert Zap == pytest.approx(8.459419593253275e100, rel=2e-3)
|
|
278
|
+
|
|
279
|
+
def test_contract_hotrg_two_layer_rand_peps(self):
|
|
280
|
+
rng = np.random.default_rng(42)
|
|
281
|
+
psi = qtn.PEPS.from_fill_fn(
|
|
282
|
+
lambda shape: rng.uniform(low=-0.1, size=shape),
|
|
283
|
+
Lx=7,
|
|
284
|
+
Ly=5,
|
|
285
|
+
bond_dim=2,
|
|
286
|
+
)
|
|
287
|
+
norm = psi.make_norm()
|
|
288
|
+
xe = norm.contract(all, optimize="auto-hq")
|
|
289
|
+
xt = norm.contract_hotrg(max_bond=5)
|
|
290
|
+
assert xt == pytest.approx(xe, rel=1e-4)
|
|
291
|
+
|
|
292
|
+
def test_ising_accuracy_regression(self):
|
|
293
|
+
tn = qtn.TN2D_classical_ising_partition_function(16, 16, 0.44)
|
|
294
|
+
for s in [("xmin",), ("xmax",), ("ymin",), ("ymax",)]:
|
|
295
|
+
Zap = tn.contract_boundary(max_bond=8, sequence=s)
|
|
296
|
+
assert Zap == pytest.approx(8.459419593253275e100, rel=2.2e-7)
|
|
297
|
+
for s in [("xmin", "xmax"), ("ymin", "ymax")]:
|
|
298
|
+
Zap = tn.contract_boundary(max_bond=8, sequence=s)
|
|
299
|
+
assert Zap == pytest.approx(8.459419593253275e100, rel=3.9e-9)
|
|
300
|
+
|
|
301
|
+
@pytest.mark.parametrize("mode", ["mps", "ctmrg", "hotrg"])
|
|
302
|
+
def test_cdl_rand_large(self, mode):
|
|
303
|
+
tn = qtn.TN2D_rand_hidden_loop(10, 10, seed=42, contract_sites=False)
|
|
304
|
+
Zex = tn.contract(...)
|
|
305
|
+
tn = qtn.TN2D_rand_hidden_loop(10, 10, seed=42, contract_sites=True)
|
|
306
|
+
|
|
307
|
+
if mode == "mps":
|
|
308
|
+
Z = tn.contract_boundary(max_bond=16)
|
|
309
|
+
elif mode == "ctmrg":
|
|
310
|
+
Z = tn.contract_ctmrg(max_bond=16)
|
|
311
|
+
elif mode == "hotrg":
|
|
312
|
+
Z = tn.contract_hotrg(max_bond=16)
|
|
313
|
+
|
|
314
|
+
assert Z == pytest.approx(Zex, rel=1e-1)
|
|
315
|
+
|
|
316
|
+
@pytest.mark.parametrize(
|
|
317
|
+
"mode,two_layer",
|
|
318
|
+
[
|
|
319
|
+
("mps", False),
|
|
320
|
+
("mps", True),
|
|
321
|
+
("full-bond", False),
|
|
322
|
+
],
|
|
323
|
+
)
|
|
324
|
+
def test_compute_x_envs(self, mode, two_layer):
|
|
325
|
+
psi = qtn.PEPS.rand(5, 4, 2, seed=42, tags="KET")
|
|
326
|
+
norm = psi.make_norm()
|
|
327
|
+
ex = norm.contract(all)
|
|
328
|
+
|
|
329
|
+
if two_layer:
|
|
330
|
+
compress_opts = {
|
|
331
|
+
"cutoff": 1e-6,
|
|
332
|
+
"max_bond": 12,
|
|
333
|
+
"mode": mode,
|
|
334
|
+
"layer_tags": ["KET", "BRA"],
|
|
335
|
+
}
|
|
336
|
+
else:
|
|
337
|
+
compress_opts = {"cutoff": 1e-6, "max_bond": 8, "mode": mode}
|
|
338
|
+
row_envs = norm.compute_x_environments(**compress_opts)
|
|
339
|
+
|
|
340
|
+
for i in range(norm.Lx):
|
|
341
|
+
norm_i = (
|
|
342
|
+
row_envs["xmin", i]
|
|
343
|
+
& norm.select(norm.x_tag(i))
|
|
344
|
+
& row_envs["xmax", i]
|
|
345
|
+
)
|
|
346
|
+
x = norm_i.contract(all)
|
|
347
|
+
assert x == pytest.approx(ex, rel=1e-2)
|
|
348
|
+
|
|
349
|
+
@pytest.mark.parametrize(
|
|
350
|
+
"mode,two_layer",
|
|
351
|
+
[
|
|
352
|
+
("mps", False),
|
|
353
|
+
("mps", True),
|
|
354
|
+
("full-bond", False),
|
|
355
|
+
],
|
|
356
|
+
)
|
|
357
|
+
def test_compute_y_envs(self, mode, two_layer):
|
|
358
|
+
psi = qtn.PEPS.rand(4, 5, 2, seed=42, tags="KET")
|
|
359
|
+
norm = psi.retag({"KET": "BRA"}).H | psi
|
|
360
|
+
ex = norm.contract(all)
|
|
361
|
+
|
|
362
|
+
if two_layer:
|
|
363
|
+
compress_opts = {
|
|
364
|
+
"cutoff": 1e-6,
|
|
365
|
+
"max_bond": 12,
|
|
366
|
+
"mode": mode,
|
|
367
|
+
"layer_tags": ["KET", "BRA"],
|
|
368
|
+
}
|
|
369
|
+
else:
|
|
370
|
+
compress_opts = {"cutoff": 1e-6, "max_bond": 8, "mode": mode}
|
|
371
|
+
col_envs = norm.compute_y_environments(**compress_opts)
|
|
372
|
+
|
|
373
|
+
for j in range(norm.Lx):
|
|
374
|
+
norm_j = (
|
|
375
|
+
col_envs["ymin", j]
|
|
376
|
+
& norm.select(norm.y_tag(j))
|
|
377
|
+
& col_envs["ymax", j]
|
|
378
|
+
)
|
|
379
|
+
x = norm_j.contract(all)
|
|
380
|
+
assert x == pytest.approx(ex, rel=1e-2)
|
|
381
|
+
|
|
382
|
+
def test_normalize(self):
|
|
383
|
+
psi = qtn.PEPS.rand(4, 5, 2, seed=42)
|
|
384
|
+
norm = (psi.H | psi).contract(all)
|
|
385
|
+
assert norm != pytest.approx(1.0)
|
|
386
|
+
psi.normalize_(balance_bonds=True, equalize_norms=True, cutoff=2e-3)
|
|
387
|
+
norm = (psi.H | psi).contract(all)
|
|
388
|
+
assert norm == pytest.approx(1.0, rel=0.01)
|
|
389
|
+
|
|
390
|
+
@pytest.mark.parametrize("normalized", [False, True])
|
|
391
|
+
@pytest.mark.parametrize("mode", ["mps", "full-bond"])
|
|
392
|
+
def test_compute_local_expectation_one_sites(self, mode, normalized):
|
|
393
|
+
peps = qtn.PEPS.rand(4, 3, 2, seed=42, dtype="complex")
|
|
394
|
+
|
|
395
|
+
# reference
|
|
396
|
+
k = peps.to_qarray()
|
|
397
|
+
if normalized:
|
|
398
|
+
qu.normalize(k)
|
|
399
|
+
coos = list(itertools.product([0, 2, 3], [0, 1, 2]))
|
|
400
|
+
terms = {coo: qu.rand_matrix(2) for coo in coos}
|
|
401
|
+
dims = [[2] * 3] * 4
|
|
402
|
+
A = sum(
|
|
403
|
+
qu.ikron(A, dims, [coo], sparse=True) for coo, A in terms.items()
|
|
404
|
+
)
|
|
405
|
+
ex = qu.expec(A, k)
|
|
406
|
+
|
|
407
|
+
opts = dict(cutoff=2e-3, max_bond=9, contract_optimize="auto-hq")
|
|
408
|
+
e = peps.compute_local_expectation(
|
|
409
|
+
terms, mode=mode, normalized=normalized, **opts
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
assert e == pytest.approx(ex, rel=1e-2)
|
|
413
|
+
|
|
414
|
+
@pytest.mark.parametrize("normalized", [False, True])
|
|
415
|
+
@pytest.mark.parametrize("mode", ["mps", "full-bond"])
|
|
416
|
+
def test_compute_local_expectation_two_sites(self, mode, normalized):
|
|
417
|
+
H = qu.ham_heis_2D(4, 3, sparse=True)
|
|
418
|
+
Hij = qu.ham_heis(2, cyclic=False)
|
|
419
|
+
|
|
420
|
+
peps = qtn.PEPS.rand(4, 3, 2, seed=42)
|
|
421
|
+
k = peps.to_qarray()
|
|
422
|
+
|
|
423
|
+
if normalized:
|
|
424
|
+
qu.normalize(k)
|
|
425
|
+
ex = qu.expec(H, k)
|
|
426
|
+
|
|
427
|
+
opts = dict(
|
|
428
|
+
mode=mode,
|
|
429
|
+
normalized=normalized,
|
|
430
|
+
cutoff=2e-3,
|
|
431
|
+
max_bond=16,
|
|
432
|
+
contract_optimize="auto-hq",
|
|
433
|
+
)
|
|
434
|
+
|
|
435
|
+
# compute 2x1 and 1x2 plaquettes separately
|
|
436
|
+
hterms = {coos: Hij for coos in peps.gen_horizontal_bond_coos()}
|
|
437
|
+
vterms = {coos: Hij for coos in peps.gen_vertical_bond_coos()}
|
|
438
|
+
|
|
439
|
+
he = peps.compute_local_expectation(hterms, **opts)
|
|
440
|
+
ve = peps.compute_local_expectation(vterms, **opts)
|
|
441
|
+
|
|
442
|
+
assert he + ve == pytest.approx(ex, rel=1e-2)
|
|
443
|
+
|
|
444
|
+
# compute all terms in 2x2 plaquettes
|
|
445
|
+
terms_all = {**hterms, **vterms}
|
|
446
|
+
e = peps.compute_local_expectation(terms_all, autogroup=False, **opts)
|
|
447
|
+
|
|
448
|
+
assert e == pytest.approx(ex, rel=1e-2)
|
|
449
|
+
|
|
450
|
+
def test_cyclic_basic(self):
|
|
451
|
+
tn = qtn.TN2D_rand(Lx=3, Ly=4, D=2, cyclic=True)
|
|
452
|
+
assert tn.is_cyclic_x()
|
|
453
|
+
assert tn.is_cyclic_y()
|
|
454
|
+
assert tn.num_indices == 2 * 3 * 4
|
|
455
|
+
tn = qtn.TN2D_rand(Lx=3, Ly=4, D=2, cyclic=(False, True))
|
|
456
|
+
assert not tn.is_cyclic_x()
|
|
457
|
+
assert tn.is_cyclic_y()
|
|
458
|
+
assert tn.num_indices == 2 * 3 * 4 - 4
|
|
459
|
+
tn = qtn.TN2D_rand(Lx=3, Ly=4, D=2, cyclic=(True, False))
|
|
460
|
+
assert tn.is_cyclic_x()
|
|
461
|
+
assert not tn.is_cyclic_y()
|
|
462
|
+
assert tn.num_indices == 2 * 3 * 4 - 3
|
|
463
|
+
tn = qtn.TN2D_rand(Lx=3, Ly=4, D=2, cyclic=(False, False))
|
|
464
|
+
assert not tn.is_cyclic_x()
|
|
465
|
+
assert not tn.is_cyclic_y()
|
|
466
|
+
assert tn.num_indices == 2 * 3 * 4 - 7
|
|
467
|
+
|
|
468
|
+
@pytest.mark.parametrize("cyclicx", [False, True])
|
|
469
|
+
@pytest.mark.parametrize("cyclicy", [False, True])
|
|
470
|
+
@pytest.mark.parametrize("mode", ["mps", "hotrg", "ctmrg"])
|
|
471
|
+
def test_cyclic_contract(self, cyclicx, cyclicy, mode):
|
|
472
|
+
Lx = 5
|
|
473
|
+
Ly = 6
|
|
474
|
+
D = 2
|
|
475
|
+
chi = 3
|
|
476
|
+
tn = qtn.TN2D_rand(
|
|
477
|
+
Lx,
|
|
478
|
+
Ly,
|
|
479
|
+
D,
|
|
480
|
+
cyclic=(cyclicx, cyclicy),
|
|
481
|
+
seed=42,
|
|
482
|
+
dist="uniform",
|
|
483
|
+
)
|
|
484
|
+
Zex = tn.contract(...)
|
|
485
|
+
if mode == "hotrg":
|
|
486
|
+
Z = tn.contract_hotrg(chi)
|
|
487
|
+
elif mode == "ctmrg":
|
|
488
|
+
Z = tn.contract_ctmrg(chi)
|
|
489
|
+
else:
|
|
490
|
+
Z = tn.contract_boundary(chi, mode=mode)
|
|
491
|
+
assert abs(1 - Z / Zex) < 1e-3
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
class TestPEPO:
|
|
495
|
+
@pytest.mark.parametrize("Lx", [3, 4, 5])
|
|
496
|
+
@pytest.mark.parametrize("Ly", [3, 4, 5])
|
|
497
|
+
def test_basic_rand(self, Lx, Ly):
|
|
498
|
+
X = qtn.PEPO.rand_herm(Lx, Ly, bond_dim=4)
|
|
499
|
+
|
|
500
|
+
assert X.max_bond() == 4
|
|
501
|
+
assert X.Lx == Lx
|
|
502
|
+
assert X.Ly == Ly
|
|
503
|
+
assert len(X.tensor_map) == Lx * Ly
|
|
504
|
+
assert X.upper_inds == tuple(
|
|
505
|
+
f"k{i},{j}" for i in range(Lx) for j in range(Ly)
|
|
506
|
+
)
|
|
507
|
+
assert X.lower_inds == tuple(
|
|
508
|
+
f"b{i},{j}" for i in range(Lx) for j in range(Ly)
|
|
509
|
+
)
|
|
510
|
+
assert X.site_tags == tuple(
|
|
511
|
+
f"I{i},{j}" for i in range(Lx) for j in range(Ly)
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
assert X.bond_size((1, 1), (1, 2)) == (4)
|
|
515
|
+
|
|
516
|
+
for i in range(Lx):
|
|
517
|
+
assert len(X.select(f"X{i}").tensor_map) == Ly
|
|
518
|
+
for j in range(Ly):
|
|
519
|
+
assert len(X.select(f"Y{j}").tensor_map) == Lx
|
|
520
|
+
|
|
521
|
+
for i in range(Lx):
|
|
522
|
+
for j in range(Ly):
|
|
523
|
+
assert X.phys_dim(i, j) == 2
|
|
524
|
+
assert isinstance(X[i, j], qtn.Tensor)
|
|
525
|
+
assert isinstance(X[f"I{i},{j}"], qtn.Tensor)
|
|
526
|
+
|
|
527
|
+
if Lx == Ly == 3:
|
|
528
|
+
X_dense = X.to_qarray(optimize="auto-hq")
|
|
529
|
+
assert X_dense.shape == (512, 512)
|
|
530
|
+
assert qu.isherm(X_dense)
|
|
531
|
+
|
|
532
|
+
X.show()
|
|
533
|
+
assert f"Lx={Lx}" in X.__str__()
|
|
534
|
+
assert f"Lx={Lx}" in X.__repr__()
|
|
535
|
+
|
|
536
|
+
def test_add_pepo(self):
|
|
537
|
+
pa = qtn.PEPO.rand(3, 4, 2)
|
|
538
|
+
pb = qtn.PEPO.rand(3, 4, 3)
|
|
539
|
+
pc = qtn.PEPO.rand(3, 4, 4)
|
|
540
|
+
pab = pa + pb
|
|
541
|
+
assert pab.max_bond() == 5
|
|
542
|
+
assert pab @ pc == pytest.approx(pa @ pc + pb @ pc)
|
|
543
|
+
|
|
544
|
+
def test_apply_pepo(self):
|
|
545
|
+
A = qtn.PEPO.rand(Lx=3, Ly=2, bond_dim=2, seed=1)
|
|
546
|
+
x = qtn.PEPS.rand(Lx=3, Ly=2, bond_dim=2, seed=0)
|
|
547
|
+
y = A.apply(x)
|
|
548
|
+
assert y.num_indices == x.num_indices
|
|
549
|
+
Ad = A.to_qarray()
|
|
550
|
+
xd = x.to_qarray()
|
|
551
|
+
yd = y.to_qarray()
|
|
552
|
+
assert_allclose(Ad @ xd, yd)
|
|
553
|
+
yc = A.apply(x, compress=True, max_bond=3)
|
|
554
|
+
assert yc.max_bond() == 3
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
class TestMisc:
|
|
558
|
+
def test_calc_plaquette_sizes(self):
|
|
559
|
+
from quimb.tensor.tensor_2d import calc_plaquette_sizes
|
|
560
|
+
|
|
561
|
+
H2 = {None: qu.ham_heis(2)}
|
|
562
|
+
ham = qtn.LocalHam2D(10, 10, H2)
|
|
563
|
+
assert calc_plaquette_sizes(ham.terms.keys()) == ((1, 2), (2, 1))
|
|
564
|
+
assert calc_plaquette_sizes(ham.terms.keys(), autogroup=False) == (
|
|
565
|
+
(2, 2),
|
|
566
|
+
)
|
|
567
|
+
H2[(1, 1), (2, 2)] = 0.5 * qu.ham_heis(2)
|
|
568
|
+
ham = qtn.LocalHam2D(10, 10, H2)
|
|
569
|
+
assert calc_plaquette_sizes(ham.terms.keys()) == ((2, 2),)
|
|
570
|
+
H2[(2, 2), (2, 4)] = 0.25 * qu.ham_heis(2)
|
|
571
|
+
H2[(2, 4), (4, 4)] = 0.25 * qu.ham_heis(2)
|
|
572
|
+
ham = qtn.LocalHam2D(10, 10, H2)
|
|
573
|
+
assert calc_plaquette_sizes(ham.terms.keys()) == (
|
|
574
|
+
(1, 3),
|
|
575
|
+
(2, 2),
|
|
576
|
+
(3, 1),
|
|
577
|
+
)
|
|
578
|
+
assert calc_plaquette_sizes(ham.terms.keys(), autogroup=False) == (
|
|
579
|
+
(3, 3),
|
|
580
|
+
)
|
|
581
|
+
|
|
582
|
+
def test_calc_plaquette_map(self):
|
|
583
|
+
from quimb.tensor.tensor_2d import calc_plaquette_map
|
|
584
|
+
|
|
585
|
+
plaquettes = [
|
|
586
|
+
# 2x2 plaquette covering all sites
|
|
587
|
+
((0, 0), (2, 2)),
|
|
588
|
+
# horizontal plaquettes
|
|
589
|
+
((0, 0), (1, 2)),
|
|
590
|
+
((1, 0), (1, 2)),
|
|
591
|
+
# vertical plaquettes
|
|
592
|
+
((0, 0), (2, 1)),
|
|
593
|
+
((0, 1), (2, 1)),
|
|
594
|
+
]
|
|
595
|
+
assert calc_plaquette_map(plaquettes) == {
|
|
596
|
+
(0, 0): ((0, 0), (2, 1)),
|
|
597
|
+
(0, 1): ((0, 1), (2, 1)),
|
|
598
|
+
(1, 0): ((1, 0), (1, 2)),
|
|
599
|
+
(1, 1): ((1, 0), (1, 2)),
|
|
600
|
+
((0, 0), (0, 1)): ((0, 0), (1, 2)),
|
|
601
|
+
((0, 0), (1, 0)): ((0, 0), (2, 1)),
|
|
602
|
+
((0, 0), (1, 1)): ((0, 0), (2, 2)),
|
|
603
|
+
((0, 1), (1, 0)): ((0, 0), (2, 2)),
|
|
604
|
+
((0, 1), (1, 1)): ((0, 1), (2, 1)),
|
|
605
|
+
((1, 0), (1, 1)): ((1, 0), (1, 2)),
|
|
606
|
+
}
|