Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gate benchmarking CI #87

Merged
merged 39 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9ac9db4
benchmark CI
antalszava Mar 12, 2021
95694a9
scripts
antalszava Mar 12, 2021
df24815
adjust
antalszava Mar 12, 2021
67eef84
whitespace
antalszava Mar 12, 2021
8e0fba2
adjust
antalszava Mar 12, 2021
d4ab3bc
get matplotlib
antalszava Mar 12, 2021
0bf6558
temp remove
antalszava Mar 12, 2021
c55da67
temp
antalszava Mar 12, 2021
281bb33
fix
antalszava Mar 12, 2021
49b42d6
Revert "fix"
antalszava Mar 12, 2021
6f027cb
Revert "temp"
antalszava Mar 12, 2021
71c8831
Revert "temp remove"
antalszava Mar 12, 2021
11a925d
main/pr
antalszava Mar 12, 2021
9140de9
creat main/pr
antalszava Mar 12, 2021
e9a893a
fix
antalszava Mar 12, 2021
daf3c3b
fix path
antalszava Mar 12, 2021
dcd20b4
qubit range; no lines
antalszava Mar 12, 2021
961546a
more
antalszava Mar 12, 2021
31fbafc
organize
antalszava Mar 12, 2021
0f3ac94
further
antalszava Mar 12, 2021
2d457ae
format
antalszava Mar 12, 2021
ca0fd6a
ns
antalszava Mar 12, 2021
ce7fa78
Merge branch 'master' into gate_bench_ci
antalszava Mar 12, 2021
88ffbae
lic
antalszava Mar 12, 2021
2ff0805
Merge branch 'gate_bench_ci' of https://github.com/XanaduAI/pennylane…
antalszava Mar 12, 2021
15013be
int labels
antalszava Mar 12, 2021
e1f3294
runs-on; no push run
antalszava Mar 16, 2021
041b413
git checkout
antalszava Mar 16, 2021
ed1bd6f
no uses
antalszava Mar 16, 2021
b125615
Revert "no uses"
antalszava Mar 16, 2021
0ca25d1
Revert "git checkout"
antalszava Mar 16, 2021
2381c8f
remove previous binaries and pip uninstall
antalszava Mar 16, 2021
2741927
no default.qubit bench (temp)
antalszava Mar 16, 2021
0b365d8
Revert "no default.qubit bench (temp)"
antalszava Mar 16, 2021
9d42fe5
Update .github/workflows/benchmarks/plot_results.py
antalszava Mar 16, 2021
81e33bc
Update .github/workflows/benchmarks/plot_results.py
antalszava Mar 16, 2021
7cc9fb3
license
antalszava Mar 16, 2021
a8be88e
adjust licensing
antalszava Mar 17, 2021
7ca605b
runs-on
antalszava Mar 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Benchmarking
on:
push:
branches:
- master
antalszava marked this conversation as resolved.
Show resolved Hide resolved
pull_request:

jobs:
benchmarks:
name: Gate benchmarks
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-18.04]
antalszava marked this conversation as resolved.
Show resolved Hide resolved

steps:

- name: Checkout PennyLane-Lightning master
uses: actions/checkout@v2
with:
path: main

- uses: actions/setup-python@v2
name: Install Python
with:
python-version: '3.7'

- name: Install dependencies
run: sudo apt-get -y -q install cmake gcc

- name: Get required Python packages
run: |
cd main
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install matplotlib

- name: Install lightning.qubit device (master)
run: |
cd main
pip install -e .

- name: Benchmark lightning master device
run: |
cd main
python .github/workflows/benchmarks/run_bench.py lightning.qubit lightning_master.json

- name: Benchmark default qubit device
run: |
cd main
python .github/workflows/benchmarks/run_bench.py default.qubit default_qubit.json

- name: Checkout PennyLane-Lightning PR
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
path: main/pr
trbromley marked this conversation as resolved.
Show resolved Hide resolved

- name: Install lightning.qubit device (PR)
run: |
cd main/pr
pip install -e .
trbromley marked this conversation as resolved.
Show resolved Hide resolved

