As Poland braced for the arrival Thursday of Pope John Paul II on a pilgrimage expected to have a mixed political impact in this troubled nation, the government hinted today at a possible compromise on a prospective meeting between the pope and former Solidarity union leader Lech Walesa.

At the same time, Walesa reported from his home in Gdansk that plainclothed police were posted outside his apartment and had advised him that he would not be allowed to leave the area, at least for the time being. The 39-year-old unionist said he considered himself under house arrest.

Plans for a repeat encounter of the two most renowned living Poles--they last met at the Vatican in January 1981 when the independent trade union was gaining power--have become the focus of a pivotal dispute during the final preparations for the papal visit.

The pope is pressing to see Walesa. But Poland's Communist authorities, recognizing that such a meeting would boost the image of a key opposition figure and serve as a papal affirmation of the ideals of the outlawed Solidarity movement, have refused to sanction it and warned that if it happened, there would be an official reaction.

Speaking to hundreds of reporters here for the pope's second homecoming, Deputy Premier Mieczyslaw Rakowski accused western forces and Polish opposition groups of manipulating Walesa, who the government insists is now just a private citizen, in "a political game" meant to keep Poland unsettled. He said the possibility of a meeting between Walesa and the pope could no longer be considered "a private matter" without political significance and he appealed to the Vatican to discuss it.

In fact, the issue has been a matter of tough behind-the-scenes exchanges between Polish and Vatican officials in recent days, according to informed sources. The pope has stood fast by the principle, agreed on in church-state negotiations preceding the pilgrimage, that anyone who wants to greet him should face no state interference.

Denied permission for a leave from his electrician's job in the Lenin Shipyard in the northern port of Gdansk to travel to central or southern Poland where the pope will be, Walesa has announced he would attempt to see him in Czestochowa this weekend.

Rakowski's remarks suggested that the controversy would be thrashed out in a meeting in Warsaw Friday between the pope and Polish leader Gen. Wojciech Jaruzelski. Asked about a compromise emerging, the deputy premier replied: "It is obvious that in politics we are frequently confronted with compromises."

One option reportedly under consideration would arrange for Walesa to have a papal audience in the company of a number of other workers, thereby avoiding the appearance at least of a private papal session set up solely for the ex-labor chief.

Such church-state friction highlights the heavily political overtones of what is being billed ostensibly as a religious pilgrimage by the pope to celebrate the 600th anniversary of the Shrine of the Black Madonna, Poland's most sacred Catholic symbol. The eight-day visit, the pope's second to his native land since being named head of the Roman Catholic Church in 1978, will take the pontiff to eight cities--including such industrial centers as Katowice, Wroclaw and Poznan, where worker involvement in Solidarity was high, and to the intellectual and cultural center of Krakow, where John Paul II, then Karol Wojtyla, served as archbishop.

Originally slated for last August, the papal trip was postponed when the government sought to hold the pope to an abbreviated itinerary and he saw that he would not be able to gain a relaxation of martial-law rigors as a concession for coming to Poland.

Martial law was suspended in December, although church demands for a full lifting of military rule and a blanket amnesty for political prisoners went unfulfilled. The Vatican has indicated the reasons for going ahead with the pilgrimage are to bolster hope in the dispirited country and to strengthen the position of the Roman Catholic Church not just in Poland, where it is already exceptionally powerful, but elsewhere in the Soviet Bloc.

Praying today before a general audience in St. Peter's Square at the Vatican, the pope called his journey "a special pastoral service in which it falls to me to carry out at a sublime, and at the same time difficult, moment in the life of my country." It is his hope, he said, that the pilgrimage "serve truth and love, freedom and justice, that it may serve reconciliation and peace."

For the Polish government, the visit risks a renewed outburst of anti-state sentiment. The pope's presence here in 1979 caused feelings of freedom, involvement and national unity under Catholicism to surge to the surface, a development widely cited as having fostered the rise of Solidarity a year later.

An Interior Affairs Ministry statement read on television last night said there is evidence of "persons and groups" wanting to exploit the pilgrimage for "purposes contrary to the national interest." The ministry declared it would "take every necessary and indispensable action to secure a calm and dignified" papal visit.

The wording echoed previous warnings by the government when it expected pro-Solidarity demonstrations. Bishops have issued letters advising parishoners to keep to religious messages on whatever banners they bring to papal gatherings and to honor the religious nature of the ceremonies.

While assorted pro-Solidarity paraphernalia marking the pilgrimage has begun to appear--including a bogus postage stamp, available in six colors, bearing a likeness of the pope and the inscription Solidarity--underground leaders have also appealed for calm during the visit.

The Communist authorities generally appear confident that the past 18 months of military rule have pacified the country sufficiently to allow the visit to proceed. Their purposes are seen as to woo the church into a more cooperative relationship and to loosen what Rakowski today termed "the ring of isolation spread around Poland by President Reagan and his allies."

Trying to diffuse tensions over the lack of major political concessions by Warsaw in response to the visit, senior Communist officials this week have stressed that if the pilgrimage goes peacefully, this will accelerate a decision to lift martial law at some future date.

Absent this time, in contrast to 1979, are efforts by the authorities to dissuade people from turning out to greet the pope. In response to an appeal by the church, a number of major factories in areas where the pope will visit have agreed to allow employes the day off in return for weekend work.

More than 12 million Poles, or about a third of the nation, are expected to pour into racetracks, stadiums and old airport fields that have been converted into papal forums for the occasion.

"We're expecting more people this time than last time," said the Rev. Zdzislaw Krol, chancellor of the Warsaw curia. "Last time we didn't trust the situation and tried to control things largely with invitation. This time, we're willing to allow for more spontaneity."

In March, when detailed planning began, joint commissions of church and state officials were established for the visit. Bargaining was hard on many points.

Authorities in Poznan, for instance, rejected a church design for the papal altar there, arguing that the proposed cross was too large. City officials in Wroclaw vetoed a papal motorcade through the city on grounds there would not be enough police to ensure order.

There were awkward moments, too, reflecting the relative newness of Catholic and Communist officials dealing so intensively with each other. An aide in Krakow who has had extensive contacts with both sides reported receiving phone calls from city authorities asking for the proper way to address a churchman or for definition of ecclesiastical terms. Church officials, in turn, phoned for help in locating the proper government office in the city's bureaucratic maze.

Complicating the preparations has been Poland's severe economic crisis, forcing organizers to scrape for decorations and such basic construction materials as nails and paint. At the same time, some local officials regarded the prospect of a pilgrimage as a vehicle to get funding from Warsaw for much-desired municipal improvements.

"We don't want to lose anything because of the visit," asserted the deputy regional director in Czestochowa, Julian Czajkowski, whose area is in line for eventual new hotels and sanitation system improvements. "On the contrary, we want to gain new investment projects."

Poland's much-touted economic reform, intended to make managers more cost-conscious and profit-oriented, can also be seen affecting papal preparations. "Four years ago, it was enough merely to mention the pope to get companies to donate things for the visit," recalled Pawel Barteczko, who has set up the press center in Czestochowa. "Today, everyone wants to know how much you're prepared to pay."

After doubting for months that the visit would actually take place, Poles have seemed less excited about this pilgrimage than they were at the start of the last one, less certain about its basic purpose and possibilities.

Church officials have sought to dampen expectations that the pontiff, for all his experience in negotiating with Communists, might somehow alter political circumstances here during his tour.

"A lot of people think that when the pope comes, everything will change," remarked a senior bishop from one of the cities on the papal route. "But those who expect a miracle in the material or social sphere will be disappointed. We have to do that with our own hands. What can be expected is a small miracle in the spiritual life of this society."