Maybe he did (and maybe he didn’t). But, you are the OP. You deliberately phrased your question to regard the validity of the Office, regardless of whether the Office belonged to the Pope (and is thus protected by ex Cathedra).
You asked if teaching heresy was an automatic disqualification from Office for a Bishop or Pope. We have answered, and told you that there is no such thing as an automatic disqualification from Office. A Bishop might be suspended from Office by an ecclesiastical Court, with a full trial (in which case the prosecution and the Bishop were allowed to present evidence). This is a judicial process (not an automatic process). But no such Court can remove a Bishop from his Office. Only the Pope can do that (and he doesn’t need a Court to do it). A Pope appointed every single Bishop of the Church, and only a Pope can remove him from his Office.
The Pope (alone) is immune from ALL Canon Law. No ecclesiastical Court can try or convict him on any offense whatsoever. Heck, the Pope could completely eradicate all Canon Law (and all ecclesiastical Courts) with nothing more than a whim. Nothing on earth binds a Pope.
Now you ask about a heretical Pope. You derail your OWN question. It does not matter if EVERY Pope was a flippin’ heretic. It does not matter with regard to his Office.
Are you asking if being a heretic automatically removes a Bishop or Pope from his Office, or are you asking if any Bishops or Popes have ever been heretics?
Answer to Question #1: No, never, absolutely not.
Answer to Question #2: Yes, absolutely.
You are the OP. This is YOUR thread, and YOUR question. But you seem to make it a moving target. What, exactly, are you asking? We can’t answer YOUR question if you change the question.