Views
No views yet
1from functools import partial
2import numpy as np
3
4import jax
5import jax.numpy as jnp
6import netket as nk
7from huggingface_hub import hf_hub_download
8
9import flax
10from flax.training import checkpoints
11
12flax.config.update('flax_use_orbax_checkpointing', False)
13
14lattice = nk.graph.Hypercube(length=10, n_dim=2, pbc=True, max_neighbor_order=3)
15
16J2 = 0.5
17J3 = 0.0
18
19assert J2 >= 0. and J2 <= 1.0 #* the model has been trained on this interval
20assert J3 >= 0. and J3 <= 0.6 #* the model has been trained on this interval
21
22from transformers import FlaxAutoModel
23wf = FlaxAutoModel.from_pretrained("nqs-models/j1j2j3_square_fnqs", trust_remote_code=True)
24N_params = nk.jax.tree_size(wf.params)
25print('Number of parameters = ', N_params, flush=True)
26
27hilbert = nk.hilbert.Spin(s=1/2, N=lattice.n_nodes, total_sz=0)
28hamiltonian = nk.operator.Heisenberg(hilbert=hilbert,
29 graph=lattice,
30 J=[1.0, J2, J3],
31 sign_rule=[False, False, False]).to_jax_operator() # No Marshall sign rule
32
33sampler = nk.sampler.MetropolisExchange(hilbert=hilbert,
34 graph=lattice,
35 d_max=2,
36 n_chains=16000,
37 sweep_size=lattice.n_nodes)
38
39key = jax.random.PRNGKey(0)
40key, subkey = jax.random.split(key, 2)
41coups = np.array([J2, J3])
42vstate = nk.vqs.MCState(sampler=sampler,
43 apply_fun=partial(wf.__call__, coups=coups),
44 sampler_seed=subkey,
45 n_samples=16000,
46 n_discard_per_chain=0,
47 variables=wf.params,
48 chunk_size=16000)
49
50# Overwrite samples with already thermalized ones
51path = hf_hub_download(repo_id="nqs-models/j1j2j3_square_fnqs", filename="spins")
52samples = checkpoints.restore_checkpoint(ckpt_dir=path, prefix="spins", target=None)
53samples = jnp.array(samples, dtype='int8')
54vstate.sampler_state = vstate.sampler_state.replace(σ = samples)
55
56import time
57# Sample the model
58for _ in range(100):
59 start = time.time()
60 E = vstate.expect(hamiltonian)
61 vstate.sample()
62
63 print("Mean: ", E.mean.real / lattice.n_nodes / 4, "\t time=", time.time()-start)1wf = FlaxAutoModel.from_pretrained("nqs-models/j1j2_square_fnqs", trust_remote_code=True, return_z=True)
2
3z = wf(wf.params, samples, J2)