B3: SWAP Qubits

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: PSL

Editorial

A quantum gate that swaps the states of qubits is called a swap gate.

In this problem, since only the qubits are swapped and the probability amplitudes remain unchanged, you might consider using an XX gate or a CXCX gate.

In fact, it is known that the swap gate can be implemented using three controlled XX gates, as shown below:

The state transitions can be expressed as follows:

a000+a110+a201+a311CX(0,1)a000+a310+a201+a111CX(1,0)a000+a310+a101+a211CX(0,1)a000+a210+a101+a311\begin{align} a_0\ket{00} + a_1\ket{10} + a_2\ket{01} + a_3\ket{11} &\xrightarrow{CX(0, 1)} a_0\ket{00} + a_3\ket{10} + a_2\ket{01} + a_1\ket{11} \nonumber\\ &\xrightarrow{CX(1, 0)} a_0\ket{00} + a_3\ket{10} + a_1\ket{01} + a_2\ket{11} \nonumber\\ &\xrightarrow{CX(0, 1)} a_0\ket{00} + \underline{a_2}\ket{10} + \underline{a_1}\ket{01} + a_3\ket{11}\\ \end{align}

The CX(0,1)CX(0, 1) gate swaps the probability amplitudes of 10\ket{10} and 11\ket{11}, while the CX(1,0)CX(1, 0) gate swaps the probability amplitudes of 01\ket{01} and 11\ket{11}.

The circuit depth is 33.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
 
 
def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
 
    qc.cx(0, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
 
    return qc