Skip to content

Commit

Permalink
Merge 4a63a13 into d6d1649
Browse files Browse the repository at this point in the history
  • Loading branch information
rum1887 committed Jun 13, 2023
2 parents d6d1649 + 4a63a13 commit 992ea61
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ qutip-qip |version|: QuTiP quantum information processing
:maxdepth: 2
:caption: Tutorials

tutorials.rst
tutorials_v5.rst

.. toctree::
:maxdepth: 2
Expand Down
134 changes: 134 additions & 0 deletions doc/source/tutorials-website/create_tutorials_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import os
import re
from jinja2 import Environment, FileSystemLoader, select_autoescape
import shutil
import subprocess
import tempfile
import mistune

def atoi(text):
return int(text) if text.isdigit() else text

def natural_keys(text):
return [atoi(c) for c in re.split('(\d+)', text)]

class notebook:
def __init__(self, path, title):
# remove ../ from path
self.path = path.replace('../', '')
self.title = title
# set url and update from markdown to ipynb
self.url = url_prefix + self.path.replace(".md", ".ipynb")

def get_title(filename):
""" Reads the title from a markdown notebook """
with open(filename, 'r') as f:
# get first row that starts with "# "
for line in f.readlines():
# trim leading/trailing whitespaces
line = line.lstrip().rstrip()
# check if line is the title
if line[0:2] == '# ':
# return title
return line[2:]

def sort_files_titles(files, titles):
""" Sorts the files and titles either by filenames or titles """
# identify numbered files and sort them
nfiles = [s for s in files if s.split('/')[-1][0].isdigit()]
nfiles = sorted(nfiles, key=natural_keys)
ntitles = [titles[files.index(s)] for s in nfiles]
# sort the files without numbering by the alphabetic order of the titles
atitles = [titles[files.index(s)] for s in files if s not in nfiles]
atitles = sorted(atitles, key=natural_keys)
afiles = [files[titles.index(s)] for s in atitles]
# merge the numbered and unnumbered sorting
return nfiles + afiles, ntitles + atitles

def get_notebooks(path):
""" Gets a list of all notebooks in a directory """
# get list of files and their titles
try:
files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.md')]
except FileNotFoundError:
return {}
titles = [get_title(f) for f in files]
# sort the files and titles for display
files_sorted, titles_sorted = sort_files_titles(files, titles)
# generate notebook objects from the sorted lists and return
notebooks = [notebook(f, t) for f, t in zip(files_sorted, titles_sorted)]
return notebooks

def generate_index_html(version_directory, tutorial_directories, title, version_note):
""" Generates the index HTML file from the given data """
# get tutorials from the different directories
tutorials = {}
for dir in tutorial_directories:
tutorials[dir] = get_notebooks(os.path.join(version_directory, dir))

# Load environment for Jinja and template
env = Environment(
loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "../")),
autoescape=select_autoescape()
)
template = env.get_template("tutorials-website/tutorials.html.jinja")

# render template and return
html = template.render(tutorials=tutorials, title=title, version_note=version_note)
return html

# Clone the qutip-tutorials repository
repo_url = 'https://github.com/qutip/qutip-tutorials.git'
cloned_repo_dir = tempfile.mkdtemp()
subprocess.run(['git', 'clone', repo_url, cloned_repo_dir])

# Set the necessary variables
url_prefix = "https://nbviewer.org/urls/qutip.org/qutip-tutorials/"
tutorial_directories = [
'pulse-level-circuit-simulation',
'quantum-circuits',
]

# Perform the operations on the cloned repository
prefix = ""
suffix = ""
#with open('prefix.html', 'r') as f:
# prefix = f.read()
#with open('suffix.html', 'r') as f:
# suffix = f.read()

# Version 4 index file
title = 'Tutorials for QuTiP Version 4'
version_note = 'These are the tutorials for QuTiP Version 4. You can find the tutorials for QuTiP Version 5 <a href="./index-v5.html">here</a>.'
html = generate_index_html(os.path.join(cloned_repo_dir, 'tutorials-v4/'), tutorial_directories, title, version_note)
with open('qutip-qip.html', 'w+') as f:
#f.write(prefix)
f.write(html)
#f.write(suffix)

# Version 5 index file
title = 'Tutorials for QuTiP Version 5'
version_note = 'These are the tutorials for QuTiP Version 5. You can find the tutorials for QuTiP Version 4 <a href="./index.html">here</a>.'
html = generate_index_html(os.path.join(cloned_repo_dir, 'tutorials-v5/'), tutorial_directories, title, version_note)
with open('qutip-qip-v5.html', 'w+') as f:
#f.write(prefix)
f.write(html)
#f.write(suffix)

