microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
20c2fb1c118e553b69d042c5289d61dda9c55e76

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/notebooks/circuits.ipynb

258lines · 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 "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 qsharp_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 "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."
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 "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",
219 "\n",
220 "This can be done by passing in `generation_method=GenerationMethod.Simulate` to the `circuit()` function.\n",
221 "\n",
222 "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 qsharp import CircuitGenerationMethod\n",
232 "\n",
233 "Circuit(qsharp.circuit(\"ResetIfOne()\", generation_method=CircuitGenerationMethod.Simulate))"
234 ]
235 }
236 ],
237 "metadata": {
238 "kernelspec": {
239 "display_name": ".venv",
240 "language": "python",
241 "name": "python3"
242 },
243 "language_info": {
244 "codemirror_mode": {
245 "name": "ipython",
246 "version": 3
247 },
248 "file_extension": ".py",
249 "mimetype": "text/x-python",
250 "name": "python",
251 "nbconvert_exporter": "python",
252 "pygments_lexer": "ipython3",
253 "version": "3.14.0"
254 }
255 },
256 "nbformat": 4,
257 "nbformat_minor": 2
258}
259