Skip to content

Commit 5a5e67a

Browse files
authored
Remove left-over Python 2-isms (#1283)
This removes Python 2 compatibility shims and constructs that were not addressed in a previous PR to remove Python 2 shims. * Removed `from builtins import map, zip` in `src/openfermion/utils/operator_utils.py`. These were legacy imports from the `future` library to provide Python 3 behavior in Python 2. * Removed explicit `(object)` inheritance from all class definitions. * Removed the redundant `u` prefix from string literals, because in Python 3, all strings are Unicode by default.
1 parent 7b92e52 commit 5a5e67a

22 files changed

Lines changed: 38 additions & 51 deletions

.pylintrc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
[MAIN]
2121

22-
# Analyse import fallback blocks. This can be used to support both Python 2 and
23-
# 3 compatible code, which means that the block might have code that exists
24-
# only in one or another interpreter, leading to false positives when analysed.
22+
# Analyse import fallback blocks.
2523
analyse-fallback-blocks=no
2624

2725
# Clear in-memory caches upon conclusion of linting. Useful if running pylint
@@ -1027,4 +1025,4 @@ init-import=yes
10271025

10281026
# List of qualified module names which can have objects that can redefine
10291027
# builtins.
1030-
redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io
1028+
redefining-builtins-modules=builtins,io

src/openfermion/chem/molecular_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def spinorb_from_spatial(one_body_integrals, two_body_integrals):
396396
return one_body_coefficients, two_body_coefficients
397397

398398

399-
class MolecularData(object):
399+
class MolecularData:
400400
"""Class for storing molecule data from a fixed basis set at a fixed
401401
geometry that is obtained from classical electronic structure
402402
packages. Not every field is filled in every calculation. All data
@@ -859,7 +859,7 @@ def load(self):
859859
# Load charge:
860860
self.charge = int(f["charge"][...])
861861
# Load description:
862-
self.description = f["description"][...].tobytes().decode('utf-8').rstrip(u'\x00')
862+
self.description = f["description"][...].tobytes().decode('utf-8').rstrip('\x00')
863863
# Load name:
864864
self.name = f["name"][...].tobytes().decode('utf-8')
865865
# Load n_atoms:

src/openfermion/contrib/representability/_dualbasis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import copy
33

44

5-
class DualBasisElement(object):
5+
class DualBasisElement:
66
"""
77
This object is named after the algebraic dual space or dual vector space
88
@@ -154,7 +154,7 @@ def __add__(self, other):
154154
raise TypeError("DualBasisElement can be added to same type or DualBasis")
155155

156156

157-
class DualBasis(object):
157+
class DualBasis:
158158
def __init__(self, elements: Optional[Union[None, List[DualBasisElement]]] = None):
159159
"""
160160
A collection of DualBasisElements

src/openfermion/contrib/representability/_multitensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from openfermion.contrib.representability._dualbasis import DualBasisElement, DualBasis
44

55

6-
class TMap(object):
6+
class TMap:
77
def __init__(self, tensors):
88
"""
99
provide a map of tensor name to tensors
@@ -22,7 +22,7 @@ def __iter__(self):
2222
yield tt
2323

2424

25-
class MultiTensor(object):
25+
class MultiTensor:
2626
def __init__(self, tensors, dual_basis=None):
2727
"""
2828
A collection of tensor objects with maps from name to tensor

src/openfermion/contrib/representability/_namedtensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from openfermion.contrib.representability._bijections import Bijection, index_index_basis
55

66

7-
class Tensor(object):
7+
class Tensor:
88
"""
99
Instantiation of named tensor
1010
"""

src/openfermion/hamiltonians/special_operators_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_init(self):
173173
# Test 'c' operator
174174
op1 = majorana_operator((2, 0))
175175
op2 = majorana_operator('c2')
176-
op3 = majorana_operator(u'c2')
176+
op3 = majorana_operator('c2')
177177
correct = FermionOperator('2^') + FermionOperator('2')
178178
self.assertEqual(op1, op2)
179179
self.assertEqual(op1, op3)

src/openfermion/linalg/davidson.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DavidsonError(Exception):
3131
pass
3232

3333

34-
class DavidsonOptions(object):
34+
class DavidsonOptions:
3535
"""Davidson algorithm iteration options."""
3636

3737
def __init__(self, max_subspace=100, max_iterations=300, eps=1e-6, real_only=False):
@@ -68,7 +68,7 @@ def set_dimension(self, dimension):
6868
self.max_subspace = min(self.max_subspace, dimension + 1)
6969

7070

71-
class Davidson(object):
71+
class Davidson:
7272
"""Davidson algorithm to get the n states with smallest eigenvalues."""
7373

7474
def __init__(self, linear_operator, linear_operator_diagonal, options=None):
@@ -350,7 +350,7 @@ def __init__(self, qubit_operator, n_qubits=None, options=None):
350350
n_qubits(int): Number of qubits.
351351
options(DavidsonOptions): Iteration options.
352352
"""
353-
super(QubitDavidson, self).__init__(
353+
super().__init__(
354354
generate_linear_qubit_operator(qubit_operator, n_qubits, options),
355355
get_linear_qubit_operator_diagonal(qubit_operator, n_qubits),
356356
options=options,
@@ -366,9 +366,7 @@ def __init__(self, sparse_matrix, options=None):
366366
sparse_matrix(scipy.sparse.spmatrix): A sparse matrix in scipy.
367367
options(DavidsonOptions): Iteration options.
368368
"""
369-
super(SparseDavidson, self).__init__(
370-
sparse_matrix, sparse_matrix.diagonal(), options=options
371-
)
369+
super().__init__(sparse_matrix, sparse_matrix.diagonal(), options=options)
372370

373371

374372
def generate_random_vectors(row, col, real_only=False):

src/openfermion/linalg/linear_qubit_operator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from openfermion.utils.operator_utils import count_qubits
2424

2525

26-
class LinearQubitOperatorOptions(object):
26+
class LinearQubitOperatorOptions:
2727
"""Options for LinearQubitOperator."""
2828

2929
def __init__(self, processes=10, pool=None):
@@ -94,7 +94,7 @@ def __init__(self, qubit_operator, n_qubits=None):
9494
)
9595

9696
n_hilbert = 2**n_qubits
97-
super(LinearQubitOperator, self).__init__(shape=(n_hilbert, n_hilbert), dtype=complex)
97+
super().__init__(shape=(n_hilbert, n_hilbert), dtype=complex)
9898
self.qubit_operator = qubit_operator
9999
self.n_qubits = n_qubits
100100

@@ -156,9 +156,7 @@ def __init__(self, qubit_operator, n_qubits=None, options=None):
156156
"""
157157
n_qubits = n_qubits or count_qubits(qubit_operator)
158158
n_hilbert = 2**n_qubits
159-
super(ParallelLinearQubitOperator, self).__init__(
160-
shape=(n_hilbert, n_hilbert), dtype=complex
161-
)
159+
super().__init__(shape=(n_hilbert, n_hilbert), dtype=complex)
162160

163161
self.qubit_operator = qubit_operator
164162
self.n_qubits = n_qubits

src/openfermion/ops/operators/binary_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class BinaryCodeError(Exception):
7373
pass
7474

7575

76-
class BinaryCode(object):
76+
class BinaryCode:
7777
r"""The BinaryCode class provides a representation of an encoding-decoding
7878
pair for binary vectors of different lengths, where the decoding is allowed
7979
to be non-linear.

src/openfermion/ops/operators/binary_polynomial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BinaryPolynomialError(Exception):
2323
pass
2424

2525

26-
class BinaryPolynomial(object):
26+
class BinaryPolynomial:
2727
r"""The BinaryPolynomial class provides an analytic representation
2828
of non-linear binary functions. An instance of this class describes
2929
a term of binary variables (variables of the values {0,1}, indexed

0 commit comments

Comments
 (0)