Efficient learning of ground and thermal states within phases of matter | Synapse