- name: Benchmark lightning PR device
run: |
cd main/pr
python .github/workflows/benchmarks/run_bench.py lightning.qubit lightning_pr.json
mv lightning_pr.json ..

- name: Plot results
run: |
cd main
python .github/workflows/benchmarks/plot_results.py

- uses: actions/upload-artifact@v2
with:
name: gates.png
path: ./main/gates.png
15 changes: 15 additions & 0 deletions .github/workflows/benchmarks/parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# 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

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
qubits = [1, 3, 5, 10, 15, 18]
ops = ["PauliX", "T", "Hadamard", "CNOT"]
100 changes: 100 additions & 0 deletions .github/workflows/benchmarks/plot_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# 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

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

# The MIT License (MIT)
#
# Copyright (c) 2009-2018 Xiuzhe (Roger) Luo,
# and other contributors.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Acknowledghing the solution for plotting from the quantum-benchmarks repository
antalszava marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'm curious, how much of this came directly from the external repo? It seems like simple matplotlib plotting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the plotting was based on the external repo. Wanted to specify some attribution as the plots would be similarly constructed and would look similar, maybe enough to just leave a mention?

# at https://github.com/Roger-luo/quantum-benchmarks.


#!/usr/bin/env python3
import os
import matplotlib
antalszava marked this conversation as resolved.
Show resolved Hide resolved
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import json
from parameters import qubits, ops
import numpy as np

colors = ["darkblue", "tab:orange", "tab:olive"]
projects = [
"lightning_master.json",
"lightning_pr.json",
"default_qubit.json",
]

COLOR = dict(zip(projects, colors))

op_results = {o: [] for o in ops}

for p in projects:
with open(p) as f:
data = json.load(f)
for k in data.keys():
op_results[k].append(data[k])

fig, ax = plt.subplots(2, 2, figsize=(10, 8))
((ax1, ax2), (ax3, ax4)) = ax

axes = ax.flatten()

for op, a in zip(ops, ax.flatten()):
a.set_xlabel("nqubits", size=16)
a.set_ylabel("ns", size=16)
a.set_title(op + " gate")
a.xaxis.set_major_locator(MaxNLocator(integer=True))

for a, op in zip(axes, op_results.keys()):
for k, v in enumerate(projects):
data = op_results[op][k]
data = np.array(data) * 1e9
a.semilogy(qubits, data, "-o", markersize=4, color=COLOR[v], linestyle="None")

plots = []
plt.tight_layout()
plt.subplots_adjust(top=0.85)

lgd = fig.legend(
plots,
labels=[p.split(".")[0] for p in projects],
loc="upper center",
ncol=4,
frameon=False,
prop={"size": 15},
borderaxespad=-0.4,
bbox_to_anchor=(0.5, 0.97),
)

plt.savefig("gates.png")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing for this PR, but I wonder if we could have something like the QML repo where the CI checks in a PR link directly to the artifact? Think it's in this workflow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be awesome, but after a look around it seems that there's no straightforward solution 😞 in the qml repo we are using a CircleCI specific action

51 changes: 51 additions & 0 deletions .github/workflows/benchmarks/run_bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# 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

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also wondering if this script is more suited to attribution, given that we are following the general approach of the external repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Moved the licensing into this file.

#!/usr/bin/env python3

# pylint: disable=cell-var-from-loop
# Generate data
import pennylane as qml
import timeit
import json
import sys
from parameters import qubits, ops

if len(sys.argv) != 3:
raise ValueError(
"Please provide the device name and the filename as the only arguments."
)

device_string, filename = sys.argv[1], sys.argv[2]

op_res = {o: [] for o in ops}

for num_q in qubits:
dev = qml.device(device_string, wires=num_q)
for gate in ops:

def apply_op():
# Calling apply to minimize the Python overhead
pennylane_op = getattr(qml, gate)
if pennylane_op.num_wires == 1:
dev.apply([pennylane_op(wires=0)])
elif num_q > 1 and pennylane_op.num_wires == 2:
dev.apply([pennylane_op(wires=[0, 1])])

number = 10000
res = timeit.timeit(apply_op, number=number) / number
op_res[gate].append(res)

with open(filename, "w") as fp:
json.dump(op_res, fp)