# Wipe off the cloned repository
shutil.rmtree(cloned_repo_dir)

def convert_html_to_rst(html_file_path, rst_file_path):
# Use the subprocess module to call the pandoc command-line tool
subprocess.run(['pandoc', html_file_path, '-o', rst_file_path])

html_file_path = 'qutip-qip.html'
html_file_path_v5 = 'qutip-qip-v5.html'

rst_file_path = '../tutorials.rst'
rst_file_path_v5 = '../tutorials_v5.rst'

#convert_html_to_rst(html_file_path, rst_file_path)
convert_html_to_rst(html_file_path_v5, rst_file_path_v5)


39 changes: 39 additions & 0 deletions doc/source/tutorials-website/tutorials.html.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div class="row">
<div class="col-md-12">
<h1>{{ title }}</h1><br>
</div>
</div>

<h4 id="quantum-information-processing">Quantum information processing</h4>
<p>This section contains tutorials for QuTiP Version 4. You can find the tutorials for QuTiP Version 5 here.
</p>

<h5 id="qip-circuits">Quantum circuits and algorithms</h5>
<ul>
{% for item in tutorials['quantum-circuits'] %}
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
{% endfor %}
</ul>

<h5 id="qip-pulse-level">Pulse-level circuit simulation</h5>
<ul>
{% for item in tutorials['pulse-level-circuit-simulation'] %}
<li><a href="{{ item.url }}">{{ item.title }}</a></li>
{% endfor %}
</ul>

<div class="row">
<div class="col-md-12">
<h3 id="contributing">Contributing</h3>
<p>If you would like to contribute a notebook or report a bug, you may open
an issue or pull request in the
<a href="https://github.com/qutip/qutip-tutorials">qutip-tutorials</a>
GitHub repository.
</p>
<p>A few of the notebooks are still maintained in the repository
<a href="https://github.com/qutip/qutip-notebooks">qutip-notebooks</a> and
a complete archive of older versions of the tutorials is maintained there.
</p>

</div>
</div>
10 changes: 0 additions & 10 deletions doc/source/tutorials.rst

This file was deleted.

63 changes: 63 additions & 0 deletions doc/source/tutorials_v5.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.. container:: row

.. container:: col-md-12

.. rubric:: Tutorials
:name: tutorials

Quantum information processing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This are the tutorials for QuTiP Version 5. You can find the tutorials
for QuTiP Version 4 `here <./qutip-qip.html>`.

.. _qip-circuits:

Quantum circuits and algorithms
'''''''''''''''''''''''''''''''

- `Imports and Exports QASM
circuit <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/quantum-circuits/qasm.ipynb>`__
- `Decomposition of the Toffoli gate in terms of CNOT and single-qubit
rotations <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/quantum-circuits/qip-toffoli-cnot.ipynb>`__
- `QuTiP example: Quantum Gates and their
usage <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/quantum-circuits/quantum-gates.ipynb>`__
- `Quantum Teleportation
Circuit <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/quantum-circuits/teleportation.ipynb>`__

.. _qip-pulse-level:

Pulse-level circuit simulation
''''''''''''''''''''''''''''''

- `Compiling and simulating a 10-qubit Quantum Fourier Transform (QFT)
algorithm <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-10-qubit-QFT-algorithm.ipynb>`__
- `Custimize the pulse-level
simulation <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-customize-device.ipynb>`__
- `Examples for
OptPulseProcessor <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-optpulseprocessor.ipynb>`__
- `Simulating the Deutsch–Jozsa algorithm at the pulse
level <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-processor-DJ-algorithm.ipynb>`__
- `Simulating randomized
benchmarking <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-randomized-benchmarking.ipynb>`__
- `measuring the relaxation time with the idling
gate <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-relaxation-measurement-with-the-idling-gate.ipynb>`__
- `Scheduler for quantum gates and
instructions <https://nbviewer.org/urls/qutip.org/qutip-tutorials/tutorials-v5/pulse-level-circuit-simulation/qip-scheduler.ipynb>`__

.. container:: row

.. container:: col-md-12

.. rubric:: Contributing
:name: contributing

If you would like to contribute a notebook or report a bug, you
may open an issue or pull request in the
`qutip-tutorials <https://github.com/qutip/qutip-tutorials>`__
GitHub repository.

A few of the notebooks are still maintained in the repository
`qutip-notebooks <https://github.com/qutip/qutip-notebooks>`__ and
a complete archive of older versions of the tutorials is
maintained there.

0 comments on commit 992ea61

Please sign in to comment.