Tutorials and programming exercises for learning Q# and quantum computing
Перейти к файлу
Andres Paz a93e56de11
Separate CI build and validation scripts for different areas (#410)
* Separate building projects versions of the katas, Jupyter Notebooks validation and building projects that create, validate and pack binaries into separate CI jobs.
* Include DumpMachine unit tests to the CI.
2020-07-22 13:21:54 -07:00
.github Update link to Q# Jupyter Notebooks install guide (#294) 2020-03-06 10:04:23 -08:00
BasicGates Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
CHSHGame Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
DeutschJozsaAlgorithm Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
DistinguishUnitaries Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
GHZGame Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
GraphColoring Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
GroversAlgorithm Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
JointMeasurements Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
KeyDistribution_BB84 Several small maintenance fixes (#417) 2020-07-21 01:17:13 -07:00
MagicSquareGame Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
Measurements [Measurements] Fix small typo in workbook for task 1.3 (#402) 2020-07-14 02:20:17 -07:00
PhaseEstimation Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
QEC_BitFlipCode Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
QFT Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
RippleCarryAdder Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
SimonsAlgorithm Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
SolveSATWithGrover [SolveSATWithGrover] Fix invalid JSON in notebook (#411) 2020-07-15 12:31:08 -07:00
SuperdenseCoding Update QDK to version 0.12.20070124 (#397) 2020-07-06 11:36:53 -07:00
Superposition [Superposition] Add workbook solution for task 2.3 (#409) 2020-07-18 22:02:53 -07:00
Teleportation [Teleportation] Simplify reference implementations to tasks 4.1 and 4.2 (#413) 2020-07-17 23:56:34 -07:00
TruthTables [TruthTables] Use '|' instead of ':' in conditional expression in task 6 statement. (#400) 2020-07-11 19:37:03 -07:00
UnitaryPatterns [UnitaryPatterns] Add the workbook (#377) 2020-07-12 01:50:56 -07:00
quickref Add multi-qubit gates to the quick reference (#289) 2020-02-28 10:49:08 -08:00
scripts Separate CI build and validation scripts for different areas (#410) 2020-07-22 13:21:54 -07:00
tutorials Several small maintenance fixes (#417) 2020-07-21 01:17:13 -07:00
utilities Replacing deprecated IconUrl with Icon and updating the assets for NuGet packages. (#407) 2020-07-15 09:32:36 -07:00
.gitattributes Use Microsoft Container Regristry image for hosting Notebooks on Binder (#152) 2019-07-22 15:06:16 -07:00
.gitignore Add tutorial on complex arithmetic (#181) 2019-10-19 21:43:28 -07:00
Dockerfile Add random number generation (QRNG) tutorial (#398) 2020-07-14 01:39:49 -07:00
LICENSE Initial commit 2018-06-06 13:02:37 -07:00
NuGet.config Update QDK to version 0.10.1910.3107 (#218) 2019-11-01 18:59:17 -07:00
README.md Add random number generation (QRNG) tutorial (#398) 2020-07-14 01:39:49 -07:00
azure-pipelines.yml Separate CI build and validation scripts for different areas (#410) 2020-07-22 13:21:54 -07:00
index.ipynb Add random number generation (QRNG) tutorial (#398) 2020-07-14 01:39:49 -07:00

README.md

Introduction

The Quantum Katas are a series of self-paced tutorials to help you learn quantum computing and Q# programming.

🆕 (November 2019) We've added a set of tutorials that introduce the learner to the most basic concepts used in quantum computing, starting with the necessary math (complex numbers and linear algebra). Read more about the new tutorials here or head to the list of tutorials to get started!

🆕 (July 2019) The Quantum Katas now include Jupyter Notebook tutorials on quantum computing! Each tutorial combines theoretical explanations with Q# code snippets and programming exercises. See index.ipynb for the list of all tutorials and instructions on running them online.

🆕 (April 2019) The Quantum Katas are now available as Jupyter Notebooks! See index.ipynb for the list of all Kata Notebooks and instructions on running them online.

Each kata is a separate project that includes:

  • A sequence of tasks progressing from easy to hard. Each task requires you to fill in some code. The first task might require just one line, and the last one might require rather complicated code.
  • A testing framework that sets up, runs, and validates your solutions. Each task is covered by a unit test which initially fails. Once you write the code to make the test pass, you can move on to the next task.
  • Links to quantum computing and Q# reference material you might need to solve the tasks.
  • Hints and reference solutions to help you if you're stuck.

Table of contents

List of tutorials

List of Katas

Quantum computing concepts

Simple algorithms

Grover's algorithm

Tools and libraries/Building up to Shor's algorithm

Entanglement games

Reversible computing

  • Truth tables. Learn to represent and manipulate Boolean functions as truth tables and to implement them as quantum operations.
  • Ripple-carry adder. Build a ripple-carry adder on a quantum computer.

Miscellaneous

  • BB84 protocol. Implement the BB84 key distribution algorithm.
  • Bit-flip error correcting code. Learn about a 3-qubit error correcting code for protecting against bit-flip errors.
  • Distinguish Unitaries. Learn to distinguish unitaries by designing and performing experiments with them.
  • Unitary Patterns. Learn to implement unitaries with matrices that follow certain patterns of zero and non-zero elements.

For a Q# programming language quick reference sheet, see Q# Language Quick Reference.

Run the katas and tutorials online

The Quantum Katas are now available as Jupyter Notebooks online! See index.ipynb for the list of all katas and tutorials, and instructions for running them online.

While running the Katas online is the easiest option to get started, if you want to save your progress and enjoy better performance, we recommend you to choose the local option.

Run the katas locally

Quantum Development Kit Installation

To use the Quantum Katas locally, you'll need the Quantum Development Kit, available for Windows 10, macOS, and Linux. If you don't already have the Quantum Development Kit installed, see the install guide for the Quantum Development Kit.

If you want to run the katas and tutorials locally as Jupyter Notebooks:

  1. Follow the steps in the QDK install guide for Python and the QDK install guide for Jupyter Notebooks.
  2. Several tutorials require installing additional Python packages:

If you want to run the katas and tutorials locally as Q# projects:

Follow the steps in the QDK install guide for Visual Studio, for Visual Studio Code or for command line with other editors.

Download the Quantum Katas

If you have Git installed, clone the Microsoft/QuantumKatas repository:

$ git clone https://github.com/Microsoft/QuantumKatas.git

[!TIP] Both Visual Studio 2019 and Visual Studio Code make it easy to clone repositories from within your development environment. For details, see the Visual Studio 2019 and Visual Studio Code documentation.

If you don't have Git installed, download the katas from https://github.com/Microsoft/QuantumKatas/archive/master.zip.

Run a kata as a Jupyter Notebook

The best way to run the katas as Jupyter Notebooks is to navigate to the root folder of the repository and to open index.ipynb using Jupyter:

$ cd QuantumKatas/
$ jupyter notebook index.ipynb

This will open the notebook that contains a list of all katas and tutorials, and you will be able to navigate to the one you want using links.

You can also open an individual notebook directly, but this might render internal links invalid:

$ cd QuantumKatas/tutorials/ComplexArithmetic
$ jupyter notebook ComplexArithmetic.ipynb

Run a kata as a Q# project

Each kata is in its own directory as a self-contained Q# project, solution and Jupyter Notebook triplet. For instance, the BasicGates directory structure is:

QuantumKatas/
  BasicGates/
    README.md                  # Instructions specific to this kata.
    .vscode/                   # Metadata used by Visual Studio Code.
    BasicGates.sln             # Visual Studio 2019 solution file.
    BasicGates.csproj          # Project file used to build both classical and quantum code.
    BasicGates.ipynb           # Jupyter Notebook front-end for this kata.

    Tasks.qs                   # Q# source code that you will fill as you solve each task.
    Tests.qs                   # Q# tests that verify your solutions.
    TestSuiteRunner.cs         # C# source code used to run the Q# tests.
    ReferenceImplementation.qs # Q# source code containing solutions to the tasks.

To open the BasicGates kata in Visual Studio 2019, open the QuantumKatas/BasicGates/BasicGates.sln solution file.

To open the BasicGates kata in Visual Studio Code, open the QuantumKatas/BasicGates/ folder. Press Ctrl + Shift + P (or ⌘ + Shift + P on macOS) to open the Command Palette. Type Open Folder on Windows 10 or Linux or Open on macOS.

[!TIP] Almost all commands available in Visual Studio Code are in the Command Palette. If you get stuck, press Ctrl + Shift + P (or ⌘ + Shift + P on macOS) and start typing to search through all available commands.

You can also launch Visual Studio Code from the command line:

$ code QuantumKatas/BasicGates/

Run kata tests

Once you have a kata open, it's time to run the tests using the following instructions. Initially all tests will fail. Don't panic! Open Tasks.qs and start filling in the code to complete the tasks. Each task is covered by a unit test. Once you fill in the correct code for a task, rebuild the project and re-run the tests, and the corresponding unit test will pass.

Visual Studio 2019

  1. Build the solution.
  2. From the main menu, open Test Explorer (Test > Windows) and select Run All to run all unit tests at once.
  3. Work on the tasks in the Tasks.qs file.
  4. To test your code changes for a task, rebuild the solution and re-run all unit tests using Run All, or run just the test for that task by right-clicking the test and selecting Run Selected Tests.

Visual Studio Code

  1. Press Ctrl + ` (or ⌘ + ` on macOS) to open the integrated terminal. The terminal should open to the kata directory. If it doesn't, navigate to the folder containing the *.csproj file for the kata using cd command.
  2. Run dotnet test in the integrated terminal. This should build the kata project and run all of the unit tests. All of the unit tests should fail.
  3. Work on the tasks in the Tasks.qs file.
  4. To test your code changes for a task, from the integrated terminal run dotnet test again.

For convenience, a tasks.json configuration file exists for each kata. It allows Visual Studio Code to run the build and test steps from the Command Palette. Press Ctrl + Shift + P (or ⌘ + Shift + P on macOS) to open the Palette and type Run Build Task or Run Test Task and press Enter.

Run katas locally with Docker

You can use the included Dockerfile to create a docker image with all the necessary tools to run the katas from the command line or Jupyter.

  1. Install Docker.
  2. Build the docker image and tag it katas:
docker build -t katas .
  1. Run the image in the container named katas-container with interactive command-line and redirect container port 8888 to local port 8888 (needed to run Jupyter):
docker run -it --name katas-container -p 8888:8888 katas /bin/bash
  1. From the same command line that you used to run the container, run the C# version of the BasicGates kata:
cd ~/BasicGates/
dotnet test
  1. Start a Jupyter Notebook within the image for the BasicGates kata:
cd ~/BasicGates/ && jupyter notebook --ip=0.0.0.0 --no-browser
  1. Once Jupyter has started, use your browser to open the kata in notebook format. You will need a token generated by Jupyter when it started on the previous step:
http://localhost:8888/notebooks/BasicGates.ipynb

To exit a docker container without killing it (daemon mode), press Ctrl+P, Ctrl+Q

To re-enter the existing katas-container (in daemon mode):

docker attach katas-container

Once you're done, remove the katas-container:

docker rm --force katas-container

Contributing

This project welcomes contributions and suggestions. See How Can I Contribute? for details.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.