Skip to content

Commit fd41d8f

Browse files
Gate benchmarking CI (#87)
* benchmark CI * scripts * adjust * whitespace * adjust * get matplotlib * temp remove * temp * fix * Revert "fix" This reverts commit 281bb33. * Revert "temp" This reverts commit c55da67. * Revert "temp remove" This reverts commit 0bf6558. * main/pr * creat main/pr * fix * fix path * qubit range; no lines * more * organize * further * format * ns * lic * int labels * runs-on; no push run * git checkout * no uses * Revert "no uses" This reverts commit ed1bd6f. * Revert "git checkout" This reverts commit 041b413. * remove previous binaries and pip uninstall * no default.qubit bench (temp) * Revert "no default.qubit bench (temp)" This reverts commit 2741927. * Update .github/workflows/benchmarks/plot_results.py Co-authored-by: Tom Bromley <[email protected]> * Update .github/workflows/benchmarks/plot_results.py Co-authored-by: Tom Bromley <[email protected]> * license * adjust licensing * runs-on Co-authored-by: Tom Bromley <[email protected]>
1 parent 19a655c commit fd41d8f

File tree

6 files changed

+279
-1
lines changed

6 files changed

+279
-1
lines changed

.github/workflows/benchmarks.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Benchmarking
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
benchmarks:
7+
name: Gate benchmarks
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
matrix:
11+
os: [ubuntu-18.04]
12+
13+
steps:
14+
15+
- name: Checkout PennyLane-Lightning master
16+
uses: actions/checkout@v2
17+
with:
18+
path: main
19+
20+
- uses: actions/setup-python@v2
21+
name: Install Python
22+
with:
23+
python-version: '3.7'
24+
25+
- name: Install dependencies
26+
run: sudo apt-get -y -q install cmake gcc
27+
28+
- name: Get required Python packages
29+
run: |
30+
cd main
31+
python -m pip install --upgrade pip
32+
pip install -r requirements.txt
33+
pip install matplotlib
34+
35+
- name: Install lightning.qubit device (master)
36+
run: |
37+
cd main
38+
pip install -e .
39+
40+
- name: Benchmark lightning master device
41+
run: |
42+
cd main
43+
python .github/workflows/benchmarks/run_bench.py lightning.qubit lightning_master.json
44+
45+
- name: Benchmark default qubit device
46+
run: |
47+
cd main
48+
python .github/workflows/benchmarks/run_bench.py default.qubit default_qubit.json
49+
50+
- name: Checkout PennyLane-Lightning PR
51+
uses: actions/checkout@v2
52+
with:
53+
ref: ${{ github.ref }}
54+
path: main/pr
55+
56+
- name: Install lightning.qubit device (PR)
57+
run: |
58+
cd main
59+
make clean
60+
pip uninstall pennylane-lightning -y
61+
cd pr
62+
pip install -e .
63+
64+
- name: Benchmark lightning PR device
65+
run: |
66+
cd main/pr
67+
python .github/workflows/benchmarks/run_bench.py lightning.qubit lightning_pr.json
68+
mv lightning_pr.json ..
69+
70+
- name: Plot results
71+
run: |
72+
cd main
73+
python .github/workflows/benchmarks/plot_results.py
74+
75+
- uses: actions/upload-artifact@v2
76+
with:
77+
name: gates.png
78+
path: ./main/gates.png
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.
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+
# http://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+
qubits = [1, 3, 5, 10, 15, 18]
15+
ops = ["PauliX", "T", "Hadamard", "CNOT"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.
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+
# http://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+
# The MIT License (MIT)
16+
#
17+
# Copyright (c) 2009-2018 Xiuzhe (Roger) Luo,
18+
# and other contributors.
19+
#
20+
# Permission is hereby granted, free of charge, to any person obtaining a copy
21+
# of this software and associated documentation files (the "Software"), to deal
22+
# in the Software without restriction, including without limitation the rights
23+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
# copies of the Software, and to permit persons to whom the Software is
25+
# furnished to do so, subject to the following conditions:
26+
#
27+
# The above copyright notice and this permission notice shall be included in all
28+
# copies or substantial portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
# Acknowledging the approach for plotting from the quantum-benchmarks repository
39+
# at https://github.com/Roger-luo/quantum-benchmarks.
40+
41+
#!/usr/bin/env python3
42+
import os
43+
import matplotlib.pyplot as plt
44+
from matplotlib.ticker import MaxNLocator
45+
import json
46+
from parameters import qubits, ops
47+
import numpy as np
48+
49+
colors = ["darkblue", "tab:orange", "tab:olive"]
50+
projects = [
51+
"lightning_master.json",
52+
"lightning_pr.json",
53+
"default_qubit.json",
54+
]
55+
56+
COLOR = dict(zip(projects, colors))
57+
58+
op_results = {o: [] for o in ops}
59+
60+
for p in projects:
61+
with open(p) as f:
62+
data = json.load(f)
63+
for k in data.keys():
64+
op_results[k].append(data[k])
65+
66+
fig, ax = plt.subplots(2, 2, figsize=(10, 8))
67+
((ax1, ax2), (ax3, ax4)) = ax
68+
69+
axes = ax.flatten()
70+
71+
for op, a in zip(ops, ax.flatten()):
72+
a.set_xlabel("nqubits", size=16)
73+
a.set_ylabel("ns", size=16)
74+
a.set_title(op + " gate")
75+
a.xaxis.set_major_locator(MaxNLocator(integer=True))
76+
77+
for a, op in zip(axes, op_results.keys()):
78+
for k, v in enumerate(projects):
79+
data = op_results[op][k]
80+
data = np.array(data) * 1e9
81+
a.semilogy(qubits, data, "-o", markersize=4, color=COLOR[v], linestyle="None")
82+
83+
plots = []
84+
plt.tight_layout()
85+
plt.subplots_adjust(top=0.85)
86+
87+
lgd = fig.legend(
88+
plots,
89+
labels=[p.split(".")[0] for p in projects],
90+
loc="upper center",
91+
ncol=4,
92+
frameon=False,
93+
prop={"size": 15},
94+
borderaxespad=-0.4,
95+
bbox_to_anchor=(0.5, 0.97),
96+
)
97+
98+
plt.savefig("gates.png")
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.
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+
# http://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+
# The MIT License (MIT)
16+
#
17+
# Copyright (c) 2009-2018 Xiuzhe (Roger) Luo,
18+
# and other contributors.
19+
#
20+
# Permission is hereby granted, free of charge, to any person obtaining a copy
21+
# of this software and associated documentation files (the "Software"), to deal
22+
# in the Software without restriction, including without limitation the rights
23+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
# copies of the Software, and to permit persons to whom the Software is
25+
# furnished to do so, subject to the following conditions:
26+
#
27+
# The above copyright notice and this permission notice shall be included in all
28+
# copies or substantial portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
# Acknowledging the benchmarking approach from the quantum-benchmarks repository
39+
# at https://github.com/Roger-luo/quantum-benchmarks.
40+
41+
#!/usr/bin/env python3
42+
43+
# pylint: disable=cell-var-from-loop
44+
# Generate data
45+
import pennylane as qml
46+
import timeit
47+
import json
48+
import sys
49+
from parameters import qubits, ops
50+
51+
if len(sys.argv) != 3:
52+
raise ValueError(
53+
"Please provide the device name and the filename as the only arguments."
54+
)
55+
56+
device_string, filename = sys.argv[1], sys.argv[2]
57+
58+
op_res = {o: [] for o in ops}
59+
60+
for num_q in qubits:
61+
dev = qml.device(device_string, wires=num_q)
62+
for gate in ops:
63+
64+
def apply_op():
65+
# Calling apply to minimize the Python overhead
66+
pennylane_op = getattr(qml, gate)
67+
if pennylane_op.num_wires == 1:
68+
dev.apply([pennylane_op(wires=0)])
69+
elif num_q > 1 and pennylane_op.num_wires == 2:
70+
dev.apply([pennylane_op(wires=[0, 1])])
71+
72+
number = 10000
73+
res = timeit.timeit(apply_op, number=number) / number
74+
op_res[gate].append(res)
75+
76+
with open(filename, "w") as fp:
77+
json.dump(op_res, fp)

.github/workflows/build.yml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ jobs:
1212
strategy:
1313
matrix:
1414
os: [ubuntu-18.04]
15-
1615
steps:
1716
- uses: actions/checkout@v2
1817

LICENSE

+11
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,14 @@
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201201
limitations under the License.
202+
203+
--------------------------------------------------------------------------------
204+
This product bundles various third-party components under other open source
205+
licenses. This section summarizes those components and their licenses.
206+
207+
208+
The MIT License (MIT)
209+
---------------------
210+
211+
.github/workflows/benchmarks/run_bench.py
212+
.github/workflows/benchmarks/plot_results.py

0 commit comments

Comments
 (0)