# Comparing Quantum States

The SWAP Test is presented as a way to compare two quantum states, but the literature I’ve seen hasn’t explained how you can quantify the difference. However, you can obtain more information from this experiment than a simple determination whether states are identical or not.

The above circuit shows the standard SWAP Test. In this example, the top two qubits are the ones you want to compare, and the bottom qubit is the one that you measure. The cswap operation is a Fredkin (controlled-SWAP) gate with its control on the ancilla qubit (the one being measured).

The way it works is that if you measure two identical qubits, like the two ground state qubits above, you will measure 0 with a probability of 1. The more different the qubits are, the more that probability decreases. However, the probability does not decrease at a 1-to-1 rate; instead, the difference is split evenly between 0 and 1 measurements. Therefore, you have 0 measurements representing similarity, and both 0 and 1 measurements representing difference.

If we use polar opposites, with one qubit in its ground state and the other with a Pauli-X (NOT) gate applied, we can see the result of comparing two maximally opposite states.

This simulation shows the 50/50 split. There is no similarity (“overlap”) between these two states, and therefore the entire difference has equal probabilities of measuring 0 or 1.

If we use a Hadamard gate instead of a Pauli-X (NOT) gate, the second qubit rotates to the |+> state. The first qubit would normally measure 0 with a probability of 1, while the second qubit would normally measure 0 and 1 with equal probabilities.

This simulation shows the similarity (“overlap”) as half. Therefore, half the result is 0 with a probability of 1, and the other half is split equally between 0 and 1. In other words, this is 50% 0 for similarity and 50% a 0/1 split (25% 0 and 25% 1) for the difference. The end result is this 75/25 split.

What’s interesting is that the SWAP Test is basis-agnostic. If we rotate both qubits to |+> and then rotate the second qubit pi/3 around the z axis, the circuit works the same.

Yes, the middle qubit can be rotated in one step. I used two steps simply to show identical probabilities when measuring the z basis.

This simulation requires some knowledge of rotations, but pi/3 rotations affect probabilities by 25%. This histogram shows the two qubits as 75% similar. That 75% overlap measures 0 with a probability of 1, and the 25% difference is split equally among 0 (12.5%) and 1 (12.5%) measurements.

By looking at these histograms, it is evident that we can learn more about two qubits than merely whether or not they are identical. In fact, two qubits that differ in only one basis tell us their angular difference; doubling the 1 measurement gives us the difference between the 1 measurements of the two states, which can be converted into radians using trigonometry.

Looking back at the first histogram, the probability of measuring 1 was .5; the 1 measurements of the two qubits normally differ by double that (1). The ground state measures 1 with a probability of 0 and the excited state measures 1 with a probability of 1. Looking back at the second histogram, the probability of measuring 1 was .25. Doubling that to .5, we get the .5 difference between a ground state measuring 1 with 0 probability and a |+> state measuring 1 with .5 probability. Looking back at the third histogram, the probability of measuring 1 was roughly 12.5%. Doubling that to 25% and measuring the y basis, we get the 25% difference between the first qubit measuring 1 with a probability of 50% and the second qubit measuring 1 with a probability of 75%. You can also measure the x basis and see the 25% difference between the first qubit measuring 1 with a probability of 0 and the second qubit measuring 1 with a probability of .25.

The math gets a little harder when states differ in more than one basis, but experimentation shows that the circuit continues to work the same. For example, I rotated one qubit pi/3 down and 4pi/3 around and another qubit 2pi/3 down and pi/3 around in order to have two states with no overlap and that differ in all 3 bases. The result was similar to the first histogram, showing no overlap and equal probability of measuring 0 or 1.