-
Notifications
You must be signed in to change notification settings - Fork 12
Add quil-plotting module #412
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
Draft
bramathon
wants to merge
3
commits into
main
Choose a base branch
from
411-create-a-schedule-plotting-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
quil-plotting/.ipynb_checkpoints/pyproject-checkpoint.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| [project] | ||
| name = "quil-plotting" | ||
| version = "0.1.0" | ||
| requires-python = ">=3.8" | ||
| description = "A Python package for visualizing Quil programs." | ||
| documentation = "https://rigetti.github.io/quil-rs/quil.html" | ||
| readme = "README.md" | ||
| license = { text = "Apache-2.0" } | ||
| authors = [{ name = "Rigetti Computing", email = "softapps@rigetti.com" }] | ||
| classifiers = [ | ||
| "Development Status :: 3 - Alpha", | ||
| "License :: OSI Approved :: Apache Software License", | ||
| "Programming Language :: Python :: 3.8", | ||
| "Programming Language :: Python :: 3.9", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Operating System :: OS Independent", | ||
| ] | ||
| dependencies = [ | ||
| "numpy>=1.2.1", | ||
| "plotly>=5.11", | ||
| "kaleido==0.2.1", | ||
| "pandas>=2.0" | ||
| ] | ||
|
|
||
| [project.optional-dependencies] | ||
| dev = [ | ||
| "ruff>=0.3.7", | ||
| "maturin>=1.2.3", | ||
| "mypy>=1.1.1", | ||
| "pytest>=7.2.2", | ||
| "pdoc>=14.1.0", | ||
| "syrupy>=3.0.6" | ||
| ] | ||
|
|
||
| # [build-system] | ||
| # requires = ["setuptools>=61.0"] | ||
| # build-backend = "setuptools.build_meta" | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [tool.ruff] | ||
| exclude = [ | ||
| ".bzr", | ||
| ".direnv", | ||
| ".eggs", | ||
| ".git", | ||
| ".git-rewrite", | ||
| ".hg", | ||
| ".ipynb_checkpoints", | ||
| ".mypy_cache", | ||
| ".nox", | ||
| ".pants.d", | ||
| ".pyenv", | ||
| ".pytest_cache", | ||
| ".pytype", | ||
| ".ruff_cache", | ||
| ".svn", | ||
| ".tox", | ||
| ".venv", | ||
| ".vscode", | ||
| "__pypackages__", | ||
| "_build", | ||
| "buck-out", | ||
| "build", | ||
| "dist", | ||
| "node_modules", | ||
| "site-packages", | ||
| "venv", | ||
| ] | ||
| line-length = 120 | ||
| indent-width = 4 | ||
| target-version = "py38" | ||
|
|
||
| [tool.ruff.lint] | ||
| select = ["D", "E4", "E7", "E9", "F", "I", "B", "S", "W"] | ||
| ignore = [ | ||
| "E741" # "Ambiguous" variable names like "I" aren't ambiguous in this contex. | ||
| ] | ||
| fixable = ["ALL"] | ||
| unfixable = [] | ||
| # Allow unused variables when underscore-prefixed. | ||
| dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" | ||
|
|
||
| [tool.ruff.lint.per-file-ignores] | ||
| "quil/**/*.py" = [ | ||
| "F403", # * imports allowed in extension module glue. | ||
| "D100", # docstrings belong in type stubs | ||
| "D104", | ||
| ] | ||
| "test/**/*.py" = [ | ||
| "D", # docstrings are not enforced in tests | ||
| "S101", # asserts are allowed in tests | ||
| "S301", # we need to test pickling | ||
| ] | ||
| "make_docs.py" = [ "D" ] | ||
|
|
||
| [tool.ruff.format] | ||
| quote-style = "double" | ||
| indent-style = "space" | ||
| skip-magic-trailing-comma = false | ||
| line-ending = "auto" | ||
|
|
||
| [tool.ruff.lint.pydocstyle] | ||
| convention = "google" | ||
|
|
||
| [tool.pyright] | ||
| # This diagnostic is raised when a type stub is found without a corresponding source file. This is | ||
| # necessarily the case for a pure Rust pyo3 module, so disabling it. | ||
| reportMissingModuleSource = false | ||
|
|
||
| [tool.mypy] | ||
| plugins = "numpy.typing.mypy_plugin" | ||
|
|
||
| [[tool.mypy.overrides]] | ||
| module = [ | ||
| "quil.quil", | ||
| ] | ||
| ignore_missing_imports = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Plotting Quil pulse schedules\n", | ||
| "\n", | ||
| "This notebook plots a Quil program's pulse schedule with both of `quil_plotting`'s backends:\n", | ||
| "\n", | ||
| "- **plotly** (`plot_schedule`) \u2014 the original backend.\n", | ||
| "- **Altair / Vega-Lite** (`plot_schedule_altair`) \u2014 renders static images in process, so it does\n", | ||
| " not need an installed copy of Chrome to write a png or svg.\n", | ||
| "\n", | ||
| "Both read the same schedule dataframe and take the same options, so you can compare them directly." | ||
| ], | ||
| "id": "cell-00" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Setup\n", | ||
| "\n", | ||
| "Locate the package and the bundled test programs. This works without installing `quil_plotting`,\n", | ||
| "as long as the notebook lives somewhere inside the package." | ||
| ], | ||
| "id": "cell-01" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import sys\n", | ||
| "from pathlib import Path\n", | ||
| "\n", | ||
| "PACKAGE_ROOT = next(p for p in [Path.cwd(), *Path.cwd().parents] if (p / \"quil_plotting\").is_dir())\n", | ||
| "sys.path.insert(0, str(PACKAGE_ROOT))\n", | ||
| "\n", | ||
| "PROGRAM_DIR = PACKAGE_ROOT / \"tests\" / \"programs\"\n", | ||
| "\n", | ||
| "# Imported after the path is set up, hence the `noqa`.\n", | ||
| "from quil.program import Program # noqa: E402\n", | ||
| "\n", | ||
| "from quil_plotting import ( # noqa: E402\n", | ||
| " add_plot_metadata,\n", | ||
| " plot_schedule,\n", | ||
| " plot_schedule_altair,\n", | ||
| " program_to_dataframe,\n", | ||
| ")\n", | ||
| "\n", | ||
| "sorted(path.stem for path in PROGRAM_DIR.glob(\"*.quil\"))" | ||
| ], | ||
| "id": "cell-02" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Choose a program\n", | ||
| "\n", | ||
| "Set `PROGRAM_NAME` to any of the names listed above and re-run the notebook. Some suggestions:\n", | ||
| "\n", | ||
| "| name | what it shows |\n", | ||
| "| --- | --- |\n", | ||
| "| `multiple_gates_with_measures` | small and quick \u2014 a good place to start |\n", | ||
| "| `single_gate_iswap` | one two-qubit gate, with the coupler frame alongside the qubits |\n", | ||
| "| `sequenced_hadamard_barrier` | how barriers serialize the schedule |\n", | ||
| "| `randomized_circuit_0` | a busier circuit, ~26 us long |\n", | ||
| "| `rotated-surface-code` | 41 frames over 8 rounds of syndrome extraction \u2014 the stress case |" | ||
| ], | ||
| "id": "cell-03" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "PROGRAM_NAME = \"multiple_gates_with_measures\"\n", | ||
| "\n", | ||
| "program = Program.parse((PROGRAM_DIR / f\"{PROGRAM_NAME}.quil\").read_text())\n", | ||
| "print(f\"{PROGRAM_NAME}: {len(program.body_instructions)} instructions, \"\n", | ||
| " f\"{len(program.calibrations.calibrations)} calibrations, {len(program.frames)} frames\")" | ||
| ], | ||
| "id": "cell-04" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## The schedule dataframe\n", | ||
| "\n", | ||
| "Both backends are built on this. Each row is one IQ sample of one pulse, carrying the metadata\n", | ||
| "needed to place, colour, and label it. `program_to_dataframe` expands the program's calibrations\n", | ||
| "and schedules the result; `add_plot_metadata` adds the plotting columns (`Offset`, `Color`,\n", | ||
| "`Normalized IQ`, `Label`, ...)." | ||
| ], | ||
| "id": "cell-05" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "df = add_plot_metadata(program_to_dataframe(program))\n", | ||
| "print(f\"{len(df)} rows, spanning {(df['Time (s)'].max() - df['Time (s)'].min()) * 1e6:.3f} us\")\n", | ||
| "df.head()" | ||
| ], | ||
| "id": "cell-06" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## The plotly backend\n", | ||
| "\n", | ||
| "**Interacting:** drag to zoom into a time range, double-click to reset, and click a legend entry to\n", | ||
| "hide that operation (double-click one to isolate it). Hovering a pulse shows its I/Q value, frame, and\n", | ||
| "channel type." | ||
| ], | ||
| "id": "cell-07" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "fig = plot_schedule(program)\n", | ||
| "fig.show()" | ||
| ], | ||
| "id": "cell-08" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## The Altair backend\n", | ||
| "\n", | ||
| "The same schedule, rendered through Vega-Lite.\n", | ||
| "\n", | ||
| "**Interacting:** drag to pan and scroll to zoom (both axes), and click a legend entry to isolate\n", | ||
| "that operation \u2014 the others dim rather than disappear, so the shape of the schedule is preserved.\n", | ||
| "Shift-click to select several. Hovering a pulse shows its I/Q value, frame, and channel type.\n", | ||
| "\n", | ||
| "Altair's default renderer loads Vega from a CDN. On a machine without internet access, run\n", | ||
| "`alt.renderers.enable(\"mimetype\")` first and JupyterLab will render the chart itself." | ||
| ], | ||
| "id": "cell-09" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "chart = plot_schedule_altair(program)\n", | ||
| "chart" | ||
| ], | ||
| "id": "cell-10" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Static export\n", | ||
| "\n", | ||
| "Altair renders png and svg in process. Plotly's `write_image` goes through Kaleido, which needs a\n", | ||
| "copy of Chrome on the machine \u2014 if it is missing, install one with `plotly_get_chrome`." | ||
| ], | ||
| "id": "cell-11" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "output_dir = Path(\"plots\")\n", | ||
| "output_dir.mkdir(exist_ok=True)\n", | ||
| "\n", | ||
| "chart.save(output_dir / f\"{PROGRAM_NAME}-altair.svg\")\n", | ||
| "chart.save(output_dir / f\"{PROGRAM_NAME}-altair.png\", ppi=100)\n", | ||
| "\n", | ||
| "# Needs Chrome; comment out if it is not installed.\n", | ||
| "fig.write_image(output_dir / f\"{PROGRAM_NAME}-plotly.png\")\n", | ||
| "\n", | ||
| "sorted(path.name for path in output_dir.iterdir())" | ||
| ], | ||
| "id": "cell-12" | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Options\n", | ||
| "\n", | ||
| "Both backends accept the same arguments, so the same call works against either. A few worth trying:\n", | ||
| "\n", | ||
| "- `runners` \u2014 what to stack up the y-axis (default `\"Qubit\"`).\n", | ||
| "- `color_by` / `label_by` \u2014 what drives the colours and the legend. Defaults to `\"Operation\"`,\n", | ||
| " the gate (or, in future, reset) each pulse came from. `\"Channel Type\"` groups by\n", | ||
| " hardware channel instead of by operation.\n", | ||
| "- `exclude_readout` \u2014 set `False` to include the readout pulses, which are long and flat and\n", | ||
| " otherwise dominate the normalization.\n", | ||
| "- `normalize_by` \u2014 the grouping whose peak amplitude scales each pulse to fit its row." | ||
| ], | ||
| "id": "cell-13" | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "plot_schedule_altair(\n", | ||
| " program,\n", | ||
| " color_by=\"Channel Type\",\n", | ||
| " label_by=\"Channel Type\",\n", | ||
| " exclude_readout=False,\n", | ||
| ")" | ||
| ], | ||
| "id": "cell-14" | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "name": "python", | ||
| "pygments_lexer": "ipython3" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a README