Skip to content

Commit c218240

Browse files
committed
New CI
Change-Id: I46059d89717c8eca89b427576264f6d060179c9d
1 parent 560375e commit c218240

File tree

6 files changed

+141
-40
lines changed

6 files changed

+141
-40
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,46 @@ on:
2020
branches: [ master ]
2121

2222
jobs:
23-
build:
23+
pytest:
24+
name: Pytest
2425
runs-on: ubuntu-latest
2526
strategy:
2627
matrix:
27-
python-version: [3.7, 3.8]
28-
28+
cirq-version: [ 'previous', 'current', 'next' ]
29+
fail-fast: false
2930
steps:
30-
- uses: actions/checkout@v2
31-
32-
- name: Set up Python ${{ matrix.python-version }}
33-
uses: actions/setup-python@v1
34-
with:
35-
python-version: ${{ matrix.python-version }}
36-
37-
- name: Install dependencies
38-
run: |
39-
python -m pip install --upgrade pip
40-
pip install -r requirements.txt
41-
pip install -e .
42-
43-
- name: Lint with flake8
44-
run: |
45-
pip install flake8
46-
# stop the build if there are Python syntax errors or undefined names
47-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
48-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
49-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
31+
- uses: actions/checkout@v2
32+
- uses: actions/setup-python@v2
33+
with:
34+
python-version: '3.9'
35+
- name: Install dependencies
36+
run: |
37+
python -m pip install --upgrade pip
38+
python dev_tools/write-ci-requirements.py --relative-cirq-version=${{ matrix.cirq-version }} --all-extras
39+
pip install -r ci-requirements.txt
40+
pip install --no-deps -e .
5041
51-
- name: Test with pytest
52-
run: |
53-
pip install pytest
54-
# RECIRQ_CHESS_TEST_SEED: make quantum_chess tests determnistic
55-
RECIRQ_CHESS_TEST_SEED=789 pytest -v
42+
- name: Lint with flake8
43+
run: |
44+
pip install flake8
45+
# stop the build if there are Python syntax errors or undefined names
46+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
47+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
48+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
5649
50+
- name: Test with pytest
51+
run: |
52+
pip install pytest
53+
# RECIRQ_CHESS_TEST_SEED: make quantum_chess tests determnistic
54+
# RECIRQ_IGNORE_PYTKET: for 'next' cirq version, skip pytket tests
55+
RECIRQ_CHESS_TEST_SEED=789 RECIRQ_IGNORE_PYTKET=y pytest -v
5756
nbformat:
5857
name: Notebook formatting
5958
runs-on: ubuntu-latest
6059
steps:
6160
- uses: actions/checkout@v2
62-
- uses: actions/setup-python@v1
61+
- uses: actions/setup-python@v2
6362
with:
64-
python-version: '3.7'
65-
architecture: 'x64'
63+
python-version: '3.9'
6664
- name: Doc check
6765
run: dev_tools/nbfmt

