  Quantum error correction is vital for implementing universal quantum
computing. A key component is the encoding circuit that maps a product state of
physical qubits into the encoded multipartite entangled logical state. Known
methods are typically not 'optimal' either in terms of the circuit depth (and
therefore the error burden) or the specifics of the target platform, i.e. the
native gates and topology of a system. This work introduces a variational
compiler for efficiently finding the encoding circuit of general quantum error
correcting codes with given quantum hardware. Focusing on the noisy
intermediate scale quantum regime, we show how to systematically compile the
circuit following an optimising process seeking to minimise the number of noisy
operations that are allowed by the noisy quantum hardware or to obtain the
highest fidelity of the encoded state with noisy gates. We demonstrate our
method by deriving novel encoders for logic states of the five qubit code and
the seven qubit Steane code. We describe ways to augment the discovered
circuits with error detection. Our method is applicable quite generally for
compiling the encoding circuits of quantum error correcting codes.
