Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _setup():
g = nk.graph.Hypercube(3, 2)
hi = nk.hilbert.Spin(g, 0.5)
ham = nk.operator.Heisenberg(hi)
ma = nk.machine.RbmSpin(hi, alpha=2)
ma.init_random_parameters()
return hi, ham, ma
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 = [
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
imgs = data.reshape(num_images, image_size*image_size)
with gzip.open("train-labels-idx1-ubyte.gz", "r") as f:
# Skip the first 8 bytes
f.read(8)
# Then read as many bytes as we need for num_images labels
buf = f.read(num_images)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
targets = data.reshape(num_images, 1)
# 256 inputs
g = gr.Hypercube(length=256, n_dim=1)
hi = hs.Qubit(graph=g)
training_samples = []
training_targets = []
for i in range(num_images):
training_samples.append(imgs[i].tolist())
training_targets.append([targets[i]])
return hi, imgs, targets
for i in range(L):
for d in [0, 1]:
# \sum_i J*sigma^z(i)*sigma^z(i+d)
mats.append((J[d] * mszsz).tolist())
sites.append([i, (i + d + 1) % L])
# \sum_i J*(sigma^x(i)*sigma^x(i+d) + sigma^y(i)*sigma^y(i+d))
mats.append(((-1.0) ** (d + 1) * J[d] * exchange).tolist())
sites.append([i, (i + d + 1) % L])
# Custom Graph
g = nk.graph.Hypercube(length=L, n_dim=1, pbc=True)
# Spin based Hilbert Space
hi = nk.hilbert.Spin(s=0.5, total_sz=0.0, graph=g)
# Custom Hamiltonian operator
op = nk.operator.LocalOperator(hi)
for mat, site in zip(mats, sites):
op += nk.operator.LocalOperator(hi, mat, site)
# Restricted Boltzmann Machine
ma = nk.machine.RbmSpin(hi, alpha=1)
ma.init_random_parameters(seed=1234, sigma=0.01)
# Sampler
sa = nk.sampler.MetropolisHamiltonianPt(machine=ma, hamiltonian=op, n_replicas=16)
# Optimizer
opt = nk.optimizer.Sgd(learning_rate=0.01)
from __future__ import print_function
import netket as nk
import sys
SEED = 3141592
# Constructing a 1d lattice
g = nk.graph.Hypercube(length=20, n_dim=1)
# Hilbert space of spins from given graph
hi = nk.hilbert.Spin(s=0.5, graph=g)
# Hamiltonian
ha = nk.operator.Ising(h=1.0, hilbert=hi)
# Machine
ma = nk.machine.RbmSpin(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=SEED, sigma=0.01)
# Sampler
sa = nk.sampler.MetropolisLocal(machine=ma)
sa.seed(SEED)
# Optimizer
op = nk.optimizer.Sgd(learning_rate=0.01)
# Variational Monte Carlo
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import netket as nk
# 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.RbmSpinReal(alpha=1, hilbert=hi)
ma.init_random_parameters(seed=1234, sigma=0.01)
# Metropolis Local Sampling
sa = nk.sampler.MetropolisLocal(machine=ma, n_chains=16, sweep_size=20)
# Optimizer
op = nk.optimizer.Sgd(learning_rate=0.1)
# Stochastic reconfiguration
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import netket as nk
# 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)
# Jastrow Machine
ma = nk.machine.Jastrow(hilbert=hi)
ma.init_random_parameters(seed=1234, sigma=0.01)
# Metropolis Local Sampling
sa = nk.sampler.MetropolisLocal(machine=ma)
# Optimizer
op = nk.optimizer.Sgd(learning_rate=0.1)
# Stochastic reconfiguration
gs = nk.variational.Vmc(
edge_colors.append([w + L2 * h, w + 1 + L2 * (L1 - 1), 2])
w = L2 - 1
if pbc:
edge_colors.append([L2 - 1 + L2 * h, L2 * h, 1])
edge_colors.append([w + L2 * h, L2 * ((h + 1) % L1), 2])
edge_colors.append([w + L2 * h, L2 * ((L1 + h - 1) % L1), 2])
if h < L1 - 1:
edge_colors.append([w + L2 * h, w + L2 * (h + 1), 1])
elif pbc:
edge_colors.append([w + L2 * h, w, 1])
g = netket.graph.CustomGraph(edge_colors)
if L1 * L2 % 2 == 0:
hi = netket.hilbert.Spin(s=0.5, total_sz=0.0, graph=g)
else:
hi = netket.hilbert.Spin(s=0.5, graph=g)
sigmaz = [[1, 0], [0, -1]]
sigmax = [[0, 1], [1, 0]]
sigmay = [[0, -1j], [1j, 0]]
interaction = numpy.kron(sigmaz, sigmaz) + numpy.kron(sigmax, sigmax) + numpy.kron(sigmay, sigmay)
bond_operator = [
(J[0] * interaction).tolist(),
(J[1] * interaction).tolist(),
]
bond_color = [1, 2]
return netket.operator.GraphOperator(hi, bondops=bond_operator, bondops_colors=bond_color)
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import netket as nk
import numpy as np
# 1D Lattice
L = 20
g = nk.graph.Hypercube(length=L, n_dim=1, pbc=True)
# Hilbert space of spins on the graph
hi = nk.hilbert.Spin(s=0.5, graph=g)
# Defining the Ising hamiltonian (with sign problem here)
# Using local operators
sx = [[0, 1], [1, 0]]
sz = [[1, 0], [0, -1]]
ha = nk.operator.LocalOperator(hi)
for i in range(L):
ha += nk.operator.LocalOperator(hi, sx, [i])
ha += nk.operator.LocalOperator(hi, np.kron(sz, sz), [i, (i + 1) % L])
# RBM Spin Machine
ma = nk.machine.RbmSpinPhase(alpha=1, hilbert=hi)
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import netket as nk
# 1D Lattice
L = 20
g = nk.graph.Hypercube(length=L, 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.RbmSpinReal(alpha=1, hilbert=hi)
ma.init_random_parameters(seed=1234, sigma=0.01)
# Metropolis Local Sampling
sa = nk.sampler.MetropolisLocal(machine=ma)
# Optimizer
op = nk.optimizer.Sgd(learning_rate=0.1)
# Stochastic reconfiguration
gs = nk.variational.Vmc(