From 3391549bc2594dc30f0e3e433439b57c211f13e1 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Thu, 26 Apr 2018 10:59:34 -0400 Subject: [PATCH] Fix docs errors and lint in ops (#318) * lint * just use one underscore * remove "from __future__ import absolute_import" --- src/openfermion/ops/_binary_code.py | 4 +-- src/openfermion/ops/_binary_code_test.py | 17 ++++++---- src/openfermion/ops/_binary_polynomial.py | 34 +++++++++---------- .../ops/_binary_polynomial_test.py | 17 +++++----- src/openfermion/ops/_fermion_operator.py | 15 ++++---- src/openfermion/ops/_fermion_operator_test.py | 6 +--- src/openfermion/ops/_givens_rotations_test.py | 10 +++--- src/openfermion/ops/_interaction_operator.py | 2 -- .../ops/_interaction_operator_test.py | 3 +- src/openfermion/ops/_interaction_rdm.py | 2 -- src/openfermion/ops/_interaction_rdm_test.py | 5 ++- src/openfermion/ops/_polynomial_tensor.py | 2 +- .../ops/_polynomial_tensor_test.py | 18 +++++----- src/openfermion/ops/_quadratic_hamiltonian.py | 16 ++++----- .../ops/_quadratic_hamiltonian_test.py | 17 +++++----- src/openfermion/ops/_qubit_operator.py | 4 +-- src/openfermion/ops/_qubit_operator_test.py | 5 +-- src/openfermion/ops/_symbolic_operator.py | 4 +-- .../ops/_symbolic_operator_test.py | 18 +++++----- 19 files changed, 91 insertions(+), 108 deletions(-) diff --git a/src/openfermion/ops/_binary_code.py b/src/openfermion/ops/_binary_code.py index 0b73a300b..03d14a7d1 100644 --- a/src/openfermion/ops/_binary_code.py +++ b/src/openfermion/ops/_binary_code.py @@ -118,8 +118,8 @@ class BinaryCode(object): BinaryCode their concatenation. Attributes: - decoder (list): list of BinaryPolynomial: Outputs the decoding functions - as components. + decoder (list): list of BinaryPolynomial: Outputs the decoding + functions as components. encoder (scipy.sparse.csc_matrix): Outputs A, the linear matrix that implements the encoding function. n_modes (int): Outputs the number of modes. diff --git a/src/openfermion/ops/_binary_code_test.py b/src/openfermion/ops/_binary_code_test.py index e408a64eb..9fe6fe3a2 100644 --- a/src/openfermion/ops/_binary_code_test.py +++ b/src/openfermion/ops/_binary_code_test.py @@ -23,7 +23,8 @@ class CodeOperatorTest(unittest.TestCase): def test_init_errors(self): with self.assertRaises(TypeError): BinaryCode(1, - [BinaryPolynomial(' w1 + w0 '), BinaryPolynomial('w0 + 1')]) + [BinaryPolynomial(' w1 + w0 '), + BinaryPolynomial('w0 + 1')]) with self.assertRaises(TypeError): BinaryCode([[0, 1], [1, 0]], '1+w1') with self.assertRaises(BinaryCodeError): @@ -41,8 +42,9 @@ def test_init_errors(self): def test_addition(self): a = BinaryCode([[0, 1, 0], [1, 0, 1]], - [BinaryPolynomial(' w1 + w0 '), BinaryPolynomial('w0 + 1'), - BinaryPolynomial('w1')]) + [BinaryPolynomial(' w1 + w0 '), + BinaryPolynomial('w0 + 1'), + BinaryPolynomial('w1')]) d = BinaryCode([[0, 1], [1, 0]], [BinaryPolynomial(' w0 '), BinaryPolynomial('w0 w1')]) summation = a + d @@ -57,8 +59,9 @@ def test_addition(self): def test_multiplication(self): a = BinaryCode([[0, 1, 0], [1, 0, 1]], - [BinaryPolynomial(' w1 + w0 '), BinaryPolynomial('w0 + 1'), - BinaryPolynomial('w1')]) + [BinaryPolynomial(' w1 + w0 '), + BinaryPolynomial('w0 + 1'), + BinaryPolynomial('w1')]) d = BinaryCode([[0, 1], [1, 0]], [BinaryPolynomial(' w0 '), BinaryPolynomial('w0 w1')]) @@ -71,9 +74,9 @@ def test_multiplication(self): "[0, 0, 0, 1], [0, 0, 1, 0]], " "'[[W0],[W0 W1],[W2],[W2 W3]]']") with self.assertRaises(BinaryCodeError): - d * a + _ = d * a with self.assertRaises(TypeError): - 2.0 * a + _ = 2.0 * a with self.assertRaises(TypeError): a *= 2.0 with self.assertRaises(ValueError): diff --git a/src/openfermion/ops/_binary_polynomial.py b/src/openfermion/ops/_binary_polynomial.py index 76359283b..0e0184ab4 100644 --- a/src/openfermion/ops/_binary_polynomial.py +++ b/src/openfermion/ops/_binary_polynomial.py @@ -30,25 +30,25 @@ class BinaryPolynomial(object): a term of binary variables (variables of the values {0,1}, indexed by integers like w0, w1, w2 and so on) that is considered to be evaluated modulo 2. This implies the following set of rules: - - the binary addition w1 + w1 = 0, - binary multiplication w2 * w2 = w2 + + the binary addition w1 + w1 = 0, + binary multiplication w2 * w2 = w2 and power rule w3 ^ 0 = 1, where raising to every other integer power than zero reproduces w3. - + Of course, we can also add a non-trivial constant, which is 1. - Due to these binary rules, every function available will be a + Due to these binary rules, every function available will be a multinomial like e.g. - + 1 + w1 w2 + w0 w1 . - + These binary functions are used for non-linear binary codes in order - to decompress qubit bases back into fermion bases. - In that instance, one BinaryPolynomial object characterizes the occupation - of single orbital given a multi-qubit state in configuration - \|w0> \|w1> \|w2> ... . - - For initialization, the preferred data types is either a string of the + to decompress qubit bases back into fermion bases. + In that instance, one BinaryPolynomial object characterizes the occupation + of single orbital given a multi-qubit state in configuration + \|w0> \|w1> \|w2> ... . + + For initialization, the preferred data types is either a string of the multinomial, where each variable and constant is to be well separated by a whitespace, or in its native form of tuples, 1 + w1 w2 + w0 w1 is represented as [(_SYMBOLIC_ONE,),(1,2),(0,1)] @@ -267,8 +267,8 @@ def evaluate(self, binary_list): Args: binary_list (list, array, str): a list of binary values - corresponding each binary variable - (in order of their indices) in the expression + corresponding each binary variable + (in order of their indices) in the expression Returns (int, 0 or 1): result of the evaluation @@ -276,7 +276,7 @@ def evaluate(self, binary_list): BinaryPolynomialError: Length of list provided must match the number of qubits indexed in BinaryPolynomial """ - if isinstance(binary_list,str): + if isinstance(binary_list, str): binary_list = list(map(int, list(binary_list))) all_qubits = self.enumerate_qubits() @@ -487,7 +487,7 @@ def __radd__(self, addend): return self + addend def __add__(self, addend): - """ + """ Left addition of BinaryPolynomial. Args: addend (int or BinaryPolynomial): The operator or int to add. diff --git a/src/openfermion/ops/_binary_polynomial_test.py b/src/openfermion/ops/_binary_polynomial_test.py index 2d0533a45..7cc97916f 100644 --- a/src/openfermion/ops/_binary_polynomial_test.py +++ b/src/openfermion/ops/_binary_polynomial_test.py @@ -52,7 +52,7 @@ def test_init_list(self): self.assertEqual(operator1.terms, [(3, 4)]) operator1 = BinaryPolynomial([(4, 3, _SYMBOLIC_ONE)]) self.assertEqual(operator1.terms, [(3, 4)]) - operator1 = BinaryPolynomial(((1,2),(1,2))) + operator1 = BinaryPolynomial(((1, 2), (1, 2))) self.assertEqual(operator1.terms, []) with self.assertRaises(ValueError): operator1 = BinaryPolynomial(((1, -2),)) @@ -74,9 +74,9 @@ def test_multiplication(self): with self.assertRaises(TypeError): operator1 *= 4.3 with self.assertRaises(TypeError): - tmp = 4.3 * operator1 + _ = 4.3 * operator1 with self.assertRaises(TypeError): - tmp = operator1 * 4.3 + _ = operator1 * 4.3 def test_addition(self): operator1 = BinaryPolynomial('w1 w2') @@ -88,7 +88,7 @@ def test_addition(self): addition = addition + 1 self.assertEqual(addition.terms, [(_SYMBOLIC_ONE,)]) with self.assertRaises(TypeError): - tmp = 4.3 + operator1 + _ = 4.3 + operator1 with self.assertRaises(TypeError): operator1 += 4.3 @@ -104,9 +104,9 @@ def test_power(self): self.assertEqual(pow_loc.terms, [(_SYMBOLIC_ONE,), (1, 2), (3, 4)]) with self.assertRaises(TypeError): - tmp = operator1 ** 4.3 + _ = operator1 ** 4.3 with self.assertRaises(TypeError): - tmp = operator1 ** (-1) + _ = operator1 ** (-1) def test_init_binary_rule(self): operator1 = BinaryPolynomial('1 + w2 w2 + w2') @@ -158,9 +158,9 @@ def test_count_qubits(self): def test_evaluate(self): operator1 = BinaryPolynomial() - self.assertEqual(operator1.evaluate('1111'),0) + self.assertEqual(operator1.evaluate('1111'), 0) operator1 = BinaryPolynomial(1) - self.assertEqual(operator1.evaluate('1111'),1) + self.assertEqual(operator1.evaluate('1111'), 1) operator1 = BinaryPolynomial('1 + w0 w2 w1 + w0 w1 + w0 w2') a = operator1.evaluate([0, 0, 1]) self.assertEqual(a, 1.0) @@ -195,4 +195,3 @@ def test_addition_evaluations(self): operator2 += operator1 self.assertEqual(operator1.terms, [(1,), (_SYMBOLIC_ONE,)]) self.assertEqual(operator2.terms, [(_SYMBOLIC_ONE,)]) - diff --git a/src/openfermion/ops/_fermion_operator.py b/src/openfermion/ops/_fermion_operator.py index 243c184f9..820fa1cf3 100644 --- a/src/openfermion/ops/_fermion_operator.py +++ b/src/openfermion/ops/_fermion_operator.py @@ -11,9 +11,7 @@ # limitations under the License. """FermionOperator stores a sum of products of fermionic ladder operators.""" -import numpy -from future.utils import iteritems from openfermion.ops import SymbolicOperator @@ -25,15 +23,15 @@ def normal_ordered_term(term, coefficient): """Return a normal ordered FermionOperator corresponding to single term. Args: - term: A tuple of tuples. The first element of each tuple is - an integer indicating the mode on which a fermion ladder + term(list or tuple): A sequence of tuples. The first element of each + tuple is an integer indicating the mode on which a fermion ladder operator acts, starting from zero. The second element of each tuple is an integer, either 1 or 0, indicating whether creation or annihilation acts on that mode. - coefficient: The coefficient of the term. + coefficient(complex or float): The coefficient of the term. Returns: - ordered_term (FermionOperator): The normal ordered form of the input. + ordered_term(FermionOperator): The normal ordered form of the input. Note that this might have more terms. In our convention, normal ordering implies terms are ordered @@ -64,11 +62,10 @@ def normal_ordered_term(term, coefficient): # Replace a a^\dagger with 1 - a^\dagger a # if indices are the same. if right_operator[0] == left_operator[0]: - new_term = term[:(j - 1)] + term[(j + 1)::] + new_term = term[:(j - 1)] + term[(j + 1):] # Recursively add the processed new term. - ordered_term += normal_ordered_term( - tuple(new_term), -coefficient) + ordered_term += normal_ordered_term(new_term, -coefficient) # Handle case when operator type is the same. elif right_operator[1] == left_operator[1]: diff --git a/src/openfermion/ops/_fermion_operator_test.py b/src/openfermion/ops/_fermion_operator_test.py index c6f1e06ae..9c53098e8 100644 --- a/src/openfermion/ops/_fermion_operator_test.py +++ b/src/openfermion/ops/_fermion_operator_test.py @@ -11,13 +11,9 @@ # limitations under the License. """Tests _fermion_operator.py.""" -import copy -import numpy import unittest -from openfermion.ops._fermion_operator import (FermionOperator, - FermionOperatorError, - normal_ordered) +from openfermion.ops._fermion_operator import FermionOperator, normal_ordered from openfermion.utils import number_operator diff --git a/src/openfermion/ops/_givens_rotations_test.py b/src/openfermion/ops/_givens_rotations_test.py index c2a5a3935..defbd81be 100644 --- a/src/openfermion/ops/_givens_rotations_test.py +++ b/src/openfermion/ops/_givens_rotations_test.py @@ -54,7 +54,7 @@ def test_bad_input(self): """Test bad input.""" with self.assertRaises(ValueError): v = numpy.random.randn(2) - G = givens_matrix_elements(v[0], v[1], which='a') + _ = givens_matrix_elements(v[0], v[1], which='a') class GivensRotateTest(unittest.TestCase): @@ -171,7 +171,7 @@ def test_bad_dimensions(self): Q = Q[:m, :n] with self.assertRaises(ValueError): - givens_rotations, V, diagonal = givens_decomposition(Q) + _ = givens_decomposition(Q) def test_identity(self): n = 3 @@ -322,12 +322,10 @@ def test_bad_dimensions(self): n, p = (3, 7) rand_mat = numpy.random.randn(n, p) with self.assertRaises(ValueError): - decomposition, left_unitary, antidiagonal = ( - fermionic_gaussian_decomposition(rand_mat)) + _ = fermionic_gaussian_decomposition(rand_mat) def test_bad_constraints(self): n = 3 ones_mat = numpy.ones((n, 2 * n)) with self.assertRaises(ValueError): - decomposition, left_unitary, antidiagonal = ( - fermionic_gaussian_decomposition(ones_mat)) + _ = fermionic_gaussian_decomposition(ones_mat) diff --git a/src/openfermion/ops/_interaction_operator.py b/src/openfermion/ops/_interaction_operator.py index f68747b94..bd66232a0 100644 --- a/src/openfermion/ops/_interaction_operator.py +++ b/src/openfermion/ops/_interaction_operator.py @@ -11,8 +11,6 @@ # limitations under the License. """Class and functions to store interaction operators.""" -from __future__ import absolute_import - import itertools from openfermion.ops import PolynomialTensor diff --git a/src/openfermion/ops/_interaction_operator_test.py b/src/openfermion/ops/_interaction_operator_test.py index 3e3016a35..ea97f09d5 100644 --- a/src/openfermion/ops/_interaction_operator_test.py +++ b/src/openfermion/ops/_interaction_operator_test.py @@ -11,10 +11,9 @@ # limitations under the License. """Tests for interaction_operator.py.""" -from __future__ import absolute_import +import unittest import numpy -import unittest from openfermion.ops import InteractionOperator diff --git a/src/openfermion/ops/_interaction_rdm.py b/src/openfermion/ops/_interaction_rdm.py index 33b1c5aa6..07dd986f1 100644 --- a/src/openfermion/ops/_interaction_rdm.py +++ b/src/openfermion/ops/_interaction_rdm.py @@ -11,8 +11,6 @@ # limitations under the License. """Class and functions to store reduced density matrices.""" -from __future__ import absolute_import - import copy import numpy diff --git a/src/openfermion/ops/_interaction_rdm_test.py b/src/openfermion/ops/_interaction_rdm_test.py index bb1c139ce..357d2c567 100644 --- a/src/openfermion/ops/_interaction_rdm_test.py +++ b/src/openfermion/ops/_interaction_rdm_test.py @@ -11,11 +11,10 @@ # limitations under the License. """Tests for interaction_rdms.py.""" -from __future__ import absolute_import - +import os import unittest -from openfermion.config import * +from openfermion.config import THIS_DIRECTORY, EQ_TOLERANCE from openfermion.hamiltonians import MolecularData from openfermion.ops import QubitOperator from openfermion.ops._interaction_rdm import InteractionRDMError diff --git a/src/openfermion/ops/_polynomial_tensor.py b/src/openfermion/ops/_polynomial_tensor.py index 0488d282b..9727aa422 100644 --- a/src/openfermion/ops/_polynomial_tensor.py +++ b/src/openfermion/ops/_polynomial_tensor.py @@ -17,7 +17,7 @@ import itertools import numpy -from openfermion.config import * +from openfermion.config import EQ_TOLERANCE class PolynomialTensorError(Exception): diff --git a/src/openfermion/ops/_polynomial_tensor_test.py b/src/openfermion/ops/_polynomial_tensor_test.py index 81f771461..4e082ef70 100644 --- a/src/openfermion/ops/_polynomial_tensor_test.py +++ b/src/openfermion/ops/_polynomial_tensor_test.py @@ -11,7 +11,7 @@ # limitations under the License. """Tests for polynomial_tensor.py.""" -from __future__ import absolute_import, division +from __future__ import division import unittest @@ -176,7 +176,7 @@ def test_getitem_2body(self): def test_invalid_getitem_indexing(self): with self.assertRaises(KeyError): - self.polynomial_tensor_a[(0, 1), (1, 1), (0, 0)] + _ = self.polynomial_tensor_a[(0, 1), (1, 1), (0, 0)] def test_invalid_setitem_indexing(self): test_tensor = copy.deepcopy(self.polynomial_tensor_a) @@ -212,11 +212,11 @@ def test_iadd(self): def test_invalid_addend(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a + 2 + _ = self.polynomial_tensor_a + 2 def test_invalid_tensor_shape_add(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a + self.polynomial_tensor_c + _ = self.polynomial_tensor_a + self.polynomial_tensor_c def test_different_keys_add(self): result = self.polynomial_tensor_a + self.polynomial_tensor_operand @@ -242,11 +242,11 @@ def test_isub(self): def test_invalid_subtrahend(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a - 2 + _ = self.polynomial_tensor_a - 2 def test_invalid_tensor_shape_sub(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a - self.polynomial_tensor_c + _ = self.polynomial_tensor_a - self.polynomial_tensor_c def test_different_keys_sub(self): result = self.polynomial_tensor_a - self.polynomial_tensor_operand @@ -284,11 +284,11 @@ def test_imul(self): def test_invalid_multiplier(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a * 'a' + _ = self.polynomial_tensor_a * 'a' def test_invalid_tensor_shape_mult(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a * self.polynomial_tensor_c + _ = self.polynomial_tensor_a * self.polynomial_tensor_c def test_different_keys_mult(self): result = self.polynomial_tensor_a * self.polynomial_tensor_operand @@ -317,7 +317,7 @@ def test_idiv(self): def test_invalid_dividend(self): with self.assertRaises(TypeError): - self.polynomial_tensor_a / 'a' + _ = self.polynomial_tensor_a / 'a' def test_iter_and_str(self): one_body = numpy.zeros((self.n_qubits, self.n_qubits)) diff --git a/src/openfermion/ops/_quadratic_hamiltonian.py b/src/openfermion/ops/_quadratic_hamiltonian.py index e6c811e83..44c5d86b1 100644 --- a/src/openfermion/ops/_quadratic_hamiltonian.py +++ b/src/openfermion/ops/_quadratic_hamiltonian.py @@ -153,12 +153,12 @@ def orbital_energies(self, non_negative=False): """ if self.conserves_particle_number and not non_negative: hermitian_matrix = self.combined_hermitian_part - orbital_energies, diagonalizing_unitary = numpy.linalg.eigh( + orbital_energies, _ = numpy.linalg.eigh( hermitian_matrix) constant = self.constant else: majorana_matrix, majorana_constant = self.majorana_form() - canonical, orthogonal = antisymmetric_canonical_form( + canonical, _ = antisymmetric_canonical_form( majorana_matrix) orbital_energies = canonical[ range(self.n_qubits), range(self.n_qubits, 2 * self.n_qubits)] @@ -278,18 +278,18 @@ def diagonalizing_bogoliubov_transform(self): A matrix representing the transformation :math:`W` of the fermionic ladder operators. If the Hamiltonian conserves particle number then this is :math:`N \\times N`; otherwise - it is :math:`2N \\times 2N`. + it is :math:`N \\times 2N`. """ if self.conserves_particle_number: - energies, diagonalizing_unitary_T = numpy.linalg.eigh( + _, diagonalizing_unitary_T = numpy.linalg.eigh( self.combined_hermitian_part) return diagonalizing_unitary_T.T else: - majorana_matrix, majorana_constant = self.majorana_form() + majorana_matrix, _ = self.majorana_form() # Get the orthogonal transformation that puts majorana_matrix # into canonical form - canonical, orthogonal = antisymmetric_canonical_form( + _, orthogonal = antisymmetric_canonical_form( majorana_matrix) # Create the matrix that converts between fermionic ladder and @@ -338,7 +338,7 @@ def diagonalizing_circuit(self): if self.conserves_particle_number: # The Hamiltonian conserves particle number, so we don't need # to use the most general procedure. - decomposition, diagonal = givens_decomposition_square( + decomposition, _ = givens_decomposition_square( transformation_matrix) circuit_description = list(reversed(decomposition)) else: @@ -357,7 +357,7 @@ def diagonalizing_circuit(self): new_transformation_matrix[:, self.n_qubits:] = numpy.conjugate( left_block) # Get the circuit description - decomposition, left_decomposition, diagonal, left_diagonal = ( + decomposition, left_decomposition, _, _ = ( fermionic_gaussian_decomposition(new_transformation_matrix)) # need to use left_diagonal too circuit_description = list(reversed( diff --git a/src/openfermion/ops/_quadratic_hamiltonian_test.py b/src/openfermion/ops/_quadratic_hamiltonian_test.py index e5c952edc..e8ce305d1 100644 --- a/src/openfermion/ops/_quadratic_hamiltonian_test.py +++ b/src/openfermion/ops/_quadratic_hamiltonian_test.py @@ -10,9 +10,10 @@ # See the License for the specific language governing permissions and # limitations under the License. """Tests for quadratic_hamiltonian.py.""" +import unittest + import numpy import scipy.sparse -import unittest from openfermion.config import EQ_TOLERANCE from openfermion.ops import (normal_ordered, FermionOperator) @@ -159,10 +160,11 @@ def test_majorana_form(self): normalization) for j in range(2 * self.n_qubits): if j < self.n_qubits: - right_op = majorana_operator((j, 0), - majorana_matrix[i, j] * normalization) + right_op = majorana_operator( + (j, 0), majorana_matrix[i, j] * normalization) else: - right_op = majorana_operator((j - self.n_qubits, 1), + right_op = majorana_operator( + (j - self.n_qubits, 1), majorana_matrix[i, j] * normalization) majorana_op += .5j * left_op * right_op # Get FermionOperator for original Hamiltonian @@ -197,8 +199,8 @@ def test_diagonalizing_bogoliubov_transform(self): left_block) # Check that the transformation is diagonalizing - majorana_matrix, majorana_constant = self.quad_ham_npc.majorana_form() - canonical, orthogonal = antisymmetric_canonical_form(majorana_matrix) + majorana_matrix, _ = self.quad_ham_npc.majorana_form() + canonical, _ = antisymmetric_canonical_form(majorana_matrix) diagonalized = ferm_unitary.conj().dot( block_matrix.dot(ferm_unitary.T.conj())) for i in numpy.ndindex((2 * self.n_qubits, 2 * self.n_qubits)): @@ -326,8 +328,7 @@ def test_canonical(self): # Obtain a random antisymmetric matrix rand_mat = numpy.random.randn(2 * n, 2 * n) antisymmetric_matrix = rand_mat - rand_mat.T - canonical, orthogonal = antisymmetric_canonical_form( - antisymmetric_matrix) + canonical, _ = antisymmetric_canonical_form(antisymmetric_matrix) for i in range(2 * n): for j in range(2 * n): if i < n and j == n + i: diff --git a/src/openfermion/ops/_qubit_operator.py b/src/openfermion/ops/_qubit_operator.py index 6cc97ae86..36ffe8ea3 100644 --- a/src/openfermion/ops/_qubit_operator.py +++ b/src/openfermion/ops/_qubit_operator.py @@ -166,11 +166,11 @@ def __imul__(self, multiplier): else: raise TypeError('Cannot in-place multiply term of invalid type ' + 'to QubitTerm.') - + def renormalize(self): """Fix the trace norm of an operator to 1""" norm = self.induced_norm(2) if norm < EQ_TOLERANCE: raise ZeroDivisionError('Cannot renormalize empty or zero operator') else: - self /= norm \ No newline at end of file + self /= norm diff --git a/src/openfermion/ops/_qubit_operator_test.py b/src/openfermion/ops/_qubit_operator_test.py index 616ce5ecf..aaed61b39 100644 --- a/src/openfermion/ops/_qubit_operator_test.py +++ b/src/openfermion/ops/_qubit_operator_test.py @@ -11,14 +11,12 @@ # limitations under the License. """Tests for _qubit_operator.py.""" -import copy import numpy import pytest from openfermion.ops._qubit_operator import (_PAULI_OPERATOR_PRODUCTS, - QubitOperator, - QubitOperatorError) + QubitOperator) def test_pauli_operator_product_unchanged(): @@ -61,7 +59,6 @@ def test_imul_qubit_op(): op1 = QubitOperator(((0, 'Y'), (3, 'X'), (8, 'Z'), (11, 'X')), 3.j) op2 = QubitOperator(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5) op1 *= op2 - correct_coefficient = 1.j * 3.0j * 0.5 correct_term = ((0, 'Y'), (1, 'X'), (3, 'Z'), (11, 'X')) assert len(op1.terms) == 1 assert correct_term in op1.terms diff --git a/src/openfermion/ops/_symbolic_operator.py b/src/openfermion/ops/_symbolic_operator.py index 3b30913c4..2cc6b1297 100644 --- a/src/openfermion/ops/_symbolic_operator.py +++ b/src/openfermion/ops/_symbolic_operator.py @@ -87,7 +87,7 @@ def __init__(self, term=None, coefficient=1.): # Parse the term # Sequence input - elif isinstance(term, tuple) or isinstance(term, list): + if isinstance(term, tuple) or isinstance(term, list): term = self._parse_sequence(term) # String input elif isinstance(term, str): @@ -503,7 +503,7 @@ def __pow__(self, exponent): exponentiated = self.__class__(()) # Handle non-zero exponents. - for i in range(exponent): + for _ in range(exponent): exponentiated *= self return exponentiated diff --git a/src/openfermion/ops/_symbolic_operator_test.py b/src/openfermion/ops/_symbolic_operator_test.py index 73d3598a4..af17e5e56 100644 --- a/src/openfermion/ops/_symbolic_operator_test.py +++ b/src/openfermion/ops/_symbolic_operator_test.py @@ -327,7 +327,6 @@ def test_imul_fermion_op(self): op1 = DummyOperator1(((0, 1), (3, 0), (8, 1), (8, 0), (11, 1)), 3.j) op2 = DummyOperator1(((1, 1), (3, 1), (8, 0)), 0.5) op1 *= op2 - correct_coefficient = 1.j * 3.0j * 0.5 correct_term = ((0, 1), (3, 0), (8, 1), (8, 0), (11, 1), (1, 1), (3, 1), (8, 0)) self.assertEqual(len(op1.terms), 1) @@ -727,35 +726,35 @@ def test_init_str_identity(self): def test_init_bad_term(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2(2) + _ = DummyOperator2(2) def test_init_bad_coefficient(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2('X0', "0.5") + _ = DummyOperator2('X0', "0.5") def test_init_bad_action(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2('Q0') + _ = DummyOperator2('Q0') def test_init_bad_action_in_tuple(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2(((1, 'Q'),)) + _ = DummyOperator2(((1, 'Q'),)) def test_init_bad_qubit_num_in_tuple(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2((("1", 'X'),)) + _ = DummyOperator2((("1", 'X'),)) def test_init_bad_tuple(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2(((0, 1, 'X'),)) + _ = DummyOperator2(((0, 1, 'X'),)) def test_init_bad_str(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2('X') + _ = DummyOperator2('X') def test_init_bad_qubit_num(self): with self.assertRaises(ValueError): - qubit_op = DummyOperator2('X-1') + _ = DummyOperator2('X-1') def test_compress(self): a = DummyOperator2('X0', .9e-12) @@ -918,7 +917,6 @@ def test_isub_bad_addend(self): def test_neg(self): op = DummyOperator2(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5) - -op # out of place self.assertTrue(op == DummyOperator2(((1, 'X'), (3, 'Y'), (8, 'Z')), 0.5))