-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetsyncanalysisv2eigen.py
343 lines (204 loc) · 8.19 KB
/
netsyncanalysisv2eigen.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 13 13:30:10 2021
@author: cosmi
"""
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import networkx as nx
from math import pi
import numpy as np
import scipy
import numpy as np
from scipy import misc
import numpy as np
import scipy.linalg as la
from matplotlib import pyplot as plt # For image viewing
from matplotlib import colors
from matplotlib import ticker
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
from random import random as rand
from random import uniform
from qutip.visualization import plot_wigner, hinton
#qutip star imports
from qutip import *
from qutip import Qobj, rand_dm, fidelity, displace, qdiags, qeye, expect
from qutip.states import coherent, coherent_dm, thermal_dm, fock_dm
from qutip.wigner import qfunc
from pygsp import graphs
#def gridsize(val):
# '''
# Number Of Particles in a Grid Shall Be Entered such that gridsize 4 = 4 x 4 i.e. 16
# Particles in Total. Note: this can only be changed at the start of a new
# Simulation Run - In This Version Do Note Change While Running the Simulation!
# '''
# global n
# n = int(val)
# return val
def initialize():
global g, nextg, hilbert_size
n = 2
g = nx.grid_graph(dim=[n,n])
#g = nx.karate_club_graph()
for i in list(g.nodes()):
g.node[i]['theta'] = 2 * pi * random()
#rows, cols = (-0.05, 0.05)
#arr = [[rand.randrange(10) for i in range(int(cols))] for j in range(int(rows))]
#a = numpy.asarray(arr)
#g.node[i]['omega'] = 1. + rand.uniform(-0.05, 0.05)
g.node[i]['omega'] = 1. + uniform(-0.05, 0.05)
nextg = g.copy()
for i in list(g.nodes()):
g.node[i]['theta'] = random()
nextg = g.copy()
grid2d = graphs.Graph.from_networkx(nextg)
#print(grid2d.W.toarray())
#print(grid2d.signals)
#print(grid2d)
grid2d.compute_fourier_basis()
grid2d.set_coordinates()
# plot spectrum
fig, ax = plt.subplots(1, 1, figsize=(7,7))
ax.plot(grid2d.e)
ax.set_xlabel('eigenvalue index (i)')
ax.set_ylabel('eigenvalue ($\lambda_{i}$)')
ax.set_title('2D-grid spectrum');
#fiedler vector highlighted graph
grid2d.plot_signal(grid2d.U[:,1])
#plot all eigenvectors as network graph frames
fig, axes = plt.subplots(2, 3, figsize=(10, 6.6))
count = 0
for j in range(2):
for i in range(2):
grid2d.plot_signal(grid2d.U[:, count*1], ax=axes[j,i],colorbar=False)
axes[j,i].set_xticks([])
axes[j,i].set_yticks([])
axes[j,i].set_title(f'Eigvec {count*1+1}')
count+=1
fig.tight_layout()
#hilbert space must be the same as the network size for this to make sense
alphas = grid2d.signals['omega']
print(alphas)
betas = grid2d.signals['theta']
print(betas)
hilbert_size = n
psi = coherent(hilbert_size, 0)
rho = coherent_dm(hilbert_size, 1-1j)
d = displace(hilbert_size, 2+2j)
#psi = sum([coherent_dm(hilbert_size, a) for a in alphas])
psi = psi.unit()
rho = psi*psi.dag()
fig, ax = plt.subplots(1, 4, figsize=(19, 4))
plot_wigner_fock_distribution(psi, fig=fig, axes=[ax[0], ax[1]])
plot_wigner_fock_distribution(d*psi, fig=fig, axes=[ax[2], ax[3]])
ax[0].set_title(r"Initial state, $\psi_{vac} = |0 \rangle$")
ax[2].set_title(r"Displaced state, $D(\alpha=2+2i )\psi_{vac}$")
plt.show()
fig, ax = plot_wigner_fock_distribution(rho, figsize=(9, 4))
ax[0].set_title("Superposition of three coherent states")
plt.show()
measured_populations = [measure_population(b, rho) for b in betas]
def observe():
global g, nextg, grid2d
subplot(1,2,1)
cla()
nx.draw(g, cmap = cm.hsv, vmin = -1, vmax = 1,
node_color = [np.sin(g.node[i]['theta']) for i in list(g.nodes())],
pos = nx.spring_layout(g) )
axis('image')
subplot(1,2,2)
cla()
plot([np.cos(g.node[i]['theta']) for i in list(g.nodes())],
[np.sin(g.node[i]['theta']) for i in list(g.nodes())], '.')
axis('image')
axis([-1.1,1.1,-1.1,1.1])
#subplot(1,2,2)
#cla()
#plot(xdata, ydata,'o',alpha = 0.05)
#axis('image')
# for space vs time plotting (chimera search)
alpha = 2 # coupling strength
beta = 1 # acceleration rate
Dt = 0.01 # Delta t
#def update():
# global g, nextg
# for i in list(g.nodes()):
# theta_i = g.node[i]['theta']
# nextg.node[i]['theta'] = theta_i + (beta * theta_i + alpha * (np.sum(sin(g.node[j]['theta'] - theta_i) for j in g.neighbors(i))) * Dt)
# g, nextg = nextg, g
def measure_population(beta, rho):
"""
Measures the photon number statistics for state rho when displaced
by angle alpha.
Parameters
----------
alpha: np.complex
A complex displacement.
rho:
The density matrix as a QuTiP Qobj (`qutip.Qobj`)
Returns
-------
population: ndarray
A 1D array for the probabilities for populations.
"""
hilbertsize = rho.shape[0]
# Apply a displacement to the state and then measure the diagonals.
D = displace(hilbertsize, beta)
rho_disp = D*rho*D.dag()
populations = np.real(np.diagonal(rho_disp.full()))
return populations
def update():
global g, nextg, eig_values, eig_vectors, rho, grid2d, theta_i
for i in list(g.nodes()):
theta_i = g.node[i]['theta']
omega_i = g.node[i]['omega']
nextg.node[i]['theta'] = theta_i + (g.node[i]['omega'] + alpha * ( \
sum(np.sin(g.node[j]['theta'] - theta_i) for j in g.neighbors(i)) \
/ g.degree(i))) * Dt
g, nextg = nextg, g
#for i, j in list(g.nodes()):
#xdata.append(g.degree(i))
#ccs = nx.connected_components(g)
#ydata.append(max(len(cc) for cc in ccs))
#xdata.append(g.degree(i)); ydata.append(g.degree(j))
#xdata.append(g.degree(j)); ydata.append(g.degree(i))
A = nx.adjacency_matrix(nextg)
print(A)
n, m = A.shape
diags = A.sum(axis=0) # 1 = outdegree, 0 = indegree
D = scipy.sparse.spdiags(diags.flatten(), [0], m, n, format="csr")
L = (A-D)
Lap = L.todense()
print(Lap)
eig_values, eig_vectors = la.eig(Lap)
fiedler_pos = np.where(eig_values.real == np.sort(eig_values.real)[1])[0][0]
fiedler_vector = np.transpose(eig_vectors)[fiedler_pos]
#print("Fiedler value: " + str(fiedler_pos.real))
# this will be an eigenbra version
print("Fiedler vector: " + str(fiedler_vector.real))
#nx.laplacian_matrix(nextg).toarray()
# applying matrix.trace() method
LTrace = np.matrix.trace(Lap)
#print(LTrace)
#print density matrix from graph laplacian
rho = np.divide(Lap,LTrace)
#we need to represent this graph density matrix as a cavity reduced density matrix to make physical sense
#print(rho)
print(theta_i)
print(omega_i)
#note you can calculate the trace faster using the hadamard product (element-wise multiplication)
# using the fiedler vector as the basis for the emergent density matrix
import pycxsimulator
pycxsimulator.GUI().start(func=[initialize, observe, update])
#plt.figure(1)
#compare red and blue pixel data
#nbins = 20
#plt.hexbin(x=plot_time_stamp, y=plot_agent, gridsize=nbins, cmap=plt.cm.jet)
#plt.xlabel('Blue Reflectance')
#plt.ylabel('NIR Reflectance')
# Add a title
#plt.title('NIR vs Blue Spectral Data')
#plt.show()