SWAP
7.4. SWAP#
input |
output |
|
|---|---|---|
\(\lvert 00\rangle\) |
\(\Rightarrow\) |
\(\lvert 00\rangle\) |
\(\lvert 01\rangle\) |
\(\Rightarrow\) |
\(\lvert 10\rangle\) |
\(\lvert 10\rangle\) |
\(\Rightarrow\) |
\(\lvert 01\rangle\) |
\(\lvert 11\rangle\) |
\(\Rightarrow\) |
\(\lvert 11\rangle\) |
SWAP gate can be a native gate for certain types of quantum computer through exchange interaction. Here we show a circuit equivalent to SWAP.
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer
from qiskit.quantum_info import Statevector
backend = Aer.get_backend('statevector_simulator')
Using the built-in SWAP gate: |01⟩⇒|10⟩
cr=ClassicalRegister(2)
qr = QuantumRegister(2)
qc = QuantumCircuit(qr,cr)
qc.x(0)
qc.barrier()
qc.swap(0,1)
qc.measure([0,1],[0,1])
qc.draw()
┌───┐ ░ ┌─┐
q0_0: ┤ X ├─░──X─┤M├───
└───┘ ░ │ └╥┘┌─┐
q0_1: ──────░──X──╫─┤M├
░ ║ └╥┘
c0: 2/════════════╩══╩═
0 1 result=backend.run(qc).result()
(result.get_statevector()).draw('latex')
\[ |10\rangle\]
Equivalent circuit to
SWAPusingCX
qr = QuantumRegister(2)
qc = QuantumCircuit(qr)
qc.x(1)
qc.cnot(0,1)
qc.cnot(1,0)
qc.cnot(0,1)
qc.draw()
┌───┐
q1_0: ───────■──┤ X ├──■──
┌───┐┌─┴─┐└─┬─┘┌─┴─┐
q1_1: ┤ X ├┤ X ├──■──┤ X ├
└───┘└───┘ └───┘result=backend.run(qc).result()
(result.get_statevector()).draw('latex')
\[ |01\rangle\]
Symmetric property: Swapping qubits themselves (not their states) should give the same results.
qr = QuantumRegister(2)
qc = QuantumCircuit(qr)
qc.x([0,1])
qc.barrier()
qc.cnot(1,0)
qc.cnot(0,1)
qc.cnot(1,0)
qc.draw()
┌───┐ ░ ┌───┐ ┌───┐
q2_0: ┤ X ├─░─┤ X ├──■──┤ X ├
├───┤ ░ └─┬─┘┌─┴─┐└─┬─┘
q2_1: ┤ X ├─░───■──┤ X ├──■──
└───┘ ░ └───┘ result=backend.run(qc).result()
(result.get_statevector()).draw('latex')
\[ |11\rangle\]
There is no particular reason to use X direction. CY should be able to realize SWAP as well.
qr = QuantumRegister(2)
qc = QuantumCircuit(qr)
qc.x(0)
qc.barrier()
qc.cy(1,0)
qc.cy(0,1)
qc.cy(1,0)
qc.draw()
┌───┐ ░ ┌───┐ ┌───┐
q3_0: ┤ X ├─░─┤ Y ├──■──┤ Y ├
└───┘ ░ └─┬─┘┌─┴─┐└─┬─┘
q3_1: ──────░───■──┤ Y ├──■──
░ └───┘ result=backend.run(qc).result()
(result.get_statevector()).draw('latex')
\[ |10\rangle\]