  We propose a general method to maximize the fidelity of writing, storage and
reading of quantum information (QI) in a spectrally inhomogeneous spin ensemble
used as quantum memory. The method is based on preselecting the optimal
spectral portion of the ensemble by a judiciously designed pulse. It allows
drastic improvement of quantum memory realized by spin ensembles that store QI
from a resonator or an optical beam.
