You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#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)
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.
4
4
ms.date: 12/08/2025
5
5
ms.author: quantumdocwriters
6
6
ms.service: azure-quantum
7
7
ms.subservice: qdk
8
8
ms.topic: how-to
9
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: Visualize Quantum Circuits with Q#
10
+
title: Visualize Quantum Circuits with the QDK
11
11
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.
13
13
---
14
14
15
-
# How to visualize quantum circuit diagrams with Q#
15
+
# How to visualize quantum circuit diagrams with the QDK
16
16
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.
18
18
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.
20
20
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).
22
22
23
23
## Prerequisites
24
24
25
+
To create circuit diagrams from Q# and OpenQASM files in VS Code, install the following:
26
+
25
27
- 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.
28
34
29
35
```bash
30
36
python -m pip install --upgrade "qdk[jupyter]"
31
37
```
32
38
33
39
## Visualize quantum circuits in VS Code
34
40
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.
36
42
37
-
### View circuit diagrams for a Q# program
43
+
To view a circuit diagram in VS Code, follow these steps:
38
44
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.
41
47
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.
43
49
44
-
The Q# circuit window opens and displays the circuit diagram foryour program. For example, the following circuit corresponds to an operation that puts a qubitin 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.":::
47
51
48
52
> [!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.
50
54
51
-
### View circuit diagrams for individual operations
55
+
### View circuit diagrams for individual Q# operations
52
56
53
57
To visualize the quantum circuit foran individual operationin a Q# file, choose the **Circuit**command from the code lens that precedes the operation.
54
58
@@ -62,15 +66,19 @@ When you use the VS Code debugger in a Q# program, you can visualize the quantum
62
66
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.
63
67
1. Set breakpoints and step through your code to see how the circuit updates as your program runs.
64
68
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.
66
74
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
68
76
69
-
### View circuit diagrams for an entry expression
77
+
To view a circuit diagramfora Q# programin Jupyter Notebook, follow these steps:
70
78
71
79
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.
74
82
75
83
```python
76
84
from qdk import qsharp
@@ -89,32 +97,38 @@ In Jupyter Notebook, you can visualize quantum circuits with the `qdk.qsharp` an
89
97
}
90
98
```
91
99
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:
93
101
94
102
```python
95
103
qsharp.circuit("BellState()")
96
104
```
97
105
106
+
The output looks like this:
107
+
98
108
```output
99
109
q_0 ── H ──── ● ──
100
110
q_1 ───────── X ──
101
111
```
102
112
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.
104
114
105
115
```python
106
116
from qdk.widgets import Circuit
107
117
108
118
Circuit(qsharp.circuit("BellState()"))
109
119
```
110
120
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
112
126
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.
114
128
115
-
You can generate circuit diagrams of operations that take qubits, or arrays of qubits, as input. The diagram shows a wire foreach 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:
116
130
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.
118
132
119
133
```qsharp
120
134
%%qsharp
@@ -125,26 +139,88 @@ You can generate circuit diagrams of operations that take qubits, or arrays of q
125
139
}
126
140
```
127
141
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:
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`.
135
169
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.
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
+
```
137
215
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:
139
217
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.":::
145
219
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.
147
222
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
149
224
150
-
The downside of traced circuits is that they only capture the measurement outcome and the consequent gate applications fora single simulation. In the above example, if the measurement outcome is `Zero`, then the `X` gate isn'tin 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)
0 commit comments