Skip to content

Commit

Permalink
Enable testing for resource estimates (#828)
Browse files Browse the repository at this point in the history
* Guard imports and skip tests if pyscf / jax not found.

* Upgrade ci to run resource estimate tests.

* Remove commented code.

* Fix formatting.

* Fix requirements filename.

* Remove argument from workflow.

* Remove comments from ci.

* Fix ci and ignore init in coverage.

* Attempt to fix windows issue using shorter ints.

* Fix formatting and overflow.

* Add rootdir to check/pytest.
  • Loading branch information
fdmalone authored Aug 4, 2023
1 parent bab637d commit 945bcaf
Show file tree
Hide file tree
Showing 27 changed files with 396 additions and 96 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ jobs:
- name: Pytest check
run: check/pytest
shell: bash
pytest_resource_estimates:
name: Pytest Resource Estimates
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: [ '3.10' ]
cirq-version: [ '~=1.0' ]
fail-fast: false
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install requirements
run: |
pip install -r dev_tools/requirements/resource_estimates.env.txt
pip install cirq-core${{matrix.cirq-version}}
- name: Pytest check resources
run: check/pytest -m 'not slow' src/openfermion/resource_estimates
shell: bash
coverage:
name: Coverage check
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ ENV/

# no .dat files
*.dat

*tmp*
8 changes: 5 additions & 3 deletions check/pytest
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$(git rev-parse --show-toplevel)"

rootdir=$(pwd)

PYTEST_ARGS=()
ACTUALLY_QUIET=""
for arg in $@; do
for arg in "$@"; do
if [[ "${arg}" == "--actually-quiet" ]]; then
ACTUALLY_QUIET=1
else
Expand All @@ -28,10 +30,10 @@ for arg in $@; do
done

if [ -z "${ACTUALLY_QUIET}" ]; then
pytest "${PYTEST_ARGS[@]}"
pytest -c dev_tools/conf/pytest.ini --rootdir=$rootdir "${PYTEST_ARGS[@]}"
else
# Filter out lines like "...F....x... [ 42%]", with coloring.
pytest -q --color=yes "${PYTEST_ARGS[@]}" |
pytest -c dev_tools/conf/pytest.ini --rootdir=$rootdir -q --color=yes "${PYTEST_ARGS[@]}" |
grep -Pv '^(.\[0m)?[\.FEsx]+(.\[36m)?\s+\[\s*\d+%\](.\[0m)?$'
exit "${PIPESTATUS[0]}"
fi
3 changes: 3 additions & 0 deletions dev_tools/conf/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
3 changes: 3 additions & 0 deletions dev_tools/requirements/deps/resource_estimates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pyscf
jax
jaxlib
1 change: 1 addition & 0 deletions dev_tools/requirements/re-pip-compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pip-compile $@ --output-file=pylint.env.txt --resolver=backtracking deps/pylint.
pip-compile $@ --output-file=pytest.env.txt --resolver=backtracking deps/pytest.txt runtime.env.txt
pip-compile $@ --output-file=mypy.env.txt --resolver=backtracking deps/mypy.txt runtime.env.txt
pip-compile $@ --output-file=dev.env.txt --resolver=backtracking deps/dev-tools.txt runtime.env.txt
pip-compile $@ --output-file=dev.env.txt --resolver=backtracking deps/resource_estimates.txt pytest.env.txt
218 changes: 218 additions & 0 deletions dev_tools/requirements/resource_estimates.env.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --output-file=resource_estimates.env.txt deps/resource_estimates.txt pytest.env.txt
#
attrs==23.1.0
# via
# -r pytest.env.txt
# jsonschema
certifi==2023.5.7
# via
# -r pytest.env.txt
# requests
charset-normalizer==3.1.0
# via
# -r pytest.env.txt
# requests
cirq-core==1.1.0
# via -r pytest.env.txt
contourpy==1.0.7
# via
# -r pytest.env.txt
# matplotlib
coverage[toml]==7.2.5
# via
# -r pytest.env.txt
# pytest-cov
cycler==0.11.0
# via
# -r pytest.env.txt
# matplotlib
deprecation==2.1.0
# via -r pytest.env.txt
duet==0.2.8
# via
# -r pytest.env.txt
# cirq-core
exceptiongroup==1.1.1
# via
# -r pytest.env.txt
# pytest
execnet==1.9.0
# via
# -r pytest.env.txt
# pytest-xdist
fastjsonschema==2.17.1
# via
# -r pytest.env.txt
# nbformat
fonttools==4.39.4
# via
# -r pytest.env.txt
# matplotlib
h5py==3.8.0
# via
# -r pytest.env.txt
# pyscf
idna==3.4
# via
# -r pytest.env.txt
# requests
iniconfig==2.0.0
# via
# -r pytest.env.txt
# pytest
jax==0.4.14
# via -r deps/resource_estimates.txt
jaxlib==0.4.14
# via -r deps/resource_estimates.txt
jsonschema==4.17.3
# via
# -r pytest.env.txt
# nbformat
jupyter-core==5.3.0
# via
# -r pytest.env.txt
# nbformat
kiwisolver==1.4.4
# via
# -r pytest.env.txt
# matplotlib
matplotlib==3.7.1
# via
# -r pytest.env.txt
# cirq-core
ml-dtypes==0.2.0
# via
# jax
# jaxlib
mpmath==1.3.0
# via
# -r pytest.env.txt
# sympy
nbformat==5.8.0
# via -r pytest.env.txt
networkx==2.8.8
# via
# -r pytest.env.txt
# cirq-core
numpy==1.23.5
# via
# -r pytest.env.txt
# cirq-core
# contourpy
# h5py
# jax
# jaxlib
# matplotlib
# ml-dtypes
# opt-einsum
# pandas
# pyscf
# scipy
opt-einsum==3.3.0
# via jax
packaging==23.1
# via
# -r pytest.env.txt
# deprecation
# matplotlib
# pytest
pandas==2.0.1
# via
# -r pytest.env.txt
# cirq-core
pillow==9.5.0
# via
# -r pytest.env.txt
# matplotlib
platformdirs==3.5.1
# via
# -r pytest.env.txt
# jupyter-core
pluggy==1.0.0
# via
# -r pytest.env.txt
# pytest
pubchempy==1.0.4
# via -r pytest.env.txt
pyparsing==3.0.9
# via
# -r pytest.env.txt
# matplotlib
pyrsistent==0.19.3
# via
# -r pytest.env.txt
# jsonschema
pyscf==2.3.0
# via -r deps/resource_estimates.txt
pytest==7.3.1
# via
# -r pytest.env.txt
# pytest-asyncio
# pytest-cov
# pytest-xdist
pytest-asyncio==0.21.0
# via -r pytest.env.txt
pytest-cov==4.0.0
# via -r pytest.env.txt
pytest-xdist==3.3.1
# via -r pytest.env.txt
python-dateutil==2.8.2
# via
# -r pytest.env.txt
# matplotlib
# pandas
pytz==2023.3
# via
# -r pytest.env.txt
# pandas
requests==2.31.0
# via -r pytest.env.txt
scipy==1.9.3
# via
# -r pytest.env.txt
# cirq-core
# jax
# jaxlib
# pyscf
six==1.16.0
# via
# -r pytest.env.txt
# python-dateutil
sortedcontainers==2.4.0
# via
# -r pytest.env.txt
# cirq-core
sympy==1.12
# via
# -r pytest.env.txt
# cirq-core
tomli==2.0.1
# via
# -r pytest.env.txt
# coverage
# pytest
tqdm==4.65.0
# via
# -r pytest.env.txt
# cirq-core
traitlets==5.9.0
# via
# -r pytest.env.txt
# jupyter-core
# nbformat
typing-extensions==4.6.0
# via
# -r pytest.env.txt
# cirq-core
tzdata==2023.3
# via
# -r pytest.env.txt
# pandas
urllib3==2.0.2
# via
# -r pytest.env.txt
# requests
10 changes: 5 additions & 5 deletions src/openfermion/resource_estimates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#coverage:ignore
# coverage:ignore
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -10,10 +10,10 @@
# 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.
#coverage: ignore
import pytest

try:
import jax
import pyscf
except ImportError:
pytest.skip('Need pyscf for resource estimates', allow_module_level=True)
HAVE_DEPS_FOR_RESOURCE_ESTIMATES = True
except ModuleNotFoundError:
HAVE_DEPS_FOR_RESOURCE_ESTIMATES = False
11 changes: 7 additions & 4 deletions src/openfermion/resource_estimates/df/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .compute_lambda_df import compute_lambda
from .compute_cost_df import compute_cost
from .factorize_df import factorize
from .generate_costing_table_df import generate_costing_table
from openfermion.resource_estimates import HAVE_DEPS_FOR_RESOURCE_ESTIMATES

if HAVE_DEPS_FOR_RESOURCE_ESTIMATES:
from .compute_cost_df import compute_cost
from .compute_lambda_df import compute_lambda
from .factorize_df import factorize
from .generate_costing_table_df import generate_costing_table
11 changes: 10 additions & 1 deletion src/openfermion/resource_estimates/df/compute_cost_df_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#coverage:ignore
"""Test cases for costing_df.py
"""
from openfermion.resource_estimates import df
import pytest

from openfermion.resource_estimates import HAVE_DEPS_FOR_RESOURCE_ESTIMATES

if HAVE_DEPS_FOR_RESOURCE_ESTIMATES:
from openfermion.resource_estimates import df


@pytest.mark.skipif(not HAVE_DEPS_FOR_RESOURCE_ESTIMATES,
reason='pyscf and/or jax not installed.')
def test_reiher_df():
""" Reproduce Reiher et al orbital DF FT costs from paper """
DE = 0.001
Expand All @@ -25,6 +32,8 @@ def test_reiher_df():
assert output == (21753, 10073183463, 3725)


@pytest.mark.skipif(not HAVE_DEPS_FOR_RESOURCE_ESTIMATES,
reason='pyscf and/or jax not installed.')
def test_li_df():
""" Reproduce Li et al orbital DF FT costs from paper """
DE = 0.001
Expand Down
11 changes: 9 additions & 2 deletions src/openfermion/resource_estimates/df/compute_lambda_df_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
"""Test cases for compute_lambda_df.py
"""
from os import path

import numpy as np
from openfermion.resource_estimates import df
from openfermion.resource_estimates.molecule import load_casfile_to_pyscf
import pytest

from openfermion.resource_estimates import HAVE_DEPS_FOR_RESOURCE_ESTIMATES, df

if HAVE_DEPS_FOR_RESOURCE_ESTIMATES:
from openfermion.resource_estimates.molecule import load_casfile_to_pyscf


@pytest.mark.skipif(not HAVE_DEPS_FOR_RESOURCE_ESTIMATES,
reason="pyscf and/or jax not installed.")
def test_reiher_df_lambda():
""" Reproduce Reiher et al orbital DF lambda from paper """

Expand Down
Loading

0 comments on commit 945bcaf

Please sign in to comment.