The Fredkin Gate - Qiskit

In this blog post, you'll understand what the Fredkin gate does, and how to implement it using Qiskit.

Understanding the Fredkin Gate

The Fredkin gate, or the Controlled-SWAP(CSWAP) gate, is a reversible 3-bit gate. It has one control bit and two target bits. If the control bit is 1, it swaps the two target bits. If the control bit is 0, it leaves the target bits unchanged.

The Fredkin gate is used in:

Implementing the Fredkin Gate Using Qiskit

from qiskit import QuantumCircuit, transpile
from qiskit.visualization import plot_histogram

# A quantum circuit with 3 qubits and 3 classical bits
q = QuantumRegister(3,"qreg")
c = ClassicalRegister(3,"creg")
qc = QuantumCircuit(q,c)

# Function to add the Fredkin (CSWAP) gate to the circuit
def fredkin_gate(circuit, control_qubit, target_qubit1, target_qubit2):
    circuit.cx(control_qubit, target_qubit2)
	circuit.ccx(control_qubit,target_qubit_2,
			target_qubit_1)
    circuit.cx(control_qubit, target_qubit2)

Going Through an Example

Let's understand what's happening in each line of code by going through an example:

Input : |q2q1q0=|101
Control qubit: q0
Target qubits: q1, q2

Screenshot 2024-07-07 at 03.57.44.png

Go ahead and prepare your state in your code. Then, apply the fredkin_gate() function you created before:

qc.x(0)  # Set qubit 0 to |1> (control qubit)
qc.x(2)  # Set qubit 2 to |1>

fredkin_gate(qc, 0, 1, 2)

What output do you anticipate from the Fredkin Gate?

fredkin_gate_qiskit.png

Since the control qubit q0 is |1, q1 and q2 get swapped. So the Fredkin Gate's output should be |011.

Now, let's go through the code and see whether you get the correct answer or not.

Step 1:

circuit.cx(1, 2)

The CX gate with control qubit q1 and target qubit q2 flips the target qubit if the control qubit is |1
Since the control qubit is not |1, nothing changes.

Before the cx gate: |101
After the cx gate: |101

Step 2

circuit.ccx(0, 2, 1)

The Toffoli gate with control qubits q0 and q2 and target qubit q1 flips the target qubit if both control qubits are |1.
In this case, both q0 and q2 are |1. Therefore, the state becomes: |111

Before the ccx gate: |101
After the ccx gate: |111

ccx-toffoli-fredkin.png

Step 3

circuit.cx(0, 2)

In this step, you're performing a CX Gate on q0 and q2 .

Since q0 is 1, q2 get flipped.

Before the cx gate: |111
After the cx gate: |011

So, the final state becomes |011 which is what you anticipated all along!
Your code works!

Now, you can go ahead and measure your circuit.

qc.measure(q,c)
job = AerSimulator().run(qc,shots=1000)
counts = job.result().get_counts(qc) 
print(counts) 
print(qc.draw())

When you run everything together, you'll get something like this:

fredkin-result.png\

Implementing Qiskit's Built-in Fredkin Gate

Qiskit has made it easy!
You can simply import CSwapGate from Qiskit's library and bam!

Here is a complete example:

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

from qiskit.circuit.library import CSwapGate

q = QuantumRegister(3, "qreg")
c = ClassicalRegister(3, "creg")
qc = QuantumCircuit(q, c)

qc.x(q[0])
qc.x(q[2])

qc.append(CSwapGate(), [q[0], q[1], q[2]])

qc.measure(q, c)

job = AerSimulator().run(qc,shots=1000)

counts = job.result().get_counts(qc)

print(qc.draw())

print(counts)

After you run this cell, you'll unsurprisingly get the same result as before:

Screenshot 2024-07-07 at 04.58.49.png

Wanna get notified when I add new blog posts?
Drop your email / discord id here