Hey,
geometry equality can be defined in many ways.
For your duplication problem, it is a simple postgres problem :
you want that for any couple (line, poly), you have at most one result (given polygon are convex, which they are if they are squares).
so at the end of you computing you just add a filtering select :
SELECT DISTINCT ON (line_id,poly_id)
If you don't want random result, you should order your result to know which line of the 4 you get (you could order by length, centroid, first point, whatever.)
WITH (your computing)
SELECT DISTINCT ON (line_id, poly_id) , poly, line
FROM your computing
ORDER BY ST_Length(line) ASC
Now if you want to solve this the PostGIS way it would be harder, as you would need a distance between shape.
Of course you could also use implicit distance, for example by snapping your result line to a given precision and doing a regular test after
(WHERE ST_Equals(ST_SnapToGrid(line1, 0.01),ST_SnapToGrid(line2, 0.01))=TRUE )
Cheers,
Rémi-C