dev_tools/write-ci-requirements.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import pathlib
2+
import re
3+
import sys
4+
from argparse import ArgumentParser
5+
from typing import Any, Dict, List
6+
7+
REPO_DIR = pathlib.Path(__file__).parent.parent.resolve()
8+
print('Repo dir:', REPO_DIR)
9+
10+
CIRQ_VERSIONS = {
11+
'previous': '==0.12.0',
12+
'current': '==0.13.0',
13+
'next': '>=0.14.0.dev',
14+
}
15+
"""Give names to relative Cirq versions so CI can have consistent names while versions
16+
get incremented."""
17+
18+
19+
def _parse_requirements(path: pathlib.Path):
20+
"""Read and strip comments from a requirements.txt-like file. """
21+
lines = [line.strip() for line in path.read_text().splitlines() if line]
22+
return [line for line in lines if not line.startswith('#')]
23+
24+
25+
def _remove_version_spec(req: str) -> str:
26+
"""Remove a version specifier like ==1.3 from a requirements.txt line."""
27+
components = re.split(r'>=|~=|<=|==', req)
28+
return components[0]
29+
30+
31+
def _set_cirq_version(core_reqs: List[str], relative_cirq_version: str) -> List[str]:
32+
"""Return a new version of `core_reqs` that pins cirq-like packages to the desired version."""
33+
cirq_version = CIRQ_VERSIONS[relative_cirq_version]
34+
to_change = 'cirq', 'cirq-google', 'cirq-core'
35+
36+
new_reqs = []
37+
for req in core_reqs:
38+
without_spec = _remove_version_spec(req)
39+
if without_spec in to_change:
40+
new_reqs.append(f'{without_spec}{cirq_version}')
41+
else:
42+
new_reqs.append(req)
43+
44+
return new_reqs
45+
46+
47+
def _set_qaoa_hacks(qaoa_reqs: List[str], relative_cirq_version: str) -> List[str]:
48+
"""Pytket pins to a specific cirq version and doesn't work with cirq "next".
49+
"""
50+
if relative_cirq_version != 'next':
51+
return qaoa_reqs
52+
53+
new_reqs = []
54+
for req in qaoa_reqs:
55+
without_spec = _remove_version_spec(req)
56+
if without_spec == 'pytket-cirq':
57+
continue
58+
else:
59+
new_reqs.append(req)
60+
61+
return new_reqs
62+
63+
64+
def main(*, out_fn: str = 'ci-requirements.txt', relative_cirq_version: str = 'current',
65+
all_extras: bool = False):
66+
"""Write a requirements.txt file for CI installation and testing.
67+
68+
Args:
69+
out_fn: The output filename
70+
relative_cirq_version: Pin the desired cirq version to either "current", "previous",
71+
or "next" version.
72+
all_extras: Whether to include all the extras_require dependencies.
73+
"""
74+
core_reqs = _parse_requirements(REPO_DIR / 'requirements.txt')
75+
core_reqs = _set_cirq_version(core_reqs, relative_cirq_version)
76+
77+
extras_require = [
78+
'otoc', 'qaoa', 'optimize', 'hfvqe', 'fermi_hubbard', 'qml_lfe'
79+
]
80+
extras_require = {
81+
r: _parse_requirements(pathlib.Path(REPO_DIR / f'recirq/{r}/extra-requirements.txt'))
82+
for r in extras_require
83+
}
84+
extras_require['qaoa'] = _set_qaoa_hacks(extras_require['qaoa'], relative_cirq_version)
85+
86+
lines = ['# Core requirements'] + core_reqs
87+
if all_extras:
88+
for name, reqs in extras_require.items():
89+
lines += ['', f'# {name}']
90+
lines += reqs
91+
lines += ['']
92+
93+
out = '\n'.join(lines)
94+
sys.stdout.write(out)
95+
(REPO_DIR / out_fn).write_text(out)
96+
97+
98+
def parse() -> Dict[str, Any]:
99+
parser = ArgumentParser()
100+
parser.add_argument('--out-fn', default='ci-requirements.txt')
101+
parser.add_argument('--relative-cirq-version', default='current')
102+
parser.add_argument('--all-extras', action='store_true', default=False)
103+
return vars(parser.parse_args())
104+
105+
106+
if __name__ == '__main__':
107+
main(**parse())
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
openfermion
1+
openfermion>=1.2.0

recirq/hfvqe/extra-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# fix bug with openfermionpyscf https://github.com/quantumlib/ReCirq/issues/200#issuecomment-923203883
22
h5py~=3.2.1
3-
openfermion~=1.2.0
3+
openfermion>=1.2.0

recirq/qaoa/extra-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pytket-cirq~=0.16.0
1+
pytket-cirq>=0.16

requirements.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
2-
# We'd like to only depend on cirq-core and cirq-google, but pip's dependency resolver
3-
# chokes since pytket depends on the cirq metapackage.
4-
cirq~=0.12.0
5-
cirq-core~=0.12.0
6-
cirq-google~=0.12.0
1+
cirq-core>=0.12.0
2+
cirq-google>=0.12.0
73
seaborn
84
sphinx
95
ipython

0 commit comments

Comments
 (0)