OTTAWA — Simon Thwaites was a master seaman analyzing radio signals and surveillance in a secret branch of Canada’s Navy in the 1980s when he was summoned by investigative officers. Strapped to a lie detector in an interrogation room, he was asked by two officers if medical records showing that he had contracted H.I.V. meant that he was gay.

Mr. Thwaites confirmed that this was the case, and not long afterward his security clearance was revoked and he was assigned to work as a janitor. Eventually Mr. Thwaites was forced out of the military on a medical release without benefits, lost his house and filed for bankruptcy.

“We were treated like something was wrong with us, but none of us did nothing wrong,” Mr. Thwaites said in a telephone interview from his home in Truro, Nova Scotia. “We did our jobs and we did our jobs well. It kind of undermines your sense of self.”

Now, Prime Minister Justin Trudeau plans to formally apologize to Mr. Thwaites and thousands of other members of the military, the public service and the Royal Canadian Mounted Police who faced discrimination, lost their jobs and, in some cases, were imprisoned because of their sexual orientation. Some of the victims are believed to have killed themselves after their careers were ruined.