Skip to content

Commit e51b7cf

Browse files
committed
Merged main into live
2 parents a0029d9 + e6ff9b8 commit e51b7cf

10 files changed

Lines changed: 207 additions & 45 deletions
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
author: azure-quantum-content
3+
description: The article gives an overview of how to interact with circuit diagram elements in the QDK extension for VS Code
4+
ms.date: 12/08/2025
5+
ms.author: quantumdocwriters
6+
ms.service: azure-quantum
7+
ms.subservice: qdk
8+
ms.topic: overview
9+
no-loc: ["Microsoft Quantum Development Kit", "Quantum Development Kit", "QDK", "Azure Quantum", "Visual Studio Code", "VS Code", "qdk", "azure-quantum", "qdk[jupyter]", "Jupyter", "Jupyter Notebook"]
10+
title: Overview of QDK circuit diagrams
11+
uid: microsoft.quantum.overview.qdk-circuit-diagrams
12+
#customer intent: As a quantum programmer, I want to understand how to read and interact with circuit diagrams in the QDK
13+
---
14+
15+
# Explore circuit diagrams in the QDK
16+
17+
Quantum algorithms are a mixture of quantum programming and classical programming. The quantum part includes qubits, quantum gates, and probabilistic measurements, while the classical part includes control flow constructs such as conditional logic and loops. Quantum circuit diagrams need to incorporate elements from both quantum and classical programming.
18+
19+
The Microsoft Quantum Development Kit (QDK) creates interactive circuit diagrams for Q# and OpenQASM programs that show how classical control groups affect the structure of your circuit.
20+
21+
For information on how to create circuit diagrams with the QDK in VS Code, see [How to visualize quantum circuit diagrams with the QDK](xref:microsoft.quantum.how-to.visualize-circuits)
22+
23+
## Classical control groups in quantum circuit diagrams
24+
25+
In some quantum algorithms, the circuit changes depending on the result of a mid-circuit measurement. For example, the circuit applies a gate to a specific qubit only if the measurement result for another qubit is 1. Or, the circuit iterates over a loop until the measurement result for a specific qubit is 0.
26+
27+
For example, the following two-qubit Q# program puts a qubit into a superposition state, measures the qubit, and then applies a different gate to the other qubit depending on the measurement result of the first qubit.
28+
29+
```qsharp
30+
import Std.Measurement.*;
31+
32+
operation Main() : Result[] {
33+
use q0 = Qubit();
34+
use q1 = Qubit();
35+
36+
H(q0);
37+
38+
let r = M(q0);
39+
40+
if r == One {
41+
X(q1);
42+
} else {
43+
Y(q1);
44+
}
45+
46+
let r1 = M(q1);
47+
48+
[r, r1]
49+
}
50+
```
51+
52+
The circuit diagram for this program looks like this:
53+
54+
:::image type="content" source="media/if-else-circuit-diagram.png" alt-text="The static circuit diagram for a Q# program with an if-else classical control flow group.":::
55+
56+
> [!TIP]
57+
> Select an element in the circuit diagram to highlight the code that creates the circuit element.
58+
59+
## Circuit diagram block types
60+
61+
QDK circuit diagrams have two types of blocks, quantum operation blocks and classical control group blocks. Some blocks are collapsible. To collapse or expand a block, select the **+/-** icon at the top left corner of the block.
62+
63+
:::image type="content" source="media/circuit-expansion-icon.png" alt-text="Screenshot of the expand/collapse icon in QDK circuit diagrams.":::
64+
65+
### Quantum operation blocks
66+
67+
Each object in your program that applies quantum gates or measurements to qubits has its own block in the circuit diagram. For Q# programs, these objects include operations, functors, and lambdas. Each operation block shows the name of the Q# or OpenQASM object that corresponds to that block.
68+
69+
### Classical control flow blocks
70+
71+
Each conditional statement and loop in your program has its own block in the circuit diagram.
72+
73+
#### Conditional blocks
74+
75+
Conditional blocks are preceded by a condition icon $C_n$, where each conditional statement in the program is identified by a unique subscript number.
76+
77+
:::image type="content" source="media/circuit-condition-icon.png" alt-text="Screenshot of a condition icon in a QDK circuit diagram.":::
78+
79+
Each conditional block is labeled with the condition that, if true, causes that part of the circuit to run during the program.
80+
81+
#### Loop blocks
82+
83+
Loop blocks are labeled with **loop: 1.. _N_**, where **_N_** is the number of iterations of the loop. To view the circuit for each iteration of the loop, select the expansion icon in the loop block.
84+
85+
## Related content
86+
87+
- [How to visualize quantum circuit diagrams with the QDK](xref:microsoft.quantum.how-to.visualize-circuits)
88+
- [Quantum circuit diagram conventions](xref:microsoft.quantum.concepts.circuits)
Lines changed: 118 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
11
---
22
author: azure-quantum-content
3-
description: Learn how to how to visually represent quantum algorithms with quantum circuit diagrams using VS Code, Python, and Jupyter Notebooks.
3+
description: Learn how to visually represent Q# and OpenQASM quantum algorithms with circuit diagrams in VS Code, Python, and Jupyter Notebook.
44
ms.date: 12/08/2025
55
ms.author: quantumdocwriters
66
ms.service: azure-quantum
77
ms.subservice: qdk
88
ms.topic: how-to
99
no-loc: ["Microsoft Quantum Development Kit", "Quantum Development Kit", "QDK", "Azure Quantum", "Visual Studio Code", "VS Code", "qdk", "azure-quantum", "qdk[jupyter]", "Jupyter", "Jupyter Notebook"]
10-
title: Visualize Quantum Circuits with Q#
10+
title: Visualize Quantum Circuits with the QDK
1111
uid: microsoft.quantum.how-to.visualize-circuits
12-
#customer intent: As a quantum programmer, I want to visually represent my quantum algorithms.
12+
#customer intent: As a quantum programmer, I want to visually represent my quantum algorithms as circuit diagrams.
1313
---
1414

