loading...

Understanding how to create Toffoli gate using Blueqat

beef_and_rice profile image Takumi Kato ・4 min read

What is Blueqat?

Blueqat is an open source library for quantum computer.
https://github.com/Blueqat/Blueqat

How to install Blueqat?

Blueqat is provided as Python (ver 3.6+) library. You can install by pip.

pip install blueqat

What is Toffoli gate?

Toffoli gate is 2-controlled NOT gate. If both 2 control qubits are |1>, Toffoli gate flip a target qubit (|0> → |1>, |1> →|0>), otherwise do nothing.

Truth table for Toffoli gate

c1 c2 t
0 0 0 → 0
0 0 1 → 1
0 1 0 → 0
0 1 1 → 1
1 0 0 → 0
1 0 1 → 1
1 1 0 → 1
1 1 1 → 0

Toffoli gate implementation of IBM OpenQASM

OpenQASM's standard library qelib1.inc defines the Toffoli gate. Here is its definition.

gate ccx a,b,c
{
  h c;
  cx b,c; tdg c;
  cx a,c; t c;
  cx b,c; tdg c;
  cx a,c; t b; t c; h c;
  cx a,b; t a; tdg b;
  cx a,b;
}

"h" is a Hadamard gate, "cx" is a CNOT gate, "t" is a T gate which rotates the Bloch sphere Z-axis by π/4 radians, and "tdg" is T† gate which rotates the Bloch spere Z-axis by -π/4 radians.

To understand this implementation, we reorder some operations. Generally, gate operations are not commutable. However, some gates can be commutable for example single qubit operation for i-th qubit and for j-th qubit, if i ≠ j, they're commutable. cx a, b and t a are also commutable because T gate rotate only Z-axis and CNOT gate don't minds control qubit's phase.

Here is reordered implementation.

gate ccx a,b,c
{
  h c;
  cx b,c; tdg c;
  cx a,c; t c;
  cx b,c; tdg c;
  cx a,c; t c;
  h c;
  t a; t b;
  cx a,b; tdg b; cx a,b;
}

This implementation can be separate to 2 parts. First part is making ccx except phase adjustment, second part is adjusting phase factors.

Create Toffoli gate

First part, some operations are sandwiched with Hadamard gates.

  h c;
  cx b,c; tdg c;
  cx a,c; t c;
  cx b,c; tdg c;
  cx a,c; t c;
  h c

To begin from conclusion, 2-controlled Z gate is sandwiched with H gates.
As we know, -H-Z-H- gate is X gate. We can confirm it with Blueqat.

from blueqat import Circuit
print(Circuit().h[0].z[0].h[0].to_unitary())
# => Matrix([[0, 1], [1, 0]]), This is X gate.
# We can see X gate definition by
print(Circuit().x[0].to_unitary())
# => Matrix([[0, 1], [1, 0]]), Yes! They're same!

If you're using Jupyter Notebook, you can get more pretty display.

Controlled Z gate sandwiched with H gates for target qubit is CNOT gate.

As same as these, 2-controlled Z gate sandwiched with H gates is Toffoli gate. However, in this part, we will get strange 2-controlled Z gate because adjustment of phase factor is not yet.

Let us implement 2-controlled Z gate using Blueqat.

cx b,c; tdg c;
cx a,c; t c;
cx b,c; tdg c;
cx a,c; t c;

We choose a = 2, b = 1, c = 0.
Implementation is very easy.

from blueqat import Circuit
c = Circuit()
c.cx[1, 0].tdg[0]
c.cx[2, 0].t[0]
c.cx[1, 0].tdg[0]
c.cx[2, 0].t[0]
c.to_unitary()

Phase factor is strange, but similar to 2-controlled Z gate.

Add H gates before first opearation and after last operation.

Is it a Toffoli gate? It seems not a Toffoli gate. But if we know, exp(-iπ/4) = i * exp(iπ/4), we can feel it is similar to Toffoli gate.

Blueqat's unitary gate is Sympy matrix and Sympy can simplify these terms automatically. Let's simplify the unitary matrix.

It is as same as Toffoli gate except phase factors.

Then, we will see second part.

 t a; t b;
 cx a,b; tdg b; cx a,b;

What is this means? Check by Blueqat.

Wow! This part multiples phase factor i if both qubit 1 and qubit 2 are |1>.

Finally, we combine first part and second part.

Is something wrong? Don't worry. First, exp(3iπ/8) = (-1)^3/8 because -1 =exp(iπ) and then, we can ignore scalar part of unitary matrix, it is "global phase".

So, divide by [0, 0]-th element and simplify the matrix, we can get Toffoli gate matrix.

Perfect!

Learn Blueqat more!

I'm a core developer of Blueqat. Blueqat has very simple and easy interface for quantum computing. I recommend to use Blueqat for learning quantum computer programming.

We prepared Blueqat tutorials and we will add more and more contents.
https://github.com/Blueqat/Blueqat-tutorials

If you have any questions or suggestions, please feel free to contact us by our Slack community. (Join from here)

Blueqat repository is here.
https://github.com/Blueqat/Blueqat

Discussion

pic
Editor guide