File size: 7,381 Bytes
1f8ee4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import numpy as np
from qiskit import BasicAer, QuantumCircuit, QuantumRegister, ClassicalRegister, execute
from qiskit import IBMQ
# provider = IBMQ.load_account()


# def misere_step(ones,piles):
#     # even number of piles of 1 eg (1,1,3,0) or (0,0,3,0)
#     if ones%2 == 0:
#         objects_to_remove = []
#         removable_amount = 1
#         for i in range(len(piles)):
#             if piles[i] > 1:
#                 objects_to_remove.append(piles[i]-1)
#             else:
#                 objects_to_remove.append(0)
#     # odd number of piles of 1 eg (1,1,3,1)
#     else:
#         objects_to_remove = []
#         removable_amount = 1
#         for i in range(len(piles)):
#             if piles[i] > 1:
#                 objects_to_remove.append(piles[i])
#             else:
#                 objects_to_remove.append(0)
#     return objects_to_remove, removable_amount

def get_piles_to_remove(piles):
    nim_sum = 0
    for p in piles:
        nim_sum = nim_sum ^ p
    objects_to_remove = []
    removable_amount = 0
    for p in piles:
        new_p = p^nim_sum
        if new_p < p:
            objects_to_remove.append(p-new_p)
            removable_amount = removable_amount + 1
        else:
            objects_to_remove.append(0)
    return objects_to_remove, removable_amount

    
def custom_qft(data_qubits):
    qr_data = QuantumRegister(data_qubits)
    qc = QuantumCircuit(qr_data)
    i = data_qubits
    while i>=1:
        n = i - 1
        qc.h(qr_data[n]) 
        for qubit in range(n):
            qc.cp(np.pi/2**(n-qubit), qr_data[qubit], qr_data[n])
        i = i-1
    return qc

def subroutine_add_const(data_qubits: int, const: int, to_gate=True):
    qc = QuantumCircuit(data_qubits)
    for i in range(data_qubits):
        angle = const*np.pi/(2**i)
        qc.p(angle,i)
    return qc.to_gate(label=" ["+str(const)+"] ") if to_gate else qc

def diffusion_operation(qc, address, flag, removable_pile):
    def nim_oracle(qc,address,flag,removable_pile):

        # 0001 -> 001
        if removable_pile[0] != 0:
            qc.x(address[1])
            qc.x(address[2])
            qc.mct(address[:],flag)
            qc.x(address[2])
            qc.x(address[1])
        
        # 0010 -> 010
        if removable_pile[1] != 0:
            qc.x(address[0])
            qc.x(address[2])
            qc.mct(address[:],flag)
            qc.x(address[2])
            qc.x(address[0])
    
        # 0100 -> 011
        if removable_pile[2] != 0:
            qc.x(address[2])
            qc.mct(address[:],flag)
            qc.x(address[2])
        
        # 1000 -> 100
        if removable_pile[3] != 0:
            qc.x(address[0])
            qc.x(address[1])
            qc.mct(address[:],flag)
            qc.x(address[1])
            qc.x(address[0])


    qc.x(flag)
    qc.h(flag)

    qc.h(address[:])
    nim_oracle(qc,address,flag,removable_pile)
    qc.h(address[:])
    qc.x(address[:])
    qc.h(address[2])
    qc.mct(address[0:2], address[2])
    qc.h(address[2])
    qc.x(address[:])
    qc.h(address[:])

    
