Skip to content

Commit

Permalink
Remove trailing whitepaces from .py files (#559)
Browse files Browse the repository at this point in the history
* remove trailing whitepaces from .py files

* format

* format
  • Loading branch information
vtomole committed Feb 27, 2020
1 parent 7513a3f commit d6b969b
Show file tree
Hide file tree
Showing 18 changed files with 976 additions and 999 deletions.
47 changes: 24 additions & 23 deletions src/openfermion/hamiltonians/_general_hubbard.py
Expand Up @@ -35,14 +35,15 @@ def number_operator(i, coefficient=1., particle_hole_symmetry=False):


def interaction_operator(i, j, coefficient=1., particle_hole_symmetry=False):
return (number_operator(i, coefficient,
particle_hole_symmetry=particle_hole_symmetry) *
return (number_operator(
i, coefficient, particle_hole_symmetry=particle_hole_symmetry) *
number_operator(j, particle_hole_symmetry=particle_hole_symmetry))


def tunneling_operator(i, j, coefficient=1.):
return (FermionOperator(((i, 1), (j, 0)), coefficient) +
FermionOperator(((j, 1), (i, 0)), coefficient.conjugate()))
return (FermionOperator(((i, 1), (j, 0)), coefficient) + FermionOperator(
((j, 1), (i, 0)), coefficient.conjugate()))


def number_difference_operator(i, j, coefficient=1.):
return number_operator(i, coefficient) - number_operator(j, coefficient)
Expand Down Expand Up @@ -195,20 +196,20 @@ def __init__(self, lattice,
.. math::
t \sum_{(i, j) \in E^{(\mathrm{edge type})}}
t \sum_{(i, j) \in E^{(\mathrm{edge type})}}
\sum_{\sigma}
\left(a_{i, a, \sigma}^{\dagger} a_{j, b, \sigma}
\left(a_{i, a, \sigma}^{\dagger} a_{j, b, \sigma}
+ a_{j, b, \sigma}^{\dagger} a_{i, a, \sigma}\right)
and in the spinless model to
and in the spinless model to
.. math::
-t \sum_{(i, j) \in E^{(\mathrm{edge type})}}
\left(a_{i, a}^{\dagger} a_{j, b}
-t \sum_{(i, j) \in E^{(\mathrm{edge type})}}
\left(a_{i, a}^{\dagger} a_{j, b}
+ a_{j, b}^{\dagger} a_{i, a}\right),
where
where
- :math:`(a, b)` is the pair of degrees of freedom given by ``dofs``;
- :math:`E^{(\mathrm{edge type})}` is the set of ordered pairs of
Expand All @@ -228,23 +229,23 @@ def __init__(self, lattice,
U \sum_{(i, j) \in E^{(\mathrm{edge type})}} \sum_{(\sigma, \sigma')}
n_{i, a, \sigma} n_{j, b, \sigma'}
where
where
- :math:`(a, b)` is the pair of degrees of freedom given by ``dofs``;
- :math:`E^{(\mathrm{edge type})}` is the set of ordered pairs of
site indices returned by ``lattice.site_pairs_iter(edge_type)``;
- :math:`U` is the ``coefficient``; and
- :math:`(\sigma, \sigma')` runs over
- all four possible pairs of spins if `spin_pairs == SpinPairs.ALL`,
- :math:`\{(\uparrow, \downarrow), (\downarrow, \uparrow)\}` if `spin_pairs == SpinPairs.DIFF`, and
- :math:`\{(\uparrow, \downarrow), (\downarrow, \uparrow)\}` if `spin_pairs == SpinPairs.DIFF`, and
- :math:`\{(\uparrow, \uparrow), (\downarrow, \downarrow)\}' if 'spin_pairs == SpinPairs.SAME`.
Each potential parameter is a tuple ``(dof, coefficient)``. For example, in the spinful model, it corresponds to the terms
.. math::
-\mu \sum_{i} \sum_{\sigma} n_{i, a, \sigma},
where
where
- :math:`i` runs over the sites of the lattice;
- :math:`a` is the degree of freedom ``dof``; and
Expand Down Expand Up @@ -273,7 +274,7 @@ def parse_tunneling_parameters(self, parameters):
parameter = TunnelingParameter(*parameter)
self.lattice.validate_edge_type(parameter.edge_type)
self.lattice.validate_dofs(parameter.dofs, 2)
if ((parameter.edge_type in self.lattice.onsite_edge_types) and
if ((parameter.edge_type in self.lattice.onsite_edge_types) and
(len(set(parameter.dofs)) == 1)):
raise ValueError('Invalid onsite tunneling parameter between '
'same dof {}.'.format(parameter.dofs))
Expand All @@ -288,13 +289,13 @@ def parse_interaction_parameters(self, parameters):
for parameter in parameters:
if len(parameter) not in (3, 4):
raise ValueError('len(parameter) not in (3, 4)')
spin_pairs = (SpinPairs.ALL if len(parameter) < 4
else parameter[-1])
spin_pairs = (SpinPairs.ALL
if len(parameter) < 4 else parameter[-1])
parameter = InteractionParameter(*parameter[:3],
spin_pairs=spin_pairs)
self.lattice.validate_edge_type(parameter.edge_type)
self.lattice.validate_dofs(parameter.dofs, 2)
if ((len(set(parameter.dofs)) == 1) and
if ((len(set(parameter.dofs)) == 1) and
(parameter.edge_type in self.lattice.onsite_edge_types) and
(parameter.spin_pairs == SpinPairs.SAME)):
raise ValueError('Parameter {} specifies '.format(parameter) +
Expand Down Expand Up @@ -351,8 +352,10 @@ def potential_terms(self):
for spin_index in self.lattice.spin_indices:
i = self.lattice.to_spin_orbital_index(
site_index, param.dof, spin_index)
terms += number_operator(i, -param.coefficient,
particle_hole_symmetry=self.particle_hole_symmetry)
terms += number_operator(
i,
-param.coefficient,
particle_hole_symmetry=self.particle_hole_symmetry)
return terms


Expand All @@ -369,7 +372,5 @@ def field_terms(self):


def hamiltonian(self):
return (self.tunneling_terms() +
self.interaction_terms() +
self.potential_terms() +
self.field_terms())
return (self.tunneling_terms() + self.interaction_terms() +
self.potential_terms() + self.field_terms())
87 changes: 46 additions & 41 deletions src/openfermion/hamiltonians/_general_hubbard_test.py
Expand Up @@ -26,26 +26,27 @@
def fermi_hubbard_from_general(x_dimension, y_dimension, tunneling, coulomb,
chemical_potential=0.,
periodic=True, spinless=False, magnetic_field=0):
lattice = HubbardSquareLattice(x_dimension, y_dimension,
periodic=periodic, spinless=spinless)
lattice = HubbardSquareLattice(x_dimension,
y_dimension,
periodic=periodic,
spinless=spinless)
interaction_edge_type = 'neighbor' if spinless else 'onsite'
model = FermiHubbardModel(lattice,
tunneling_parameters=(('neighbor', (0, 0), tunneling),),
interaction_parameters=((interaction_edge_type, (0, 0), coulomb),),
potential_parameters=((0, chemical_potential),),
magnetic_field=magnetic_field)
model = FermiHubbardModel(lattice,
tunneling_parameters=(('neighbor', (0, 0),
tunneling),),
interaction_parameters=((interaction_edge_type,
(0, 0), coulomb),),
potential_parameters=((0, chemical_potential),),
magnetic_field=magnetic_field)
return model.hamiltonian()


@pytest.mark.parametrize(
'x_dimension,y_dimension,tunneling,coulomb,' +
'chemical_potential,spinless,periodic,magnetic_field',
itertools.product(
range(1, 4), range(1, 4),
(random.uniform(0, 2.),), (random.uniform(0, 2.),),
(random.uniform(0, 2.),), (True, False), (True, False),
(random.uniform(-1, 1),))
)
'x_dimension,y_dimension,tunneling,coulomb,' +
'chemical_potential,spinless,periodic,magnetic_field',
itertools.product(range(1, 4), range(1, 4), (random.uniform(0, 2.),),
(random.uniform(0, 2.),), (random.uniform(0, 2.),),
(True, False), (True, False), (random.uniform(-1, 1),)))
def test_fermi_hubbard_square_special_general_equivalence(
x_dimension, y_dimension, tunneling, coulomb,
chemical_potential, spinless, periodic, magnetic_field):
Expand All @@ -61,8 +62,9 @@ def test_fermi_hubbard_square_special_general_equivalence(

def random_parameters(lattice, probability=0.5, distinguish_edges=False):
parameters = {}
edge_types = (('onsite', 'horizontal_neighbor', 'vertical_neighbor') if
distinguish_edges else ('onsite', 'neighbor'))
edge_types = (('onsite', 'horizontal_neighbor',
'vertical_neighbor') if distinguish_edges else
('onsite', 'neighbor'))

parameters['tunneling_parameters'] = [
(edge_type, dofs, random.uniform(-1, 1))
Expand All @@ -72,12 +74,12 @@ def random_parameters(lattice, probability=0.5, distinguish_edges=False):

possible_spin_pairs = (SpinPairs.ALL,) if lattice.spinless else (SpinPairs.SAME, SpinPairs.DIFF)
parameters['interaction_parameters'] = [
(edge_type, dofs, random.uniform(-1, 1), spin_pairs)
for edge_type in edge_types
for spin_pairs in possible_spin_pairs
for dofs in lattice.dof_pairs_iter(edge_type == 'onsite' and
spin_pairs in (SpinPairs.ALL, SpinPairs.SAME))
if random.random() <= probability]
(edge_type, dofs, random.uniform(-1, 1), spin_pairs)
for edge_type in edge_types for spin_pairs in possible_spin_pairs
for dofs in lattice.dof_pairs_iter(edge_type == 'onsite' and spin_pairs
in (SpinPairs.ALL, SpinPairs.SAME))
if random.random() <= probability
]

parameters['potential_parameters'] = [
(dof, random.uniform(-1, 1))
Expand All @@ -86,7 +88,7 @@ def random_parameters(lattice, probability=0.5, distinguish_edges=False):

if random.random() <= probability:
parameters['magnetic_field'] = random.uniform(-1, 1)

return parameters


Expand Down Expand Up @@ -137,21 +139,24 @@ def test_fermi_hubbard_square_lattice_random_parameters(
if len(spin_orbitals) == 2:
(i, a, s), (ii, aa, ss) = (
lattice.from_spin_orbital_index(i) for i in spin_orbitals)
edge_type = ({(0, 0): 'onsite', (0, 1): 'vertical_neighbor',
(1, 0): 'horizontal_neighbor'}
if distinguish_edges else
{(0, 0): 'onsite', (0, 1): 'neighbor',
(1, 0): 'neighbor'}
)[lattice.delta_mag(i, ii, True)]
edge_type = ({
(0, 0): 'onsite',
(0, 1): 'vertical_neighbor',
(1, 0): 'horizontal_neighbor'
} if distinguish_edges else {
(0, 0): 'onsite',
(0, 1): 'neighbor',
(1, 0): 'neighbor'
})[lattice.delta_mag(i, ii, True)]
dofs = tuple(sorted((a, aa)))
if len(term) == 2:
parameter = (edge_type, dofs, -coefficient)
assert parameter in parameters['tunneling_parameters']
terms_per_parameter['tunneling', parameter] += 1
else:
assert len(term) == 4
spin_pairs = (SpinPairs.ALL if lattice.spinless else
(SpinPairs.SAME if s == ss else SpinPairs.DIFF))
spin_pairs = (SpinPairs.ALL if lattice.spinless else
(SpinPairs.SAME if s == ss else SpinPairs.DIFF))
parameter = (edge_type, dofs, coefficient, spin_pairs)
assert parameter in parameters['interaction_parameters']
terms_per_parameter['interaction', parameter] += 1
Expand All @@ -163,19 +168,19 @@ def test_fermi_hubbard_square_lattice_random_parameters(
potential_coefficient = -coefficient
if not lattice.spinless:
spin = (-1) ** spin_index
potential_coefficient -= (
((-1) ** spin_index) *
parameters.get('magnetic_field', 0))
potential_coefficient -= (((-1)**spin_index) *
parameters.get('magnetic_field', 0))
if not potential_coefficient:
continue
parameter = (dof, potential_coefficient)
assert parameter in parameters['potential_parameters']
terms_per_parameter['potential', parameter] += 1
edge_type_to_n_site_pairs = {
'onsite': lattice.n_sites,
'neighbor': lattice.n_neighbor_pairs(False),
'vertical_neighbor': lattice.n_vertical_neighbor_pairs(False),
'horizontal_neighbor': lattice.n_horizontal_neighbor_pairs(False)}
'onsite': lattice.n_sites,
'neighbor': lattice.n_neighbor_pairs(False),
'vertical_neighbor': lattice.n_vertical_neighbor_pairs(False),
'horizontal_neighbor': lattice.n_horizontal_neighbor_pairs(False)
}
for (term_type, parameter), n_terms in terms_per_parameter.items():
if term_type == 'potential':
assert n_terms == lattice.n_sites * lattice.n_spin_values
Expand All @@ -191,8 +196,8 @@ def test_fermi_hubbard_square_lattice_random_parameters(
expected_n_terms *= len(set(parameter.dofs))
if not lattice.spinless:
assert parameter.spin_pairs != SpinPairs.ALL
if (parameter.edge_type == 'onsite' and
parameter.spin_pairs == SpinPairs.DIFF):
if (parameter.edge_type == 'onsite' and
parameter.spin_pairs == SpinPairs.DIFF):
expected_n_terms *= len(set(parameter.dofs))
else:
expected_n_terms *= 2
Expand Down
2 changes: 1 addition & 1 deletion src/openfermion/hamiltonians/_molecular_data.py
Expand Up @@ -972,7 +972,7 @@ def load_molecular_hamiltonian(
else:
n_core_orbitals = (molecule.n_electrons - n_active_electrons) // 2
occupied_indices = list(range(n_core_orbitals))

if n_active_orbitals is None:
active_indices = None
else:
Expand Down
2 changes: 1 addition & 1 deletion src/openfermion/measurements/_qubit_partitioning.py
Expand Up @@ -67,7 +67,7 @@ def binary_partition_iterator(qubit_list, num_iterations=None):
# which we delete.
if qubit_list[-1] is None:
del qubit_list[-1]


def partition_iterator(qubit_list, partition_size, num_iterations=None):
"""Generator for a list of k-partitions of N qubits such that
Expand Down
3 changes: 1 addition & 2 deletions src/openfermion/ops/_ising_operator_test.py
Expand Up @@ -30,7 +30,6 @@ class GeneralTest(unittest.TestCase):
def test_ising_operator(self):
equals_tester = EqualsTester(self)

group = [IsingOperator('Z0 Z3'),
IsingOperator([(0, 'Z'), (3, 'Z')])]
group = [IsingOperator('Z0 Z3'), IsingOperator([(0, 'Z'), (3, 'Z')])]

equals_tester.add_equality_group(*group)
2 changes: 1 addition & 1 deletion src/openfermion/ops/_majorana_operator.py
Expand Up @@ -96,7 +96,7 @@ def with_basis_rotated_by(self, transformation_matrix):
The input to this method is a real orthogonal matrix :math:`O`.
It returns a new MajoranaOperator which is equivalent to the old one
but rewritten in terms of a new basis of Majorana operators.
Let the original Majorana operators be denoted by
Let the original Majorana operators be denoted by
:math:`\gamma_i` and the new operators be denoted by
:math:`\tilde{\gamma_i}`. Then they are related by the equation
Expand Down
2 changes: 1 addition & 1 deletion src/openfermion/ops/_polynomial_tensor.py
Expand Up @@ -362,7 +362,7 @@ def projected_n_body_tensors(self, selection, exact=False):
selection (Union[int, Iterable[int]): If int, keeps terms with at
most (exactly, if exact is True) that many unique indices. If
iterable, keeps only terms containing (all of, if exact is
True) the specified indices.
True) the specified indices.
exact (bool): Whether or not the selection is strict.
"""
comparator = (operator.eq if exact else operator.le)
Expand Down
4 changes: 2 additions & 2 deletions src/openfermion/transforms/_binary_code_transform.py
Expand Up @@ -71,7 +71,7 @@ def dissolve(term):

def make_parity_list(code):
"""Create the parity list from the decoder of the input code.
The output parity list has a similar structure as code.decoder.
The output parity list has a similar structure as code.decoder.
Args:
code (BinaryCode): the code to extract the parity list from.
Expand Down Expand Up @@ -102,7 +102,7 @@ def binary_code_transform(hamiltonian, code):
property N>n, when the Fermion basis is smaller than the fermionic Fock
space. The binary_code_transform function can transform Fermion operators
to qubit operators for custom- and qubit-saving mappings.
Note:
Logic multi-qubit operators are decomposed into Pauli-strings (e.g.
CPhase(1,2) = 0.5 * (1 + Z1 + Z2 - Z1 Z2 ) ), which might increase
Expand Down

0 comments on commit d6b969b

Please sign in to comment.