Only if. If U1=eiφU2, the phases cancel in U1PU1†.
If. Put W=U2†U1. The hypothesis gives WPW†=P, i.e. WP=PW, for each generator
P, hence for every product of generators, which (up to phases) are all 4n Pauli strings. The Pauli
strings form a basis of the space of 2n×2n complex matrices, so W commutes with every
matrix. Taking the matrix units Ejk=∣j⟩⟨k∣: WEjk=EjkW applied to ∣k⟩
gives W∣j⟩=⟨k∣W∣k⟩∣j⟩ for all j,k, so W is diagonal with all diagonal entries
equal, W=cI. Since W is unitary, ∣c∣=1, so c=eiφ and U1=eiφU2.
Algorithm. Represent a signed Pauli string on n qubits by 2n bits and a sign bit. Starting from
the identity tableau (row j is Xj, row n+j is Zj), apply the gates of C1 in order,
conjugating every row: H, S and CNOT map each Pauli string to another one with a sign given by a
simple Boolean formula, and only the columns of the one or two qubits the gate touches change, so
each gate costs O(n) operations on the 2n rows (Aaronson and Gottesman).
Do the same for C2 and compare the two tableaux, O(n2) bits. The total time is
O(n(m1+m2)+n2).