Skip to content

Commit

Permalink
Reformat src/openfermion using black formatter. (#846)
Browse files Browse the repository at this point in the history
* Add black / isort configuration to pyproject.toml.

* Fix configuration.

* Reformat using black.

* Ignore reformat for git blame.

* Up max line lenght in pylintrc.
  • Loading branch information
fdmalone authored Nov 22, 2023
1 parent 85e533e commit 0c3c528
Show file tree
Hide file tree
Showing 314 changed files with 12,874 additions and 13,356 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# migrating to the black formatter
5cd8b96e605039af1496b6ad97ea490ef2fa7b82
10 changes: 7 additions & 3 deletions src/openfermion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@
make_reduced_hamiltonian,
)

from openfermion.functionals import (contextuality, get_one_norm_mol,
get_one_norm_mol_woconst, get_one_norm_int,
get_one_norm_int_woconst)
from openfermion.functionals import (
contextuality,
get_one_norm_mol,
get_one_norm_mol_woconst,
get_one_norm_int,
get_one_norm_int_woconst,
)

from openfermion.hamiltonians import (
FermiHubbardModel,
Expand Down
7 changes: 3 additions & 4 deletions src/openfermion/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import warnings


def wrap_module(module: ModuleType,
deprecated_attributes: Dict[str, Tuple[str, str]]):
def wrap_module(module: ModuleType, deprecated_attributes: Dict[str, Tuple[str, str]]):
"""Wrap a module with deprecated attributes.
Args:
Expand All @@ -30,7 +29,6 @@ def wrap_module(module: ModuleType,
"""

class Wrapped(ModuleType):

__dict__ = module.__dict__

def __getattr__(self, name):
Expand All @@ -42,7 +40,8 @@ def __getattr__(self, name):
f'openfermion {version}.\n'
f'{fix}\n',
DeprecationWarning,
stacklevel=2)
stacklevel=2,
)
return getattr(module, name)

return Wrapped(module.__name__, module.__doc__)
5 changes: 1 addition & 4 deletions src/openfermion/_compat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def f():


def test_deprecated_test():

@deprecated_test
def test():
f()
Expand All @@ -63,14 +62,12 @@ def test():

def test_wrap_module():
openfermion.deprecated_attribute = None
wrapped_openfermion = wrap_module(openfermion,
{'deprecated_attribute': ('', '')})
wrapped_openfermion = wrap_module(openfermion, {'deprecated_attribute': ('', '')})
with pytest.deprecated_call():
_ = wrapped_openfermion.deprecated_attribute


def test_cirq_deprecations():

@deprecated(deadline="v0.12", fix="use new_func")
def old_func():
pass
Expand Down
25 changes: 15 additions & 10 deletions src/openfermion/chem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .chemical_series import (
make_atom,
make_atomic_lattice,
make_atomic_ring,
)
from .chemical_series import make_atom, make_atomic_lattice, make_atomic_ring

from .molecular_data import (angstroms_to_bohr, bohr_to_angstroms,
MolecularData, name_molecule, geometry_from_file,
load_molecular_hamiltonian, periodic_table,
periodic_hash_table, periodic_polarization,
antisymtei, j_mat, k_mat)
from .molecular_data import (
angstroms_to_bohr,
bohr_to_angstroms,
MolecularData,
name_molecule,
geometry_from_file,
load_molecular_hamiltonian,
periodic_table,
periodic_hash_table,
periodic_polarization,
antisymtei,
j_mat,
k_mat,
)

from .pubchem import geometry_from_pubchem

Expand Down
45 changes: 18 additions & 27 deletions src/openfermion/chem/chemical_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@

import numpy

from openfermion.chem.molecular_data import (MolecularData, periodic_hash_table,
periodic_polarization)
from openfermion.chem.molecular_data import (
MolecularData,
periodic_hash_table,
periodic_polarization,
)


# Define error objects which inherit from Exception.
class MolecularLatticeError(Exception):
pass


def make_atomic_ring(n_atoms,
spacing,
basis,
atom_type='H',
charge=0,
filename=''):
def make_atomic_ring(n_atoms, spacing, basis, atom_type='H', charge=0, filename=''):
"""Function to create atomic rings with n_atoms.
Note that basic geometry suggests that for spacing L between atoms
Expand All @@ -47,36 +45,30 @@ def make_atomic_ring(n_atoms,
"""
# Make geometry.
geometry = []
theta = 2. * numpy.pi / float(n_atoms)
radius = spacing / (2. * numpy.cos(numpy.pi / 2. - theta / 2.))
theta = 2.0 * numpy.pi / float(n_atoms)
radius = spacing / (2.0 * numpy.cos(numpy.pi / 2.0 - theta / 2.0))
for atom in range(n_atoms):
x_coord = radius * numpy.cos(atom * theta)
y_coord = radius * numpy.sin(atom * theta)
geometry += [(atom_type, (x_coord, y_coord, 0.))]
geometry += [(atom_type, (x_coord, y_coord, 0.0))]

# Set multiplicity.
n_electrons = n_atoms * periodic_hash_table[atom_type]
n_electrons -= charge
if (n_electrons % 2):
if n_electrons % 2:
multiplicity = 2
else:
multiplicity = 1

# Create molecule and return.
description = 'ring_{}'.format(spacing)
molecule = MolecularData(geometry, basis, multiplicity, charge, description,
filename)
molecule = MolecularData(geometry, basis, multiplicity, charge, description, filename)
return molecule


