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

It's creating a real confusion for me especially the parameterized circuit which I have to create. Can anybody please solve this for me? I want to create this circuit. • Please clarify what is your confusion and what are you trying to solve. Feb 22 at 6:16
• See I edited the question. Feb 22 at 6:20

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\rangle$$ amd $$|\psi \rangle = |000\rangle$$ , then you can execute it as: where $$I$$ is the identity operator. It doesn't need to be there because the qubits start in the state $$|0\rangle$$. I just put them there as a place holder.

Now this can be done with qiskit as:

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

num_qubits = 3
circuit1 = QuantumCircuit(num_qubits)
for i in range(num_qubits):
circuit1.x(i)

circuit2 = QuantumCircuit(num_qubits)
for i in range(num_qubits):
circuit2.id(i)

swap_test_circuit = QuantumCircuit(2*num_qubits + 1,1)
swap_test_circuit.compose(circuit1, qubits=[1,2,3], inplace=True )
swap_test_circuit.compose(circuit2, qubits=[4,5,6], inplace=True )
swap_test_circuit.h(0)
for i in range(num_qubits):
swap_test_circuit.cswap(0,i+1,i+4)
swap_test_circuit.h(0)
swap_test_circuit.measure(,)

• Thank you, I understood. Feb 22 at 7:09