A federal judge ruled yesterday that Canadian Ambassador Allan Gotlieb cannot be compelled to testify at the perjury trial of former White House aide Michael K. Deaver.

U.S. District Court Judge Thomas Penfield Jackson denied independent counsel Whitney North Seymour Jr.'s motion to compel service on Gotlieb and quashed the subpoena.

Deaver is accused in a five-count indictment of lying about the extent of his involvement with the acid-rain issue before he resigned as deputy White House chief of staff to become a lobbyist for Canada and other clients.

Seymour said Gotlieb was the only witness who could prove that Deaver lied when he said he could not recall having lunch Jan. 5, 1985, with the ambassador and his wife, Sondra.

Seymour touched off a diplomatic flap last month when he sent an FBI agent to serve subpoenas on Gotlieb and his wife at the Canadian Embassy. Officials there refused to accept them and later accused Seymour of violating international law by trying to serve them.

Seymour then responded by accusing the Canadian government of "duplicitous" behavior by cooperating with his investigation to satisfy political critics but ultimately refusing to provide crucial testimony in the case against Deaver.

Jackson rejected Seymour's contention that Canada had waived its diplomatic immunity by agreeing to answer written questions posed last year by Seymour.

"The Canadian note of July 1986 is neither expressly nor by any fair implication a commitment by Canada to subject its ambassador, or any other of its diplomatic personnel, to the American judicial process in this case," Jackson said.

Deaver is scheduled to go to trial July 13 on charges of lying to a grand jury and a House subcommittee that investigated his lobbying business for possible ethics law violations.

A federal appeals court last week refused to consider Deaver's challenge to the constitutionality of the statute under which Seymour was appointed by a court to investigate and prosecute the case.

Deaver's attorneys yesterday appealed that decision to the Supreme Court, an action that could delay the trial, which originally was scheduled to begin June 8.