  We prove several theorems characterizing the existence of homological error
correction codes both classically and quantumly. Not every classical code is
homological, but we find a family of classical homological codes saturating the
Hamming bound. In the quantum case, we show that for non-orientable surfaces it
is impossible to construct homological codes based on qudits of dimension
$D>2$, while for orientable surfaces with boundaries it is possible to
construct them for arbitrary dimension $D$. We give a method to obtain planar
homological codes based on the construction of quantum codes on compact
surfaces without boundaries. We show how the original Shor's 9-qubit code can
be visualized as a homological quantum code. We study the problem of
constructing quantum codes with optimal encoding rate. In the particular case
of toric codes we construct an optimal family and give an explicit proof of its
optimality. For homological quantum codes on surfaces of arbitrary genus we
also construct a family of codes asymptotically attaining the maximum possible
encoding rate. We provide the tools of homology group theory for graphs
embedded on surfaces in a self-contained manner.

Quantum Physics;Other Condensed Matter;High Energy Physics - Theory