14 votes

How and why does swap test works?

What that cSWAP test does (and doesn't) do The important thing about the controlled-SWAP test is that what it does isn't just to SWAP, or to not SWAP, the two inputs. The controlled-SWAP test ...
Niel de Beaudrap's user avatar
9 votes

How and why does swap test works?

The key to understanding many quantum protocols and circuits is in the following circuit: This is especially true in the case where $U^2=I$, such that $U$ has eigenvalues $\pm1$. You can readily ...
DaftWullie's user avatar
7 votes

Swap Test for vector difference - how are different sized inputs combined?

You are not swapping the first register (one qubit) with the entire second register ($k$ qubits), but just with the first qubit of the second register. What you need to know is what is meant by $\...
Peter Shor's user avatar
6 votes
Accepted

Swap test vs measurement in a specific basis

Probably the most important difference between the swap test and the projective measurement technique is that performing the swap test does not require knowledge of its inputs. When applying the swap ...
forky40's user avatar
  • 6,358
6 votes

Can the SWAP test only compare registers with the same number of qubits?

Yes they should be the same size. Otherwise, if you use it for getting the inner product between them, it would not make sense they aren't.
cnada's user avatar
  • 4,694
6 votes

Distance calculation between two vectors

Thanks for the answers from @forky40. I accept it as the right answer, but do want to provide a complete derivation as follows. (Same as in the original question) First, initialize per DistCalc: $$ |...
czwang's user avatar
  • 849
5 votes
Accepted

What is the "additive error" of Swap Test?

Additive error $\epsilon$ means that, if the true value of the expectation value we are attempting to measure is $\langle \hat{O} \rangle_{\textrm{True}}$, then our estimate $\langle \hat{O} \rangle_{\...
bm442's user avatar
  • 1,037
4 votes
Accepted

Distance calculation between two vectors

The problem is that you applied a Swap gate when you should have applied a CSWAP, and so you never entangled the readout qubit with your query states (as a result the readout qubit will always return ...
forky40's user avatar
  • 6,358
4 votes
Accepted

How to implement the swap test with the help of qiskit?

Assuming that you are not trying to keep $|\psi \rangle$ and $|\phi\rangle$ for further computation after measuring the ancilla qubit then you can do it as follows: Let's suppose $|\phi\rangle = |111\...
KAJ226's user avatar
  • 13.8k
4 votes
Accepted

SWAP test: clarification of measurement output

Please look at picture of the circuit. The state in the second last step, is the state after the second $H$ gate. Now, we only have to measure, and we are interested in the probability of the two ...
Abdullah Khalid's user avatar
4 votes
Accepted

Can we test whether $|\psi\rangle$ is orthogonal to $|\phi\rangle$ without creating a coherent superposition therebetween?

No, you can't do it (except for trivial things). Think about what you're asking for: a map that performs $$ |0\rangle|\psi\rangle|\psi^\perp\rangle\longrightarrow |1\rangle|\psi\rangle|\psi^\perp\...
DaftWullie's user avatar
4 votes
Accepted

Alternatives to the swap test using a smaller number of qubits

Let us say that you want to evaluate $|\langle\psi|\varphi\rangle|^2$. Let us denote $U$ and $V$ the unitaires associated to your parametrized quantum circuits, producing respectively $|\psi\rangle$ ...
Tristan Nemoz's user avatar
3 votes
Accepted

How are mixed states given to a quantum algorithm?

Given any density matrix $\rho$, we can find a (non-unique) pure state $|\rho\rangle$ that purifies it. For instance, if $$ \rho = (|0\rangle\langle 0| + |1\rangle\langle 1|)/2, $$ then one choice of $...
Abdullah Khalid's user avatar
3 votes

pennylane:fidelity calculation after swap test between entagled states. Swap test issue

As the error suggests, you cannot use QubitStateVector twice in the same QNode. Take, for instance, this simpler example that reproduces your error. Here, I want to ...
isaac's user avatar
  • 171
3 votes
Accepted

What's the matrix representation of the CSWAP?

To get the $CSWAP$ matrix representation in Qiskit, you can use the Operator class defined in the qiskit.quantum_info module: <...
SimoneGasperini's user avatar
3 votes
Accepted

Fidelity (overlap) test over reduced density matrices on quantum circuit

For any two density matrices, no matter where they originated, the SWAP test can be used to calculate the desired quantity. Let's write this explicitly. We take three modes, one for the control that I'...
Quantum Mechanic's user avatar
3 votes

SWAPing Schmidt vectors

TL;DR: In general, such "inner product" is not well-defined. This can be remedied by appropriate choice of $A$ and $B$. Still, the inner product depends on the global phase and thus cannot ...
Adam Zalcman's user avatar
  • 21.7k
3 votes

How do you test a pair of unknown qubits for orthogonality with certainty?

It is not possible for a measurement to deterministically give one outcome or the other depending on whether two states are equal or orthogonal. Such a measurement would be some two-outcome POVM $\mu$ ...
glS's user avatar
  • 24k
3 votes
Accepted

How to calculate inner product of quantum states with other method than swap test?

I doubt that this is possible. Given a state $|\phi\rangle$, we have no method for distinguishing it from $e^{i\varphi}|\phi\rangle$ for any phase $\varphi$. This means that we have no way of ...
Quantum Mechanic's user avatar
3 votes
Accepted

In the swap test, how is the final probability $P(0)$ calculated?

Note that \begin{align}|\psi \rangle &= \frac {1}{2}(| 0 \rangle_1|\psi \rangle_2|\phi \rangle_3 + | 1 \rangle_1|\psi \rangle_2|\phi \rangle_3 + | 0 \rangle_1|\phi \rangle_2|\psi \rangle_3 - | 1 \...
KAJ226's user avatar
  • 13.8k
3 votes

Confusion in computing the $1+|\langle\phi|\psi\rangle|^2$ term in the quantum swap test algorithm

Remember that when you're multiplying out a term like $$ (\langle\phi|\langle\psi|+\langle\psi|\langle\phi|)(|\phi\rangle|\psi\rangle+|\psi\rangle|\phi\rangle) $$ that (i) you get all the corss terms (...
DaftWullie's user avatar
3 votes
Accepted

Swap Test for vector difference - how are different sized inputs combined?

The paper you refer is incomplete and not very right on this part. First a minus sign should be present in : $$ |\phi\rangle = \frac{1}{\sqrt{Z}} (|a||0\rangle - |b||1\rangle) $$ Secondly, if you ...
cnada's user avatar
  • 4,694
3 votes
Accepted

Can the SWAP test only compare registers with the same number of qubits?

Qubits can be only with a size of 2, which means a dimensionality of 2. For $|\alpha \rangle,|\beta\rangle$ here, for the SWAP gate to make sense, they must be of the same dimensionality (in the ...
Siddhant Singh's user avatar
2 votes

In a Swap Test why is the control qubit influenced and aren't the target qubits altered?

Deciding which qubit is a source and which is a target and changes depends on a basis. In a basis-independent sense, the qubits get entangled, and it affects both sides. In a swap test you apply ...
kludg's user avatar
  • 3,204
2 votes
Accepted

Implementing a SWAP-Test for samples with large numbers of features

You can employ a method described in paper Transformation of quantum states using uniformly controlled rotations. This allows you to prepare arbitrary quantum state. If you have $x$ features, you can ...
Martin Vesely's user avatar
2 votes

What's the matrix representation of the CSWAP?

2-qubit gates can be simulated effectively, much faster than potentially large matrix multiplies. They can be applied basically with linear complexity over the size of the full state (Python example ...
rhundt's user avatar
  • 913
2 votes
Accepted

Integral over Haar measure of squared density matrix of Haar random state is proportional to the identity plus swap operator

I guess the gist of the question here is the relation between expressions like $\int d\psi \,\mathbb{P}_\psi^{\otimes t}$, $\mathbb{P}_\psi\equiv|\psi\rangle\!\langle\psi|$, and expressions like $\int ...
glS's user avatar
  • 24k
2 votes
Accepted

How to implement the state $|\psi\rangle = \frac{1}{\sqrt{2}}\left[|0\rangle \otimes |X_i\rangle + |1\rangle \otimes |X_j\rangle\right]$

Suppose that $U_i$ prepares the state $\left|X_i\right\rangle$. Start by applying an Hadamard gate on the first qubit: $$\frac{1}{\sqrt{2}}\left(|00\rangle+|10\rangle\right)$$ Then apply $U_j$ ...
Tristan Nemoz's user avatar
2 votes

Alternatives to the swap test using a smaller number of qubits

The SWAP test is great when you don't have access to $U$ or $V$ and have no other way to prepare $|\psi\rangle$ or $|\phi\rangle$, and uses $2n+1$ qubits - while Tristan's test uses only $n$ qubits, ...
Mark Spinelli's user avatar
1 vote
Accepted

perform a SWAP measurement using local operations and classical feedback

Measure SWAP from 7 CNOTs, linear connectivity, ancilla outside: (Note: improved this to not need any feedback anymore, and to just generally be cleaner.)
Craig Gidney's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible