<p>There are standard ways to approximate any unitary operation with just CNOTs, Hs, and Ts. In the specific case of the Toffoli gate you don't need something so general.</p>

<p>Start with the Toffoli gate:</p>

<p><a href="https://i.sstatic.net/ZaHuh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZaHuh.png" alt="Step 1"></a></p>

<p>Use a construction that relies on operations having a square root to cut the worst-case number of controls from 2 to 1:</p>

<p><a href="https://i.sstatic.net/Aqaiq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Aqaiq.png" alt="Step 2"></a></p>

<p>Use a construction that moves controls off of arbitrary single-qubit operations and onto CNOTs:</p>

<p><a href="https://i.sstatic.net/2ocSo.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2ocSo.png" alt="Step 3"></a></p>

<p>Oh hey that looks kinda familiar ;)</p>

<p>Now simplify by sliding gates around so that some of them cancel. Zs can move over controls, but not over NOTs (but can hop over the space between two NOTs that undo each other). It's also useful to know how to move a CNOT over another CNOT's control by introducing a third CNOT. Hadamards cancel when paired, and turn Zs into Xs (and vice versa) when hopping since $HX = ZH$. Anyways, after some fiddling...:</p>

<p><a href="https://i.sstatic.net/8ct9t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8ct9t.png" alt="Done"></a></p>

<p>(Note: $T = Z^{1/4}$ and $T^\dagger = Z^{-1/4}$)</p>

<p>You can <a href="http://algorithmicassertions.com/quirk#circuit=%7B%22cols%22%3A%5B%5B%22Counting3%22%5D%2C%5B%22Chance%22%2C%22Chance%22%2C%22Chance%22%5D%2C%5B%22%E2%80%A6%22%2C%22%E2%80%A6%22%2C%22%E2%80%A6%22%5D%2C%5B1%2C1%2C%22H%22%5D%2C%5B%22Z%5E%C2%BC%22%2C%22Z%5E%C2%BC%22%2C%22Z%5E%C2%BC%22%5D%2C%5B1%2C%22%E2%80%A2%22%2C%22X%22%5D%2C%5B1%2C1%2C%22Z%5E-%C2%BC%22%5D%2C%5B%22%E2%80%A2%22%2C1%2C%22X%22%5D%2C%5B1%2C1%2C%22Z%5E%C2%BC%22%5D%2C%5B1%2C%22%E2%80%A2%22%2C%22X%22%5D%2C%5B1%2C1%2C%22Z%5E-%C2%BC%22%5D%2C%5B%22%E2%80%A2%22%2C%22X%22%2C%22X%22%5D%2C%5B1%2C%22Z%5E-%C2%BC%22%2C%22H%22%5D%2C%5B%22%E2%80%A2%22%2C%22X%22%5D%5D%7D" rel="nofollow noreferrer">confirm the circuit works by toying with it in Quirk</a>.</p>

<p>These constructions are explained in more detail in textbooks such as <a href="http://rads.stackoverflow.com/amzn/click/1107002176" rel="nofollow noreferrer">Nielsen and Chuang</a>. The 'moving controls' one is particularly tricky, because you have to find $A$, $B$, $C$ such that $ABC=I$ but $AXBXCe^{i\theta}=U$. Or you can read <a href="http://algorithmicassertions.com/circuits/2015/06/22/Using-Quantum-Gates-instead-of-Ancilla-Bits.html" rel="nofollow noreferrer">a few blog posts about making a controlled-by-every-other-wire NOT using $O(n)$ basic gates</a> that also uses these constructions but explains them in more detail.</p>

CC BY-SA 3.0
2016-07-20T17:37:45.463Z
2016-07-20T17:50:12.620Z
2016-07-20T17:50:12.620Z
Craig Gidney
<p>I want to create other quantum gates from the basic building blocks of a universal quantum gate set. I've been playing with IBM's quantum computing interface for that.</p>

