# Generate a 3-qubit SWAP unitary in terms of elementary gates

I wish to generate the following unitary

[1,0,0,0,0,0,0,0;
0,1,0,0,0,0,0,0;
0,0,1,0,0,0,0,0;
0,0,0,0,1,0,0,0;
0,0,0,1,0,0,0,0;
0,0,0,0,0,1,0,0;
0,0,0,0,0,0,1,0;
0,0,0,0,0,0,0,1;]


However the three qubit Toffoli and Fredkin gates do not appear to generate this. Does anyone know a simple way to generate this unitary? It seems to me that it should not be hard to produce as it is so close to the identity.

• Title change suggestion: "Circuit for unitary that maps $| 011 \rangle \rightarrow | 100 \rangle$ and $| 100 \rangle \rightarrow |011 \rangle$" – Davit Khachatryan Sep 19 '20 at 14:58

By using similar ideas from this answer I have found this circuit:

Thought process:

The unitary is a permutation matrix that doesn't change bitstrings except $$U |100\rangle \rightarrow |011\rangle$$ and $$U |011\rangle \rightarrow |100\rangle$$ (identity action on the rest of the bitstrings). Here I am going to use Qiskit's indexing convention (qubit indexing in Qiskit $$|q_2 q_1 q_0 \rangle$$). Note that the Toffoli gate and $$2$$ CNOT's after it will do the job for $$U |011\rangle \rightarrow |100\rangle$$ and the same Tofalli with $$2$$ CNOTs before it will do the job for $$U |100\rangle \rightarrow |011\rangle$$. How I came to this solution? I tried to write down a circuit only for these two bitstring transformations not worrying about the rest, then I have tried to correct the rest transformations by adding additional gates. Although this sounds good, actually this strategy didn't work perfectly but gave me a draft circuit...then I just started to play with that circuit and obtained this solution.

Qiskit code for prove:

from qiskit import *
import qiskit.quantum_info as qi

circuit = QuantumCircuit(3)

circuit.cx(2, 1)
circuit.cx(2, 0)
circuit.ccx(0, 1, 2)
circuit.cx(2, 1)
circuit.cx(2, 0)

matrix = qi.Operator(circuit)
print(matrix.data)


The output (the matrix that corresponds to the circuit):

[[1 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0]
[0 0 0 0 1 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 1]]


Qiskit supports gates defined by arbitrary unitary matrices and unitary synthesis.

First, define the matrix as a Numpy array, convert it into a gate and add it to a circuit:

import numpy as np
from qiskit import QuantumCircuit
from qiskit.extensions import UnitaryGate

matrix = np.array([[1,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0],
[0,0,0,0,1,0,0,0],
[0,0,0,1,0,0,0,0],
[0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,1]], dtype=np.complex)

circuit = QuantumCircuit(3)
circuit.append(UnitaryGate(matrix), [0,1,2])


Then, transpile it into your desired basis (the current synthesizer only supports 1q- and 2q-gates basis):

from qiskit import transpile

new_circuit = transpile(circuit, basis_gates=['cx', 'u1', 'u2', 'u3'])
new_circuit.draw('mpl')


You can check the equivalence with Operator

from qiskit.quantum_info import Operator
Operator(new_circuit).equiv(circuit)

True