samuelmsoares
samuelmsoares

Reputation: 39

Large matrices for the quantum Ising Model

I made a Python code that computes the density matrix of states for the Ising model with a transverse field and then calculates the von Neumann entropy for the system. The problem is that the density matrix of states grows with $2^N$, where N is the number of spins in the lattice. The program works well up to N = 14, but for N = 15 it crashes. It is important to note that I am using Google Colab to run the code, and I have 12.7 GB of RAM available. Could someone help me optimize the code to run with larger matrices?

import numpy as np
import matplotlib.pyplot as plt
from qutip import sigmax, sigmaz, qeye, tensor, Qobj, entropy_vn

def hamiltonian(N, lambd):
    sx = sigmax()
    sz = sigmaz()
    si = qeye(2)
    
    H = 0
    for i in range(N):
        H -= lambd * tensor([sx if j == i or j == (i+1)%N else si for j in range(N)])
        H -= tensor([sz if j == i else si for j in range(N)])
    
    return H

def reduced_density_matrix(psi, N):
    rho = psi.ptrace(0)  # Partial trace to get the reduced density matrix
    return rho

def von_neumann_entropy(N, lambd):
    H = hamiltonian(N, lambd)
    eigvals, eigvecs = H.eigenstates()
    ground_state = eigvecs[0]
    rho_i = reduced_density_matrix(ground_state, N)
    S_vn = entropy_vn(rho_i)  # Compute the von Neumann entropy
    return S_vn

# Plotting the von Neumann entropy for various values of N
Ns = range(2, 16)  # Values of N from 2 to 15
lambd = 1.0  # Transverse field strength
entropies = []

for N in Ns:
    S_vn = von_neumann_entropy(N, lambd)
    entropies.append(S_vn)

plt.figure(figsize=(10, 6))
plt.plot(Ns, entropies, marker='o')
plt.xlabel('Number of Spins (N)')
plt.ylabel('Von Neumann Entropy')
plt.title('Von Neumann Entropy vs. Number of Spins for Transverse Field Ising Model')
plt.grid(True)
plt.show()

I tried to compute just the first eigenvalue using:

H.eigenstates(eigvals=1, sparse=True)

but it just work for N = 14, for higher values of N it still doesn't work. I also tried to make a interpolation/extrapolation but was not a good approximation and I want to compute exactly the density matrix.

Upvotes: 3

Views: 78

Answers (1)

Bruno Augusto Veloso
Bruno Augusto Veloso

Reputation: 50

I've tested your code, and noticed that, removing the eigvals=1 and using only a sparse matrix, can lead to a serious drop in the memory usage. I don't know what is happening that makes the eigvals=1 consume so much memory, but removing it does the trick.

As a side note, if you need more RAM, consider using Linux SWAP memory. It is a useful way to use part of your HD or SSD as a secondary RAM memory. It will be slower, but can be useful as a substitute to the actual RAM.

Upvotes: 0

Related Questions