def make_atomic_lattice(nx_atoms,
ny_atoms,
nz_atoms,
spacing,
basis,
atom_type='H',
charge=0,
filename=''):
def make_atomic_lattice(
nx_atoms, ny_atoms, nz_atoms, spacing, basis, atom_type='H', charge=0, filename=''
):
"""Function to create atomic lattice with n_atoms.
Args:
Expand Down Expand Up @@ -110,7 +102,7 @@ def make_atomic_lattice(nx_atoms,
n_atoms = nx_atoms * ny_atoms * nz_atoms
n_electrons = n_atoms * periodic_hash_table[atom_type]
n_electrons -= charge
if (n_electrons % 2):
if n_electrons % 2:
multiplicity = 2
else:
multiplicity = 1
Expand All @@ -127,8 +119,7 @@ def make_atomic_lattice(nx_atoms,
raise MolecularLatticeError('Invalid lattice dimensions.')

# Create molecule and return.
molecule = MolecularData(geometry, basis, multiplicity, charge, description,
filename)
molecule = MolecularData(geometry, basis, multiplicity, charge, description, filename)
return molecule


Expand All @@ -142,9 +133,9 @@ def make_atom(atom_type, basis, filename=''):
Returns:
atom: An instance of the MolecularData class.
"""
geometry = [(atom_type, (0., 0., 0.))]
geometry = [(atom_type, (0.0, 0.0, 0.0))]
atomic_number = periodic_hash_table[atom_type]
spin = periodic_polarization[atomic_number] / 2.
spin = periodic_polarization[atomic_number] / 2.0
multiplicity = int(2 * spin + 1)
atom = MolecularData(geometry, basis, multiplicity, filename=filename)
return atom
40 changes: 19 additions & 21 deletions src/openfermion/chem/chemical_series_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@
import unittest
import numpy

from openfermion.chem.chemical_series import (MolecularLatticeError,
make_atomic_lattice, make_atom,
make_atomic_ring)
from openfermion.chem.molecular_data import (periodic_table,
periodic_polarization)
from openfermion.chem.chemical_series import (
MolecularLatticeError,
make_atomic_lattice,
make_atom,
make_atomic_ring,
)
from openfermion.chem.molecular_data import periodic_table, periodic_polarization


class ChemicalSeries(unittest.TestCase):

def test_make_atomic_ring(self):
spacing = 1.
spacing = 1.0
basis = 'sto-3g'
for n_atoms in range(2, 10):
molecule = make_atomic_ring(n_atoms, spacing, basis)

# Check that ring is centered.
vector_that_should_sum_to_zero = 0.
vector_that_should_sum_to_zero = 0.0
for atom in molecule.geometry:
for coordinate in atom[1]:
vector_that_should_sum_to_zero += coordinate
self.assertAlmostEqual(vector_that_should_sum_to_zero, 0.)
self.assertAlmostEqual(vector_that_should_sum_to_zero, 0.0)

# Check that the spacing between the atoms is correct.
for atom_index in range(n_atoms):
Expand All @@ -44,18 +45,18 @@ def test_make_atomic_ring(self):
atom_a = molecule.geometry[atom_index - 1]
coords_a = atom_a[1]
observed_spacing = numpy.sqrt(
numpy.square(coords_b[0] - coords_a[0]) +
numpy.square(coords_b[1] - coords_a[1]) +
numpy.square(coords_b[2] - coords_a[2]))
numpy.square(coords_b[0] - coords_a[0])
+ numpy.square(coords_b[1] - coords_a[1])
+ numpy.square(coords_b[2] - coords_a[2])
)
self.assertAlmostEqual(observed_spacing, spacing)

def test_make_atomic_lattice_1d(self):
spacing = 1.7
basis = 'sto-3g'
atom_type = 'H'
for n_atoms in range(2, 10):
molecule = make_atomic_lattice(n_atoms, 1, 1, spacing, basis,
atom_type)
molecule = make_atomic_lattice(n_atoms, 1, 1, spacing, basis, atom_type)

# Check that the spacing between the atoms is correct.
for atom_index in range(n_atoms):
Expand All @@ -73,8 +74,7 @@ def test_make_atomic_lattice_2d(self):
basis = 'sto-3g'
atom_type = 'H'
atom_dim = 7
molecule = make_atomic_lattice(atom_dim, atom_dim, 1, spacing, basis,
atom_type)
molecule = make_atomic_lattice(atom_dim, atom_dim, 1, spacing, basis, atom_type)

# Check that the spacing between the atoms is correct.
for atom in range(atom_dim**2):
Expand All @@ -93,8 +93,7 @@ def test_make_atomic_lattice_3d(self):
basis = 'sto-3g'
atom_type = 'H'
atom_dim = 4
molecule = make_atomic_lattice(atom_dim, atom_dim, atom_dim, spacing,
basis, atom_type)
molecule = make_atomic_lattice(atom_dim, atom_dim, atom_dim, spacing, basis, atom_type)

# Check that the spacing between the atoms is correct.
for atom in range(atom_dim**3):
Expand All @@ -118,15 +117,14 @@ def test_make_atomic_lattice_0d_raise_error(self):
atom_type = 'H'
atom_dim = 0
with self.assertRaises(MolecularLatticeError):
make_atomic_lattice(atom_dim, atom_dim, atom_dim, spacing, basis,
atom_type)
make_atomic_lattice(atom_dim, atom_dim, atom_dim, spacing, basis, atom_type)

def test_make_atom(self):
basis = 'sto-3g'
largest_atom = 30
for n_electrons in range(1, largest_atom):
atom_name = periodic_table[n_electrons]
atom = make_atom(atom_name, basis)
expected_spin = periodic_polarization[n_electrons] / 2.
expected_spin = periodic_polarization[n_electrons] / 2.0
expected_multiplicity = int(2 * expected_spin + 1)
self.assertAlmostEqual(expected_multiplicity, atom.multiplicity)
Loading

0 comments on commit 0c3c528

Please sign in to comment.