Skip to content

Conversation

@thisac
Copy link
Collaborator

@thisac thisac commented Jun 6, 2023

Adds a demos page to the documentation, including a beginner's tutorial on how to use dwave-gate (taken from the examples folder). The demos are stored as Python notebooks in the docs/demos/demos folder and converted to Sphinx rst files and Jupyter notebooks using nbconvert and jupytext.

To convert a notebook into a valid Python notebook, run jupytext --to py:sphinx name_of_notebook_file.ipynb.

  • Builds a page in the documentation with linked cards for the different demos.
  • Stores the demos as Python notebooks for easier version control and reviews.
  • Converts Python notebooks into Sphinx rst files for rendering.
  • Converts Python notebooks into Jupyter notebooks for downloading.
  • Runs every notebook and renders the outputs on each build.

Note: Requires Pandoc to build the correct files, along with Python packages jupytext and nbconvert (already in doc requirements), which might require some changes to the SDK build.

@thisac thisac requested a review from JoelPasvolsky June 7, 2023 00:00
@codecov
Copy link

codecov bot commented Jun 7, 2023

Codecov Report

Merging #35 (9f50c55) into master (bcc2140) will not change coverage.
Report is 9 commits behind head on master.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master      #35   +/-   ##
=======================================
  Coverage   99.87%   99.87%           
=======================================
  Files          18       18           
  Lines        1559     1559           
  Branches      263      263           
=======================================
  Hits         1557     1557           
  Misses          1        1           
  Partials        1        1           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@thisac thisac self-assigned this Jun 7, 2023
Copy link
Contributor

@JoelPasvolsky JoelPasvolsky left a comment

Choose a reason for hiding this comment

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

Just a couple of comments on installing dependencies to get the JN conversions working. It took me a while but mostly because I tried and moved on from Win and Py 3.7 environments before installing a pyenv env in my Ubuntu 16.04 VM and working with Py 3.9.
Now that I have it working locally, I'll go through the rest.

Copy link
Contributor

@JoelPasvolsky JoelPasvolsky left a comment

Choose a reason for hiding this comment

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

@thisac, I'm about halfway through the first demo, so I'll submit this half in case you have reservations, we can talk through before I make similar suggestions for the rest. It's too be more consistent with the writing style in the rest of our docs

@thisac thisac marked this pull request as ready for review June 22, 2023 18:11
@thisac thisac requested review from JoelPasvolsky and randomir June 22, 2023 18:11
@thisac thisac added the documentation Improvements or additions to documentation label Jun 22, 2023
sphinx_autodoc_typehints==1.19.5
sphinx-design==0.4.1
ipykernel==6.23.1
matplotlib==3.71
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm on an old Ubuntu with Py3.9, so this might not be supported anyway but I get

ERROR: Could not find a version that satisfies the requirement matplotlib==3.71

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, should most definitely have been 3.7.1. Thanks for the discovery.

Copy link
Member

@randomir randomir left a comment

Choose a reason for hiding this comment

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

A few comments on the makefile.

# gates accept slightly different arguments, although you can _always_ pass the
# qubits as sequences via the keyword argument `qubits`.
#
# :::note
Copy link
Member

Choose a reason for hiding this comment

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

Not working for me as well (in the jupyter notebook).

cp -a $(DEMOFOLDER)/. $(OUTFOLDER)
# 1. find all Python demos and convert them to Jupyter notebooks
find $(DEMOFOLDER) -name '*.py' -exec sh -c '$(JUPYTEXT) --to ipynb $$1 --output "$(OUTFOLDER)/$$(basename $$1 .py).ipynb"' sh {} \;
Copy link
Member

Choose a reason for hiding this comment

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

You can use shell's built-in parameter expansion to avoid calling (external) baseline.

Comment on lines +42 to +57
find $(OUTFOLDER) -name '*.rst' -exec sh -c 'sed --in-place \
-e $(ADD_FILE_NAME_LABEL) \
-e "s/\`\`Circuit\`\`/:class:\`~dwave.gate.circuit.Circuit\`/g" \
-e "s/\`\`Circuit\.\(\w*\)\`\`/:meth:\`~dwave.gate.circuit.Circuit.\1\`/g" \
-e "s/\`\`ParametricCircuit\`\`/:class:\`~dwave.gate.circuit.ParametricCircuit\`/g" \
-e "s/\`\`Operation\`\`/:class:\`~dwave.gate.operations.base.Operation\`/g" \
-e "s/\`\`operations\`\`/:mod:\`~dwave.gate.operations\`/g" \
-e "s/\`\`ops\.\(\w*\)\`\`/:class:\`~dwave.gate.operations.operations.\1\`/g" \
-e "s/\`\`Measurement\`\`/:mod:\`~dwave.gate.operations.base.Measurement\`/g" \
-e "s/\`\`ParametricOperation\`\`/:mod:\`~dwave.gate.operations.base.ParametricOperation\`/g" \
-e "s/\`\`ControlledOperation\`\`/:mod:\`~dwave.gate.operations.base.ControlledOperation\`/g" \
-e "s/\`\`ParametricControlledOperation\`\`/:mod:\`~dwave.gate.operations.base.ParametricControlledOperation\`/g" \
-e "s/\`\`simulate\`\`/:mod:\`~dwave.gate.simulator.simulate\`/g" \
-e "s/\`\`simulator\.\(\w*\)\`\`/:class:\`~dwave.gate.simulator.\1\`/g" \
-e $(ADD_DOWNLOAD_BUTTONS) \
$$1' sh {} \; \
Copy link
Member

Choose a reason for hiding this comment

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

If you prefer sed, I'd extract these substitution commands in a stand-alone sed script, just to avoid the escape hell.

@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: demos
demos:
Copy link
Member

Choose a reason for hiding this comment

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

This build rule is sufficiently complex to warrant a stand-alone shell script.

Or, given how shell scripts are generally fragile, unmaintainable and unscalable, perhaps switch to something more portable and extensible, like Python? 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thought about it, and it's a good idea. I don't necessarily mind the simplicity of having it like this though. Perhaps leaving it like this for the time being and potentially reevaluate later.

find $(DEMOFOLDER) -name '*.py' -exec sh -c '$(JUPYTEXT) --to ipynb $$1 --output "$(OUTFOLDER)/$$(basename $$1 .py).ipynb"' sh {} \;
# 2. find all Jupyter notebooks and convert them into Sphinx rst files
find $(OUTFOLDER) -name '*.ipynb' -exec $(NBCONVERT) {} --to rst --output-dir=$(OUTFOLDER) --execute --RegexRemovePreprocessor.patterns="^%" \;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you'll want to skip processing notebooks under .ipynb_checkpoints/.

:download:\`Download as Jupyter Notebook \<$$(basename $$1 .rst).ipynb\>\`\n"
# Script to add file names as labels to the top of the rst files
ADD_FILE_NAME_LABEL = "1s/^/.. _$$(basename $$1 .rst):\n\n/"
Copy link
Member

Choose a reason for hiding this comment

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

You can also use sed's insert command to prepend text (slightly simpler expression). When combined with parameter expansion switch as suggested below:

Suggested change
ADD_FILE_NAME_LABEL = "1s/^/.. _$$(basename $$1 .rst):\n\n/"
ADD_FILE_NAME_LABEL = "1i .. _$${1%.rst}:\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants