Laser cooling and trapping of atoms and atomic ions has led to advances including the observation of exotic phases of matter1,2, the development of precision sensors3and state-of-the-art atomic clocks4. The same level of control in molecules could also lead to important developments such as controlled chemical reactions and sensitive probes of fundamental theories5, but the vibrational and rotational degrees of freedom in molecules pose a challenge for controlling their quantum mechanical states. Here we use quantum-logic spectroscopy6, which maps quantum information between two ion species, to prepare and non-destructively detect quantum mechanical states in molecular ions7. We develop a general technique for optical pumping and preparation of the molecule into a pure initial state. This enables us to observe high-resolution spectra in a single ion (CaH+) and coherent phenomena such as Rabi flopping and Ramsey fringes. The protocol requires a single, far-off-resonant laser that is not specific to the molecule, so many other molecular ions, including polyatomic species, could be treated using the same methods in the same apparatus by changing the molecular source. Combined with the long interrogation times afforded by ion traps, a broad range of molecular ions could be studied with unprecedented control and precision. Our technique thus represents a critical step towards applications such as precision molecular spectroscopy, stringent tests of fundamental physics, quantum computing and precision control of molecular dynamics8.