Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import netket as nk
import networkx as nx
import igraph as ig
import math
nxg = nx.star_graph(10)
graphs = [
nk.graph.Hypercube(length=10, n_dim=1, pbc=True),
nk.graph.Hypercube(length=4, n_dim=2, pbc=True),
nk.graph.Hypercube(length=5, n_dim=1, pbc=False),
nk.graph.CustomGraph(nxg.edges()),
nk.graph.Lattice(
basis_vectors=[[1.0, 0.0], [1.0 / 2.0, math.sqrt(3) / 2.0]],
extent=[10, 10],
pbc=[0, 0],
atoms_coord=[[0, 0]],
),
nk.graph.Lattice(
basis_vectors=[[1.5, math.sqrt(3) / 2.0], [0, math.sqrt(3)]],
extent=[3, 5],
atoms_coord=[[0, 0], [1, 0]],
),
nk.graph.Lattice(
basis_vectors=[[2.0, 0.0], [1.0, math.sqrt(3)]],
extent=[4, 4],
atoms_coord=[[0, 0], [1.0 / 2.0, math.sqrt(3) / 2.0], [1.0, 0.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]]
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
# 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 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]))
import numpy as np
import netket as nk
# Exact ground state energy of AKLT model is zero by construction, see above references.
Sz = [[1, 0, 0], [0, 0, 0], [0, 0, -1]]
Sup = [[0, np.sqrt(2), 0], [0, 0, np.sqrt(2)], [0, 0, 0]]
Sdn = [[0, 0, 0], [np.sqrt(2), 0, 0], [0, np.sqrt(2), 0]]
# Heisenberg term
heisenberg = 0.5 * (np.kron(Sup, Sdn) + np.kron(Sdn, Sup)) + np.kron(Sz, Sz)
# AKLT two-site projector
P2_AKLT = 0.5 * heisenberg + np.dot(heisenberg, heisenberg) / 6.0 + np.identity(9) / 3.0
# 1D Lattice
g = nk.graph.Hypercube(length=10, n_dim=1, pbc=True)
# Hilbert space of spin-1s on the graph
hi = nk.hilbert.Spin(s=1, graph=g)
# AKLT model Hamiltonian as graph
ha = nk.operator.GraphOperator(hilbert=hi, bondops=[P2_AKLT.tolist()])
# Perform Lanczos Exact Diagonalization to get lowest three eigenvalues
res = nk.exact.lanczos_ed(ha, first_n=3, compute_eigenvectors=True)
# Print eigenvalues
print("eigenvalues:", res.eigenvalues)
# Compute energy of ground state
print("ground state energy:", res.mean(ha, 0))
def run_netket(args):
g = nk.graph.Hypercube(length=args.input_size, n_dim=1)
hi = nk.hilbert.Spin(s=0.5, total_sz=0, graph=g)
ha = nk.operator.Heisenberg(hilbert=hi)
middle_layer = (nk.layer.ConvolutionalHypercube(length=args.input_size,
n_dim=1,
input_channels=args.width,
output_channels=args.width,
kernel_length=args.kernel_size),
nk.layer.Lncosh(input_size=args.width * args.input_size))
middle_layers = middle_layer * (args.depth - 1)
first_layer = (nk.layer.ConvolutionalHypercube(length=args.input_size,
n_dim=1,
input_channels=1,
output_channels=args.width,
kernel_length=args.kernel_size),
nk.layer.Lncosh(input_size=args.width * args.input_size),)
ma = nk.machine.FFNN(hi, first_layer + middle_layers)
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]
)
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 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
# with total Sz equal to 0
hi = nk.hilbert.Spin(s=0.5, graph=g, total_sz=0)
# Heisenberg hamiltonian
ha = nk.operator.Heisenberg(hilbert=hi)
# Symmetric RBM Spin Machine
ma = nk.machine.JastrowSymm(hilbert=hi)
ma.init_random_parameters(seed=1234, sigma=0.01)
# Metropolis Exchange Sampling
# Notice that this sampler exchanges two neighboring sites
# thus preservers the total magnetization
sa = nk.sampler.MetropolisExchange(machine=ma, graph=g)