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

Implement improved demo build pipeline #1286

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b57ba0f
create new demos directory structure
brownj85 Dec 20, 2024
4dc5233
pick lib.qml tool
brownj85 Dec 20, 2024
b2e3edb
fix pytorch constraint
brownj85 Dec 20, 2024
bb76dae
restructure lib
brownj85 Dec 20, 2024
22acefc
Apply suggestions from code review
brownj85 Jan 16, 2025
326e586
docstrings
brownj85 Jan 16, 2025
5fcda7f
specify poetry<2
brownj85 Jan 16, 2025
fb46d78
sync
brownj85 Jan 16, 2025
6b19a63
requirements
brownj85 Jan 16, 2025
bea7530
fix tutorial quantum chemistry paths
brownj85 Jan 16, 2025
c51c68e
docs
brownj85 Jan 16, 2025
e768e21
docstrings
brownj85 Jan 16, 2025
ceea64f
update lockfile
brownj85 Jan 16, 2025
39d96d3
fmt
brownj85 Jan 27, 2025
8441b41
add core dependencies
brownj85 Jan 27, 2025
c54066c
fmt
brownj85 Jan 27, 2025
7aecbae
Merge branch 'master' into sc-79849-implement-improved-demo-build-pip…
brownj85 Jan 27, 2025
1fc423e
sync
brownj85 Jan 27, 2025
60e982e
fix v2
brownj85 Jan 27, 2025
424b4b0
clean conf.py
brownj85 Jan 27, 2025
70797e2
add requirements-parser
brownj85 Jan 27, 2025
00f517c
qiskit-aer spec
brownj85 Jan 27, 2025
36d8ec1
Merge branch 'sc-79849-implement-improved-demo-build-pipeline' into s…
brownj85 Jan 30, 2025
8b828ce
single demo builds
brownj85 Jan 31, 2025
8d1e3ec
don't generate full requirements
brownj85 Feb 7, 2025
0aded43
Merge branch 'master' into sc-79849-implement-improved-demo-build-pip…
brownj85 Feb 7, 2025
a505b55
sync
brownj85 Feb 7, 2025
dd0347a
Merge branch 'sc-79849-implement-improved-demo-build-pipeline' into s…
brownj85 Feb 7, 2025
1e86b65
pack demos
brownj85 Feb 7, 2025
03363b4
copy hardware images
brownj85 Feb 7, 2025
bbd53cd
skip non-executable demos
brownj85 Feb 7, 2025
b4c1e66
jax
brownj85 Feb 7, 2025
2212966
jax
brownj85 Feb 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ backreferences
.DS_Store
demos_community.rst
.venv/
.venv-build/
**/*.egg-info/

# Generated after installation
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.analysis.ignore": [
".venv/**",
".venv-build/**",
"_build/**"
]
}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ environment:
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane.git#egg=pennylane;\
fi;\
fi

fmt:
poetry run ruff check --fix lib/
poetry run ruff format lib/
9 changes: 5 additions & 4 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@
"extension",
]


html_baseurl = "https://pennylane.ai/qml/"
demo_staging_dir = os.getenv("DEMO_STAGING_DIR", "demonstrations")
gallery_output_dir = os.getenv("GALLERY_OUTPUT_DIR", "demos")

sphinx_gallery_conf = {
# path to your example scripts
"examples_dirs": ["demonstrations"],
"examples_dirs": [demo_staging_dir],
# path where to save gallery generated examples
"gallery_dirs": ["demos"],
"gallery_dirs": [gallery_output_dir],
# execute files that match the following filename pattern,
# and skip those that don't. If the following option is not provided,
# all example scripts in the 'examples_dirs' folder will be skiped.
Expand Down Expand Up @@ -133,7 +134,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "*venv", "sphinxext"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "*venv", "*venv-build", "sphinxext"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"
Expand Down
25 changes: 25 additions & 0 deletions constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pennylane==0.40.0
pennylane-cirq==0.40.0
pennylane-qiskit==0.40.0
pennylane-qulacs==0.40.0
pennylane-catalyst==0.10.0
pennylane-qrack==0.11.1
pyqrack==1.32.12
numpy~=1.24
matplotlib==3.7.2
jax==0.4.28
jaxlib==0.4.28
jaxopt==0.8.3
aiohttp==3.9.5
fsspec==2024.6.1
h5py==3.11.0
openqaoa-core==0.2.5
qiskit>=1.0.0
qiskit-aer>=0.14.0,<0.16.0
qiskit_ibm_runtime==0.29.0
torch==2.1.2+cpu ; sys_platform != 'darwin'
torch==2.1.2 ; sys_platform == 'darwin'
torchvision==0.16.2+cpu ; sys_platform != 'darwin'
torchvision==0.16.2 ; sys_platform == 'darwin'
tensorflow==2.14.1
optax==0.2.3
File renamed without changes.
2 changes: 1 addition & 1 deletion demonstrations/tutorial_quantum_chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

from pennylane import qchem

symbols, coordinates = qchem.read_structure("h2o.xyz")
symbols, coordinates = qchem.read_structure("quantum_chemistry/h2o.xyz")

##############################################################################
# The xyz format is supported.
Expand Down
147 changes: 147 additions & 0 deletions demonstrations_v2/adjoint_diff_benchmarking/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
r"""

.. _adjoint_differentiation_benchmarking:

Adjoint Differentiation
=======================

.. meta::
:property="og:description": Benchmarking file for adjoint diff demonstration.
:property="og:image": https://pennylane.ai/qml/_static/demo_thumbnails/opengraph_demo_thumbnails/code.png


*Author: Christina Lee — Posted: 23 November 2021. Last updated: 04 July 2024.*

"""

##############################################################################
# This page is supplementary material to the
# `Adjoint Differentiation <https://pennylane.ai/qml/demos/tutorial_adjoint_diff.html>`__
# demonstration. The below script produces the benchmarking images used.

import timeit
import matplotlib.pyplot as plt
import pennylane as qml
import jax

jax.config.update("jax_platform_name", "cpu")
jax.config.update('jax_enable_x64', True)

plt.style.use("bmh")

n_samples = 5


def get_time(qnode, params):
globals_dict = {'grad': jax.grad, 'circuit': qnode, 'params': params}
return timeit.timeit("grad(circuit)(params)", globals=globals_dict, number=n_samples)


def wires_scaling(n_wires, n_layers):
key = jax.random.PRNGKey(42)

t_adjoint = []
t_ps = []
t_backprop = []

def circuit(params, wires):
qml.StronglyEntanglingLayers(params, wires=range(wires))
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2))

for i_wires in n_wires:
dev = qml.device("lightning.qubit", wires=i_wires)
dev_python = qml.device("default.qubit", wires=i_wires)

circuit_adjoint = qml.QNode(lambda x: circuit(x, wires=i_wires), dev, diff_method="adjoint")
circuit_ps = qml.QNode(lambda x: circuit(x, wires=i_wires), dev, diff_method="parameter-shift")
circuit_backprop = qml.QNode(lambda x: circuit(x, wires=i_wires), dev_python, diff_method="backprop")

# set up the parameters
param_shape = qml.StronglyEntanglingLayers.shape(n_wires=i_wires, n_layers=n_layers)
params = jax.random.normal(key, param_shape)

t_adjoint.append(get_time(circuit_adjoint, params))
t_backprop.append(get_time(circuit_backprop, params))
t_ps.append(get_time(circuit_ps, params))

return t_adjoint, t_backprop, t_ps


def layers_scaling(n_wires, n_layers):
key = jax.random.PRNGKey(42)

dev = qml.device("lightning.qubit", wires=n_wires)
dev_python = qml.device('default.qubit', wires=n_wires)

t_adjoint = []
t_ps = []
t_backprop = []

def circuit(params):
qml.StronglyEntanglingLayers(params, wires=range(n_wires))
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2))

circuit_adjoint = qml.QNode(circuit, dev, diff_method="adjoint")
circuit_ps = qml.QNode(circuit, dev, diff_method="parameter-shift")
circuit_backprop = qml.QNode(circuit, dev_python, diff_method="backprop")

for i_layers in n_layers:
# set up the parameters
param_shape = qml.StronglyEntanglingLayers.shape(n_wires=n_wires, n_layers=i_layers)
params = jax.random.normal(key, param_shape)

t_adjoint.append(get_time(circuit_adjoint, params))
t_backprop.append(get_time(circuit_backprop, params))
t_ps.append(get_time(circuit_ps, params))

return t_adjoint, t_backprop, t_ps


if __name__ == "__main__":

wires_list = [3, 6, 9, 12, 15]
n_layers = 6
adjoint_wires, backprop_wires, ps_wires = wires_scaling(wires_list, n_layers)

layers_list = [3, 9, 15, 21, 27]
n_wires = 12
adjoint_layers, backprop_layers, ps_layers = layers_scaling(n_wires, layers_list)

# Generating the graphic
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

ax1.plot(wires_list, adjoint_wires, '.-', label="adjoint")
ax1.plot(wires_list, ps_wires, '.-', label="parameter-shift")
ax1.plot(wires_list, backprop_wires, '.-', label="backprop")

ax1.legend()

ax1.set_xlabel("Number of wires")
ax1.set_xticks(wires_list)
ax1.set_ylabel("Log Time")
ax1.set_yscale("log")
ax1.set_title("Scaling with wires")

ax2.plot(layers_list, adjoint_layers, '.-', label="adjoint")
ax2.plot(layers_list, ps_layers, '.-', label="parameter-shift")
ax2.plot(layers_list, backprop_layers, '.-', label="backprop")

ax2.legend()

ax2.set_xlabel("Number of layers")
ax2.set_xticks(layers_list)
ax2.set_ylabel("Time")
ax2.set_title("Scaling with Layers")

plt.savefig("scaling.png")

##############################################################################
#
# .. figure:: ../_static/demonstration_assets/adjoint_diff/scaling.png
# :width: 80%
# :align: center
#
#
# About the author
# ----------------
#
24 changes: 24 additions & 0 deletions demonstrations_v2/adjoint_diff_benchmarking/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "Adjoint Differentiation – Supplementary Material",
"authors": [
{
"username": "christina"
}
],
"dateOfPublication": "2021-11-23T00:00:00+00:00",
"dateOfLastModification": "2024-10-07T00:00:00+00:00",
"categories": [],
"tags": [],
"previewImages": [
{
"type": "thumbnail",
"uri": "/_static/demo_thumbnails/regular_demo_thumbnails/thumbnail_adjoint_differentiation.png"
}
],
"seoDescription": "Benchmarking file for adjoint diff demonstration.",
"doi": "",
"references": [],
"basedOnPapers": [],
"referencedByPapers": [],
"relatedContent": []
}
2 changes: 2 additions & 0 deletions demonstrations_v2/adjoint_diff_benchmarking/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jax
jaxlib
Loading