Views
No views yet
1from functools import partial
2import jax
3import jax.numpy as jnp
4import netket as nk
5import math
6import flax
7from flax.training import checkpoints
8import numpy as np
9from netket.operator.spin import sigmax, sigmaz, sigmay
10
11flax.config.update('flax_use_orbax_checkpointing', False)
12
13p = 0.7 #* fix the value of the external field
14L = 6
15revision = f"L{L}_p{p}"
16
17def edges_square_lattice(L):
18 Ns = L*L
19 indices = np.arange(Ns)
20 indices_right = (indices+1)%L + L*(indices//L)
21 indices_down = (indices+L)%Ns
22 first = np.c_[indices, indices_right]
23 second = np.c_[indices, indices_down]
24
25 edges = np.concatenate([first, second], axis=0)
26 return edges
27
28def coupling_heis_random(random_J, edges):
29 edges_with_random_vars = list(zip(edges, random_J))
30 return edges_with_random_vars
31
32def si_sj(hi, i, j, txy=1.0):
33 # 0.25 factor is to take into account for spin operators
34 return 0.25*(txy * (sigmax(hi, i) * sigmax(hi, j) + sigmay(hi, i) * sigmay(hi, j)) + sigmaz(hi, i) * sigmaz(hi, j))
35
36def heisenberg_hamiltonian(edges_Js, hi, txy=1.0):
37 ham = 0.0
38 for (ij, J) in edges_Js:
39 ham += J * si_sj(hi, ij[0], ij[1], txy)
40 return ham
41
42from transformers import FlaxAutoModel
43wf = FlaxAutoModel.from_pretrained("nqs-models/heisenberg_disorder_fnqs",
44 trust_remote_code=True,
45 revision=revision)
46
47N_params = nk.jax.tree_size(wf.params)
48print('Number of parameters = ', N_params, flush=True)
49
50lattice = nk.graph.Hypercube(length=L, n_dim=2, pbc=True)
51hilbert = nk.hilbert.Spin(s=1/2, N=lattice.n_nodes, total_sz=0)
52
53# Random Heisenberg Hamiltonian
54from huggingface_hub import hf_hub_download
55coups_path = hf_hub_download(repo_id="nqs-models/heisenberg_disorder_fnqs", filename="coups", revision=revision)
56random_J = np.loadtxt(coups_path)[0]
57edges = edges_square_lattice(L)
58edges_Js = coupling_heis_random(random_J=random_J, edges=edges)
59
60N_mc = 6000
61
62hamiltonian = heisenberg_hamiltonian(edges_Js, hilbert)
63sampler = nk.sampler.MetropolisExchange(hilbert=hilbert,
64 graph=lattice,
65 d_max=2,
66 n_chains=N_mc,
67 sweep_size=lattice.n_nodes)
68
69key = jax.random.key(0)
70key, subkey = jax.random.split(key, 2)
71vstate = nk.vqs.MCState(sampler=sampler,
72 apply_fun=partial(wf.__call__, coups=random_J),
73 sampler_seed=subkey,
74 n_samples=N_mc,
75 n_discard_per_chain=0,
76 variables=wf.params,
77 chunk_size=N_mc)
78
79path = hf_hub_download(repo_id="nqs-models/heisenberg_disorder_fnqs", filename="spins", revision=revision)
80samples = checkpoints.restore_checkpoint(path, target=None)
81samples = jnp.array(samples, dtype='int8')
82vstate.sampler_state = vstate.sampler_state.replace(σ = samples)
83
84import time
85# Sample the model
86for _ in range(10):
87 start = time.time()
88 E = vstate.expect(hamiltonian)
89 vstate.sample()
90
91 print("Mean: ", E.mean.real / lattice.n_nodes, "\t time=", time.time()-start, flush=True)