All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
At times when it is impermissible to kill ants (if they are harmless) and a person mistakenly kills them, then there is no harm in that. Allaah, The Exalted, says (what means): {… And there is no blame upon you for that in which you have erred but [only for] what your hearts intended. And ever is Allaah Forgiving and Merciful.} [Quran 33:5] Ibn ‘Abbaas reported that the Prophet said: “Verily, Allaah has forgiven my nation for mistakes and forgetfulness, and what they are forced to do under duress.” [Ibn Maajah - Al-Albaani: graded it Saheeh (sound)]
Undoubtedly, killing ants, even if it is forbidden, is not compared to killing a human being which is one of the grave major sins.
Allaah Knows best.