Basis-Specific SWAP Test (Comparing Quantum States)

The standard SWAP Test is basis-agnostic. It measures the “overlap” of quantum states, which is to say you measure 0 with a probability of 1 when the states are identical and 0 with a probability of .5 when the states are maximally different (on completely opposite sides of the Bloch sphere such as |0> and |1>, |+> and |->, |i> and |-i>, and all other opposite points). Measurements between 0 and .5 show how close or far apart the vectors are, but not along which basis. Two states differing in the z basis by the same angle as two states that differ in the y basis will produce the same result. Furthermore, two states differing in the z basis will produce the same result even if you measure the x basis or the y basis.

The workaround for this, if you want to know the overlap in each basis, is to use three ancilla qubits per basis, instead of just the one ancilla qubit used in the standard SWAP Test. Each of the two states to be compared is the control qubit for a CNOT to one of the ancilla qubits. The standard SWAP Test is then performed on the ancilla qubits. You can then rotate the data qubits as if to measure in the x basis or the y basis, but repeating the same procedure with CNOTs and SWAP Tests on other sets of three ancilla qubits. To measure all three bases, therefore, 11 qubits are required: 2 for the data qubits and 9 for the ancilla qubits (3 for the z basis, 3 for the x basis, and 3 for the y basis). As with the standard SWAP Test, only one measurement should be taken per basis.

Comparing the Z Basis

Measuring the z basis is straightforward. As shown below, use CNOTs to pair each data qubit with an ancilla qubit, and then perform the standard SWAP Test on the ancilla qubits.

|0> and |1> in the z basis

measures 0 with a probability of .5

|0> and |+> in the z basis

measures 0 with a probability of .75

|1> and a 2pi/3 y rotation in the z basis

measures 0 with a probability of about .87

At this point, adding ancilla qubits does not appear to have made any difference. The results for these circuits would be the same if using only three qubits and performing the standard SWAP Test.

Not Comparing the X Basis

The difference between this method and the standard SWAP Test becomes apparent when rotating around the z axis. With one data qubit in the |+> state, z rotations have no effect on the results.

|+> and |+> with pi/4 z rotation

|+> and |i>

|+> and |->

all produce similar results, measuring 0 with a probability in the range of .72 to .75

Therefore, using this method, measuring the z basis ignores rotations around the z axis. The result shows the overlap of two states exclusively in the z basis.

Comparing the X Basis

To compare the x basis, rotate the data qubits as if to measure the x basis. Then repeat the same circuit as before, pairing each data qubit with an ancilla qubit and then performing the standard SWAP Test on the ancilla qubits.

|+> and |+> in the x basis

measures 0 with a probability of 1: these are identical states

|+> and |+> with pi/4 z rotation

measures 0 with a probability of about .92

|+> and |i> in the x basis

measures 0 with a probability of .75

|+> and |-> in the x basis

measures 0 with a probability of .5

Unlike comparing in the z basis before, rotating around the z axis now changes the result and we can measure overlap in the x basis.

Comparing the Y Basis

To compare the y basis, rotate the data qubits as if to measure the y basis. Then repeat the same steps as with comparing in the z basis and x basis, pairing each data qubit with an ancilla qubit and then performing the standard SWAP Test on the ancilla qubits.

|i> and |i> with -pi/4 z rotation

measures 0 with a probability of about .92

|i> and |+> in the y basis

measures 0 with a probability of .75

|i> and |i> with -3pi/4 z rotation

measures 0 with a probability of about .57

|i> and |-i>

measures 0 with a probability of .5

Like comparing qubits in the x basis, using this method with a different rotation on the data qubits now allows a comparison in the y basis.

Putting It All Together

Comparing all three bases using this method would require either three 5-qubit circuits, comparing bases sequentially, or one 15-qubit circuit, comparing bases in parallel. However, the following circuit shows how a 15-qubit circuit could be reduced to an 11-qubit circuit.

This circuit is admittedly too deep to run on a real quantum processor. For that matter, it simulates poorly, as well. There is also no device that I am aware of with the connectivity to optimize this circuit. However, that’s why I broke this circuit down into digestible chunks: to prove conceptually that comparisons can be made in each basis.

The sections above from left to right:

  1. Set the states of the data qubits
  2. Pair each data qubit with an ancilla qubit
  3. Perform the standard SWAP Test on the ancilla qubits
  4. Apply Hadamard gates to the data qubits as if to measure in the x basis
  5. Pair each data qubit with another ancilla qubit
  6. Perform the standard SWAP Test on the new ancilla qubits
  7. Apply Hadamard gates to the data qubits to reverse the previous transformations
  8. Apply S dagger and Hadamard gates to the data qubits as if to measure in the y basis
  9. Pair each data qubit with yet another ancilla qubit
  10. Perform the standard SWAP Test on the newest ancilla qubits
  11. Measure only the control qubits for each CSWAP (Fredkin) gate

You may notice that the measurements are not in order. This is merely personal preference. The easiest way to build this particular circuit is to compare the z basis, then the x basis, and then the y basis. However, I like to read the results in xyz order.

And, although I just stated that “the easiest way” to build this circuit is in z-x-y order, that only pertains to this explanation. Reversing previous basis rotations and performing new basis rotations can be done in only one step. I intentionally left them as the textbook rotations for the potential benefit of readers who may be unfamiliar with measuring in anything other than the z basis.

Future Work

My introduction to the SWAP Test was as a tool to determine whether or not qubits are identical. Clearly, more information is available from tbe SWAP Test than a simple yes/no answer to that question. My logical next question is how much more information may still be available, if any? After all, I merely added rotations and CNOTs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store