# Basic Multiplier on quantum circuit using Blueqat

### Introduction

We just implement using basic adder circuit and toffoli gate on blueqat quantum computer SDK.

### 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`

The overlayed state has shifted so that 0 and 1 come to q2 in about half probability.

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

### Multiplier of binary number

First thinking about toffoli gate to multiply binary number.

0*0=0
0*1=0
1*0=0
1*1=1

And just add each place.

### Example

Just do it. Let’s try to solve 1*2=?

First convert 2 on binary number as 10

``01*   10-------    00   01-------   0  0-------  0010``

We want to realize 01*10=0010

Let’s try to implement this circuit.

Now we want to realize |a,b,x> => |a,b,a*b> and get x0,x2,x4,x8. c is ancilla bit and z is carry bit.

``a0  0--b0  0--c0  0--a2  0--b2  0--c21 0--c22 0--c4  0--z4  0--z8  0--``
``x0  0--x2  0--x4  0--x8  0--``

### First toffoli gate on each number

Let’s multiply binary number using toffoli gate.

``a0  0--*---*---b0  0--*-*-|---c0  0--X-|-|---a2  0----*-|-*-b2  0----|-*-*-c21 0----X-|-|-c22 0------X-|-c4  0--------X-z4  0----------z8  0----------``
``x0  0----------x2  0----------x4  0----------x8  0----------``

To looks very bothering…

### Carry

We have z4 and z8 for carry bit.

``a0  0--*---*-------b0  0--*-*-|-------c0  0--X-|-|-------a2  0----*-|-*-----b2  0----|-*-*-----c21 0----X-|-|-*---c22 0------X-|-*---c4  0--------X-|-*-z4  0----------X-*-z8  0------------X-                  x0  0--------------x2  0--------------x4  0--------------x8  0--------------``

### Sum all values

Summing these values using cx gate.

``#0  a0  0--*---*-------------------#1  b0  0--*-*-|-------------------#2  c0  0--X-|-|-------*-----------#3  a2  0----*-|-*-----|-----------#4  b2  0----|-*-*-----|-----------#5  c21 0----X-|-|-*---|-*---------#6  c22 0------X-|-*---|-|-*-------#7  c4  0--------X-|-*-|-|-|-*-----#8  z4  0----------X-*-|-|-|-|-*---#9  z8  0------------X-|-|-|-|-|-*-                       | | | | | |#10 x0  0--------------X-|-|-|-|-|-#11 x2  0----------------X-X-|-|-|-#12 x4  0--------------------X-X-|-#13 x8  0------------------------X-``

It looks wasteful of qubits… but now we just left it.

### Blueqat code

This is the blueqat code

``C1 = Circuit().ccx[0,1,2].ccx[1,3,5].ccx[0,4,6].ccx[3,4,7].ccx[5,6,8].ccx[7,8,9].cx[2,10].cx[5,11].cx[6,11].cx[7,12].cx[8,12].cx[9,13] ``
``("input data here" +C1).m[:].run(shots=100)``

We read the number from the last number.

``#00 * 00 = 0000(Circuit() + C1).m[:].run(shots=100)Counter({'00000000000000': 100})``
``#01 * 01 = 0001(Circuit().x[0].x[1] + C1).m[:].run(shots=100)Counter({'11100000001000': 100})``
``#10 * 01 = 0010(Circuit().x[3].x[1] + C1).m[:].run(shots=100)Counter({'01010100000100': 100})``
``#01 * 10 = 0010(Circuit().x[0].x[4] + C1).m[:].run(shots=100)Counter({'10001010000100': 100})``
``#10 * 10 = 0100(Circuit().x[3].x[4] + C1).m[:].run(shots=100) Counter({'00011001000010': 100})``
``#11 * 10 = 0110(Circuit().x[0].x[3].x[4] + C1).m[:].run(shots=100)Counter({'10011011000110': 100})``
``#10 * 11 = 0110(Circuit().x[1].x[3].x[4] + C1).m[:].run(shots=100)Counter({'01011101000110': 100})``
``#11 * 11 = 1001(Circuit().x[0].x[1].x[3].x[4] + C1).m[:].run(shots=100) Counter({'11111111111001': 100})``

### Discussion

We consumed a lot of qubits… There may be a lot of way to make an efficient circuit than the one we implement this time.