# Is there a circuit to compare two quantum states?

Lets have two quantum states (single qubits ones for simplicity) $$|\psi\rangle$$ and $$|\phi\rangle$$:

$$|\psi\rangle = \alpha_\psi|0\rangle+\mathrm{e^{i\varphi_\psi}}\beta_\psi|1\rangle$$

$$|\phi\rangle = \alpha_\phi|0\rangle+\mathrm{e^{i\varphi_\phi}}\beta_\phi|1\rangle$$

These states are equal in case

$$\alpha_\psi = \alpha_\phi$$ $$\beta_\psi = \beta_\phi$$ $$\varphi_\psi = \varphi_\phi,$$ i.e. they have same complex amplitudes or same amplitudes expressed by real parameters $$\alpha$$ and $$\beta$$ and same phase $$\varphi$$.

One way how to compare two state is to do a quantum tomography, however, to do so we would need many copies of the states and moreover, the quantum tomography complexity is exponential in number of qubits in case of multiqubit states.

So my question: Is there a circuit allowing to compare two quantum states? A result should be some ancilla qubit in state $$|0\rangle$$ if states are different and $$|1\rangle$$ if states are the same (in sense described above).

The task that you describe in your question — a circuit which flips a single qubit, if and only if the two input states are different — is not possible. We can show this as follows.

First, there is no way to distinguish two states which differ only by a global phase, because no quantum operations can distinguish between two state-vectors which only differ in a global phase. (In fact, for two such states as input, there is no real way to describe which of the two states has that phase: the global phase applies to the total state, including all tensor factors.)

Setting that aside, however, there is a more significant obstacle: the linearity of quantum mechanics.

Suppose that you had a unitary circuit $$U$$, which performed the following transformation for inputs $$\lvert x \rangle$$ and $$\lvert y \rangle$$ in the states $$\lvert 0 \rangle$$ and $$\lvert 1 \rangle$$:

\begin{align} \lvert 0 \rangle \;\lvert x \rangle\; \lvert y \rangle\; \lvert \text{work space} \rangle \;\mapsto\; \lvert \delta_{x,y} \rangle \;\lvert \psi_{x,y} \rangle, \end{align} where $$\delta_{x,y}$$ is the Kronecker delta, "$$\text{work space}$$" is some fixed initial state of auxiliary qubits provided as work space, and $$\lvert \psi_{x,y} \rangle$$ is some (possibly complicated) quantum state that depends on $$x$$ and $$y$$. We take the first qubit to be the answer qubit. Note that the states $$\lvert \psi_{x,y} \rangle$$ will be orthogonal to one another for different values of $$x$$ and $$y$$. Then, what happens if we introduce a state which is not in the standard basis? $$\lvert 0 \rangle \; \lvert 1 \rangle \; \lvert + \rangle \; \lvert \text{work space} \rangle \mapsto \tfrac{1}{\sqrt 2} \Bigl( \lvert 0 \rangle \; \lvert \psi_{1,0} \rangle \;+\; \lvert 1 \rangle \; \lvert \psi_{1,1} \rangle \Bigr)$$ so that the answer qubit is entangled (and in fact maximally entangled) with the rest of the qubits. In particular, it does not give you $$\lvert 0 \rangle$$, which is what you wanted for your procedure.

As @MicheleAmoretti and @MarkusHeinrich indicate, the best that you can do is to use the controlled-SWAP test, which will succeed with probability 1/2 if the two input states are orthogonal, and which will have a worse and worse success probability (as a means of distinguishing distinct states) for distinct states which have larger and larger fidelities.

The technical term is "quantum state discrimination". One has to carefully formulate the problem, because it is generally hard to identify an arbitrary state (tomography) as you noticed.

However, given the promise that two states are prepared at random, there's an upper bound on the probability of correctly identifying the state via a single measurement (Holevo-Helstrom theorem): $$\mathrm{Pr}_\mathrm{success} \leq \frac 1 2 \left( 1 + \big\| \lambda \rho_0 - (1-\lambda) \rho_1 \big\|_1 \right)$$ Here, $$\rho_0$$, $$\rho_1$$ are the states which are prepared with (known) propability $$\lambda$$ and $$(1-\lambda)$$. Moreover, this bound is tight, i.e. there is always a projective measurement achieving that bound which can be computed from the states. Note that perfect discrimination can only be achieved for orthogonal pure states. Then, the optimal strategy would be to measure that basis.

The answer is: There is no circuit, but a protocol which discriminates correctly between two known states with optimal probability. This is the best you can get since the states could be arbitrarily close.

There's e.g. a chapter on this in John Watrous' lecture notes / his book and there is a review by Joonwoo Bae and Leong-Chuan Kwek: https://arxiv.org/abs/1707.02571

Edit: Ok, I might have misunderstood the question. If you have access to two pure states at once, you can do the SWAP test.