[Python] Double counting problems for Ising model I am trying to make (counting diagonally)

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to make a python code for calculating the energy (Hamiltonian). By having a matrix (state with 1's and -1's), my plan is to go through each element to its nearest diagonal neighbours and sum them (weighted). But my results differ when I count differently (this seems to be a result of double counting). I manually calculated what I got with the states I receive (the lowest possible energies) and it gives me an answer of -36 but the code gives -64.

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    from itertools import product

    size = 4 #Size of model
    beta = 2 #Constant parameter (for later)
    J = 1

    def energy2(matrix):
    engsum2 = 0
    for i in range(size):
    for j in range(size):
    leftup = matrix[(i-1) % size, (j+1) % size]
    leftdown = matrix[(i-1) % size, (j-1) % size]
    rightup = matrix[(i+1) % size, (j+1) % size]
    rightdown = matrix[(i+1) % size, (j-1) % size]
    s = matrix[i,j]
    e_n2 = s*rightup + s*rightdown + s*leftup + s*leftdown
    engsum2 = e_n2 + engsum2
    return J * engsum2

    matrices = product([1, -1], repeat=size*size) #Produces all combinations of the matrix states
    matrices = np.reshape(list(matrices), (-1, size, size)) #Reshapes them, makes it easier to manipulate
    engs2 = []

    for j in range(2**(size*size)):
    engs2.append(energy2(matrices[j]))

    engs2 = np.array(engs2)
    mineng2 = np.min(engs2)

    print("The state(s) with the minimum energy is/are:", matrices[engs2 == mineng2,:,:],"With the energy of,", mineng2)```

    Continue reading...

Compartilhe esta Página