def setMBS(self, mbs):
            
        self.mbs = mbs

        parity = 1
        occPos = 0
        empPos = 0
        self.occList = []
        self.nextEmptyList = []
        self.emptyList = []

        # Loop through each of the sps's:
        for s in xrange(self.nStates):
            self.parity[s] = parity # set the parity

            if self.mbs & (0x1 << s): # if s is an occupied state

                if self.vsList[s]:  # if it is a valid state for the operator
                    self.occList.append(s)
                    self.nextEmptyList.append(empPos)
                    occPos = occPos + 1

                parity = -parity # if occupied, change the next state parity

            elif self.vsList[s]:
                self.emptyList.append(s) # mark unoccupied
                empPos = empPos + 1

        # So, we need to find n = p electrons choose p electron states
        # p electrons is nParticles - f electrons
        # p states is fixed (well, based on the vsList)
        # see discussion in __init__

        if self.optStates > 0:
            #print self.optStates, occPos
            return int(CG.binomialCoeff(self.optStates, self.nParticles - occPos))
        else:
            return 1
def genFermionStateTable(nFermions):

    plist = Plist.AtomicOrbitalPlist()
    plist.readPlist()
    nStates = plist.aoList.nStates

    debugFlag = True

    # In real situations, more than 32 states will overflow memory
    # But in small situations, this is allowed
    if nStates > 32:
        print '*** WARNING: *** nStates is larger than 32!! Continuing ... '
        

    # The number of fermions cannot be more than the number of states
    if nFermions > nStates:
        raise Exception('Number of particles exceeds the number of states! '+str(nFermions)+' '+str(nStates))
        return

    # The dimension is the total number of many-body states
    dim = CG.binomialCoeff(nStates, nFermions)
    print '<------------------------ State Table -------------------->'
    print 'Fermions = ', nFermions, ' ... States = ', nStates, '... Dimension = ', dim

    # Create an empty python dictionary
    stateTableDict = dict()
    stateTable = []

    # initialize the first state 
    # the low nFermions bits are set to 1
    state = Bit.BitStr(nStates)
    for ferm in range(nFermions):
        state.set(ferm)
        
    # cursor is the position of the highest bit in this group of ones
    cursor = nFermions - 1
    # ones is the total number of ones in this group of ones
    ones = nFermions

    # --------------------------------------------------------------------
    # Loop through each many-body state
    # --------------------------------------------------------------------
    for mbState in range(dim):

        stateTableDict[state.value] = mbState # set the key & value
        stateTable.append(state.value)
        if dim == 1:
            break

        # move the high bit up
        state.clear(cursor)
        cursor = cursor
        state.set(cursor+1)
        ones = ones - 1 # decrement the number of ones in the group
        
        if ones > 0:
            # --------------------------------------------------------------------
            # move all of the lower ones down
            # --------------------------------------------------------------------
            for sbState in range(cursor):
                if sbState < ones:
                    state.set(sbState)
                else:
                    state.clear(sbState)
            # --------------------------------------------------------------------
            cursor = ones - 1  # move the cursor to end of group

        else: # no more ones in the group
            # --------------------------------------------------------------------
            # find the next one to move
            for sbState in range(cursor+1,nStates):
                if not state.get(sbState):
                    cursor = sbState - 1
                    break
                else:
                    ones = ones + 1
            # --------------------------------------------------------------------
            # end for sbState in range(cursor+1,nStates+1):
            # --------------------------------------------------------------------

    f = open('./stateTable.pkl', 'w')
    pickle.dump(stateTable, f)
    f.close()

    f = open('./stateTableDict.pkl','w')
    pickle.dump(stateTableDict, f)
    f.close()

    del stateTable
    del stateTableDict

    # --------------------------------------------------------------------
    # Dump out the table for debugging
    # --------------------------------------------------------------------
    if debugFlag:
        # Read in the state table pickles
        f = open('stateTable.pkl', 'r')
        dbgStateTable = pickle.load(f)
        f.close()

        f = open('stateTableDict.pkl', 'r')
        dbgStateTableDict = pickle.load(f)
        f.close()

        for rStateIndex, rState in enumerate(dbgStateTable):
            state = Bit.BitStr(nStates, rState)
            print  'State %d = %s' % (rStateIndex, state.display())
            if rStateIndex != dbgStateTableDict[rState]:
                raise Exception('Mismatch between state index and dictionary!'+str(rStateIndex))
                
            

    f = open('./mbDimension.pkl','w')
    pickle.dump(dim, f)
    f.close()

    f = open('./nParticles.pkl','w')
    pickle.dump(nFermions, f)
    f.close()

    print '<------------------------ End State Table -------------------->'
    print