How to write any partition function? So I am familiar with the derivation of the partition function for a canonical and a grand canonical ensemble. I have seen definitions of the partition function for some of the quantum counterparts of these. However, I am yet to understand how to write the partition function of a general system.
I've been told that you need to write the energy of a single particle, and take it to the power of N (nr of particles). This only works if the particles are the same tho. What happens if I have some distribution of them?
Let's say my system consists of N particles and M states. The M(1) state can contain let's say 3 particles - degenerate - and corresponds to an energy of E. Let the M(2) state be non-degerative, and let it have energy twice of the first state. And so on, I could define any number os states in any degeneracy, and write all fancy stuff for the
 energies. Then, let's say every particle interacts with any particle who has energy that is in some range of the given particle. So, all kinds of random rules. How to I write the partition function then?What is the mosz general possible formula for the partition function?
 A: I will only discuss the quantum case. To be more specific I will consider the case of spinless Bosons. What you have to do is sum over all possible states and the space of all possible states is called Fock space (see the wiki page). It is constructed by consider a series of subspaces. First the single particle Hilbert space is spanned by the position basis, in other words we can write the Hilbert space as the set of all position states
$$\mathcal{H}_1=\{|r\rangle:r\in\mathbb{R}^3\}.$$ 
Next the two particle Hilbert space is spanned by tensor products 
$$\mathcal{H}_2=\{|r_a\rangle\otimes|r_b\rangle:r_a\in\mathbb{R}^3, r_b\in\mathbb{R}^3\}.$$
This notation simply means that particle one is at position $r_a$ and particle 2 is at position $r_b$. NOTE: This is not quite right for bosons. We know that bosons are identical particles and are symmetric under exchange so we really want
$$\mathcal{H}_2=\{|r_a\rangle\otimes|r_b\rangle+|r_b\rangle\otimes|r_a\rangle:r_a\in\mathbb{R}^3, r_b\in\mathbb{R}^3\}$$
so that we cannot tell which particle is at position $r_a$ and which is at position $r_b$. We only know that one of the particles is at position $r_a$ and the other is at position $r_b$. Similarly we can construct the three and four particle Hilbert spaces and so-on. Finally, Fock space is constructed by collecting all the different states from these subspaces $\mathcal{H}=\mathcal{H}_0\oplus\mathcal{H}_1\oplus\mathcal{H}_2\oplus...$ Now to answer you question the grand partition function is
$$\mathcal{Z}=\sum_{|\psi\rangle\in\mathcal{H}}\langle\psi|\exp-\beta(\hat{H}-\mu\hat{N})|\psi\rangle$$
where $\hat{H}$ is the Hamiltonian and $\hat{N}$ is the number operator. To return to your own notation we can in theory diagonalize $\hat{H}$ to find energy eigenstates $|m\rangle$ with energy eigenvalues $E_m$ but this is very hard for Hamiltonians with interactions. The eigenstates will have different numbers of particles. Supposing you could actually find these eigenstates the grand partition function is just
$$\mathcal{Z}=\sum_m\exp-\beta(E_m-N_m)$$
where $N_m$ is the number of particles in the $m$ eigenstate. This could fail if the eigenstates of $\hat{H}$ are not number eigenstates. In that case the sum over $\psi\in\mathcal{H}$ is still valid.
