[Python] Matrix free arnoldi

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I want to do a "matrix-free eigenvalue evaluation" of a unitary matrix using ARPACK package. Unitary matrix comes from a sparse hermitian matrix, something that happens in many-body problem.

    I used the following code to construct a random sparse matrix ( here it is small size for reason that the matrix free version takes exponentially long time as compared to this version for evaluation)

    import time
    import numpy as np
    import scipy as sp
    from scipy.sparse.linalg import LinearOperator
    from scipy.sparse.linalg import expm, expm_multiply # For matrix exponentials
    from scipy.sparse.linalg import eigs, eigsh
    from scipy.stats import unitary_group
    # Generate a random unitary matrix
    N = 20 # Size of the matrix
    den = 0.01

    # Define Hermitian matrices H1 and H2 (only stored implicitly)
    H1 = sp.sparse.random_array((N,N), density=den)
    H1 = (H1 + H1.conj().T) / 2
    H2 = sp.sparse.random_array((N,N), density=den)
    H2 =(H2 + H2.conj().T) / 2

    U= expm(-1j * H2) @ expm(-1j * H1)

    start = time.time()
    # Compute eigenvalues using scipy
    eigenvalues,_= eigs(U,5,sigma= np.exp(1j * np.pi / 4), which = 'SR')
    end = time.time()
    print("The time of execution of dense matrix program is :",(end-start) * 10**3, "ms")
    # Print the results
    print("Eigenvalues of the unitary matrx:")
    print(eigenvalues)


    Now I have the unitary matrix as $U= e^{-i H_{2}} e^{-i H_{1}}$. To have it in a matrix free form I wrote the following code and used ARPACK to find the eigenvalues

    def apply_U(v):
    v= expm_multiply(-1j * H2, expm_multiply(-1j* H1,v))
    return v
    A = LinearOperator((N, N), matvec=apply_U)

    start = time.time()
    # Compute eigenvalues using scipy
    eigenvalues,_= eigs(A,5,sigma= np.exp(1j * np.pi / 4), which = 'SR')
    end = time.time()
    print("The time of execution of matrix-free is :",(end-start) * 10**3, "ms")
    # Print the results
    print("Eigenvalues of the unitary matrx:")
    print(eigenvalues)


    I have checked that both of them give the same result except the matrix-free version is very slow. My expectations were that it would be faster. Please help me optimize the code, as I am new to python ( I coded this all with my intuition from Mathematica in which I am fluent). I would like to know if it is an expected behavior from the matrix free evaluations or is there any problem with my code specifically, thus making it slow

    Continue reading...

Compartilhe esta Página