# Check if $|\psi\rangle$ is equal to a fixed basis state using the standard set of gates

Consider a circuit acting on $$n\geq2p+1$$ qubits. The first $$p$$ qubits encode some unknown state $$|\psi\rangle$$. Next $$p$$ qubits encode a fixed given basis state $$|\phi\rangle=|\ldots f_2f_1f_0\rangle$$. The $$(2p+1)$$th qubit is initialized in the $$|0\rangle$$ state. Using the standard set of gates, how can I check if $$|\psi\rangle$$ is equal to $$|\phi\rangle$$ and put the result of this comparison into the $$2p+1$$th qubit? (I wrote "$$\geq$$" in case we need ancillas).

I guess, the implementation has to do something with the SWAP test, but I don't want to do any measurements. Feels like my question is purely on classical logic, and there's nothing quantum about it.

Please note that my question is NOT about comparing two arbitrary states. One state is a known state from the computational basis.

I guess you assume that $$\psi$$ is a bit string, so $$|\psi\rangle$$ is not fully arbitrary (there is no way to compare with an arbitrary state without measurements).

Logically, it's just $$(\phi_0=\psi_0 ~AND~ \phi_1=\psi_1 ~AND~ ...)$$.

To implement the first comparison you can use $$CNOT_{0a} \cdot CNOT_{pa} \cdot X_{a}$$ where $$a$$ is the index of an ancilla qubit (0-initialized). $$CNOT$$ is basically a sum modulo 2 (in the target qubit), so the value of the ancilla $$a$$ will be $$\phi_0 \oplus \psi_0 \oplus 1$$, which is the same as $$\phi_0=\psi_0$$. Similarly for other comparisons.

To accumulate results in the ancillas $$a, a+1, a+2, ..., a+p-1$$ you can use a multiply controlled $$NOT$$ gate (Toffoli generalization) where the target is $$(2p+1)$$-th qubit. To implement such gate you can use a bunch of Toffoli gates $$-$$ a standard scheme, see e.g. Nielsen&Chuang book.

And it's good practice to uncompute, even if $$\psi$$ is just a bit string.

Yes, SWAP can be used, this paper gives a quantum mechanical method in it's supplementary to test the fidelity between a known state and an unknown state using two Hadamard gate and a controlled-SWAP operation(a measurement on the single-qubit control is needed, and it is acquired to do several times, say 10**3 times).

There are two metrics to compare the similarity between two quantum states, trace distance $$D(p_x,q_x)=\frac{1}{2}\displaystyle\sum_x|p_x-q_x|$$ where ps and qs are probility distribution, and fidelity $$F(\rho,\sigma)=tr\sqrt{\rho^{1/2}\sigma\rho^{1/2}}$$ where $$\sigma$$ and $$\rho$$ are density matrices, see Nielson's book for more detail.

Once you get the probability distribution of the density matrix you can use trace distance or fidelity to compare the similarity between two quantum states, but to get the two data, repeated measurements are required(or say quantum state tomography to get the density matrix), since we can not know a thing which is quantum(so we have to convert it to classical many many times, i.e. frequent measurements).

• I think you misunderstood my question, maybe I posed it unclearly. I meant a simple circuit which would do a comparison like "the first qubit is 1 AND the second qubit is 0 AND ...", and put the result of this comparison into some register. Nov 22, 2020 at 16:44