/
test_sampler.py
183 lines (137 loc) · 5.57 KB
/
test_sampler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import netket as nk
import networkx as nx
import numpy as np
import pytest
from pytest import approx
samplers = {}
# TESTS FOR SPIN HILBERT
# Constructing a 1d lattice
g = nk.graph.Hypercube(length=6, n_dim=1)
# Hilbert space of spins from given graph
hi = nk.hilbert.Spin(s=0.5, graph=g)
ma = nk.machine.RbmSpin(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=1234, sigma=0.2)
sa = nk.sampler.MetropolisLocal(machine=ma)
samplers["MetropolisLocal RbmSpin"] = sa
sa = nk.sampler.MetropolisLocalPt(machine=ma, n_replicas=4)
samplers["MetropolisLocalPt RbmSpin"] = sa
ha = nk.operator.Ising(hilbert=hi, h=1.0)
sa = nk.sampler.MetropolisHamiltonian(machine=ma, hamiltonian=ha)
samplers["MetropolisHamiltonian RbmSpin"] = sa
# Test with uniform probability
maz = nk.machine.RbmSpin(hilbert=hi, alpha=1)
maz.init_random_parameters(seed=1234, sigma=0)
sa = nk.sampler.MetropolisLocal(machine=maz)
samplers["MetropolisLocal RbmSpin ZeroPars"] = sa
mas = nk.machine.RbmSpinSymm(hilbert=hi, alpha=1)
mas.init_random_parameters(seed=1234, sigma=0.2)
sa = nk.sampler.MetropolisHamiltonianPt(machine=mas, hamiltonian=ha, n_replicas=4)
samplers["MetropolisHamiltonianPt RbmSpinSymm"] = sa
hi = nk.hilbert.Boson(graph=g, n_max=4)
ma = nk.machine.RbmSpin(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=1234, sigma=0.2)
sa = nk.sampler.MetropolisLocal(machine=ma)
g = nk.graph.Hypercube(length=4, n_dim=1)
samplers["MetropolisLocal Boson"] = sa
sa = nk.sampler.MetropolisLocalPt(machine=ma, n_replicas=4)
samplers["MetropolisLocalPt Boson"] = sa
ma = nk.machine.RbmMultiVal(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=1234, sigma=0.2)
sa = nk.sampler.MetropolisLocal(machine=ma)
samplers["MetropolisLocal Boson MultiVal"] = sa
hi = nk.hilbert.Spin(s=0.5, graph=g)
g = nk.graph.Hypercube(length=6, n_dim=1)
ma = nk.machine.RbmSpinSymm(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=1234, sigma=0.2)
l = hi.size
X = [[0, 1], [1, 0]]
move_op = nk.operator.LocalOperator(
hilbert=hi, operators=[X] * l, acting_on=[[i] for i in range(l)]
)
sa = nk.sampler.CustomSampler(machine=ma, move_operators=move_op)
samplers["CustomSampler Spin"] = sa
sa = nk.sampler.CustomSamplerPt(machine=ma, move_operators=move_op, n_replicas=4)
samplers["CustomSamplerPt Spin"] = sa
# Two types of custom moves
# single spin flips and nearest-neighbours exchanges
spsm = [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
ops = [X] * l
ops += [spsm] * l
acting_on = [[i] for i in range(l)]
acting_on += [[i, (i + 1) % l] for i in range(l)]
move_op = nk.operator.LocalOperator(hilbert=hi, operators=ops, acting_on=acting_on)
sa = nk.sampler.CustomSampler(machine=ma, move_operators=move_op)
samplers["CustomSampler Spin 2 moves"] = sa
# Diagonal density matrix sampling
ma = nk.machine.NdmSpinPhase(
hilbert=hi,
alpha=1,
beta=1,
use_visible_bias=True,
use_hidden_bias=True,
use_ancilla_bias=True,
)
ma.init_random_parameters(seed=1234, sigma=0.2)
dm = nk.machine.DiagonalDensityMatrix(ma)
sa = nk.sampler.MetropolisLocal(machine=dm)
samplers["Diagonal Density Matrix"] = sa
def test_states_in_hilbert():
for name, sa in samplers.items():
print("Sampler test: %s" % name)
hi = sa.hilbert
ma = sa.machine
localstates = hi.local_states
for sw in range(100):
sa.sweep()
visible = sa.visible
assert len(visible) == hi.size
for v in visible:
assert v in localstates
assert np.min(sa.acceptance) >= 0 and np.max(sa.acceptance) <= 1.0
def test_machine_func():
for name, sa in samplers.items():
print("Sampler test: %s" % name)
sa.machine_func = lambda x: np.absolute(x) ** 2.0
maf = sa.machine_func(3.0 + 1.0j)
assert maf == approx(np.absolute(3.0 + 1.0j) ** 2.0)
sa.machine_func = np.absolute
maf = sa.machine_func(3.0 + 1.0j)
assert maf == approx(np.absolute(3.0 + 1.0j))
# Testing that samples generated from direct sampling are compatible with those
# generated by markov chain sampling
# here we use the L_1 test presented in https://arxiv.org/pdf/1308.3946.pdf
def test_correct_sampling():
for name, sa in samplers.items():
print("Sampler test: %s" % name)
hi = sa.hilbert
ma = sa.machine
n_states = hi.n_states
n_samples = max(10 * n_states, 10000)
for ord in (1, 2):
if ord == 1:
sa.machine_func = np.absolute
if ord == 2:
sa.machine_func = lambda x: np.absolute(x) * np.absolute(x)
hist_samp = np.zeros(n_states)
# fill in the histogram for sampler
for sw in range(n_samples):
sa.sweep()
visible = sa.visible
hist_samp[hi.state_to_number(visible)] += 1
hist_exsamp = np.zeros(n_states)
sa = nk.sampler.ExactSampler(machine=ma)
if ord == 1:
sa.machine_func = np.absolute
if ord == 2:
sa.machine_func = lambda x: np.absolute(x) * np.absolute(x)
# fill in histogram for exact sampler
for sw in range(n_samples):
sa.sweep()
visible = sa.visible
hist_exsamp[hi.state_to_number(visible)] += 1
# now test that histograms are close in norm
delta = hist_samp - hist_exsamp
z = np.sum(delta * delta - hist_exsamp - hist_samp)
z = np.sqrt(np.abs(z)) / float(n_samples)
eps = np.sqrt(1.0 / float(n_samples))
assert z == approx(0.0, rel=10 * eps, abs=10 * eps)