# Decomposition of the Toffoli gate in terms of CNOT and single-qubit rotations¶

Copyright (C) 2011 and later, Paul D. Nation & Robert J. Johansson

This notebooks demonstrates how a toffoli gate can be rewritten in terms of CNOT gates and single qubit gates, and verifies the equivalence of the two gate sequences by comparing their matrix representations. For more information about the toffoli decomposition, see Nielsen & Chuang, Sec. 4.3, p178.

Note: The circuit image visualizations require ImageMagick for display.

ImageMagick can be easily installed with the command conda install imagemagick if you have conda installed. Otherwise, please follow the installation instructions on the ImageMagick documentation.

In [1]:
from qutip import *
from qutip_qip.operations import *
from qutip_qip.circuit import QubitCircuit, Gate

In [2]:
q = QubitCircuit(3, reverse_states=False)

In [3]:
q.png

Out[3]:
In [4]:
U = gate_sequence_product(q.propagators())

U.tidyup()

Out[4]:
Quantum object: dims = [[2, 2, 2], [2, 2, 2]], shape = (8, 8), type = oper, isherm = True\begin{equation*}\left(\begin{array}{*{11}c}1.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 1.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 1.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 1.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 1.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 1.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 1.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 1.0 & 0.0 & 0.0\\\end{array}\right)\end{equation*}
In [5]:
q2 = q.resolve_gates()

In [6]:
q2.png

Out[6]:
In [7]:
U2 = gate_sequence_product(q2.propagators())

U2.tidyup()

Out[7]:
Quantum object: dims = [[2, 2, 2], [2, 2, 2]], shape = (8, 8), type = oper, isherm = True\begin{equation*}\left(\begin{array}{*{11}c}1.000 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 1.000 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 1.000 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 1.000 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 1.000 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 1.000\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 1.000 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 1.000 & 0.0 & 0.0\\\end{array}\right)\end{equation*}
In [8]:
U == U2

Out[8]:
True

## Versions¶

In [9]:
from qutip.ipynbtools import version_table
version_table()

Out[9]:
SoftwareVersion
QuTiP4.7.0+f635fb8
Numpy1.20.2
SciPy1.6.3
matplotlib3.4.1
Cython0.29.23
Number of CPUs12
BLAS InfoGeneric
IPython7.21.0
Python3.9.4 | packaged by conda-forge | (default, May 10 2021, 22:10:34) [MSC v.1916 64 bit (AMD64)]
OSnt [win32]
Tue May 25 20:22:02 2021 W. Europe Daylight Time