microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e938e457db07d9fd7602266d24d3ce6310f6b6c0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/notebooks/circuits.ipynb

303lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Synthesizing circuit diagrams from Q# code"
8 ]
9 },
10 {
11 "cell_type": "code",
12 "execution_count": null,
13 "metadata": {},
14 "outputs": [],
15 "source": [
16 "import qsharp"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "The `dump_circuit()` function displays a circuit that contains the gates that have been applied in the simulator up to this point."
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": null,
29 "metadata": {
30 "vscode": {
31 "languageId": "qsharp"
32 }
33 },
34 "outputs": [],
35 "source": [
36 "%%qsharp\n",
37 "\n",
38 "// Prepare a Bell State.\n",
39 "use register = Qubit[2];\n",
40 "H(register[0]);\n",
41 "CNOT(register[0], register[1]);"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "execution_count": null,
47 "metadata": {},
48 "outputs": [],
49 "source": [
50 "qsharp.dump_circuit()"
51 ]
52 },
53 {
54 "cell_type": "markdown",
55 "metadata": {},
56 "source": [
57 "If you have the Q# widgets installed, you can display the circuit as an SVG image.\n",
58 "\n",
59 "_Run `pip install qsharp-widgets` in the command line to install the Q# widgets._"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": null,
65 "metadata": {},
66 "outputs": [],
67 "source": [
68 "from qsharp_widgets import Circuit\n",
69 "\n",
70 "Circuit(qsharp.dump_circuit())"
71 ]
72 },
73 {
74 "cell_type": "markdown",
75 "metadata": {},
76 "source": [
77 "You can synthesize a circuit diagram for any program by calling `qsharp.circuit()` with an entry expression."
78 ]
79 },
80 {
81 "cell_type": "code",
82 "execution_count": null,
83 "metadata": {
84 "vscode": {
85 "languageId": "qsharp"
86 }
87 },
88 "outputs": [],
89 "source": [
90 "%%qsharp\n",
91 "\n",
92 "operation GHZSample(n: Int) : Result[] {\n",
93 " use qs = Qubit[n];\n",
94 "\n",
95 " H(qs[0]);\n",
96 " ApplyToEach(CNOT(qs[0], _), qs[1...]);\n",
97 "\n",
98 " let results = MeasureEachZ(qs);\n",
99 " ResetAll(qs);\n",
100 " return results;\n",
101 "}"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": null,
107 "metadata": {},
108 "outputs": [],
109 "source": [
110 "Circuit(qsharp.circuit(\"GHZSample(3)\"))"
111 ]
112 },
113 {
114 "cell_type": "markdown",
115 "metadata": {},
116 "source": [
117 "Circuit diagrams can also be generated for any operation that takes qubits or arrays of qubits.\n",
118 "\n",
119 "The diagram will show as many wires as there are input qubit, plus any additional qubits that are allocated within the operation.\n",
120 "\n",
121 "When the operation takes an array of qubits (`Qubit[]`), the circuit will show the array as a register of 2 qubits."
122 ]
123 },
124 {
125 "cell_type": "code",
126 "execution_count": null,
127 "metadata": {
128 "vscode": {
129 "languageId": "qsharp"
130 }
131 },
132 "outputs": [],
133 "source": [
134 "%%qsharp\n",
135 "\n",
136 "operation PrepareCatState(register : Qubit[]) : Unit {\n",
137 " H(register[0]);\n",
138 " ApplyToEach(CNOT(register[0], _), register[1...]);\n",
139 "}"
140 ]
141 },
142 {
143 "cell_type": "code",
144 "execution_count": null,
145 "metadata": {},
146 "outputs": [],
147 "source": [
148 "Circuit(qsharp.circuit(operation=\"PrepareCatState\"))"
149 ]
150 },
151 {
152 "cell_type": "markdown",
153 "metadata": {},
154 "source": [
155 "Circuit synthesis takes into account the currently chosen target, and will perform the same gate decompositions and other transformations that compiling for that target would produce.\n",
156 "\n",
157 "Here, we show what the circuit looks like for a random bit generator when the Unrestricted target profile is chosen."
158 ]
159 },
160 {
161 "cell_type": "code",
162 "execution_count": null,
163 "metadata": {
164 "vscode": {
165 "languageId": "qsharp"
166 }
167 },
168 "outputs": [],
169 "source": [
170 "%%qsharp\n",
171 "\n",
172 "operation TwoRandomBits() : Result[] {\n",
173 " let r1 = RandomBit();\n",
174 " let r2 = RandomBit();\n",
175 " return [r1, r2];\n",
176 "}\n",
177 "\n",
178 "operation RandomBit() : Result {\n",
179 " use q = Qubit();\n",
180 " H(q);\n",
181 " MResetZ(q)\n",
182 "}"
183 ]
184 },
185 {
186 "cell_type": "code",
187 "execution_count": null,
188 "metadata": {},
189 "outputs": [],
190 "source": [
191 "Circuit(qsharp.circuit(\"TwoRandomBits()\"))"
192 ]
193 },
194 {
195 "cell_type": "markdown",
196 "metadata": {},
197 "source": [
198 "Conditionals that compare `Result` values are not permitted during circuit synthesis. This is because they may introduce nondeterminism such that the circuit will look different depending on measurement outcome. Representing conditionals visually is not supported."
199 ]
200 },
201 {
202 "cell_type": "code",
203 "execution_count": null,
204 "metadata": {
205 "vscode": {
206 "languageId": "qsharp"
207 }
208 },
209 "outputs": [],
210 "source": [
211 "%%qsharp\n",
212 "\n",
213 "operation ResetIfOne() : Result {\n",
214 " use q = Qubit();\n",
215 " H(q);\n",
216 " let r = M(q);\n",
217 " if (r == One) {\n",
218 " Message(\"result was One, applying X gate\");\n",
219 " X(q);\n",
220 " } else {\n",
221 " Message(\"result was Zero\");\n",
222 " }\n",
223 " Reset(q);\n",
224 " return r\n",
225 "}"
226 ]
227 },
228 {
229 "cell_type": "code",
230 "execution_count": null,
231 "metadata": {},
232 "outputs": [],
233 "source": [
234 "# Program can be simulated. Differerent shots may produce different results.\n",
235 "print(\"Simulating program...\")\n",
236 "qsharp.run(\"ResetIfOne()\", 3)\n",
237 "\n",
238 "print()\n",
239 "\n",
240 "# The same program cannot be synthesized as a circuit because of the conditional X gate.\n",
241 "print(\"Synthesizing circuit for program (should raise error)...\")\n",
242 "try:\n",
243 " qsharp.circuit(\"ResetIfOne()\")\n",
244 "except qsharp.QSharpError as e:\n",
245 " print(e)"
246 ]
247 },
248 {
249 "cell_type": "markdown",
250 "metadata": {},
251 "source": [
252 "Even though we can't synthesize the above program into a circuit, we still have the option of running it in the simulator, and displaying the resulting circuit.\n",
253 "\n",
254 "Note that the resulting circuit diagram shows only one of the two branches that could have been taken."
255 ]
256 },
257 {
258 "cell_type": "code",
259 "execution_count": null,
260 "metadata": {
261 "vscode": {
262 "languageId": "qsharp"
263 }
264 },
265 "outputs": [],
266 "source": [
267 "%%qsharp\n",
268 "\n",
269 "ResetIfOne()"
270 ]
271 },
272 {
273 "cell_type": "code",
274 "execution_count": null,
275 "metadata": {},
276 "outputs": [],
277 "source": [
278 "Circuit(qsharp.dump_circuit())"
279 ]
280 }
281 ],
282 "metadata": {
283 "kernelspec": {
284 "display_name": "Python 3",
285 "language": "python",
286 "name": "python3"
287 },
288 "language_info": {
289 "codemirror_mode": {
290 "name": "ipython",
291 "version": 3
292 },
293 "file_extension": ".py",
294 "mimetype": "text/x-python",
295 "name": "python",
296 "nbconvert_exporter": "python",
297 "pygments_lexer": "ipython3",
298 "version": "3.11.11"
299 }
300 },
301 "nbformat": 4,
302 "nbformat_minor": 2
303}
304