B3: SWAP Qubits

Time Limit: 3 seconds

Memory Limit: 512 MiB

Score: 200 points

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 complex 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:

a0∣00⟩+a1∣10⟩+a2∣01⟩+a3∣11⟩→CX(0,1)a0∣00⟩+a3∣10⟩+a2∣01⟩+a1∣11⟩→CX(1,0)a0∣00⟩+a3∣10⟩+a1∣01⟩+a2∣11⟩→CX(0,1)a0∣00⟩+a2‾∣10⟩+a1‾∣01⟩+a3∣11⟩\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 complex amplitudes of ∣10⟩\ket{10} and ∣11⟩\ket{11}, while the CX(1,0)CX(1, 0) gate swaps the complex amplitudes of ∣01⟩\ket{01} and ∣11⟩\ket{11}.

The quantum 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