def qc_process(qc,objects_to_remove,address,flag,piles,removable_pile,removable_count):

    if removable_count == 0:
        for i in range(len(removable_pile)):
            if piles[i] > 0:
                removable_pile[i] = 1
                removable_count += 1

    if removable_count == 4:
        removable_pile[removable_pile.index(min(removable_pile))] = 0
        removable_count = removable_count - 1


    qft_gate = custom_qft(3).to_gate()
    inverse_qft_gate = custom_qft(3).inverse().to_gate()

    if removable_count == 1:
        qc.swap(objects_to_remove[0],objects_to_remove[2])
        qc.append(qft_gate,objects_to_remove[:])
        # 0001 -> 001
        if removable_pile[0] != 0:
            add_gate = subroutine_add_const(3,removable_pile[0])
            qc.x(address[0])
        # 0010 -> 010
        elif removable_pile[1] != 0:
            add_gate = subroutine_add_const(3,removable_pile[1])
            qc.x(address[1])
        # 0100 -> 011
        elif removable_pile[2] != 0:
            add_gate = subroutine_add_const(3,removable_pile[2])
            qc.x(address[0])
            qc.x(address[1])
        # 1000 -> 100
        elif removable_pile[3] != 0:
            add_gate = subroutine_add_const(3,removable_pile[3])
            qc.x(address[2])

        qc.append(add_gate,objects_to_remove[:])
        qc.append(inverse_qft_gate,objects_to_remove[:])
        qc.swap(objects_to_remove[0],objects_to_remove[2])

    else:
        diffusion_operation(qc,address, flag, removable_pile)
        qc.swap(objects_to_remove[0],objects_to_remove[2])
        qc.append(qft_gate,objects_to_remove[:])
        for i,remove_amount in enumerate(removable_pile):
            if remove_amount != 0:

                bin_i = list(bin(i+1)[2:])
                while len(bin_i) != 3:
                    bin_i.insert(0,'0')
                bin_i = bin_i[::-1]
                for j in range(len(bin_i)):
                    if bin_i[j] == '0':
                        qc.x(address[j])

                controlled_add_gate = subroutine_add_const(3,remove_amount).control(3)    
                qc.append(controlled_add_gate,address[:]+objects_to_remove[:])

                for j in range(len(bin_i)):
                    if bin_i[j] == '0':
                        qc.x(address[j])

        qc.append(inverse_qft_gate,objects_to_remove[:])
        qc.swap(objects_to_remove[0],objects_to_remove[2])

def get_quantum_move(piles, backend=None):

    # REMOVE MISERE STEP
    # ones = piles.count(1)
    # zeros = piles.count(0)
    # non_zeros = 4 - (ones+zeros)

    # # all zeros except one eg (0,0,0,7) OR some zeros some ones some non_zeros
    # # leave odd piles of 1s
    # if non_zeros == 1: 
    #     removable_pile, removable_count = misere_step(ones, piles) 
    # else:
    #     removable_pile, removable_count = get_piles_to_remove(piles)


    removable_pile, removable_count = get_piles_to_remove(piles)
    objects_to_remove = QuantumRegister(3,'piles')
    flag = QuantumRegister(1,'flag')
    output_piles = ClassicalRegister(3,'final_piles')
    address = QuantumRegister(3,'address')
    pick_pile = ClassicalRegister(3,'choose_pile')
    qc = QuantumCircuit(objects_to_remove,address,flag,output_piles,pick_pile)
    qc_process(qc,objects_to_remove,address,flag,piles,removable_pile,removable_count)

    qc.measure(address[:],pick_pile[:])
    qc.measure(objects_to_remove[:],output_piles[:])
    
    if backend == None:
        backend = BasicAer.get_backend('qasm_simulator')
        # backend = provider.backends.ibmq_qasm_simulator
    job = execute(qc,backend,shots=500)
    result = job.result()
    counts = result.get_counts()

    try:
        qc_move = (counts.most_frequent())    
    except Exception as e:
        print(e)
        vals = list(dict(counts).values())
        max_count = max(vals,key=vals.count)
        for key in counts:
            if counts[key] == max_count:
                qc_move = key
                break
    
    board_choice = qc_move.split(' ')[0]
    board_choice = int(board_choice,2) - 1

    print("Pick from:",board_choice+1)

    board_state = qc_move.split(' ')[1]
    board_state = board_state[::-1]
    amount = int(board_state,2)
    print("Amount:", amount)
    return board_choice,amount