A suite of integrated tools for creating, running, and managing tests to ensure code quality and reliability.
To run and test Q# code (with or without Python) in Visual Studio Code, use the Quantum Development Kit (QDK) and its testing/debugging features.
- Install and set up Q# in VS Code
- Install Visual Studio Code.
- Install the QDK extension for VS Code.
- If using Python with Q#, also install the
qsharpandazure-quantumPython packages as described in the QDK setup.
- Choose how to organize Q# and Python In VS Code, Q# can be used in three main formats:
- Pure Q#:
.qsfiles only. - Q# + Python:
.qsplus a Python host.pyfile that calls Q# operations. - Jupyter Notebook:
.ipynbwith both Python and Q# cells using the%%qsharpmagic.
- Run and debug pure Q# programs in VS Code
- Open a
.qsfile that contains an entry point operation. - In the editor, use the CodeLens commands next to the entry point:
- Select Run (or press
Ctrl + F5) to run on the built-in local simulator. - Select Debug (or press
F5) to step through the code, inspect state, and debug.
- Select Run (or press
- Output appears in the Debug Console.
- Run and test Q# from a Jupyter Notebook (Python kernel)
- In VS Code, open the Command Palette (
Ctrl + Shift + P) and select Create: New Jupyter Notebook. - In the first cell, import QDK support:
from qdk import qsharp import qdk.azure - Add a new cell, switch it to Q# with
%%qsharp, and define operations, for example:%%qsharp operation Random() : Result { use q = Qubit(); H(q); let result = M(q); Reset(q); return result } operation RandomNBits(N: Int): Result[] { mutable results = []; for i in 0 .. N - 1 { let r = Random(); results += [r]; } return results } - Test the operation on the local simulator from a Python cell:
qsharp.eval("RandomNBits(4)") - Run multiple shots and collect results:
qsharp.run("RandomNBits(4)", shots=10) - Run and test Q# from a Python script (Q# project)
- Create a Q# project and place Q# code in
Source.qs(for example theRandomandRandomNBitsoperations above). - In the project root (where
qsharp.jsonis), createrandomNum.pywith:from qdk import qsharp from qdk.azure import Workspace qsharp.init(project_root = '../MyProjectRootFolder') print(qsharp.eval("Source.RandomNBits(4)"))- If no namespace is specified in
Source.qs, the file nameSourceis used as the default namespace, so the operation isSource.RandomNBits.
- If no namespace is specified in
- Optionally use
qsharp.run("Source.RandomNBits(4)", shots=10)to run multiple shots and get a list of results. - Test and validate Q# code with assertions
- Use
failexpressions insideifstatements to enforce conditions:operation Main() : Unit { use qs = Qubit[6]; let n_qubits = Length(qs); if n_qubits != 3 { fail $"The system should have 3 qubits, not {n_qubits}."; } } - Or use
Std.Diagnostics.Factfor unit-test-like checks:import Std.Diagnostics.Fact; operation Main() : Unit { use qs = Qubit[6]; let n_qubits = Length(qs); Fact(n_qubits == 3, $"The system should have 3 qubits, not {n_qubits}."); } - Run with
Ctrl + F5to see whether tests pass or fail.
- Inspect and debug quantum state from Python
- In a Jupyter Notebook, after preparing qubits in a Q# cell, inspect the state with:
dump = qsharp.dump_machine() dump - To inspect gate matrices, define operations via
qsharp.evaland then calldump_operation, for example:qsharp.eval("operation SingleH(qs : Qubit[]) : Unit { H(qs[0]) }") res = dump_operation("SingleH", 1) print("Single-qubit Hadamard gate:\n", res)
These steps allow running, testing, and debugging Q# code and Q#–Python integrations entirely within Visual Studio Code using the QDK.
References: