Skip to content

Commit d7bd940

Browse files
authoredFeb 19, 2021
Airspeed Velocity (asv) performance benchmarks setup: part-1 (quantumlib#3822)
* adds asv benchmarks, asv config, readme.md * removes old pytest-benchmarks, configs and requirements.
1 parent f2bf60f commit d7bd940

18 files changed

+188
-120
lines changed
 

‎.dockerignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ rtd_docs/generated
2929
# Mac only
3030
.DS_Store
3131

32-
# From pytest-benchmark
33-
*.benchmarks/
32+
# From asv
33+
*.asv/
3434

3535
# Ignore generated Jupyter files
3636
*.ipynb_checkpoints/

‎.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
# TODO: move to requirements.txt after #3704
122122
pip install codeowners==0.1.2 # linux only package
123123
- name: Pytest check
124-
run: check/pytest --ignore=cirq/contrib --benchmark-skip --actually-quiet
124+
run: check/pytest --ignore=cirq/contrib --actually-quiet
125125
build_docs:
126126
name: Build docs
127127
runs-on: ubuntu-16.04
@@ -189,7 +189,7 @@ jobs:
189189
pip install -r requirements.txt
190190
pip install -r dev_tools/conf/pip-list-dev-tools.txt
191191
- name: Pytest Windows
192-
run: check/pytest --ignore=cirq/contrib --benchmark-skip --actually-quiet
192+
run: check/pytest --ignore=cirq/contrib --actually-quiet
193193
shell: bash
194194
macos:
195195
name: Pytest MacOS
@@ -208,7 +208,7 @@ jobs:
208208
pip install -r requirements.txt
209209
pip install -r dev_tools/conf/pip-list-dev-tools.txt
210210
- name: Pytest check
211-
run: check/pytest --ignore=cirq/contrib --benchmark-skip
211+
run: check/pytest --ignore=cirq/contrib
212212
notebooks-stable:
213213
name: Changed Notebooks Isolated Test against Cirq stable
214214
runs-on: ubuntu-16.04

‎.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ docs/api_docs
3232
# Mac only
3333
.DS_Store
3434

35-
# From pytest-benchmark
36-
*.benchmarks/
35+
# From asv
36+
*.asv/
3737

3838
# Ignore generated Jupyter files
3939
*.ipynb_checkpoints/

‎asv.conf.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// See https://asv.readthedocs.io/en/stable/asv.conf.json.html for documentation.
3+
"version": 1,
4+
"project": "Cirq",
5+
"project_url": "https://quantumai.google/cirq",
6+
"repo": ".",
7+
"branches": ["master"],
8+
"dvcs": "git",
9+
"environment_type": "virtualenv",
10+
"show_commit_url": "https://github.com/quantumlib/Cirq/commit/",
11+
"pythons": ["3.6", "3.7", "3.8"],
12+
"benchmark_dir": "benchmarks",
13+
"env_dir": ".asv/env",
14+
"results_dir": ".asv/results",
15+
"html_dir": ".asv/html",
16+
"hash_length": 8,
17+
}

‎benchmarks/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Cirq Performance Benchmarks
2+
Performance benchmarking Cirq with [Airspeed Velocity](https://asv.readthedocs.io/en/stable/index.html).
3+
4+
## Overview
5+
The benchmark files (`bench_*.py`) stored in the current package (`benchmarks/*`) are used by asv to run benchmark tests for Cirq. For more information on how to write new benchmarks, please refer [Writing benchmarks guide by ASV](https://asv.readthedocs.io/en/stable/writing_benchmarks.html)
6+
7+
## Usage
8+
To run all benchmarks, navigate to the root Cirq directory at the command line and execute
9+
10+
```bash
11+
./check/asv_run
12+
```
13+
14+
You can also pass arguments to the script, which would be forwarded to the `asv run` command. For eg:
15+
```bash
16+
./check/asv_run --quick --bench bench_examples --python 3.8
17+
```
18+
19+
Please refer [Running Benchmarks guide by ASV](https://asv.readthedocs.io/en/stable/using.html#running-benchmarks) for more information.
20+
21+
## Results Database
22+
TODO([#3838](https://github.com/quantumlib/Cirq/issues/3838)): Add details regarding GCP setup.

‎benchmarks/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2021 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

‎benchmarks/bench_examples.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2021 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from sys import path
16+
from os.path import dirname as dir
17+
18+
path.append(dir(path[0]))
19+
20+
import examples.bernstein_vazirani
21+
import examples.place_on_bristlecone
22+
import examples.hello_qubit
23+
import examples.bell_inequality
24+
import examples.quantum_fourier_transform
25+
import examples.bcs_mean_field
26+
import examples.grover
27+
import examples.phase_estimator
28+
import examples.quantum_teleportation
29+
import examples.superdense_coding
30+
31+
32+
class ExamplesTest:
33+
"""Benchmark algorithms in Cirq/examples/ using ASV."""
34+
35+
def time_example_runs_bernstein_vazirani_perf(self):
36+
examples.bernstein_vazirani.main(qubit_count=3)
37+
38+
def time_example_runs_hello_line_perf(self):
39+
examples.place_on_bristlecone.main()
40+
41+
def time_example_runs_hello_qubit_perf(self):
42+
examples.hello_qubit.main()
43+
44+
def time_example_runs_bell_inequality_perf(self):
45+
examples.bell_inequality.main()
46+
47+
def time_example_runs_quantum_fourier_transform_perf(self):
48+
examples.quantum_fourier_transform.main()
49+
50+
def time_example_runs_bcs_mean_field_perf(self):
51+
examples.bcs_mean_field.main()
52+
53+
def time_example_runs_grover_perf(self):
54+
examples.grover.main()
55+
56+
def time_example_runs_phase_estimator_perf(self):
57+
examples.phase_estimator.main(qnums=(2,), repetitions=2)
58+
59+
def time_example_runs_quantum_teleportation(self):
60+
examples.quantum_teleportation.main()
61+
62+
def time_example_runs_superdense_coding(self):
63+
examples.superdense_coding.main()
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2021 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import numpy as np
16+
17+
import cirq
18+
19+
# yapf: disable
20+
SWAP = np.array([[1, 0, 0, 0],
21+
[0, 0, 1, 0],
22+
[0, 1, 0, 0],
23+
[0, 0, 0, 1]])
24+
CNOT = np.array([[1, 0, 0, 0],
25+
[0, 1, 0, 0],
26+
[0, 0, 0, 1],
27+
[0, 0, 1, 0]])
28+
CZ = np.diag([1, 1, 1, -1])
29+
# yapf: enable
30+
31+
32+
def time_kak_decomposition(target):
33+
"""Benchmark kak_decomposition
34+
kak_decomposition is benchmarked because it was historically slow.
35+
See https://github.com/quantumlib/Cirq/issues/3840 for status of other benchmarks.
36+
"""
37+
cirq.kak_decomposition(target)
38+
39+
40+
time_kak_decomposition.params = [ # type: ignore
41+
[np.eye(4), SWAP, SWAP * 1j, CZ, CNOT, SWAP.dot(CZ)]
42+
+ [cirq.testing.random_unitary(4) for _ in range(10)]
43+
]
44+
time_kak_decomposition.param_names = ["gate"] # type: ignore

‎check/performance ‎check/asv_run

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env bash
22

33
################################################################################
4-
# Runs performance tests
4+
# Runs performance benchmark tests using asv
55
#
66
# Usage:
7-
# check/performance
7+
# check/asv_run [--flags for asv run]
88
################################################################################
99

1010
# Get the working directory to the repo root.
1111
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1212
cd "$(git rev-parse --show-toplevel)"
1313

14-
# Test will fail if Mean is 0.1 seconds slower for any test.
15-
pytest --benchmark-autosave --benchmark-only --benchmark-compare --benchmark-compare-fail=mean:0.1 \
16-
&& cd .benchmarks/Linux-CPython-3.6-64bit/ && ls -t | tail -n +2 | xargs rm -- && ls
14+
export ASV_PYTHONPATH="$(git rev-parse --show-toplevel)/examples"
15+
asv run $@

‎check/pytest-and-incremental-coverage

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ check/pytest . \
5656
--actually-quiet \
5757
--cov \
5858
--cov-report=annotate \
59-
--cov-config=dev_tools/conf/.coveragerc \
60-
--benchmark-skip
59+
--cov-config=dev_tools/conf/.coveragerc
6160
pytest_result=$?
6261

6362
# Analyze coverage files.

‎check/pytest-changed-files-and-incremental-coverage

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ fi
8787
check/pytest "${changed_python_tests[@]}" \
8888
"${cov_changed_python_file_dirs[@]}" \
8989
--cov-report=annotate \
90-
--cov-config=dev_tools/conf/.coveragerc \
91-
--benchmark-skip
90+
--cov-config=dev_tools/conf/.coveragerc
9291
pytest_result=$?
9392

9493
# Analyze coverage files.

‎cirq/linalg/decompositions_perf_test.py

-34
This file was deleted.

‎dev_tools/bash_scripts_test.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
352352
assert result.out == (
353353
'INTERCEPTED check/pytest '
354354
'. --actually-quiet --cov --cov-report=annotate '
355-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
355+
'--cov-config=dev_tools/conf/.coveragerc\n'
356356
'INTERCEPTED '
357357
'python dev_tools/check_incremental_coverage_annotations.py HEAD\n'
358358
)
@@ -376,7 +376,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
376376
assert result.out == (
377377
'INTERCEPTED check/pytest '
378378
'. --actually-quiet --cov --cov-report=annotate '
379-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
379+
'--cov-config=dev_tools/conf/.coveragerc\n'
380380
'INTERCEPTED python '
381381
'dev_tools/check_incremental_coverage_annotations.py master\n'
382382
)
@@ -392,7 +392,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
392392
assert result.out == (
393393
'INTERCEPTED check/pytest '
394394
'. --actually-quiet --cov --cov-report=annotate '
395-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
395+
'--cov-config=dev_tools/conf/.coveragerc\n'
396396
'INTERCEPTED python '
397397
'dev_tools/check_incremental_coverage_annotations.py origin/master\n'
398398
)
@@ -408,7 +408,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
408408
assert result.out == (
409409
'INTERCEPTED check/pytest '
410410
'. --actually-quiet --cov --cov-report=annotate '
411-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
411+
'--cov-config=dev_tools/conf/.coveragerc\n'
412412
'INTERCEPTED python '
413413
'dev_tools/check_incremental_coverage_annotations.py upstream/master\n'
414414
)
@@ -424,7 +424,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
424424
assert result.out == (
425425
'INTERCEPTED check/pytest '
426426
'. --actually-quiet --cov --cov-report=annotate '
427-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
427+
'--cov-config=dev_tools/conf/.coveragerc\n'
428428
'INTERCEPTED python '
429429
'dev_tools/check_incremental_coverage_annotations.py upstream/master\n'
430430
)
@@ -452,7 +452,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
452452
assert result.out == (
453453
'INTERCEPTED check/pytest '
454454
'. --actually-quiet --cov --cov-report=annotate '
455-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
455+
'--cov-config=dev_tools/conf/.coveragerc\n'
456456
'INTERCEPTED python '
457457
'dev_tools/check_incremental_coverage_annotations.py HEAD\n'
458458
)
@@ -468,7 +468,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
468468
assert result.out == (
469469
'INTERCEPTED check/pytest '
470470
'. --actually-quiet --cov --cov-report=annotate '
471-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
471+
'--cov-config=dev_tools/conf/.coveragerc\n'
472472
'INTERCEPTED python '
473473
'dev_tools/check_incremental_coverage_annotations.py master\n'
474474
)
@@ -491,7 +491,7 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
491491
assert result.out.startswith(
492492
'INTERCEPTED check/pytest '
493493
'. --actually-quiet --cov --cov-report=annotate '
494-
'--cov-config=dev_tools/conf/.coveragerc --benchmark-skip\n'
494+
'--cov-config=dev_tools/conf/.coveragerc\n'
495495
'INTERCEPTED python '
496496
'dev_tools/check_incremental_coverage_annotations.py '
497497
)

‎dev_tools/conf/.coveragerc

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
# Note: this means coverage must be run from the cirq repo root.
44
# Failure to do so will result in false positives.
55
include = ./*
6+
7+
omit =
8+
benchmarks/*

‎dev_tools/conf/pip-list-dev-tools.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
12
# For testing and analyzing code.
3+
asv
4+
virtualenv
25
black==20.8b1
36
mypy~=0.782.0
47
pylint~=2.6.0
58
pytest~=5.4.1
69
pytest-asyncio~=0.12.0
710
pytest-cov~=2.5.0
8-
pytest-benchmark~=3.2.0
911
filelock~=3.0.12
1012

1113
# For generating protobufs

‎dev_tools/incremental_coverage.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
r'^setup\.py$', # Installation code.
2727
r'^cirq/google/engine/client/.+.py$', # Generated gRPC client code.
2828
r'^cirq/google/api/v1/.+.py$', # deprecated API code
29+
r'^benchmarks/',
2930
]
3031
IGNORED_BLOCK_PATTERNS = [
3132
r'^\s*if TYPE_CHECKING:$', # imports needed only while type-checking.

‎dev_tools/packaging/verify-published-package.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ for PYTHON_VERSION in python3; do
9494

9595
# Run tests.
9696
echo Installing pytest requirements
97-
"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet pytest pytest-benchmark pytest-asyncio
97+
"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet pytest pytest-asyncio
9898
PY_VER=$(ls "${tmp_dir}/${PYTHON_VERSION}/lib")
9999
echo Running cirq tests
100100
cirq_dir="${tmp_dir}/${PYTHON_VERSION}/lib/${PY_VER}/site-packages/${PROJECT_NAME}"

‎examples/examples_perf_test.py

-60
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.