Quantum theory of magnetism