/
qdqn_paper.py
170 lines (154 loc) · 6.72 KB
/
qdqn_paper.py
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
import gym
import numpy as np
import random
import matplotlib.pyplot as plt
import math
from collections import deque
import tensorflow as tf
import tensorflow_quantum as tfq
import cirq
import sympy
class QDQN(object):
def __init__(self, action_space, state_space) -> None:
super().__init__()
self.action_space = action_space
self.state_space = state_space
self.qubits = [cirq.GridQubit(0, i) for i in range(4)]
self.q_network = self.make_func_approx()
self.learning_rate = 0.01
self.opt = tf.keras.optimizers.Adam(lr=self.learning_rate)
self.buff = 10000
self.batch = 32
self.states = np.zeros((self.buff, self.state_space))
self.actions = np.zeros((self.buff, 1))
self.rewards = np.zeros((self.buff, 1))
self.dones = np.zeros((self.buff, 1))
self.next_states = np.zeros((self.buff, self.state_space))
# Q Learning
self.gamma = 0.99
self.epsilon = 1.0
self.epsilon_decay = 0.9
self.epsilon_min = 0.01
self.counter = 0
def make_func_approx(self):
readout_operators = [cirq.Z(self.qubits[i]) for i in range(2,4)]
inputs = tf.keras.Input(shape=(), dtype=tf.dtypes.string)
diff = tfq.differentiators.ParameterShift()
init = tf.keras.initializers.Zeros
pqc = tfq.layers.PQC(self.make_circuit(self.qubits), readout_operators, differentiator=diff, initializer=init)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=pqc)
return model
def convert_data(self, classical_data, flag=True):
ops = cirq.Circuit()
for i, ang in enumerate(classical_data):
ang = 0 if ang < 0 else 1
ops.append(cirq.rx(np.pi * ang).on(self.qubits[i]))
ops.append(cirq.rz(np.pi * ang).on(self.qubits[i]))
if flag:
return tfq.convert_to_tensor([ops])
else:
return ops
def one_qubit_unitary(self, bit, symbols):
return cirq.Circuit(
cirq.X(bit)**symbols[0],
cirq.Y(bit)**symbols[1],
cirq.Z(bit)**symbols[2])
def two_qubit_pool(self, source_qubit, sink_qubit, symbols):
pool_circuit = cirq.Circuit()
sink_basis_selector = self.one_qubit_unitary(sink_qubit, symbols[0:3])
source_basis_selector = self.one_qubit_unitary(source_qubit, symbols[3:6])
pool_circuit.append(sink_basis_selector)
pool_circuit.append(source_basis_selector)
pool_circuit.append(cirq.CNOT(control=source_qubit, target=sink_qubit))
pool_circuit.append(sink_basis_selector**-1)
return pool_circuit
def make_circuit(self, qubits):
m = cirq.Circuit()
symbols = sympy.symbols('q0:48') # 4 qubits * 3 weights per bit * 3 layers + 2 * 6 pooling = 36 + 12 = 48
m += self.layer(symbols[:12], qubits)
m += self.layer(symbols[12:24], qubits)
m += self.layer(symbols[24:36], qubits)
print(m)
m += self.two_qubit_pool(self.qubits[0], self.qubits[2], symbols[36:42])
m += self.two_qubit_pool(self.qubits[1], self.qubits[3], symbols[42:])
return m
def layer(self, weights, qubits):
l = cirq.Circuit()
for i in range(len(qubits) - 1):
l.append(cirq.CNOT(qubits[i], qubits[i+1]))
l.append([cirq.Moment([cirq.rx(weights[j]).on(qubits[j]) for j in range(4)])])
l.append([cirq.Moment([cirq.ry(weights[j + 4]).on(qubits[j]) for j in range(4)])])
l.append([cirq.Moment([cirq.rz(weights[j + 8]).on(qubits[j]) for j in range(4)])])
return l
def remember(self, state, action, reward, next_state, done):
i = self.counter % self.buff
self.states[i] = state
self.actions[i] = action
self.rewards[i] = reward
self.next_states[i] = next_state
self.dones[i] = int(done)
self.counter += 1
def get_action(self, obs):
if random.random() < self.epsilon:
return np.random.choice(self.action_space)
else:
return np.argmax(self.q_network.predict(self.convert_data(obs)))
#@tf.function
def train(self):
batch_indices = np.random.choice(min(self.counter, self.buff), self.batch)
state_batch = tfq.convert_to_tensor([self.convert_data(i, False) for i in self.states[batch_indices]])
action_batch = tf.convert_to_tensor(self.actions[batch_indices], dtype=tf.int32)
action_batch = [[i, action_batch[i][0]] for i in range(len(action_batch))]
reward_batch = tf.convert_to_tensor(self.rewards[batch_indices], dtype=tf.float32)
dones_batch = tf.convert_to_tensor(self.dones[batch_indices], dtype=tf.float32)
next_state_batch = tfq.convert_to_tensor([self.convert_data(i, False) for i in self.next_states[batch_indices]])
with tf.GradientTape() as tape:
next_q = self.q_network(next_state_batch)
next_q = tf.expand_dims(tf.reduce_max(next_q, axis=1), -1)
y = reward_batch + (1 - dones_batch) * self.gamma * next_q
q_guess = self.q_network(state_batch, training=True)
pred = tf.gather_nd(q_guess, action_batch)
pred = tf.reshape(pred, [self.batch, 1])
msbe = tf.math.reduce_mean(tf.math.square(y - pred))
grads = tape.gradient(msbe, self.q_network.trainable_variables)
self.opt.apply_gradients(zip(grads, self.q_network.trainable_variables))
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
iterations = 200
rolling_avg = 50
learn_delay = 1000
env = gym.make("CartPole-v1")
agent = QDQN(env.action_space.n, env.observation_space.shape[0])
rewards = []
avg_reward = deque(maxlen=iterations)
best_avg_reward = avg = -math.inf
rs = deque(maxlen=rolling_avg)
for i in range(iterations):
s1 = env.reset()
total_reward = 0
done = False
while not done:
#env.render()
action = agent.get_action(s1)
s2, reward, done, _ = env.step(action)
total_reward += reward
agent.remember(s1, action, reward, s2, done)
if agent.counter > learn_delay and done:
agent.train()
if done:
rewards.append(total_reward)
rs.append(total_reward)
else:
s1 = s2
avg = np.mean(rs)
avg_reward.append(avg)
if avg > best_avg_reward:
best_avg_reward = avg
print("\rEpisode {}/{} || Best average reward {}, Current Avg {}, Current Iteration Reward {}, eps {}".format(i, iterations, best_avg_reward, avg, total_reward, agent.epsilon), end='', flush=True)
plt.ylim(0, 200)
plt.plot(rewards, color='blue', alpha=0.2, label='Reward')
plt.plot(avg_reward, color='red', label='Average')
plt.legend()
plt.ylabel('Reward')
plt.xlabel('Iteration')
plt.show()