<p>I wanted to create a Toffoli gate, which I managed to in the end, but part of it was by trial and error. I want to create some other gates now, and I want to avoid the long arduous process of trial and error.</p>

<p>Is there a better way?</p>

<p>Is there also a way to how to construct the gate from the minimum number of gates necessary?</p>

<p>I'm going to add my work on the Toffoli here for anyone to see / critique.</p>

<pre><code>% Matlab / Octave code
q=[1 0];
X=[0 1;1 0];
Y=[0 -1i; 1i 0];
Z=[1 0;0 -1];
H=[1 1;1 -1]/sqrt(2);
S = [1 0;0 1i];
St = [1 0;0 -1i];
C = [1 0 0 0;0 1 0 0; 0 0 0 1; 0 0 1 0];
Cgap = [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 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 0 1; 0 0 0 0 0 0 1 0];
T = [1 0; 0 exp(pi*1i/4)];
Tt = [1 0; 0 exp(-pi*1i/4)];
I = eye(2);
I2 = eye(4);
I3 = eye(8);

CS = kron(T,Tt)*C*kron(Tt,Tt)*C*kron(T,S);
CSt = kron(Tt,St)*C*kron(T,T)*C*kron(Tt,T);
CgapS = kron(T,kron(I,Tt))*Cgap*kron(Tt,kron(I,Tt))*Cgap*kron(T,kron(I,S));

Toffoli = kron(I2,H)*kron(I,CS)*kron(C,I)*kron(I,CSt)*kron(C,I)*CgapS*kron(I2, H)
</code></pre>

<p>Toffoli gate program using IBM's Quantum Experience (the two X gates on the very left are used to set the values of the qubits to 1; they can be removed to run the gate with 0 values):
<a href="https://i.sstatic.net/G4CtA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/G4CtA.png" alt="Toffoli gate program using IBM&#39;s Quantum Experience"></a></p>

<p>The way I figured it out was I found some lecture notes with the gate expressed as seven building blocks, but I didn't have some of the blocks, so I constructed those by trial and error. Here's the pdf:
<a href="https://inst.eecs.berkeley.edu/~cs191/fa07/lectures/lecture9_fa07.pdf" rel="nofollow noreferrer">https://inst.eecs.berkeley.edu/~cs191/fa07/lectures/lecture9_fa07.pdf</a></p>

<quantum-computing>
How do I figure out how to combine simpler quantum gates to create the gate I want?
Gabi
# How do I figure out how to combine simpler quantum gates to create the gate I want?

(Asked by: username_0 on 2016-05-18)

I want to create other quantum gates from the basic building blocks of a universal quantum gate set. I've been playing with IBM's quantum computing interface for that.
I wanted to create a Toffoli gate, which I managed to in the end, but part of it was by trial and error. I want to create some other gates now, and I want to avoid the long arduous process of trial and error.
Is there a better way?
Is there also a way to how to construct the gate from the minimum number of gates necessary?
I'm going to add my work on the Toffoli here for anyone to see / critique.
% Matlab / Octave code
q=[1 0];
X=[0 1;1 0];
Y=[0 -1i; 1i 0];
Z=[1 0;0 -1];
H=[1 1;1 -1]/sqrt(2);
S = [1 0;0 1i];
St = [1 0;0 -1i];
C = [1 0 0 0;0 1 0 0; 0 0 0 1; 0 0 1 0];
Cgap = [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 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 0 1; 0 0 0 0 0 0 1 0];
T = [1 0; 0 exp(pi*1i/4)];
Tt = [1 0; 0 exp(-pi*1i/4)];
I = eye(2);
I2 = eye(4);
I3 = eye(8);

CS = kron(T,Tt)*C*kron(Tt,Tt)*C*kron(T,S);
CSt = kron(Tt,St)*C*kron(T,T)*C*kron(Tt,T);
CgapS = kron(T,kron(I,Tt))*Cgap*kron(Tt,kron(I,Tt))*Cgap*kron(T,kron(I,S));

