Skip to content

Commit ce43d75

Browse files
authored
Merge branch 'main' into add-pr-text-checker
2 parents 89f2827 + 5e4855e commit ce43d75

8 files changed

Lines changed: 188 additions & 380 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 130 additions & 322 deletions
Large diffs are not rendered by default.

.github/workflows/scorecard-scanner.yaml

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# yamllint disable rule:line-length
16-
1715
name: Scorecard analysis
1816
run-name: Run Scorecard scanner for security best practices
1917

@@ -34,11 +32,6 @@ on:
3432

3533
# Allow manual invocation.
3634
workflow_dispatch:
37-
inputs:
38-
debug:
39-
description: 'Run with debugging options'
40-
type: boolean
41-
default: true
4235

4336
concurrency:
4437
# Cancel any previously-started but still active runs on the same branch.
@@ -50,7 +43,11 @@ permissions: read-all
5043

5144
jobs:
5245
run-scorecard:
53-
if: github.repository_owner == 'quantumlib'
46+
# Skip fork PRs to avoid "Analysis configuration not found" errors.
47+
if: >-
48+
github.repository_owner == 'quantumlib' &&
49+
(github.event_name != 'pull_request' ||
50+
github.event.pull_request.head.repo.fork == false)
5451
name: Scorecard analyzer
5552
runs-on: ubuntu-24.04
5653
permissions:
@@ -64,33 +61,21 @@ jobs:
6461
persist-credentials: false
6562

6663
- name: Run Scorecard analysis
64+
# yamllint disable rule:line-length
6765
uses: ossf/scorecard-action@4eaacf0543bb3f2c246792bd56e8cdeffafb205a # v2.4.3
6866
with:
69-
# Save the results
7067
results_file: scorecard-results.sarif
7168
results_format: sarif
72-
# Only publish results for non-fork PRs or scheduled runs.
73-
publish_results: >-
74-
${{github.event_name != 'pull_request'
75-
|| github.event.pull_request.head.repo.fork == false}}
69+
publish_results: true
7670

7771
- name: Upload results to code-scanning dashboard
78-
# Skip upload for fork PRs to avoid "Analysis configuration not found" / 404 errors.
79-
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false
80-
uses: github/codeql-action/upload-sarif@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
72+
# yamllint disable rule:line-length
73+
uses: github/codeql-action/upload-sarif@95e58e9a2cdfd71adc6e0353d5c52f41a045d225 # v4.35.2
8174
with:
8275
sarif_file: scorecard-results.sarif
8376

84-
- if: github.event.inputs.debug == true || runner.debug == true
85-
name: Upload results as artifacts to the workflow Summary page
86-
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
87-
with:
88-
name: Scorecard SARIF file
89-
path: scorecard-results.sarif
90-
retention-days: 5
91-
92-
# Scorecard currently (ver. 2.4.x) doesn't allow submissions from jobs having
93-
# steps that use "run:". To print to the summary, we need to use another job.
77+
# Scorecard doesn't allow submissions from jobs having steps that use "run:".
78+
# Printing a summary needs to use "run:", so we have to use a separate job.
9479
write-summary:
9580
name: Scorecard results
9681
needs: run-scorecard

