building density matrix and rotation matrix