Skip to content

Commit dc8a6f2

Browse files
committed
Bugfixes commutativity_tools
1 parent 5025d7d commit dc8a6f2

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/qrisp/operators/qubit/commutativity_tools.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def inverse_mod2(matrix):
6262
return result
6363

6464

65-
def gaussian_elimination_mod2(matrix, type='row', show_pivots=False):
65+
def gaussian_elimination_mod2(matrix, type='row', reduced=False, show_pivots=False):
6666
r"""
6767
Performs Gaussian elimination in the field $F_2$.
6868
@@ -71,8 +71,11 @@ def gaussian_elimination_mod2(matrix, type='row', show_pivots=False):
7171
matrix : numpy.array
7272
A binary matrix.
7373
type : str, optional
74-
Available aore ``row`` for row echelon form, and ``column`` for column echelon form.
74+
Available are ``row`` for row echelon form, and ``column`` for column echelon form.
7575
The default is ``row``.
76+
reduced : Boolean, optional
77+
If ``True``, the reduced row (column) echelon form is calculated.
78+
The default is ``False``.
7679
show_pivots : Boolean, optional
7780
If ``True``, the pivot columns (rows) are returned.
7881
@@ -106,14 +109,22 @@ def gaussian_elimination_mod2(matrix, type='row', show_pivots=False):
106109
# Swap the current row with the pivot row
107110
matrix[[row_index, max_row]] = matrix[[max_row, row_index]]
108111

109-
# Eliminate all rows below the pivot
112+
# Eliminate all rows after the pivot
110113
for j in range(row_index + 1, rows):
111114
if matrix[j, column_index] == 1:
112115
matrix[j] = (matrix[j] + matrix[row_index]) % 2
113116

114117
row_index+=1
115118
column_index+=1
116119

120+
# Backward elimination (optional, for reduced row echelon form)
121+
if reduced:
122+
for i in range(min(rows, columns) - 1, -1, -1):
123+
for j in range(i - 1, -1, -1):
124+
if matrix[j, i] == 1:
125+
matrix[j] = (matrix[j] + matrix[i]) % 2
126+
127+
117128
elif type=='column':
118129
while row_index<rows and column_index<columns:
119130
# Find the pivot column
@@ -128,16 +139,23 @@ def gaussian_elimination_mod2(matrix, type='row', show_pivots=False):
128139
# Swap the current column with the pivot column
129140
matrix[:,[column_index, max_column]] = matrix[:,[max_column, column_index]]
130141

131-
# Eliminate all rows below the pivot
142+
# Eliminate all columns after the pivot
132143
for j in range(column_index + 1, columns):
133144
if matrix[row_index, j] == 1:
134145
matrix[:,j] = (matrix[:,j] + matrix[:,column_index]) % 2
135146

136147
row_index+=1
137148
column_index+=1
138149

150+
# Backward elimination (optional, for column row echelon form)
151+
if reduced:
152+
for i in range(min(rows, columns) - 1, -1, -1):
153+
for j in range(i - 1, -1, -1):
154+
if matrix[j, i] == 1:
155+
matrix[:,j] = (matrix[:,j] + matrix[:,i]) % 2
156+
139157
if show_pivots:
140-
return matrix, pivots #(pivot_rows,pivot_cols)
158+
return matrix, pivots
141159
else:
142160
return matrix
143161

@@ -172,7 +190,7 @@ def construct_change_of_basis(S):
172190
# Step 0: Calculate S_0: Independent columns (i.e., Pauli terms) of S
173191
####################
174192

175-
S_reduced, independent_cols = gaussian_elimination_mod2(S, show_pivots=True)
193+
S_reduced, independent_cols = gaussian_elimination_mod2(S, reduced=True, show_pivots=True)
176194
k = len(independent_cols)
177195

178196
S0 = S[:,independent_cols]
@@ -199,8 +217,9 @@ def construct_change_of_basis(S):
199217
# Construct permutation achieving that the first k rows in X component of S1 are independent
200218
perm = np.arange(0,n)
201219
for index1, index2 in enumerate(independent_rows):
220+
curr = perm[index1]
202221
perm[index1] = index2
203-
perm[index2] = index1
222+
perm[index2] = curr
204223

205224
# Apply permutation to rows of S1 for Z and X component
206225
S1 = np.vstack((S1[:n,:][perm],S1[-n:,:][perm]))

src/qrisp/operators/qubit/qubit_operator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,6 @@ def get_operator_variance(self, n = 1):
13231323
for term, coeff in pauli_form.terms_dict.items():
13241324
if len(term.factor_dict) != 0:
13251325
var += abs(coeff)**2
1326-
print(coeff)
13271326
alpha_n = 1 - 1/(2**n + 1)
13281327

13291328
return var*alpha_n

0 commit comments

Comments
 (0)