@yuichirominato 2019.02.10更新 12views

[shor]Subtractor with reverse circuit of adder


Introduction

We implemented the adder circuit before. This time we easily implement subtractor just reversing the adder circuit.

Reference

The adder circuit

By the reference just reverse it and we get the subtractor.

Just make the reverse circuit

We use the same code as adder circuit

from blueqat import Circuit

#carry bit
def carry(a):
    return Circuit().ccx[a+1,a+2,a+3].cx[a+1,a+2].ccx[a,a+2,a+3]

#reverse carry bit
def carry_reverse(a):
    return Circuit().ccx[a,a+2,a+3].cx[a+1,a+2].ccx[a+1,a+2,a+3]

#sum bits
def sum(a):
    return Circuit().cx[a+1,a+2].cx[a,a+2] 

#reverse sum bits
def sum_reverse(a):
    return Circuit().cx[a,a+2].cx[a+1,a+2]

#decimal to binary
def tobinary(A):
    return bin(A)[2:] 

#arrange into the circuit
def digits(a,b): 
     aa = tobinary(a)  
     bb = tobinary(b)  
     alen = len(aa)  
     blen = len(bb)  
     maxlen = max(alen,blen) 
     if alen>blen: 
         bb = bb.zfill(alen) 
     elif blen>alen: 
         aa = aa.zfill(blen) 
  
     str = '' 
     for i in range(maxlen): 
         str += '0' + aa[maxlen-i-1] + bb[maxlen-i-1] 
     str += '0' 
     return str

#input bits to Xgate
def tox(a): 
     cir = Circuit(len(a)) 
     for i in range(len(a)): 
         if a[i] == "1": 
             cir += Circuit().x[i] 
     return cir

#binary to decimal
def todecimal(A):
    return int(str(A),2) 

#get the answer
def getb(result): 
     str = result[-1]
     digi = int((len(result)-1)/3) 
     for i in range(digi): 
         str += result[-2-i*3] 
     return todecimal(str)

Just do it.

def minus(a,ab): 
     qubits = len(digits(a,ab)) 
     cir1 = tox(digits(a,ab)) 
     digi = int((len(digits(a,ab))-1)/3) 

     cir4 = Circuit(qubits) 
     for i in range(digi-1): 
         cir4 += sum_reverse(i*3)
         cir4 += carry(i*3) 

     cir3 = sum_reverse((digi-1)*3) + Circuit(qubits).cx[-3,-2] 
 
     cir2 = Circuit(qubits)     
     for i in range(digi): 
         cir2 += carry_reverse((digi-1-i)*3)
 
     result = (cir1 + cir4 + cir3 + cir2).m[:].run(shots=1) 
     return getb(result.most_common()[0][0])

Let’s start calculation

It’s like a magic.

#8-2
minus(2,8)                                                                                                
6

#4-2
minus(2,4)                                                                                                
2

#16-2
minus(2,16)                                                                                               
14

#22-2
minus(2,22)                                                                                               
20

#50-2
minus(2,50)                                                                                               
48

#50-24
minus(24,50)                                                                                              
26

Discussion

If you can make adder, subtractor is just a reverse of it.

Recommend


CONTACT

info@mdrft.com

Back To Top

量子コンピュータ一般

量子ゲートアルゴリズム

量子アニーリング一般

量子アニーリングアルゴリズム

BlueqatSDKの使い方