# Parameterized swap test and perfect swap test

Suppose one has parameterized a swap test by using an ansatz $$U(\theta) = \exp(-i\theta \text{ CSWAP})$$, and one tries to find an angle $$\theta$$ such that one can distinguish given two quantum states maximally. Let the discriminator represent the probability of measuring state $$|{0}\rangle$$ at the end of the swap test circuit.

According to the paper (paper), it says that the discriminator is sufficiently expressive to reach the optimal discriminator during optimization when $$U$$ performs the perfect swap test, i.e. when $$\theta = \frac{\pi}{2}$$. Why does the perfect swap test give the best expressiveness of discriminating two quantum states?

According to Equation 6 of the paper, the state one is considering is: $$\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}|1\rangle\left(|\zeta\rangle|\psi\rangle-|\psi\rangle|\zeta\rangle\right)+\frac{\mathrm{e}^{-\mathrm{i}\theta_d}+\cos\left(\theta_d\right)}{2}|0\rangle|\psi\rangle|\zeta\rangle-\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}|0\rangle|\zeta\rangle|\psi\rangle$$ which one can rewrite as: $$\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}|1\rangle|\zeta\rangle|\psi\rangle-\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}|1\rangle|\psi\rangle|\zeta\rangle+\frac{2\cos\left(\theta_d\right)-\mathrm{i}\sin\left(\theta_d\right)}{2}|0\rangle|\psi\rangle|\zeta\rangle-\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}|0\rangle|\zeta\rangle|\psi\rangle.$$ Thus, the probability of measuring $$|0\rangle$$ is equal to: \begin{align}&\left(\frac{2\cos\left(\theta_d\right)+\mathrm{i}\sin\left(\theta_d\right)}{2}\langle\psi|\langle\zeta|+\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}\langle\zeta|\langle\psi|\right)\left(\frac{2\cos\left(\theta_d\right)-\mathrm{i}\sin\left(\theta_d\right)}{2}|\psi\rangle|\zeta\rangle-\frac{\mathrm{i}\sin\left(\theta_d\right)}{2}|\zeta\rangle|\psi\rangle\right)\\={}&\cos^2\left(\theta_d\right)+\frac12\sin^2\left(\theta_d\right)+\frac12\sin^2\left(\theta_d\right)\langle\zeta|\psi\rangle^2\\={}&\frac12+\frac12\cos^2\left(\theta_d\right)+\frac12\sin^2\left(\theta_d\right)\langle\psi|\zeta\rangle^2\\={}&\frac12\left[1+\cos^2\left(\theta_d\right)+\sin^2\left(\theta_d\right)\langle\psi|\zeta\rangle^2\right].\end{align} Since we are considering pure states, $$\langle\psi|\zeta\rangle^2$$ is the fidelity between $$|\psi\rangle\langle\psi|$$ and $$|\zeta\rangle\langle\zeta|$$. Thus, Equation 7 of the paper indeed describes the probability for the discriminator to measure $$|0\rangle$$.
Note that if the discriminator measures $$|0\rangle$$, then it thinks that both states are equal. Now, two cases are to be dealt with.
1. If the discriminator is presented with the same states as input, it is supposed to measure $$|0\rangle$$. The probability of it measuring $$|0\rangle$$ in this case is $$1$$, since $$\langle\psi|\zeta\rangle^2=1$$, which is independant of $$\theta_d$$.
2. If the discriminator is presented with $$|\psi\rangle$$ and $$|\zeta\rangle$$ s.t. $$\langle\zeta|\psi\rangle^2<1$$, then it is supposed to measure $$|1\rangle$$. Equivalently, the optimal $$\theta_d$$ is the one such that the probability of measuring $$|0\rangle$$ is minimal. Intuitively, since $$\langle\zeta|\psi\rangle^2<1$$, your goal is to maximize the sine and minimize the cosine. You can formally prove it by differentiating this expression and proving extremum are reached for $$\theta_d=0\ \mathrm{mod}\ \frac\pi2$$, that the probability is $$\pi$$-periodic and that finally the probability is minimal in $$\theta=\frac\pi2$$, where it is equal to $$\frac{1+\langle\zeta|\psi\rangle^2}{2}$$.
All in all, an optimal discriminator, that is a discriminator with the maximum probability of success using this ancilla, is the one using $$\theta_d=\frac12$$.