-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,15 +40,14 @@ def construct_unitary_F(fdef, in_size=None): | |
| cmatrix = np.zeros([2**reg_size,2**reg_size]) | ||
| for base, i_col in zip(dummy_bases, range(0,2**reg_size,2)): | ||
| if base in fdef: | ||
| target_state = int(base+"1",2) | ||
| target_state = int(f"{base}1", 2) | ||
| cmatrix[target_state,i_col] = 1 | ||
| target_state = int(base+"0",2) | ||
| cmatrix[target_state,i_col+1] = 1 | ||
| target_state = int(f"{base}0", 2) | ||
| else: | ||
| target_state = int(base+"0",2) | ||
| target_state = int(f"{base}0", 2) | ||
| cmatrix[target_state,i_col] = 1 | ||
| target_state = int(base+"1",2) | ||
| cmatrix[target_state,i_col+1] = 1 | ||
| target_state = int(f"{base}1", 2) | ||
| cmatrix[target_state,i_col+1] = 1 | ||
|
Comment on lines
-43
to
+50
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return MGate(cmatrix,reg_size) | ||
|
|
||
| class Gate(): | ||
|
|
@@ -87,7 +86,10 @@ def apply(self, qreg): | |
| ---------- | ||
| new_qreg : QReg object created after transformation | ||
| """ | ||
| assert qreg.nbits == self.qreg_size, "This gate cannot be applied to register of size {}. Expected size: {}".format(qreg.nbits, self.qreg_size) | ||
| assert ( | ||
| qreg.nbits == self.qreg_size | ||
| ), f"This gate cannot be applied to register of size {qreg.nbits}. Expected size: {self.qreg_size}" | ||
|
|
||
|
Comment on lines
-90
to
+92
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise NotImplementedError("Apply method not implemented yet!") | ||
|
|
||
| class Sequence(Gate): | ||
|
|
@@ -99,10 +101,7 @@ def __init__(self, qreg_size): | |
| super(Sequence, self).__init__(qreg_size) | ||
|
|
||
| def add(self, other): | ||
| if isinstance(other, Sequence): | ||
| self.seq += other.seq | ||
| else: | ||
| self.seq += [other] | ||
| self.seq += other.seq if isinstance(other, Sequence) else [other] | ||
|
Comment on lines
-102
to
+104
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def apply(self, qreg): | ||
| tqreg = qreg.copy() | ||
|
|
@@ -211,14 +210,14 @@ def __init__(self, matrix, qbpos, qreg_size): | |
| *qreg_size* : size of register it will act on | ||
| """ | ||
| super().__init__(qreg_size) | ||
|
|
||
| self.matrix = matrix | ||
| self.msize = 2**len(qbpos) | ||
| self.qsize = 2**qreg_size | ||
| self.bases = np.arange(self.qsize) | ||
|
|
||
| self.qbpos = qbpos | ||
| self.specpos = list(filter(lambda x : not x in qbpos, range(qreg_size))) | ||
| self.specpos = list(filter(lambda x: x not in qbpos, range(qreg_size))) | ||
|
Comment on lines
-214
to
+220
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # time to shuffle indices | ||
| # first group the indices which has same spectators | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,10 +90,10 @@ def __len__(self): | |
| def __repr__(self): | ||
| """print state of qreg as a|00..0> + b|00..1> + ... | ||
| """ | ||
| representation = "" | ||
| for coeff, base in zip(self.state, self.bases): | ||
| representation += "{0:.2f} \t |{1}> \n".format(coeff, base) | ||
| return representation | ||
| return "".join( | ||
| "{0:.2f} \t |{1}> \n".format(coeff, base) | ||
| for coeff, base in zip(self.state, self.bases) | ||
| ) | ||
|
Comment on lines
-93
to
+96
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def get_qbit(self, i_qbit): | ||
| """Get state of ith qbit | ||
|
|
@@ -102,4 +102,6 @@ def get_qbit(self, i_qbit): | |
| state_1_proba = (state_1_coeffs**2).sum() | ||
| state_0_coeffs = self.state[[b[i_qbit] == '0' for b in self.bases]] | ||
| state_0_proba = (state_0_coeffs**2).sum() | ||
| return "{}|0> + {}|1>".format(state_0_proba, state_1_proba), QBit([state_0_proba, state_1_proba]) | ||
| return f"{state_0_proba}|0> + {state_1_proba}|1>", QBit( | ||
| [state_0_proba, state_1_proba] | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,18 +19,16 @@ def func(self): | |
| print(M.gcd(self.a,self.N)) | ||
| if M.gcd(self.a,self.N) == 1: | ||
| self.QFT() | ||
| if self.period % 2: | ||
| break | ||
| else: | ||
| if not self.period % 2: | ||
| self.root1 = M.gcd(int(self.a**(self.period/2)+1),self.N) | ||
| self.root2 = M.gcd(int(self.a**(self.period/2)-1),self.N) | ||
| break | ||
| break | ||
|
Comment on lines
-22
to
+25
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| else: | ||
| self.root3 = self.N/self.gcd | ||
|
|
||
|
|
||
| def generateSequence(self): | ||
| for i in range(0,self.Q): | ||
| for i in range(self.Q): | ||
|
Comment on lines
-33
to
+31
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self.seqList.append([i, self.a**i % self.N]) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,8 @@ def test_smatrix(): | |
| sM = sparseMatrix(3,4) | ||
|
|
||
|
|
||
| for i in range(0,3): | ||
| for j in range(0,3): | ||
| for i in range(3): | ||
| for j in range(3): | ||
|
Comment on lines
-21
to
+22
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if i%2 == 0: | ||
| sN.elements.append(element(i,j,1)) | ||
| if j%2 == 0: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,8 +43,8 @@ def dot(self, vector): | |
| def multiplySparseMatrix(self,that): | ||
| elements = [] | ||
| assert self.colSize == that.rowSize,"Matrix Sizes Are Incompatible!!!" | ||
| for i in range(0,self.rowSize): | ||
| for j in range(0,that.colSize): | ||
| for i in range(self.rowSize): | ||
| for j in range(that.colSize): | ||
|
Comment on lines
-46
to
+47
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| values = [] | ||
| for n in self.elements: | ||
| for m in that.elements: | ||
|
|
@@ -65,30 +65,29 @@ def convertToSparse(matrix): | |
| _ = matrix.shape[1] | ||
| except IndexError: | ||
| matrix = matrix.reshape([-1,1]) | ||
| new = SparseMatrix(matrix.shape[0],matrix.shape[1]) | ||
| for i in range(0,matrix.shape[0]): | ||
| for j in range(0,matrix.shape[1]): | ||
| new = SparseMatrix(matrix.shape[0],matrix.shape[1]) | ||
| for i in range(matrix.shape[0]): | ||
| for j in range(matrix.shape[1]): | ||
|
Comment on lines
-68
to
+70
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if matrix[i][j] != 0: | ||
| new.elements.append(element(i,j,matrix[i][j])) | ||
| return new | ||
|
|
||
| def scalarMultiply(self,scalar): | ||
| for i in range(0,len(self.elements)): | ||
| for i in range(len(self.elements)): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self.elements[i].value = scalar*self.elements[i].value | ||
| @staticmethod | ||
| def kronSparse(this,that): | ||
| new1 = [] | ||
| for element in this.elements: | ||
| new = copy.deepcopy(that) | ||
| for i in range(0,len(new.elements)): | ||
| for i in range(len(new.elements)): | ||
| new.elements[i].value = element.value*new.elements[i].value | ||
| cornerRow = element.rowIndex*that.rowSize | ||
| cornerCol = element.colIndex*that.colSize | ||
| for i in range (0,len(new.elements)): | ||
| for i in range(len(new.elements)): | ||
| new.elements[i].rowIndex += cornerRow | ||
| new.elements[i].colIndex += cornerCol | ||
| for element in new.elements: | ||
| new1.append(element) | ||
| new1.extend(iter(new.elements)) | ||
|
Comment on lines
-83
to
+90
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| kronProd = SparseMatrix(this.rowSize*that.rowSize, this.colSize*that.colSize) | ||
| kronProd.elements = new1 | ||
| return kronProd | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
mainrefactored with the following changes:use-fstring-for-concatenation)assign-if-exp)