Closed JohanSchott closed 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
@kalvdans kindly said IRL that one can use & ~
instead of ^
to remove one bit, and that this is a more standard operation.
uint
increate.py
andremove.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