Academy K

Implementing Gibbs Sampling

InterviewCodingGibbs samplingimplementationIsing model

Related module: Stochastic computing and sampling

Problem statement

Implement Gibbs sampling for the 2D Ising model:

E=−J∑⟨i,j⟩σiσj−h∑iσiE = -J\sum_{\langle i,j\rangle}\sigma_i\sigma_j - h\sum_i\sigma_i

where the sum is over nearest-neighbor pairs on a square lattice.

Write code to:
1. Update a single spin given its neighbors.
2. Perform one sweep of Gibbs sampling.
3. Measure the magnetization.

Practise this problem

Verify you understand it—not just that you read the answer.

Learn the foundations

The Thermodynamic Computing track covers this topic and more in depth.