Toffoli = kron(I2,H)*kron(I,CS)*kron(C,I)*kron(I,CSt)*kron(C,I)*CgapS*kron(I2, H)

Toffoli gate program using IBM's Quantum Experience (the two X gates on the very left are used to set the values of the qubits to 1; they can be removed to run the gate with 0 values):

The way I figured it out was I found some lecture notes with the gate expressed as seven building blocks, but I didn't have some of the blocks, so I constructed those by trial and error. Here's the pdf:
https://inst.eecs.berkeley.edu/~cs191/fa07/lectures/lecture9_fa07.pdf

**Tags:** quantum-computing

---

## 1 answers

### Answer by username_1 on 2016-07-20. Score: 2 | Accepted answer

There are standard ways to approximate any unitary operation with just CNOTs, Hs, and Ts. In the specific case of the Toffoli gate you don't need something so general.

Start with the Toffoli gate:

[![Step 1](https://i.sstatic.net/ZaHuh.png)](https://i.sstatic.net/ZaHuh.png)

Use a construction that relies on operations having a square root to cut the worst-case number of controls from 2 to 1:

[![Step 2](https://i.sstatic.net/Aqaiq.png)](https://i.sstatic.net/Aqaiq.png)

Use a construction that moves controls off of arbitrary single-qubit operations and onto CNOTs:

[![Step 3](https://i.sstatic.net/2ocSo.png)](https://i.sstatic.net/2ocSo.png)

Oh hey that looks kinda familiar ;)

Now simplify by sliding gates around so that some of them cancel. Zs can move over controls, but not over NOTs (but can hop over the space between two NOTs that undo each other). It's also useful to know how to move a CNOT over another CNOT's control by introducing a third CNOT. Hadamards cancel when paired, and turn Zs into Xs (and vice versa) when hopping since $HX = ZH$. Anyways, after some fiddling...:

[![Done](https://i.sstatic.net/8ct9t.png)](https://i.sstatic.net/8ct9t.png)

(Note: $T = Z^{1/4}$ and $T^\dagger = Z^{-1/4}$)

You can [confirm the circuit works by toying with it in Quirk](http://algorithmicassertions.com/quirk#circuit=%7B%22cols%22%3A%5B%5B%22Counting3%22%5D%2C%5B%22Chance%22%2C%22Chance%22%2C%22Chance%22%5D%2C%5B%22%E2%80%A6%22%2C%22%E2%80%A6%22%2C%22%E2%80%A6%22%5D%2C%5B1%2C1%2C%22H%22%5D%2C%5B%22Z%5E%C2%BC%22%2C%22Z%5E%C2%BC%22%2C%22Z%5E%C2%BC%22%5D%2C%5B1%2C%22%E2%80%A2%22%2C%22X%22%5D%2C%5B1%2C1%2C%22Z%5E-%C2%BC%22%5D%2C%5B%22%E2%80%A2%22%2C1%2C%22X%22%5D%2C%5B1%2C1%2C%22Z%5E%C2%BC%22%5D%2C%5B1%2C%22%E2%80%A2%22%2C%22X%22%5D%2C%5B1%2C1%2C%22Z%5E-%C2%BC%22%5D%2C%5B%22%E2%80%A2%22%2C%22X%22%2C%22X%22%5D%2C%5B1%2C%22Z%5E-%C2%BC%22%2C%22H%22%5D%2C%5B%22%E2%80%A2%22%2C%22X%22%5D%5D%7D).

These constructions are explained in more detail in textbooks such as [Nielsen and Chuang](http://rads.stackoverflow.com/amzn/click/1107002176). The 'moving controls' one is particularly tricky, because you have to find $A$, $B$, $C$ such that $ABC=I$ but $AXBXCe^{i\theta}=U$. Or you can read [a few blog posts about making a controlled-by-every-other-wire NOT using $O(n)$ basic gates](http://algorithmicassertions.com/circuits/2015/06/22/Using-Quantum-Gates-instead-of-Ancilla-Bits.html) that also uses these constructions but explains them in more detail.


---

