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=2)
15
16J2 = 0.5
17
18assert J2 >= 0.4 and J2 <= 0.6 #* the model has been trained on this interval
19
20from transformers import FlaxAutoModel
21wf = FlaxAutoModel.from_pretrained("nqs-models/j1j2_square_fnqs", trust_remote_code=True)
22N_params = nk.jax.tree_size(wf.params)
23print('Number of parameters = ', N_params, flush=True)
24
25hilbert = nk.hilbert.Spin(s=1/2, N=lattice.n_nodes, total_sz=0)
26hamiltonian = nk.operator.Heisenberg(hilbert=hilbert,
27 graph=lattice,
28 J=[1.0, J2],
29 sign_rule=[False, False]).to_jax_operator() # No Marshall sign rule
30
31sampler = nk.sampler.MetropolisExchange(hilbert=hilbert,
32 graph=lattice,
33 d_max=2,
34 n_chains=16000,
35 sweep_size=lattice.n_nodes)
36
37key = jax.random.PRNGKey(0)
38key, subkey = jax.random.split(key, 2)
39vstate = nk.vqs.MCState(sampler=sampler,
40 apply_fun=partial(wf.__call__, coups=J2),
41 sampler_seed=subkey,
42 n_samples=16000,
43 n_discard_per_chain=0,
44 variables=wf.params,
45 chunk_size=16000)
46
47# Overwrite samples with already thermalized ones
48path = hf_hub_download(repo_id="nqs-models/j1j2_square_fnqs", filename="spins")
49samples = checkpoints.restore_checkpoint(ckpt_dir=path, prefix="spins", target=None)
50samples = jnp.array(samples, dtype='int8')
51vstate.sampler_state = vstate.sampler_state.replace(σ = samples)
52
53import time
54# Sample the model
55for _ in range(10):
56 start = time.time()
57 E = vstate.expect(hamiltonian)
58 vstate.sample()
59
60 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)