Solution (source code)

= Solution

In the <Hadamard basis>, $X_D$ is diagonal with projectors $Q_\pm=(I\pm X_D)/2$. Rewrite the same <CNOT gate> in the original tensor-factor order $S\otimes D$ as
$$
\boxed{U=I_S\otimes Q_+ + Z_S\otimes Q_-.}
$$
Expanding $P_0\otimes I+P_1\otimes X_D$ verifies this identity directly. Now $Z_S|+\rangle=|-\rangle$ and $Z_S|-\rangle=|+\rangle$, so $Z_S$ is $\widetilde X_S$ in the complementary basis. If $D$ is plus, the identity acts on $S$; if $D$ is minus, its sign-basis label flips. Hence $D$ is the control and $S$ the target in this basis.

Equivalently,
$$
(H\otimes H)\operatorname{CNOT}_{S\to D}(H\otimes H)
=\operatorname{CNOT}_{D\to S}.
$$
This <CNOT control reversal in the Hadamard basis> concerns the matrix in changed local bases, rather than a physical exchange of the two carriers.