Skip to content

Commit

Permalink
Fix docs errors and lint in ops (#318)
Browse files Browse the repository at this point in the history
* lint

* just use one underscore

* remove "from __future__ import absolute_import"
  • Loading branch information
kevinsung committed Apr 26, 2018
1 parent a8491b8 commit 3391549
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/openfermion/ops/_binary_code.py
Expand Up @@ -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.
Expand Down
17 changes: 10 additions & 7 deletions src/openfermion/ops/_binary_code_test.py
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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')])

Expand All @@ -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):
Expand Down
34 changes: 17 additions & 17 deletions src/openfermion/ops/_binary_polynomial.py
Expand Up @@ -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)]
Expand Down Expand Up @@ -267,16 +267,16 @@ 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
Raises:
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()
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions src/openfermion/ops/_binary_polynomial_test.py
Expand Up @@ -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),))
Expand All @@ -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')
Expand All @@ -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

Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -195,4 +195,3 @@ def test_addition_evaluations(self):
operator2 += operator1
self.assertEqual(operator1.terms, [(1,), (_SYMBOLIC_ONE,)])
self.assertEqual(operator2.terms, [(_SYMBOLIC_ONE,)])

15 changes: 6 additions & 9 deletions src/openfermion/ops/_fermion_operator.py
Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
6 changes: 1 addition & 5 deletions src/openfermion/ops/_fermion_operator_test.py
Expand Up @@ -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


Expand Down
10 changes: 4 additions & 6 deletions src/openfermion/ops/_givens_rotations_test.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 0 additions & 2 deletions src/openfermion/ops/_interaction_operator.py
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/openfermion/ops/_interaction_operator_test.py
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/openfermion/ops/_interaction_rdm.py
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions src/openfermion/ops/_interaction_rdm_test.py
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/openfermion/ops/_polynomial_tensor.py
Expand Up @@ -17,7 +17,7 @@
import itertools
import numpy

from openfermion.config import *
from openfermion.config import EQ_TOLERANCE


class PolynomialTensorError(Exception):
Expand Down
18 changes: 9 additions & 9 deletions src/openfermion/ops/_polynomial_tensor_test.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 3391549

Please sign in to comment.