Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
g = nk.graph.CustomGraph(edges=edges)
hi = nk.hilbert.CustomHilbert(local_states=[-1, 1], graph=g)
ha = nk.operator.GraphOperator(
hi, siteops=[sigmax], bondops=[mszsz], bondops_colors=[0]
)
operators["Graph Hamiltonian"] = ha
# Custom Hamiltonian
sx = [[0, 1], [1, 0]]
sy = [[0, 1.0j], [-1.0j, 0]]
sz = [[1, 0], [0, -1]]
g = nk.graph.CustomGraph(edges=[[i, i + 1] for i in range(20)])
hi = nk.hilbert.CustomHilbert(local_states=[1, -1], graph=g)
sx_hat = nk.operator.LocalOperator(hi, [sx] * 3, [[0], [1], [5]])
sy_hat = nk.operator.LocalOperator(hi, [sy] * 4, [[2], [3], [4], [9]])
szsz_hat = nk.operator.LocalOperator(hi, sz, [0]) * nk.operator.LocalOperator(
hi, sz, [1]
)
szsz_hat += nk.operator.LocalOperator(hi, sz, [4]) * nk.operator.LocalOperator(
hi, sz, [5]
)
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)
sy = [[0, 1.0j], [-1.0j, 0]]
sz = [[1, 0], [0, -1]]
sm = [[0, 0], [1, 0]]
sp = [[0, 1], [0, 0]]
g = nk.graph.CustomGraph(edges=[[i, i + 1] for i in range(8)])
hi = nk.hilbert.CustomHilbert(local_states=[1, -1], graph=g)
sx_hat = nk.operator.LocalOperator(hi, [sx] * 3, [[0], [1], [4]])
sy_hat = nk.operator.LocalOperator(hi, [sy] * 4, [[1], [2], [3], [4]])
szsz_hat = nk.operator.LocalOperator(hi, sz, [0]) * nk.operator.LocalOperator(
hi, sz, [1]
)
szsz_hat += nk.operator.LocalOperator(hi, sz, [4]) * nk.operator.LocalOperator(
hi, sz, [5]
)
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]
)
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)
def test_no_segfault():
g = nk.graph.Hypercube(8, 1)
hi = nk.hilbert.Spin(g, 0.5)
lo = nk.operator.LocalOperator(hi, [[1, 0], [0, 1]], [0])
lo = lo.transpose()
hi = None
lo = lo * lo
assert True
)
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]
)
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()
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
def build_rotation(hi, basis):
localop = op.LocalOperator(hi, 1.0)
U_X = 1.0 / (ma.sqrt(2)) * np.asarray([[1.0, 1.0], [1.0, -1.0]])
U_Y = 1.0 / (ma.sqrt(2)) * np.asarray([[1.0, -1j], [1.0, 1j]])
N = hi.size
assert len(basis) == hi.size
for j in range(hi.size):
if basis[j] == "X":
localop *= op.LocalOperator(hi, U_X, [j])
if basis[j] == "Y":
localop *= op.LocalOperator(hi, U_Y, [j])
return localop
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 in the phase representation
ma = nk.machine.RbmSpinPhase(hi, alpha=1)
ma.init_random_parameters(seed=1234, sigma=0.1)
# Sampler
sa = nk.sampler.MetropolisExchange(machine=ma, graph=g)
# Optimizer
opt = nk.optimizer.Sgd(learning_rate=0.01)
# Variational Monte Carlo
gs = nk.variational.Vmc(
hamiltonian=op, sampler=sa, optimizer=opt, n_samples=1000, method="Sr"
)
def build_rotation(hi, basis):
localop = op.LocalOperator(hi, 1.0)
U_X = 1.0 / (ma.sqrt(2)) * np.asarray([[1.0, 1.0], [1.0, -1.0]])
U_Y = 1.0 / (ma.sqrt(2)) * np.asarray([[1.0, -1j], [1.0, 1j]])
N = hi.size
assert len(basis) == hi.size
for j in range(hi.size):
if basis[j] == "X":
localop *= op.LocalOperator(hi, U_X, [j])
if basis[j] == "Y":
localop *= op.LocalOperator(hi, U_Y, [j])
return localop