forked from cnktysz/HepTrkX-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QEN_evaluate.py
160 lines (142 loc) · 4.85 KB
/
QEN_evaluate.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
import numpy as np
import matplotlib.pyplot as plt
from qiskit import(
QuantumCircuit,
QuantumRegister,
ClassicalRegister,
execute,
BasicAer)
from qiskit.aqua.operator import Operator
from qiskit.aqua.components.initial_states import Zero
from qiskit.visualization import plot_histogram
from datasets.hitgraphs import HitGraphDataset
import sys
def TTN_edge_forward(B,theta_learn):
backend = BasicAer.get_backend('qasm_simulator')
q = QuantumRegister(len(B))
c = ClassicalRegister(1)
circuit = QuantumCircuit(q,c)
# STATE PREPARATION
for i in range(len(B)):
circuit.ry(B[i],q[i])
# APPLY forward sequence
circuit.ry(theta_learn[0],q[0])
circuit.ry(theta_learn[1],q[1])
circuit.cx(q[0],q[1])
circuit.ry(theta_learn[2],q[2])
circuit.ry(theta_learn[3],q[3])
circuit.cx(q[2],q[3])
circuit.ry(theta_learn[4],q[4])
circuit.ry(theta_learn[5],q[5])
circuit.cx(q[5],q[4]) # reverse the order
circuit.ry(theta_learn[6],q[1])
circuit.ry(theta_learn[7],q[3])
circuit.cx(q[1],q[3])
circuit.ry(theta_learn[8],q[3])
circuit.ry(theta_learn[9],q[4])
circuit.cx(q[3],q[4])
circuit.ry(theta_learn[10],q[4])
circuit.measure(q[4],c)
##circuit.draw(filename='png/QEN_circuit.png')
result = execute(circuit, backend, shots=100).result()
counts = result.get_counts(circuit)
out = 0
for key in counts:
if key=='1':
out = counts[key]/100
return(out)
sys.exit()
def normalize(B):
r_min_o = min(B[:,0])
r_min_i = min(B[:,3])
phi_min_o = min(B[:,1])
phi_min_i = min(B[:,4])
z_min_o = min(B[:,2])
z_min_i = min(B[:,5])
r_max_o = min(B[:,0])
r_max_i = max(B[:,3])
phi_max_o = max(B[:,1])
phi_max_i = max(B[:,4])
z_max_o = max(B[:,2])
z_max_i = max(B[:,5])
r_min = min(r_min_o,r_min_i)
r_max = max(r_max_o,r_max_i)
phi_min = min(phi_min_o,phi_min_i)
phi_max = max(phi_max_o,phi_max_i)
z_min = min(z_min_o,z_min_i)
z_max = max(z_max_o,z_max_i)
#print('r: ' + str(r_min) + ' - ' + str(r_max))
#print('phi: ' + str(phi_min) + ' - ' + str(phi_max))
#print('z: ' + str(z_min) + ' - ' + str(z_max))
# Map between 0 - 2PI
B[:,0] = 2*np.pi * (B[:,0]-r_min)/(r_max-r_min)
B[:,1] = 2*np.pi * (B[:,1]-phi_min)/(phi_max-phi_min)
B[:,2] = 2*np.pi * (B[:,2]-z_min)/(z_max-z_min)
B[:,3] = 2*np.pi * (B[:,3]-r_min)/(r_max-r_min)
B[:,4] = 2*np.pi * (B[:,4]-phi_min)/(phi_max-phi_min)
B[:,5] = 2*np.pi * (B[:,5]-z_min)/(z_max-z_min)
return B
def draw_sample(X, Ri, Ro, y, out,cmap='bwr_r', alpha_labels=True, figsize=(15, 7)):
# Select the i/o node features for each segment
feats_o = X[np.where(Ri.T)[1]]
feats_i = X[np.where(Ro.T)[1]]
edges = np.concatenate((feats_i,feats_o),axis=1)
# Prepare the figure
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=figsize)
cmap = plt.get_cmap(cmap)
# Draw the hits (r, z)
ax0.scatter(X[:,2], X[:,0], c='k')
ax1.scatter(X[:,2], X[:,0], c='k')
# Draw the segments
for j in range(y.shape[0]):
seg_args = dict(c='r', alpha=float(y[j]))
ax0.plot([feats_o[j,2], feats_i[j,2]],
[feats_o[j,0], feats_i[j,0]], '-', **seg_args)
seg_args = dict(c='r', alpha=float(out[j]))
ax1.plot([feats_o[j,2], feats_i[j,2]],
[feats_o[j,0], feats_i[j,0]], '-', **seg_args)
# Adjust axes
ax0.set_xlabel('$z$')
ax1.set_xlabel('$z$')
ax0.set_ylabel('$r$')
ax1.set_ylabel('$r$')
plt.tight_layout()
#plt.show()
plt.savefig('png/QEN_output_RvsZ.png')
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=figsize)
cmap = plt.get_cmap(cmap)
# Draw the hits (r, phi)
ax0.scatter(X[:,1], X[:,0], c='k')
ax1.scatter(X[:,1], X[:,0], c='k')
# Draw the segments
for j in range(y.shape[0]):
seg_args = dict(c='r', alpha=float(y[j]))
ax0.plot([feats_o[j,1], feats_i[j,1]],
[feats_o[j,0], feats_i[j,0]], '-', **seg_args)
seg_args = dict(c='r', alpha=float(out[j]))
ax1.plot([feats_o[j,1], feats_i[j,1]],
[feats_o[j,0], feats_i[j,0]], '-', **seg_args)
# Adjust axes
ax0.set_xlabel('$\phi$')
ax1.set_xlabel('$\phi$')
ax0.set_ylabel('$r$')
ax1.set_ylabel('$r$')
plt.tight_layout()
#plt.show()
plt.savefig('png/QEN_output_RvsPhi.png')
############################################################################################
input_dir = '/home/cenktuysuz/MyRepos/HepTrkX-quantum/data/hitgraphs'
theta_learn = [5.81938258, 0.65791055, 3.50325001, 5.99779941, 2.18404964, 0.03780523, 0.12155696, 3.44766096, 5.7402678, 4.45497403, 2.91924544]
data = HitGraphDataset(input_dir, 1)
X,Ro,Ri,y = data[0]
n_edges = len(y)
out = np.zeros(n_edges)
bo = np.dot(Ro.T, X)
bi = np.dot(Ri.T, X)
B = np.concatenate((bo,bi),axis=1)
B = normalize(B)
epoch=n_edges
for i in range(epoch):
out[i] = round(TTN_edge_forward(B[i],theta_learn))
# Plot the results
draw_sample(X, Ri, Ro, y, out)