How to use the netket.operator function in NetKet

To help you get started, we’ve selected a few NetKet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github netket / netket / Test / Hilbert / test_hilbert.py View on Github external
for k, state in enumerate(hi.states()):
                assert hi.state_to_number(state) == k
        else:
            assert not hi.is_indexable

            with pytest.raises(RuntimeError):
                hi.n_states

            op = nk.operator.Heisenberg(hi)

            with pytest.raises(RuntimeError):
                m1 = op.to_dense()
            with pytest.raises(RuntimeError):
                m2 = op.to_sparse()
            with pytest.raises(RuntimeError):
                dw = nk.operator.DirectMatrixWrapper(op)
github netket / netket / Test / Operator / test_operator.py View on Github external
)
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)

operators["Pauli Hamiltonian"] = nk.operator.PauliStrings(
    ["XX", "YZ", "IZ"], [0.1, 0.2, -1.4]
)
github netket / netket / Examples / PyNetKet / ground_state.py View on Github external
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import netket as nk

SEED = 12345

# 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.1)

# Variational Monte Carlo
vmc = nk.variational.Vmc(
    hamiltonian=ha,
    sampler=sa,
github netket / netket / Examples / Ising1d / ising1d.py View on Github external
# 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.RbmSpin(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(
    hamiltonian=ha,
    sampler=sa,
    optimizer=op,
github netket / netket / Tutorials / BoseHubbard1d / bosehubbard1d_jastrow.py View on Github external
# 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 Periodic Lattice
g = nk.graph.Hypercube(length=12, n_dim=1, pbc=True)

# Boson Hilbert Space
hi = nk.hilbert.Boson(graph=g, n_max=3, n_bosons=12)

# Bose Hubbard Hamiltonian
ha = nk.operator.BoseHubbard(U=4.0, hilbert=hi)

# Jastrow Machine with Symmetry
ma = nk.machine.JastrowSymm(hilbert=hi)
ma.init_random_parameters(seed=1234, sigma=0.01)

# Sampler
sa = nk.sampler.MetropolisHamiltonian(machine=ma, hamiltonian=ha)

# Stochastic gradient descent optimization
op = nk.optimizer.Sgd(learning_rate=0.1)

# Variational Monte Carlo
vmc = nk.gs.Vmc(
    hamiltonian=ha,
    sampler=sa,
    optimizer=op,
github netket / netket / Examples / PyNetKet / hamiltonian.py View on Github external
ha = nk.operator.Ising(h=1.0, hilbert=hi)

print("\n")
print("Diagonalizing the Hamiltonian with the internal NetKet solver...")

t1 = datetime.now()
ed_result = nk.gs.LanczosEd(operator=ha, first_n=1, get_groundstate=False)
t2 = datetime.now()

print("Elapsed time =", (t2 - t1).total_seconds(), " s\n")

# Scipy sparse diagonalization
print("Diagonalizing the Hamiltonian with scipy...")

t1 = datetime.now()
sm = nk.operator.SparseMatrixWrapper(operator=ha)._matrix
vals = sparsediag.eigs(sm, k=1, return_eigenvectors=False, which='SR')
t2 = datetime.now()

print("Elapsed time =", (t2 - t1).total_seconds(), " s\n")

print('Energy = ', ed_result.eigenvalues, vals[0].real)
print('Expected = ', -1.274549484318e+00 * 20)