JohanSchott / impurityModel

Calculate many-body states of an impurity Anderson model and spectra (e.g. XPS, XAS, RIXS, NIXS)
MIT License
22 stars 2 forks source link

create and remove using integer representation do not need to go through string representation #46

Closed JohanSchott closed 11 months ago

JohanSchott commented 11 months ago

uint in create.py and remove.py convert the product state from the integer representation to a string representation. This is not needed. One can stay in the integer representation by using bit-operations: <<, >>, &, |, ^ and python3.10 method https://docs.python.org/3.10/library/stdtypes.html#int.bit_count

JohanSchott commented 11 months ago

Perhaps something like this for removing an electron in spin-orbital i from product state state:

def uint(n_spin_orbitals, i, state):
    i_right = n_spin_orbitals - 1 - i
    if state & (1 << i_right):
        # Spin-orbital is occupied
        # Remove electron with XOR-operator
        state_new = state ^ (1 << i_right)
        # Want to count number of electrons in spin-orbitals with index lower than i. 
        # First right bit-shift to get rid of electrons with index equal or bigger than i.
        # Then count if number of electrons are even or odd. 
        tmp = state >> (i_right + 1)
        amp = 1 if tmp.bit_count() % 2 == 0 else -1
        return state_new, amp
    else:
        # Can't remove since spin-orbital is unoccupied   
        return -1, 0

And perhaps something like this for creating an electron in spin-orbital i from product state state:

def uint(n_spin_orbitals, i, state):
    i_right = n_spin_orbitals - 1 - i
    if state & (1 << i_right):
        # Spin-orbital is already occupied.
        # Can't add one more electron in that spin-orbital    
        return -1, 0
    else:        
        # Create electron with OR-operator
        state_new = state |  (1 << i_right)
        # Want to count number of electrons in spin-orbitals with index lower than i. 
        # First right bit-shift to get rid of electrons with index equal or bigger than i.
        # Then count if number of electrons are even or odd. 
        tmp = state >> (i_right + 1)
        amp = 1 if tmp.bit_count() % 2 == 0 else -1
        return state_new, amp     
JohanSchott commented 11 months ago

@kalvdans kindly said IRL that one can use & ~ instead of ^ to remove one bit, and that this is a more standard operation.