docs/fqe/tutorials/hamiltonian_time_evolution_and_expectation_estimation.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285
"cell_type": "markdown",
286286
"metadata": {},
287287
"source": [
288-
"The BCS hamiltonian evovles spin conserved and number broken wavefunctions."
288+
"The BCS hamiltonian evolves spin conserved and number broken wavefunctions."
289289
]
290290
},
291291
{
@@ -357,7 +357,7 @@
357357
"cell_type": "markdown",
358358
"metadata": {},
359359
"source": [
360-
"Exact evolution of individual n-body anti-Hermitian gnerators"
360+
"Exact evolution of individual n-body anti-Hermitian generators"
361361
]
362362
},
363363
{

src/openfermion/chem/molecular_data.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
+ 0.5 * \sum_{p,q,r,s} h[p,q,r,s] a_p^\dagger a_q^\dagger a_r a_s
3232
"""
3333

34-
# Define a compatible basestring for checking between Python 2 and 3
35-
try:
36-
basestring
37-
except NameError: # pragma: no cover
38-
basestring = str
39-
4034

4135
# Define error objects which inherit from Exception.
4236
class MoleculeNameError(Exception):
@@ -248,7 +242,7 @@ def name_molecule(geometry, basis, multiplicity, charge, description):
248242
Raises:
249243
MoleculeNameError: If spin multiplicity is not valid.
250244
"""
251-
if not isinstance(geometry, basestring):
245+
if not isinstance(geometry, str):
252246
# Get sorted atom vector.
253247
atoms = [item[0] for item in geometry]
254248
atom_charge_info = [(atom, atoms.count(atom)) for atom in set(atoms)]
@@ -501,7 +495,7 @@ def __init__(
501495

502496
# Metadata fields with default values.
503497
self.charge = charge
504-
if not isinstance(description, basestring):
498+
if not isinstance(description, str):
505499
raise TypeError("description must be a string.")
506500
self.description = description
507501

@@ -518,7 +512,7 @@ def __init__(
518512
self.filename = data_directory + '/' + self.name
519513

520514
# Attributes generated automatically by class.
521-
if not isinstance(geometry, basestring):
515+
if not isinstance(geometry, str):
522516
self.n_atoms = len(geometry)
523517
self.atoms = sorted(
524518
[row[0] for row in geometry], key=lambda atom: periodic_hash_table[atom]
@@ -707,7 +701,7 @@ def save(self):
707701
with h5py.File("{}.hdf5".format(tmp_name), "w") as f:
708702
# Save geometry (atoms and positions need to be separate):
709703
d_geom = f.create_group("geometry")
710-
if not isinstance(self.geometry, basestring):
704+
if not isinstance(self.geometry, str):
711705
atoms = [numpy.bytes_(item[0]) for item in self.geometry]
712706
positions = numpy.array([list(item[1]) for item in self.geometry])
713707
else:
@@ -909,11 +903,10 @@ def load(self):
909903
key.tobytes().decode('utf-8'): value
910904
for key, value in zip(keys[...], values[...])
911905
}
906+
else:
907+
self.general_calculations = {}
912908
else:
913-
# TODO: test the no cover
914-
# no coverage here because pathway is check on
915-
# bad user generated file
916-
self.general_calculations = None # pragma: nocover
909+
self.general_calculations = {}
917910

918911
def get_from_file(self, property_name):
919912
"""Helper routine to re-open HDF5 file and pull out single property.

src/openfermion/chem/molecular_data_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
"""Tests for molecular_data."""
1313

1414
import os
15+
import tempfile
1516
import unittest
17+
from unittest.mock import patch
18+
import h5py
1619
import numpy.random
1720
import scipy.linalg
1821
import numpy as np
@@ -347,3 +350,32 @@ def test_missing_calcs_for_integrals(self):
347350
molecule.get_k()
348351
with self.assertRaises(MissingCalculationError):
349352
molecule.get_antisym()
353+
354+
def test_load_no_general_calculations(self):
355+
# Make fake molecule.
356+
with tempfile.NamedTemporaryFile(suffix='.hdf5') as tmp_file:
357+
filename = tmp_file.name
358+
geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))]
359+
basis = '6-31g*'
360+
multiplicity = 1
361+
molecule = MolecularData(geometry, basis, multiplicity, filename=filename)
362+
molecule.save()
363+
364+
# Manually remove the general_calculations_keys dataset.
365+
with h5py.File(filename, 'r+') as f:
366+
del f['general_calculations_keys']
367+
368+
# Load the molecule and check that general_calculations is empty.
369+
new_molecule = MolecularData(filename=filename)
370+
self.assertEqual(new_molecule.general_calculations, {})
371+
372+
def test_get_from_file_exceptions(self):
373+
with patch('h5py.File') as mock_file:
374+
mock_file.return_value.__enter__.return_value.__getitem__.side_effect = KeyError
375+
data = self.molecule.get_from_file("nonexistent_property")
376+
self.assertIsNone(data)
377+
378+
with patch('h5py.File') as mock_file:
379+
mock_file.side_effect = IOError
380+
data = self.molecule.get_from_file("any_property")
381+
self.assertIsNone(data)

src/openfermion/chem/pubchem_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
"""Tests for pubchem.py."""
1414

15-
import time
1615
import unittest
1716
from unittest.mock import patch
1817

1918
import numpy
2019
import pytest
20+
import pubchempy
2121

2222
from openfermion.chem.pubchem import geometry_from_pubchem
2323
from openfermion.testing.testing_utils import module_importable
@@ -152,7 +152,7 @@ def test_water_2d(self):
152152
with pytest.raises(ValueError, match='Incorrect value for the argument structure'):
153153
_ = geometry_from_pubchem('water', structure='foo')
154154

155-
@pytest.mark.flaky(retries=3, delay=2)
155+
@pytest.mark.flaky(retries=3, delay=2, only_on=[pubchempy.ServerBusyError])
156156
def test_geometry_from_pubchem_live_api(self):
157-
water_geometry = geometry_from_pubchem('water') # pragma: no cover
158-
self.assertEqual(len(water_geometry), 3) # pragma: no cover
157+
water_geometry = geometry_from_pubchem('water')
158+
self.assertEqual(len(water_geometry), 3)

src/openfermion/ops/operators/binary_code.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
from openfermion.ops.operators import BinaryPolynomial
2121

22-
# import openfermion.ops.operators._binary_polynomial as bp
23-
2422

2523
def shift_decoder(decoder, shift_constant):
2624
"""Shifts the indices of a decoder by a constant.

src/openfermion/testing/testing_utils.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,9 @@ def module_importable(module):
338338
bool
339339
340340
"""
341-
import sys
341+
from importlib import util
342342

343-
if sys.version_info >= (3, 4):
344-
from importlib import util
345-
346-
plug_spec = util.find_spec(module)
347-
else:
348-
# Won't enter unless Python<3.4, so ignore for testing
349-
import pkgutil # pragma: ignore
350-
351-
plug_spec = pkgutil.find_loader(module) # pragma: no cover
343+
plug_spec = util.find_spec(module)
352344
if plug_spec is None:
353345
return False
354346
else:

0 commit comments

Comments
 (0)