Skip to content

Editing Q# in VS Code

Alex Hansen edited this page Jul 16, 2024 · 12 revisions

Editing Q# in VS Code

The recommended way of developing Q# code is in VS Code with the Azure Quantum Developer Kit extension. This extension provides both a rich Q# development environment and Azure Quantum integration. Features of the extension include:

  • Syntax highlighting and basic syntax features (e.g. brace matching)
  • Q# cell support in Jupyter notebooks. The extension will detect %%qsharp magic cells and automatically update the cell language to Q#
  • Error checking in Q# source files
  • Breakpoint debugging and script execution for Q# source files
  • Integration with Azure Quantum for quantum job submission
  • Hover-definition and docs
  • Go-to-definition
  • Function signature help
  • Snippet and sample support
  • Completions

Samples

Several common quantum algorithms are provided as samples in the completion list. After creating a new .qs document, type sample to filter the completion list to them, and tab-complete to insert the sample.

image

Running and Debugging Q# Code

After developing a Q# quantum algorithm, you have the option to run your code. This can be done either locally on a quantum simulator, for free; or via submission to Azure Quantum, for a price.

Running Locally on a Quantum Simulator

In VS Code, access the Command Palette (ctrl/cmd+shift+P). Run the command "Debug: Run Q# File". This will run the file on your computer, using a quantum simulator. (You can also use the Play icon and drop-down in the top-right of the editor. See the screenshot in Debugging Locally section following. Or simply press Ctrl-F5). Note that this command only appears if you currently have a ".qs" file selected and open. You can see the output from the program in the VS Code Debug Console output pane.

Debugging Locally

The extension includes a debugger. The debugger can handle typical breakpoint-style debugging (step into, over, out of, etc) as well as render program state.

To start the debugger, click the debug icon in the top left of VS Code. Select "Debug Q# File". image

From here, the state of the program should be visible in the debugger pane on the left, and breakpoints will work. You can see the qubit state as well as local variable state in the "Variables" section:

image For fun, try stepping through an adjoint section. The debugger will show you how the adjoint is generated and applied.

Running via Azure Quantum

See this wiki article for instructions on submitting to Azure.

Q# Projects

Q# supports the notion of Q# projects, which are multiple Q# files that are compiled together as one unit. To create a Q# project, create a folder and add a file called qsharp.json to it. qsharp.json should look like this:

{
  "author": "your name here",
  "license": "your chosen license here"
}

Q# source files are read from src/:

.
├── qsharp.json
└── src/
    ├── Main.qs
    ├── Foo.qs
    └── bar/
        └── Bar.qs

A Q# project, unlike a single-file compilation, can have dependencies on other Q# projects. If you've used libraries or library code in other languages, you will be familiar with this concept.