Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
szsz_hat += nk.operator.LocalOperator(hi, sz, [6]) * nk.operator.LocalOperator(
hi, sz, [8]
)
szsz_hat += nk.operator.LocalOperator(hi, sz, [7]) * nk.operator.LocalOperator(
hi, sz, [0]
)
operators["Custom Hamiltonian"] = sx_hat + sy_hat + szsz_hat
operators["Custom Hamiltonian Prod"] = sx_hat * 1.5 + (2.0 * sy_hat)
operators["Pauli Hamiltonian"] = nk.operator.PauliStrings(
["XX", "YZ", "IZ"], [0.1, 0.2, -1.4]
)
rg = nk.utils.RandomEngine(seed=1234)
def test_produce_elements_in_hilbert():
for name, ha in operators.items():
hi = ha.hilbert
print(name, hi)
assert len(hi.local_states) == hi.local_size
assert hi.size > 0
rstate = np.zeros(hi.size)
local_states = hi.local_states
for i in range(1000):
hi.random_vals(rstate, rg)
conns = ha.get_conn(rstate)
def test_log_derivative():
for name, machine in merge_dicts(machines, dm_machines).items():
print("Machine test: %s" % name)
npar = machine.n_par
# random visibile state
hi = machine.hilbert
assert hi.size > 0
rg = nk.utils.RandomEngine(seed=1234)
v = np.zeros(hi.size)
for i in range(100):
hi.random_vals(v, rg)
randpars = 0.1 * (np.random.randn(npar) + 1.0j * np.random.randn(npar))
machine.parameters = randpars
der_log = machine.der_log(v)
if "Jastrow" in name:
assert np.max(np.imag(der_log)) == approx(0.0)
num_der_log = central_diff_grad(log_val_f, randpars, 1.0e-8, machine, v)
same_derivatives(der_log, num_der_log)
hi, sz, [0]
)
herm_operators["sx (real op)"] = sx_hat
herm_operators["sy"] = sy_hat
herm_operators["Custom Hamiltonian"] = sx_hat + sy_hat + szsz_hat
herm_operators["Custom Hamiltonian Prod"] = sx_hat * 1.5 + (2.0 * sy_hat)
sm_hat = nk.operator.LocalOperator(hi, [sm] * 3, [[0], [1], [4]])
sp_hat = nk.operator.LocalOperator(hi, [sp] * 3, [[0], [1], [4]])
generic_operators["sigma +/-"] = (sm_hat, sp_hat)
rg = nk.utils.RandomEngine(seed=1234)
def same_matrices(matl, matr, eps=1.0e-6):
assert np.max(np.abs(matl - matr)) == approx(0.0, rel=eps, abs=eps)
def test_hermitian_local_operator_transpose_conjugation():
for name, op in herm_operators.items():
op_t = op.transpose()
op_c = op.conjugate()
op_h = op.transpose().conjugate()
mat = op.to_dense()
mat_t = op_t.to_dense()
mat_c = op_c.to_dense()
mat_h = op_h.to_dense()
def test_random_states():
""""""
for name, hi in hilberts.items():
assert hi.size > 0
assert hi.local_size > 0
assert len(hi.local_states) == hi.local_size
if hi.is_discrete:
rstate = np.zeros(hi.size)
rg = nk.utils.RandomEngine(seed=1234)
local_states = hi.local_states
for i in range(100):
hi.random_vals(rstate, rg)
for state in rstate:
assert state in local_states
def test_log_val_diff():
for name, machine in merge_dicts(machines, dm_machines).items():
print("Machine test: %s" % name)
npar = machine.n_par
randpars = 0.5 * (np.random.randn(npar) + 1.0j * np.random.randn(npar))
machine.parameters = randpars
hi = machine.hilbert
rg = nk.utils.RandomEngine(seed=1234)
# loop over different random states
for i in range(100):
# generate a random state
rstate = np.zeros(hi.size)
local_states = hi.local_states
hi.random_vals(rstate, rg)
tochange = []
newconfs = []
# random number of changes
for i in range(100):
n_change = np.random.randint(low=0, high=hi.size)
# generate n_change unique sites to be changed
def random_states(self, num_of_states):
import netket
random_engine = netket.utils.RandomEngine()
hilbert_space = self.netket_operator.hilbert
results = numpy.zeros((num_of_states, hilbert_space.size))
for i in range(num_of_states):
hilbert_space.random_vals(results[i, :], random_engine)
return numpy.reshape(results, (num_of_states, ) + self.hilbert_state_shape)