15-
# How to visualize quantum circuit diagrams with Q#
15+
# How to visualize quantum circuit diagrams with the QDK
1616

17-
Quantum circuit diagrams are a visual representation of quantum operations. Circuit diagrams show the flow of qubits through the quantum program, including the gates and measurements that the program applies to the qubits.
17+
Quantum circuit diagrams are a visual representation of quantum algorithms. Circuit diagrams show the flow of qubits through a quantum program, including the gates and measurements that the program applies to the qubits.
1818

19-
In this article, you learn how to visually represent quantum algorithms with quantum circuit diagrams in the Microsoft Quantum Development Kit (QDK) using Visual Studio Code (VS Code) and Jupyter Notebook.
19+
In this article, you learn how to create circuit diagrams for Q# and OpenQASM programs with the Microsoft Quantum Development Kit (QDK) using Visual Studio Code (VS Code) and Jupyter Notebook.
2020

21-
For more information about quantum circuit diagrams, see [Quantum circuit conventions](xref:microsoft.quantum.concepts.circuits).
21+
For more information about quantum circuit diagrams, see [Quantum circuit diagram conventions](xref:microsoft.quantum.concepts.circuits).
2222

2323
## Prerequisites
2424

25+
To create circuit diagrams from Q# and OpenQASM files in VS Code, install the following:
26+
2527
- The latest version of [VS Code](https://code.visualstudio.com/download), or open [VS Code for the Web](https://vscode.dev/).
26-
- The latest version of the [QDK extension](https://marketplace.visualstudio.com/items?itemName=quantum.qsharp-lang-vscode), [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python), and [Jupyter extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) installed in VS Code.
27-
- The latest version of the `qdk` Python library with the optional `jupyter` extra.
28+
- The latest version of the [QDK extension](https://marketplace.visualstudio.com/items?itemName=quantum.qsharp-lang-vscode) in VS Code.
29+
30+
To create circuit diagrams from Python programs in Jupyter Notebook, install the following:
31+
32+
- The [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) and [Jupyter extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) in VS Code.
33+
- The latest version of the `qdk` Python library with the `jupyter` extra.
2834

2935
```bash
3036
python -m pip install --upgrade "qdk[jupyter]"
3137
```
3238

3339
## Visualize quantum circuits in VS Code
3440

35-
To visualize quantum circuits of Q# programs in VS Code, complete the following steps.
41+
With the QDK extension in VS Code, you can create circuit diagrams for Q# (`.qs`) and OpenQASM (`.qasm`) files.
3642

37-
### View circuit diagrams for a Q# program
43+
To view a circuit diagram in VS Code, follow these steps:
3844

39-
1. Open a Q# file in VS Code, or [load one of the quantum samples](xref:microsoft.quantum.submit-jobs#load-a-q-sample-program).
40-
1. Choose the **Circuit** command from the code lens that precedes your entry point operation.
45+
1. Open a Q# or OpenQASM file in VS Code, or [load one of the quantum samples from the QDK](xref:microsoft.quantum.submit-jobs#load-a-q-sample-program).
46+
1. Choose the **Circuit** command from the code lens that precedes your program.
4147

42-
:::image type="content" source="media/codelens-circuit.png" alt-text="Screenshot of a Q# file in Visual Studio Code that shows where to find the code lens circuit command.":::
48+
The **QDK Circuit** window opens and displays the circuit diagram for your program. For example, the following circuit diagram corresponds to a program that produces a random bit. The circuit puts the qubit into a superposition state and then measures the qubit.
4349

44-
The Q# circuit window opens and displays the circuit diagram for your program. For example, the following circuit corresponds to an operation that puts a qubit in a superposition state and then measures the qubit. The circuit diagram shows one qubit register that's initialized to the $\ket{0}$ state. Then, a Hadamard gate is applied to the qubit, followed by a [measurement operation](xref:microsoft.quantum.concepts.circuits#measurement-operator), which is represented by a meter symbol. In this case, the measurement result is zero.
45-
46-
:::image type="content" source="media/circuit-vscode-randombit.png" alt-text="Screenshot of the Q# circuit window that shows the circuit diagram for the random bit operation.":::
50+
:::image type="content" source="media/circuit-vscode-randombit.png" alt-text="Screenshot of the QDK Circuit window that shows the circuit diagram for a random bit program.":::
4751

4852
> [!TIP]
49-
> In Q# and OpenQASM files, select an element in the circuit diagram to highlight the code that creates the circuit element.
53+
> Select an element in the circuit diagram to highlight the code that creates the circuit element.
5054

51-
### View circuit diagrams for individual operations
55+
### View circuit diagrams for individual Q# operations
5256

5357
To visualize the quantum circuit for an individual operation in a Q# file, choose the **Circuit** command from the code lens that precedes the operation.
5458

@@ -62,15 +66,19 @@ When you use the VS Code debugger in a Q# program, you can visualize the quantum
6266
1. In the **Run and Debug** pane, expand the **Quantum Circuit** dropdown in the **VARIABLES** menu. The **QDK Circuit** panel opens, which shows the circuit while you step through the program.
6367
1. Set breakpoints and step through your code to see how the circuit updates as your program runs.
6468

65-
## Quantum circuits in Jupyter Notebook
69+
## Visualize quantum circuits in Jupyter Notebook
70+
71+
In Jupyter Notebook, you can visualize quantum circuits for Q# and OpenQASM programs with the `qdk.qsharp` and `qdk.widgets` Python modules. The `widgets` module provides a widget that renders a quantum circuit diagram as an SVG image.
72+
73+
For more examples of circuit diagram generation in Jupyter Notebook, see the [Circuits sample notebook](https://github.com/microsoft/qdk/blob/main/samples/notebooks/circuits.ipynb) on the QDK GitHub repository.
6674

67-
In Jupyter Notebook, you can visualize quantum circuits with the `qdk.qsharp` and `qdk.widgets` Python modules. The `widgets` module provides a widget that renders a quantum circuit diagram as an SVG image.
75+
### View circuit diagrams for Q# programs
6876

69-
### View circuit diagrams for an entry expression
77+
To view a circuit diagram for a Q# program in Jupyter Notebook, follow these steps:
7078

7179
1. In VS Code, open the **View** menu and choose **Command Palette**.
72-
1. Enter and select **Create: New Jupyter Notebook**.
73-
1. In the first cell of the notebook, run the following code to import the `qsharp` package.
80+
1. Enter **Create: New Jupyter Notebook**. An empty Jupyter Notebook file opens in a new tab.
81+
1. In the first cell of the notebook, run the following code to import the `qsharp` module.
7482

7583
```python
7684
from qdk import qsharp
@@ -89,32 +97,38 @@ In Jupyter Notebook, you can visualize quantum circuits with the `qdk.qsharp` an
8997
}
9098
```
9199

92-
1. To display a simple quantum circuit based on the current state of your program, pass an entry point expression to the `qsharp.circuit` function. For example, the circuit diagram of the preceding code shows two qubit registers that are initialized to the $\ket{0}$ state. Then, a Hadamard gate is applied to the first qubit. Finally, a CNOT gate is applied where the first qubit is the control, represented by a dot, and the second qubit is the target, represented by an X.
100+
1. To display a quantum circuit diagram, pass the Q# operation to the `qsharp.circuit` function. Run the following code in a new cell:
93101

94102
```python
95103
qsharp.circuit("BellState()")
96104
```
97105
106+
The output looks like this:
107+
98108
```output
99109
q_0 ── H ──── ● ──
100110
q_1 ───────── X ──
101111
```
102112
103-
1. To visualize a quantum circuit as an SVG image, use the `widgets` module. Create a new cell, then run the following code to visualize the circuit that you created in the previous cell.
113+
1. To render an SVG image of the quantum circuit, use the `widgets` module. Create a new cell, then run the following code to visualize the same circuit that you created in the previous cell.
104114
105115
```python
106116
from qdk.widgets import Circuit
107117
108118
Circuit(qsharp.circuit("BellState()"))
109119
```
110120
111-
:::image type="content" source="media/circuits-jupyter-notebook-bellstate.png" alt-text="Screenshot of a Jupyter Notebook that shows how to visualize the circuit for a Q# operation.":::
121+
The circuit diagram looks like this:
122+
123+
:::image type="content" source="media/circuits-jupyter-notebook-bellstate.png" alt-text="Screenshot of a Jupyter Notebook that shows how to visualize the circuit for a Q# program.":::
124+
125+
#### View circuit diagrams for operations that take qubits as input
112126
113-
### View circuit diagrams for operations with qubits
127+
In the previous Bell state example, the operation `BellState` doesn't take qubits as input. If the operation takes qubits or qubit arrays as input, then omit the parentheses when you pass the operation to draw circuit diagrams.
114128
115-
You can generate circuit diagrams of operations that take qubits, or arrays of qubits, as input. The diagram shows a wire for each input qubit, along with wires for additional qubits that you allocate within the operation. When the operation takes an array of qubits `(Qubit[])`, the circuit shows the array as a register of 2 qubits.
129+
For example, follow these steps to draw circuit diagrams for an operation that takes qubits:
116130
117-
1. Add a new cell, and then copy and run the following Q# code. This code prepares a cat state.
131+
1. In a new cell, run the following Q# code. This code prepares a cat state.
118132
119133
```qsharp
120134
%%qsharp
@@ -125,26 +139,88 @@ You can generate circuit diagrams of operations that take qubits, or arrays of q
125139
}
126140
```
127141
128-
1. Add a new cell and run the following code to visualize the circuit of the `PrepareCatState` operation.
142+
1. To draw the circuit as a text diagram, run the following code:
143+
144+
```python
145+
Circuit(qsharp.circuit(operation="PrepareCatState"))
146+
```
147+
148+
1. To render the circuit diagram as an SVG image, run the following code:
129149
130150
```python
131151
Circuit(qsharp.circuit(operation="PrepareCatState"))
132152
```
133153
134-
## Circuit diagrams for dynamic circuits
154+
When the operation takes an array of qubits `(Qubit[])`, the circuit shows the array as a register of two qubits.
155+
156+
### View circuit diagrams for OpenQASM programs
157+
158+
To view a circuit diagram for an OpenQASM program in Jupyter Notebook, follow these steps:
159+
160+
1. In VS Code, open the **View** menu and choose **Command Palette**.
161+
1. Enter **Create: New Jupyter Notebook**. An empty Jupyter Notebook file opens in a new tab.
162+
1. In the first cell of the notebook, run the following code to import the necessary objects to create and call OpenQASM functions with the QDK Python library:
163+
164+
```python
165+
from qsharp.openqasm import import_openqasm, ProgramType
166+
```
167+
168+
1. In a new cell, write your OpenQASM program in a Python string and pass the string to the `import_openqasm` function. To call the program in your Python code, give the function a name and set `program_type` to `ProgramType.File`.
135169
136-
Circuit diagrams are generated by executing the classical logic within a Q# program and keeping track of all allocated and applied gates. Loops and conditionals are supported when they deal only with classical values.
170+
```python
171+
source = """
172+
include "stdgates.inc";
173+
bit[2] c;
174+
qubit[2] q;
175+
h q[0];
176+
cx q[0], q[1];
177+
c = measure q;
178+
"""
179+
180+
import_openqasm(source, name="bell", program_type=ProgramType.File)
181+
```
182+
183+
1. In a new cell, import the OpenQASM program as a Python function and call the program to get the measurement results.
184+
185+
```python
186+
from qsharp.code.qasm_import import bell
187+
188+
bell()
189+
```
190+
191+
1. To draw the circuit as a text diagram, run the following code in a new cell:
192+
193+
```python
194+
from qdk.qsharp import circuit
195+
196+
circuit(bell)
197+
```
198+
199+
The output looks like this:
200+
201+
```output
202+
q_0 ── H ──── ● ──── M ──
203+
│ ╘═══
204+
q_1 ───────── X ──── M ──
205+
╘═══
206+
```
207+
208+
1. To render the circuit diagram as an SVG image, run the following code in a new cell:
209+
210+
```python
211+
from qsharp_widgets import Circuit
212+
213+
Circuit(qsharp.circuit(bell))
214+
```
137215
138-
However, programs that contain loops and conditional expressions that use qubit measurement results are trickier to represent with a circuit diagram. For example, consider the following expression:
216+
The circuit diagram looks like this:
139217
140-
```qsharp
141-
if (M(q) == One) {
142-
X(q)
143-
}
144-
```
218+
:::image type="content" source="media/circuits-jupyter-openqasm-bellstate.png" alt-text="Screenshot of a Jupyter Notebook that shows how to visualize the circuit for an OpenQASM program.":::
145219
146-
This expression can't be represented with a straightforward circuit diagram because the gates are conditional on a measurement result. Circuits with gates that depend on measurement results are called dynamic circuits.
220+
> [!NOTE]
221+
> For OpenQASM programs, you can't view circuit diagrams for individual functions. You can view circuit diagrams for only the whole program.
147222
148-
You can generate diagrams for dynamic circuits by running the program in the quantum simulator, and tracing the gates as they're applied. This is called trace mode because the qubits and gates are traced as the simulation is performed.
223+
## Related content
149224
150-
The downside of traced circuits is that they only capture the measurement outcome and the consequent gate applications for a single simulation. In the above example, if the measurement outcome is `Zero`, then the `X` gate isn't in the diagram. If you run the simulation again, then you might get a different circuit.
225+
- [Explore circuit diagrams in the QDK](xref:microsoft.quantum.overview.qdk-circuit-diagrams)
226+
- [Quantum circuit diagram conventions](xref:microsoft.quantum.concepts.circuits)

0 commit comments

Comments
 (0)