API Documentation#

The montecarlo package exposes three top-level classes.

BitString#

class montecarlo.BitString(N)[source]#

Bases: object

Simple class to implement a configuration of bits.

flip_site(i)[source]#

Flip the bit at site i.

integer()[source]#

Return the decimal integer corresponding to BitString.

off()[source]#

Return number of bits that are off.

on()[source]#

Return number of bits that are on.

set_config(s)[source]#

Set the config from a list of integers.

set_integer_config(dec)[source]#

Convert a decimal integer to binary and set as config.

IsingHamiltonian#

class montecarlo.IsingHamiltonian(G: Graph)[source]#

Bases: object

Ising Hamiltonian defined on a graph.

H = sum_{(i,j) in E} J_ij s_i s_j + sum_i mu_i s_i

where s_i = +1 if bit i is 1 (up) and s_i = -1 if bit i is 0 (down).

compute_average_values(T: float)[source]#

Compute exact thermodynamic averages at temperature T.

Enumerates all 2^N configurations and computes Boltzmann-weighted averages of energy, magnetization, heat capacity, and magnetic susceptibility.

Parameters:

T (float) – Temperature

Returns:

E : average energy M : average magnetization HC : heat capacity MS : magnetic susceptibility

Return type:

tuple of (E, M, HC, MS)

energy(config: BitString)[source]#

Compute the energy of a BitString configuration.

Parameters:

config (BitString) – Input spin configuration

Returns:

Energy of the configuration

Return type:

float

set_mu(mus)[source]#

Set the local magnetic field terms.

Parameters:

mus (array-like) – Magnetic field values for each site

Returns:

Returns self for chaining

Return type:

self

MonteCarlo#

class montecarlo.MonteCarlo(ham: IsingHamiltonian)[source]#

Bases: object

Metropolis Monte Carlo sampler for an Ising Hamiltonian.

Parameters:

ham (IsingHamiltonian) – The Hamiltonian to sample

run(T: float, n_samples: int = 1000, n_burn: int = 100)[source]#

Run Metropolis Monte Carlo sampling.

For each MC step, sweep over all sites proposing single spin flips. Accept or reject based on the Metropolis criterion:

  • If dE <= 0: accept

  • If dE > 0: accept with probability exp(-dE/T)

Parameters:
  • T (float) – Temperature

  • n_samples (int) – Number of samples to collect after burn-in

  • n_burn (int) – Number of burn-in sweeps to discard

Returns:

energies : np.ndarray of length n_samples magnetizations : np.ndarray of length n_samples

Return type:

tuple of (energies, magnetizations)