@yuichirominato 2019.02.09更新 89views

[Gate] Basic Adder and Subtractor on blueqat quantum computer SDK


Introduction

We try to implement basic adder and subtractor circuit on our blueqat SDK

Decimal and Binary number

First you have to convert decimal to binary number using qubits.

15 is ,

$q_0*2^0+q_1*2^1+q_2*2^2+q_3*2^3$

when,

$q_0=q_1=q_2=q_3=1$

Reference

This is a very useful and good reference in Japanese language.
https://qiita.com/converghub/items/c61b2b91b311cf730e18

Adder of 2qubits

Now we realize adder of 2qubits,

$\mid a,b,0 \rangle = \mid a,b,a+b \rangle$
Prepare $a=a_0*2^0+a_1*2^1$ and $b=b_0*2^0+b_1*2^1$ and $c=c_0*2^0+c_1*2^1+c_2*2^2$.

We need additional carry bit for the answer C

a0 ----------------
a1 ----------------
b0 ----------------
b1 ----------------
c0 ----------------
c1 ----------------
c2 ----------------

First we implement all the carry. a0,b0 to c1 and a1,b1,c1 using 3 toffoli gate.

a0 -*--------------
a1 -|-*-*----------
b0 -*-|-|----------
b1 -|-*-|-*--------
c0 -|-|-|-|--------
c1 -X-|-*-*--------
c2 ---X-X-X--------

and sum


a0 -*-----------*------
a1 -|-*-*---*---|------
b0 -*-|-|---|---|-*----
b1 -|-*-|-*-|-*-|-|----
c0 -|-|-|-|-|-|-X-X----
c1 -X-|-*-*-X-X--------
c2 ---X-X-X------------

That’s all. Now we can use adder with input of data with X gate.

Toffoli gate can implement with ccx[c,c,x] and cnot gate with cx[c,x].

Create each circuit using instance and just add each other finally use .run()

from blueqat import Circuit 

#adder
a=Circuit().ccx[0,2,5].ccx[1,3,6].ccx[1,5,6].ccx[3,5,6].cx[1,5].cx[3,5].cx[0,4].cx[2,4].m[:]

#0+1
(Circuit().x[2] + a).run(shots=100)
Counter({'0010100': 100})

#1+0
(Circuit().x[0] + a).run(shots=100)
Counter({'1000100': 100})

#1+1
(Circuit().x[0,2]).run(shots=100)
Counter({'1010010': 100})

#0+2
(Circuit().x[3]+a).run(shots=100)
Counter({'0001010': 100})

#2+0
(Circuit().x[1]+a).run(shots=100)
Counter({'0100010': 100})

#1+2
(Circuit().x[0,3]+a).run(shots=100)
Counter({'1001110': 100})                                                                       

#2+1
(Circuit().x[1,2]+a).run(shots=100)
Counter({'0110110': 100})

#2+2
(Circuit().x[1,3]+a).run(shots=100)
Counter({'0101001': 100})
                                                                                
#2+3
(Circuit().x[1:4]+a).run(shots=100)
Counter({'0111101': 100})

#3+2
(Circuit().x[0,1,3]+a).run(shots=100)
Counter({'1101101': 100})

#3+3
(Circuit().x[0:4]+a).run(shots=100)
Counter({'1111011': 100})

Subtractor

To implement subtractor is simple just reverse the circuit because the quantum circuit is reversible.

a0 ---*-----------*----
a1 ---|---*---*-*-|----
b0 -*-|---|---|-|-*----
b1 -|-|-*-|-*-|-*-|----
c0 -X-X-|-|-|-|-|-|----
c1 -----X-X-*-*-|-X----
c2 ---------X-X-X------

Let’s see the code.

#Subtractor
b =Circuit().cx[2,4].cx[0,4].cx[3,5].cx[1,5].ccx[3,5,6].ccx[1,5,6].ccx[1,3,6].ccx[0,2,5].m[:]

#3-3
(Circuit().x[0,1,4,5] + b).run(shots=100)
Counter({'1100000': 100})
#3-1
(Circuit().x[2,4,5] + b).run(shots=100)
Counter({'0010010': 100})
#3-1-1
(Circuit().x[0,2,4,5] + b).run(shots=100)
Counter({'1010100': 100})

Basic subtractor is also easy to implement.

Improvement of the code and about toffoli gate

This time we use integrated toffoli gate. If you want to make the adder circuit with basic gate set you need to implement a complicated and long logical circuit before like,

from blueqat import Circuit 
import math  
tof1 = Circuit().h[5].cx[2,5].rz(-math.pi/4)[5].cx[0,5].rz(math.pi/4)[5].cx[2,5].rz(-math.pi/4)[5].cx[0,5].rz(-math.pi/4)[2].rz(math.pi/4)[5].cx[0,2].h[5].rz(math.pi/4)[0].rz(-math.pi/4)[2].cx[0,2]
tof2 = Circuit().h[6].cx[3,6].rz(-math.pi/4)[6].cx[1,6].rz(math.pi/4)[6].cx[3,6].rz(-math.pi/4)[6].cx[1,6].rz(-math.pi/4)[3].rz(math.pi/4)[6].cx[1,3].h[6].rz(math.pi/4)[1].rz(-math.pi/4)[3].cx[1,3]
tof3 = Circuit().h[6].cx[5,6].rz(-math.pi/4)[6].cx[1,6].rz(math.pi/4)[6].cx[5,6].rz(-math.pi/4)[6].cx[1,6].rz(-math.pi/4)[5].rz(math.pi/4)[6].cx[1,5].h[6].rz(math.pi/4)[1].rz(-math.pi/4)[5].cx[1,5]
tof4 = Circuit().h[6].cx[5,6].rz(-math.pi/4)[6].cx[3,6].rz(math.pi/4)[6].cx[5,6].rz(-math.pi/4)[6].cx[3,6].rz(-math.pi/4)[5].rz(math.pi/4)[6].cx[3,5].h[6].rz(math.pi/4)[3].rz(-math.pi/4)[5].cx[3,5]
after = Circuit().cx[1,5].cx[3,5].cx[0,4].cx[2,4]
measure = Circuit().m[:]
c01 = Circuit().x[2]                                                                                                                 
e = c01 + tof1 + tof2 + tof3 + tof4 + after + measure
e.run()
e.last_result()
(0, 0, 1, 0, 1, 0, 0)

but now,

from blueqat import Circuit 
#adder
a=Circuit().ccx[0,2,5].ccx[1,3,6].ccx[1,5,6].ccx[3,5,6].cx[1,5].cx[3,5].cx[0,4].cx[2,4].m[:]
#0+1
(Circuit().x[2] + a).run(shots=100)
Counter({'0010100': 100})

It’s quite simple, let’s contribute and give some request for developers.

Recommend


Back To Top