The development of ultralow-loss silicon-nitride-based waveguide platforms has enabled the realization of integrated optical filters with unprecedented performance. Such passive circuits, when combined with phase modulators and low-noise lasers, have the potential to improve the current state of the art of the most critical components in coherent communications, beam steering, and microwave photonics applications. However, the large refractive index difference between silicon nitride and common III-V gain materials in the telecom wavelength range hampers the integration of electrically pumped III-V semiconductor lasers on a silicon nitride waveguide chip. Here, we present an approach to overcome this refractive index mismatch by using an intermediate layer of hydrogenated amorphous silicon, followed by the microtransfer printing of a prefabricated III-V semiconductor optical amplifier. Following this approach, we demonstrate a heterogeneously integrated semiconductor optical amplifier on a silicon nitride waveguide circuit with up to 14 dB gain and a saturation power of 8 mW. We further demonstrate a heterogeneously integrated ring laser on a silicon nitride circuit operating around 1550 nm. This heterogeneous integration approach would not be limited to silicon-nitride-based platforms: it can be used advantageously for any waveguide platform with low-refractive-index waveguide materials such as lithium niobate.