-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSparse.py
345 lines (288 loc) · 10.5 KB
/
Sparse.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
344
345
"""
This module includes all of the implementation for creating Sparse and Lazy Matrices.
"""
from MatrixInterface import MatrixElement, Matrix, Vector, SquareMatrix
import numpy as np
def makeSparse(matrix):
"""
Converts dense matrix into sparse matrix in (row, column, value) form
:param matrix: (list) Matrix to be converted to Sparse
:return: (list) Sparse matrix
"""
n = matrix[0].size
elements = []
for i in range(n):
for j in range(n):
if matrix[i][j] != 0 :
temp = MatrixElement(i, j, matrix[i][j])
elements.append(temp)
return SparseMatrix(n, elements)
class ColumnElement():
"""
Column element ina sparse matrix
"""
def __init__(self, j, val):
self.Row = j
self.Val = complex(val)
class SparseMatrix(Matrix, object):
"""
Creates sparse matrix, assumes they are square matrices
:param n: (int) dimensions of matrix
:param elements: (list) objects the requisite elements of the matrix
"""
def __init__(self, n, elements):
super().__init__(n, elements)
def multiply(self, b):
"""
Multiplies matrix with some other matrix b, will make this apply to none sparse matrices
can be called by A*b where A is a sparse matrix
:param b: SparseMatrix()
:return: (list) the product of two matrices
"""
assert(self.Dimension == b.Dimension)
p = []
for meb in b.Elements:
for mea in self.Elements:
if mea.j == meb.i:
temp = mea.val * meb.val
temp = MatrixElement(mea.i, meb.j, temp)
p.append(temp)
p = SparseMatrix(len(p), p)
#print(p)
return p
def apply(self, v):
"""
Applies the sparse Matrix to some vector V
:param v: some vector of the Vector() class
:return: (list) The resultant vector from applying the matrix to v
"""
u = np.zeros(self.Dimension, dtype=complex)
for me in self.Elements:
for index in range(v.Elements.size):
if index == me.j:
u[me.i] += me.val * v.Elements[index]
u = Vector(u)
return u
def makedense(self):
"""
makes a dense matrix
"""
M = np.zeros((self.Dimension, self.Dimension), dtype= complex)
for me in self.Elements:
M[me.i][me.j] = me.val
return M
def tensorProduct(self, a):
"""
Returns the tensor product of two matrices,
currently applies to two sparse matrices
:param a: (list) sparse matrix to operate on
:return: (list) result of tensor product
"""
assert (type(a) == SparseMatrix), 'Incompatible Matrices'
elements = []
dimension = self.Dimension * a.Dimension
for me1 in self.Elements:
for mea in a.Elements:
row = me1.i*a.Dimension + mea.i
col = me1.j*a.Dimension + mea.j
value = complex(me1.val * mea.val)
elements.append(MatrixElement(int(row), int(col), complex(value)))
return SparseMatrix(dimension, elements)
def __str__(self):
temp = ''
for element in self.Elements:
temp += f'{element}\n'
return temp
class ColMatrix(SquareMatrix):
"""
A type of sparse matrix extending the Square Matrix class
"""
def __init__(self, dims):
"""
Constructor
:param dims: (int) dimension of the matrix
"""
super().__init__(dims)
self.Columns = [[] for i in range(dims)]
def __setitem__(self, pos, val):
"""
Sets a specific item in the matrix
:param pos: (tuple) Position of the item to be set
:param val: (complex) Value to set the item to
"""
row, col = pos
if len(self.Columns[col])==0:
self.Columns[col].append(ColumnElement(row, val))
elif self.Columns[col][-1].Row < row:
self.Columns[col].append(ColumnElement(row, val))
else:
for index, element in enumerate(self.Columns[col]):
if element.Row == row:
self.Columns[col][index] = ColumnElement(row, val)
break
elif element.Row >= row:
self.Columns.insert(index-1, ColumnElement(row, val))
break
def __getitem__(self, pos):
"""
Gets a specific item from the matrix
:param pos: (tuple) Position of the item to acquire
:return: (complex) number at that position in the matrix
"""
row, col = pos
if len(self.Columns[col])==0:
return complex(0)
for element in self.Columns[col]:
if element.Row == row: return element.Val
if element.Row >= row: return complex(0)
return complex(0)
def __str__(self):
toPrint = ''
for c, column in enumerate(self.Columns):
for element in column:
toPrint += f'{element.Row}, {c}, {element.Val} \n'
return toPrint
def __iter__(self):
for col, column in enumerate(self.Columns):
for element in column:
yield MatrixElement(element.Row, col, element.Val)
def tensorProduct(self, otherMatrix):
"""
Calculates the tensor product of two Column Matrices.
:param otherMatrix: (ColMatrix) the matrix on the right hand side of the tensor product
:return newMatrix: (ColMatrix) new matrix representing the tensor product
"""
newMatrix = ColMatrix(self.Dimension*otherMatrix.Dimension)
for col1, column in enumerate(self.Columns):
for element in column:
for col2, othercolumn in enumerate(otherMatrix.Columns):
for otherElement in othercolumn:
newMatrix[element.Row*otherMatrix.Dimension+otherElement.Row, col1*otherMatrix.Dimension + col2] = element.Val*otherElement.Val
return newMatrix
def toDense(self):
"""
Creates a dense numpy matrix representation of the matrix
:return dense: (numpy.ndarray) Numpy array representing the matrix
"""
dense = np.zeros((self.Dimension, self.Dimension), dtype=complex)
for col, column in enumerate(self.Columns):
for element in column:
dense[element.Row, col] = element.Val
return dense
def multiply(self, m):
"""
Multiplies self with another matrix
:param m: (ColMatrix) other matriix on the right hand side of the multiplication
:return: (ColMatrix) Product of the multiplication
"""
p = ColMatrix(self.Dimension)
for me in m:
column = self.Columns[me.Row]
for ce in column:
p[ce.Row, me.Col] += ce.Val*me.Val
return p
class LazyMatrix(SquareMatrix):
"""
Creates a lazy matrix
"""
def __init__(self, dims):
"""
Initialiser
:param dims: (int) dimension of the matrix
"""
super().__init__(dims)
self.Cache = None
def multiply(self, m):
"""
This operation is useless in our case and doesn't actually work, so ... yeah
:param m: (LazyMatrix) matrix to multiply by (perhaps implemented in the future)
:return:
"""
assert self.Dimension == m.Dimension, 'Incompatible dimensions'
pass
def __getitem__(self, pos):
if len(self.Cache)==0:
self.Evaluate()
return self.Cache[pos]
def __setitem__(self, pos, val):
print('cannot set elemtent of lazymatrix')
def Evaluate(self):
"""
Evaluates the entire matrix, not recommended to ever call it. Takes a long time and is useless for our purposes
Puts the evaluated ColMatrix into self.Cache
"""
cache = ColMatrix(self.Dimension)
for col in range(self.Dimension):
basisElement = Vector(np.zeros(self.Dimension))
basisElement[col] = 1
column = self.apply(basisElement)
for row in range(self.Dimension):
cache[row, col] = column[row]
self.Cache = cache
def __str__(self):
if self.Cache==None:
self.Evaluate()
return self.Cache.__str__()
class Gate(LazyMatrix):
"""
Lazy representation of a quantum gate
"""
def __init__(self, dims, sm, qbpos):
"""
Initialises Gate
:param dims: (int) dimensions of the large gate
:param sm: (ColMatrix) The sparse matrix representing the quantum gate
:param qbpos: (list) The qubits which the gate is applied to
"""
super().__init__(dims)
self.SM = sm
self.smDim = sm.Dimension
self.qbpos = np.array(qbpos)
def apply(self, v):
"""
Applies the Gate to a vector
:param v: (array or Vector) The vector the gate will be applied to, must be the same dimesion as self
:return w: (Vector) The result of the application of the gate
"""
w = Vector(np.zeros(self.Dimension))
for i in range(self.Dimension):
r = self.__gather(i)
i0 = i & ~self.__scatter(r)
for c in range(self.smDim):
j = i0 | self.__scatter(c)
w[i] += self.SM[r,c] * v[j]
return w
def __gather(self, i):
"""
Magic method
:param i: (int) The row number of the gate
:return j: (int)
"""
j = 0
for k in range(len(self.qbpos)):
j |= ((i >> self.qbpos[k]) & 1) << k
return j
def __scatter(self, j):
"""
Magic method
:param i: (int)
:return j: (int)
"""
i = 0
for k in range(len(self.qbpos)):
i |= ((j>>k)&1) << self.qbpos[k]
return i
def toColMat(mat):
"""
Creates a ColMatrix representation from an numpy array
:param mat: (numpy.ndarray) Array to be converted
:return: (ColMatrix) Column matrix representation of mat
"""
mat = np.array(mat, dtype=complex)
toReturn = ColMatrix(mat[0].size)
for i in range(mat[0].size):
for j in range(mat[0].size):
if mat[i,j] != 0: toReturn[i,j] = mat[i,j]
return toReturn
if __name__ == "__main__":
pass