  We report a density functional study on the evolution of the electronic and
lattice structure in BiFeO$_3$ with injected electrons and holes. First, the
self-trapping of electrons and holes were investigated. We found that the
injected electrons tend to be localized on Fe sites due to the local lattice
expansion, the on-site Coulomb interaction of Fe $3d$ electrons, and the
antiferromagnetic order in BiFeO$_3$. The injected holes tend to be delocalized
if the on-site Coulomb interaction of O $2p$ is weak (in other words,
$U_\mathrm{O}$ is small). Single center polarons and multi-center polarons are
formed with large and intermediate $U_\mathrm{O}$, respectively. With
intermediate $U_\mathrm{O}$, multi-center polarons can be formed. We also
studied the lattice distortion with the injection of carriers by assuming the
delocalization of these carriers. We found that the ferroelectric off-centering
of BiFeO$_3$ increases with the concentration of the electrons injected and
decreases with that of the holes injected. It was also found that a structural
phase transition from $R3c$ to the non-ferroelectric $Pbnm$ occurs, with the
hole concentration over 8.7$\times10^{19} cm^{-3}$. The change of the
off-centering is mainly due to the change of the lattice volume. The
understanding of the carrier localization mechanism can help to optimize the
functionality of ferroelectric diodes and the ferroelectric photovoltage
devices, while the understanding of the evolution of the lattice with carriers
can help tuning the ferroelectric properties by the carriers in BiFeO$_3$.
