Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_imag_time_propagation():
g = nk.graph.Hypercube(length=8, n_dim=1, pbc=True)
hi = nk.hilbert.Spin(s=0.5, graph=g)
ha = nk.operator.Ising(h=0.0, hilbert=hi)
stepper = nk.dynamics.timestepper(hi.n_states, rel_tol=1e-10, abs_tol=1e-10)
psi0 = np.random.rand(hi.n_states)
driver = nk.exact.ExactTimePropagation(
ha, stepper, t0=0, initial_state=psi0, propagation_type="imaginary"
)
for step in driver.iter(dt=0.1, n_iter=1000):
pass
assert driver.get_observable_stats()["Energy"]["Mean"] == approx(-8.0)
def _setup_vmc():
g = nk.graph.Hypercube(length=8, n_dim=1)
hi = nk.hilbert.Spin(s=0.5, graph=g)
ma = nk.machine.RbmSpin(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=SEED, sigma=0.01)
ha = nk.operator.Ising(hi, h=1.0)
sa = nk.sampler.MetropolisLocal(machine=ma)
sa.seed(SEED)
op = nk.optimizer.Sgd(learning_rate=0.1)
vmc = nk.variational.Vmc(
hamiltonian=ha, sampler=sa, optimizer=op, n_samples=500, diag_shift=0.01
)
# Add custom observable
X = [[0, 1], [1, 0]]
sx = nk.operator.LocalOperator(hi, [X] * 8, [[i] for i in range(8)])
vmc.add_observable(sx, "SigmaX")
return ma, vmc
# 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)
def test_ed():
first_n = 3
g = nk.graph.Hypercube(length=8, n_dim=1, pbc=True)
hi = nk.hilbert.Spin(s=0.5, graph=g)
ha = nk.operator.Ising(h=1.0, hilbert=hi)
# Test Lanczos ED with eigenvectors
res = nk.exact.lanczos_ed(ha, first_n=first_n, compute_eigenvectors=True)
assert len(res.eigenvalues) == first_n
assert len(res.eigenvectors) == first_n
gse = res.mean(ha, 0)
fse = res.mean(ha, 1)
assert gse == approx(res.eigenvalues[0], rel=1e-12, abs=1e-12)
assert fse == approx(res.eigenvalues[1], rel=1e-12, abs=1e-12)
# Test Lanczos ED without eigenvectors
res = nk.exact.lanczos_ed(ha, first_n=first_n, compute_eigenvectors=False)
assert len(res.eigenvalues) == first_n
assert len(res.eigenvectors) == 0
# Test Full ED with eigenvectors
def _setup_vmc():
L = 4
g = nk.graph.Hypercube(length=L, n_dim=1)
hi = nk.hilbert.Spin(s=0.5, graph=g)
ma = nk.machine.RbmSpin(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=SEED, sigma=0.01)
ha = nk.operator.Ising(hi, h=1.0)
sa = nk.sampler.ExactSampler(machine=ma)
sa.seed(SEED)
op = nk.optimizer.Sgd(learning_rate=0.1)
# Add custom observable
X = [[0, 1], [1, 0]]
sx = nk.operator.LocalOperator(hi, [X] * L, [[i] for i in range(8)])
driver = nk.variational.Vmc(ha, sa, op, 1000)
return ha, sx, ma, sa, driver
import netket as nk
import networkx as nx
import numpy as np
operators = {}
# Ising 1D
g = nk.graph.Hypercube(length=20, n_dim=1, pbc=True)
hi = nk.hilbert.Spin(s=0.5, graph=g)
operators["Ising 1D"] = nk.operator.Ising(h=1.321, hilbert=hi)
# Heisenberg 1D
g = nk.graph.Hypercube(length=20, n_dim=1, pbc=True)
hi = nk.hilbert.Spin(s=0.5, total_sz=0, graph=g)
operators["Heisenberg 1D"] = nk.operator.Heisenberg(hilbert=hi)
# Bose Hubbard
g = nk.graph.Hypercube(length=3, n_dim=2, pbc=True)
hi = nk.hilbert.Boson(n_max=3, n_bosons=6, graph=g)
operators["Bose Hubbard"] = nk.operator.BoseHubbard(U=4.0, hilbert=hi)
# Graph Hamiltonian
sigmax = [[0, 1], [1, 0]]
mszsz = [[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]
edges = [
[0, 1],
def generate(N, n_basis=20, n_shots=1000, seed=1234):
g = gr.Hypercube(length=N, n_dim=1, pbc=False)
hi = hs.Spin(g, s=0.5)
ha = op.Ising(hilbert=hi, h=1)
res = exact.lanczos_ed(ha, first_n=1, compute_eigenvectors=True)
psi = res.eigenvectors[0]
rotations = []
training_samples = []
training_bases = []
np.random.seed(seed)
for m in range(n_basis):
basis = np.random.choice(
list("XYZ"), size=N, p=[1.0 / N, 1.0 / N, (N - 2.0) / N]
)
rotation = build_rotation(hi, basis)
import cProfile
import netket as nk
import numpy as np
from netket.operator import local_values
# 1D Lattice
g = nk.graph.Hypercube(length=20, n_dim=1, pbc=True)
# Hilbert space of spins on the graph
hi = nk.hilbert.Spin(s=0.5, graph=g)
# Ising spin hamiltonian
ha = nk.operator.Ising(h=1.0, hilbert=hi)
# RBM Spin Machine
ma = nk.machine.RbmSpin(alpha=1, hilbert=hi)
ma.init_random_parameters(seed=1234, sigma=0.01)
# Metropolis Local Sampling
sa = nk.sampler.MetropolisLocal(machine=ma, n_chains=8)
n_samples = 1000
samples = np.zeros((n_samples, sa.sample_shape[0], sa.sample_shape[1]))
for i, sample in enumerate(sa.samples(n_samples)):
samples[i] = sample
loc = np.empty(samples.shape[0:2], dtype=np.complex128)