microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
amcasey/WrapInArray

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/notebooks/circuits.ipynb

309lines · 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 "from qdk import qsharp"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "You can synthesize a circuit diagram for any program by calling `qsharp.circuit()` with an entry expression."
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 "operation GHZSample(n: Int) : Result[] {\n",
39 " use qs = Qubit[n];\n",
40 "\n",
41 " H(qs[0]);\n",
42 " ApplyToEach(CNOT(qs[0], _), qs[1...]);\n",
43 "\n",
44 " let results = MeasureEachZ(qs);\n",
45 " ResetAll(qs);\n",
46 " return results;\n",
47 "}"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": null,
53 "metadata": {},
54 "outputs": [],
55 "source": [
56 "qsharp.circuit(\"GHZSample(3)\")"
57 ]
58 },
59 {
60 "cell_type": "markdown",
61 "metadata": {},
62 "source": [
63 "If you have the Q# widgets installed, you can display the circuit as an SVG image.\n",
64 "\n",
65 "_Run `pip install qsharp-widgets` in the command line to install the Q# widgets._"
66 ]
67 },
68 {
69 "cell_type": "code",
70 "execution_count": null,
71 "metadata": {},
72 "outputs": [],
73 "source": [
74 "from qdk.widgets import Circuit\n",
75 "\n",
76 "Circuit(qsharp.circuit(\"GHZSample(3)\"))"
77 ]
78 },
79 {
80 "cell_type": "markdown",
81 "metadata": {},
82 "source": [
83 "Circuit diagrams can also be generated for any operation that takes qubits or arrays of qubits.\n",
84 "\n",
85 "The diagram will show as many wires as there are input qubit, plus any additional qubits that are allocated within the operation.\n",
86 "\n",
87 "When the operation takes an array of qubits (`Qubit[]`), the circuit will show the array as a register of 2 qubits."
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": null,
93 "metadata": {
94 "vscode": {
95 "languageId": "qsharp"
96 }
97 },
98 "outputs": [],
99 "source": [
100 "%%qsharp\n",
101 "\n",
102 "operation PrepareCatState(register : Qubit[]) : Unit {\n",
103 " H(register[0]);\n",
104 " ApplyToEach(CNOT(register[0], _), register[1...]);\n",
105 "}"
106 ]
107 },
108 {
109 "cell_type": "code",
110 "execution_count": null,
111 "metadata": {},
112 "outputs": [],
113 "source": [
114 "Circuit(qsharp.circuit(operation=\"PrepareCatState\"))"
115 ]
116 },
117 {
118 "cell_type": "markdown",
119 "metadata": {},
120 "source": [
121 "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",
122 "\n",
123 "Here, we show what the circuit looks like for a random bit generator when the Unrestricted target profile is chosen."
124 ]
125 },
126 {
127 "cell_type": "code",
128 "execution_count": null,
129 "metadata": {
130 "vscode": {
131 "languageId": "qsharp"
132 }
133 },
134 "outputs": [],
135 "source": [
136 "%%qsharp\n",
137 "\n",
138 "operation TwoRandomBits() : Result[] {\n",
139 " let r1 = RandomBit();\n",
140 " let r2 = RandomBit();\n",
141 " return [r1, r2];\n",
142 "}\n",
143 "\n",
144 "operation RandomBit() : Result {\n",
145 " use q = Qubit();\n",
146 " H(q);\n",
147 " MResetZ(q)\n",
148 "}"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "Circuit(qsharp.circuit(\"TwoRandomBits()\"))"
158 ]
159 },
160 {
161 "cell_type": "markdown",
162 "metadata": {},
163 "source": [
164 "By default, conditionals that compare `Result` values are not permitted during circuit synthesis. This is because the default method traces a single execution path through the program, and a conditional may produce a different circuit depending on measurement outcome."
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": null,
170 "metadata": {
171 "vscode": {
172 "languageId": "qsharp"
173 }
174 },
175 "outputs": [],
176 "source": [
177 "%%qsharp\n",
178 "\n",
179 "operation ResetIfOne() : Result {\n",
180 " use q = Qubit();\n",
181 " H(q);\n",
182 " let r = M(q);\n",
183 " if (r == One) {\n",
184 " Message(\"result was One, applying X gate\");\n",
185 " X(q);\n",
186 " } else {\n",
187 " Message(\"result was Zero\");\n",
188 " }\n",
189 " Reset(q);\n",
190 " return r\n",
191 "}"
192 ]
193 },
194 {
195 "cell_type": "code",
196 "execution_count": null,
197 "metadata": {},
198 "outputs": [],
199 "source": [
200 "# Program can be simulated. Differerent shots may produce different results.\n",
201 "print(\"Simulating program...\")\n",
202 "qsharp.run(\"ResetIfOne()\", 3)\n",
203 "\n",
204 "print()\n",
205 "\n",
206 "# The same program cannot be synthesized as a circuit because of the conditional X gate.\n",
207 "print(\"Synthesizing circuit for program (should raise error)...\")\n",
208 "try:\n",
209 " qsharp.circuit(\"ResetIfOne()\")\n",
210 "except qsharp.QSharpError as e:\n",
211 " print(e)"
212 ]
213 },
214 {
215 "cell_type": "markdown",
216 "metadata": {},
217 "source": [
218 "There are two options for synthesizing circuits for programs that contain measurement-based conditionals.\n",
219 "\n",
220 "### Option 1: Simulate\n",
221 "\n",
222 "By passing `generation_method=CircuitGenerationMethod.Simulate`, the program is run in the simulator and the resulting gates are recorded. Note that the resulting circuit diagram shows only one of the two branches that could have been taken."
223 ]
224 },
225 {
226 "cell_type": "code",
227 "execution_count": null,
228 "metadata": {},
229 "outputs": [],
230 "source": [
231 "from qdk.qsharp import CircuitGenerationMethod\n",
232 "\n",
233 "Circuit(qsharp.circuit(\"ResetIfOne()\", generation_method=CircuitGenerationMethod.Simulate))"
234 ]
235 },
236 {
237 "cell_type": "markdown",
238 "metadata": {},
239 "source": [
240 "### Option 2: Static\n",
241 "\n",
242 "By passing `generation_method=CircuitGenerationMethod.Static`, the circuit is generated via static analysis of the program. This produces a circuit that shows _all_ conditional branches, rendered as classically controlled groups. This requires a target profile that supports mid-circuit measurement, such as `Adaptive_RIF`."
243 ]
244 },
245 {
246 "cell_type": "code",
247 "execution_count": null,
248 "metadata": {},
249 "outputs": [],
250 "source": [
251 "qsharp.init(target_profile=qsharp.TargetProfile.Adaptive_RIF)"
252 ]
253 },
254 {
255 "cell_type": "code",
256 "execution_count": null,
257 "metadata": {
258 "vscode": {
259 "languageId": "qsharp"
260 }
261 },
262 "outputs": [],
263 "source": [
264 "%%qsharp\n",
265 "\n",
266 "operation ResetIfOne2() : Result {\n",
267 " use q = Qubit();\n",
268 " H(q);\n",
269 " let r = M(q);\n",
270 " if (r == One) {\n",
271 " X(q);\n",
272 " }\n",
273 " Reset(q);\n",
274 " return r\n",
275 "}"
276 ]
277 },
278 {
279 "cell_type": "code",
280 "execution_count": null,
281 "metadata": {},
282 "outputs": [],
283 "source": [
284 "Circuit(qsharp.circuit(\"ResetIfOne2()\", generation_method=CircuitGenerationMethod.Static))"
285 ]
286 }
287 ],
288 "metadata": {
289 "kernelspec": {
290 "display_name": ".venv",
291 "language": "python",
292 "name": "python3"
293 },
294 "language_info": {
295 "codemirror_mode": {
296 "name": "ipython",
297 "version": 3
298 },
299 "file_extension": ".py",
300 "mimetype": "text/x-python",
301 "name": "python",
302 "nbconvert_exporter": "python",
303 "pygments_lexer": "ipython3",
304 "version": "3.14.0"
305 }
306 },
307 "nbformat": 4,
308 "nbformat_minor": 2
309}
310