Skip to content

Commit cd356f5

Browse files
authored
Avoid inefficient list construction in qubit_tapering_from_stabilizer.py (#1262)
This replaces some inefficient `list += [item]` patterns with `list.append(item)` in `_reduce_terms` and `_reduce_terms_keep_length` of the file `qubit_tapering_from_stabilizer.py`. This alternative approach avoids unnecessary temporary list creation and is a standard Python optimization for iterative list building.
1 parent 5a5e67a commit cd356f5

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/openfermion/transforms/repconversions/qubit_tapering_from_stabilizer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _reduce_terms(terms, stabilizer_list, manual_input, fixed_positions):
168168
# Find first position non-fixed position with non-trivial Pauli.
169169
for qubit_pauli in selected_stab:
170170
if qubit_pauli[0] not in fixed_positions:
171-
fixed_positions += [qubit_pauli[0]]
171+
fixed_positions.append(qubit_pauli[0])
172172
fixed_op = qubit_pauli[1]
173173
break
174174

@@ -191,11 +191,11 @@ def _reduce_terms(terms, stabilizer_list, manual_input, fixed_positions):
191191
)
192192
updated_stabilizers = []
193193
for update_stab in stabilizer_list[1:]:
194-
updated_stabilizers += [
194+
updated_stabilizers.append(
195195
fix_single_term(
196196
update_stab, fixed_positions[i], fixed_op, other_op, stabilizer_list[0]
197197
)
198-
]
198+
)
199199

200200
# Update terms and stabilizer list.
201201
terms = new_terms
@@ -243,7 +243,7 @@ def _reduce_terms_keep_length(terms, stabilizer_list, manual_input, fixed_positi
243243
# Finds qubit position and its Pauli.
244244
for qubit_pauli in selected_stab:
245245
if qubit_pauli[0] not in fixed_positions:
246-
fixed_positions += [qubit_pauli[0]]
246+
fixed_positions.append(qubit_pauli[0])
247247
fixed_op = qubit_pauli[1]
248248
break
249249
else:
@@ -261,15 +261,15 @@ def _reduce_terms_keep_length(terms, stabilizer_list, manual_input, fixed_positi
261261
new_list = []
262262
updated_stabilizers = []
263263
for y in term_list:
264-
new_list += [
264+
new_list.append(
265265
fix_single_term(y, fixed_positions[i], fixed_op, other_op, stabilizer_list[0])
266-
]
266+
)
267267
for update_stab in stabilizer_list[1:]:
268-
updated_stabilizers += [
268+
updated_stabilizers.append(
269269
fix_single_term(
270270
update_stab, fixed_positions[i], fixed_op, other_op, stabilizer_list[0]
271271
)
272-
]
272+
)
273273
term_list = new_list
274274
stabilizer_list = updated_stabilizers
275275

0 commit comments

Comments
 (0)