microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9831093db0098b3a3e55cbadf3929222d7dd4805

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/notebooks/repeat_until_success.ipynb

365lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {
6 "nteract": {
7 "transient": {
8 "deleting": false
9 }
10 }
11 },
12 "source": [
13 "# Repeat-Until-Success Sample\n",
14 "This sample shows the use of branching on measurement and qubit reuse to implement a Repeat-Until-Success algorithm on Quantinuum hardware targets. It is drawn from a collaboration between Microsoft and Quantinuum research teams, specifically the authors Natalie C. Brown, John Peter Campora III, Ciarán Ryan-Anderson, Dominic Lucchetti, and Alex Chernoguzov from Quantinuum; Stefan Wernli, Adam Paetznick, Martin Roetteler, and Krysta Svore from Microsoft; as well as Bettina Heim and Cassandra Granade.\n",
15 "\n",
16 "The program uses RUS and two sub-circuits to perform the single-qubit unitary V<sub>3</sub> = (I + 2iZ)/(√5) as described in detail in [Repeat-Until-Success: Non-deterministic decomposition of single-qubit unitaries](https://arxiv.org/abs/1311.1074) by Adam Paetznik and Krysta Svore.\n",
17 "\n"
18 ]
19 },
20 {
21 "cell_type": "markdown",
22 "metadata": {
23 "nteract": {
24 "transient": {
25 "deleting": false
26 }
27 }
28 },
29 "source": [
30 "## 1. Connect to Azure Quantum and set up target\n",
31 "\n",
32 "First, we must configure the qsharp module to connect the azure workspace and specify a target. The Quantinuum H2 emulator target `quantinuum.sim.h2-1e` is chosen by default, which performs hardware-modeled noisy simulation. Running the experiments in this notebook against that target will consume approximately 25 HQC.\n",
33 "\n",
34 "We also configure the target profile as `Adaptive_RI` to indicate we use the QIR profile with support for mid-circuit measurement, measurement-based control flow, and classical integer computation as part of compilation.\n",
35 "\n",
36 "Replace the `subscription_id`, `resource_group`, `name`, and `location` connection parameters with the values for your configured Azure Quantum Workspace, or try copying the Python code for connecting to your workspace from the VS Code Azure Quantum Extension."
37 ]
38 },
39 {
40 "cell_type": "code",
41 "execution_count": null,
42 "metadata": {
43 "jupyter": {
44 "outputs_hidden": false,
45 "source_hidden": false
46 },
47 "nteract": {
48 "transient": {
49 "deleting": false
50 }
51 }
52 },
53 "outputs": [],
54 "source": [
55 "import qsharp\n",
56 "\n",
57 "qsharp.init(target_profile=qsharp.TargetProfile.Adaptive_RI)"
58 ]
59 },
60 {
61 "cell_type": "code",
62 "execution_count": null,
63 "metadata": {},
64 "outputs": [],
65 "source": [
66 "import azure.quantum\n",
67 "\n",
68 "workspace = azure.quantum.Workspace(\n",
69 " subscription_id = \"SUBSCRIPTION ID\",\n",
70 " resource_group = \"RESOURCE GROUP\",\n",
71 " name = \"WORKSPACE NAME\",\n",
72 " location = \"LOCATION\",\n",
73 ")"
74 ]
75 },
76 {
77 "cell_type": "code",
78 "execution_count": null,
79 "metadata": {},
80 "outputs": [],
81 "source": [
82 "target = workspace.get_targets(\"quantinuum.sim.h2-1e\")"
83 ]
84 },
85 {
86 "cell_type": "markdown",
87 "metadata": {
88 "nteract": {
89 "transient": {
90 "deleting": false
91 }
92 }
93 },
94 "source": [
95 "## 2. Compile the RUS Program"
96 ]
97 },
98 {
99 "cell_type": "code",
100 "execution_count": null,
101 "metadata": {
102 "jupyter": {
103 "outputs_hidden": false,
104 "source_hidden": false
105 },
106 "microsoft": {
107 "language": "qsharp"
108 },
109 "nteract": {
110 "transient": {
111 "deleting": false
112 }
113 },
114 "vscode": {
115 "languageId": "qsharp"
116 }
117 },
118 "outputs": [],
119 "source": [
120 "%%qsharp\n",
121 "import Std.Math.*;\n",
122 "\n",
123 "/// # Summary\n",
124 "/// Example of a Repeat-until-success algorithm implementing a circuit\n",
125 "/// that achieves exp(i*ArcTan(2)*Z), also known as the \"V gate\"\n",
126 "/// by Paetznick & Svore.\n",
127 "///\n",
128 "/// # References\n",
129 "/// - [ *Adam Paetznick, Krysta M. Svore*,\n",
130 "/// Quantum Information & Computation 14(15 & 16): 1277-1301 (2014)\n",
131 "/// ](https://arxiv.org/abs/1311.1074)\n",
132 "/// For circuit diagram, see file RUS.png.\n",
133 "///\n",
134 "/// # Input\n",
135 "/// ## inputValue\n",
136 "/// Boolean value for input qubit (true maps to One, false maps to Zero)\n",
137 "/// ## inputBasis\n",
138 "/// Pauli basis in which to prepare input qubit\n",
139 "/// ## limit\n",
140 "/// Integer limit to number of repeats of circuit\n",
141 "///\n",
142 "/// # Remarks\n",
143 "/// The program executes a circuit on a \"target\" qubit using an \"auxiliary\"\n",
144 "/// and \"resource\" qubit. The circuit consists of two parts (red and blue\n",
145 "/// in image).\n",
146 "/// The goal is to measure Zero for both the auxiliary and resource qubit.\n",
147 "/// If this succeeds, the program will have effectively applied an\n",
148 "/// Rz(arctan(2)) gate (also known as V_3 gate) on the target qubit.\n",
149 "/// If this fails, the program reruns the circuit up to <limit> times.\n",
150 "operation AllocateQubitsAndApplyRzArcTan2 (\n",
151 " inputValue : Bool,\n",
152 " inputBasis : Pauli,\n",
153 " measurementBasis : Pauli,\n",
154 " limit : Int\n",
155 ") : Result[] {\n",
156 " use (auxiliary, resource, target) = (Qubit(), Qubit(), Qubit());\n",
157 "\n",
158 " // Prepare target qubit in a zero- or one-state, based on input value\n",
159 " if (inputValue) {\n",
160 " X(target);\n",
161 " }\n",
162 " PreparePauliEigenstate(inputBasis, target);\n",
163 "\n",
164 " within {\n",
165 " H(auxiliary);\n",
166 " H(resource);\n",
167 " }\n",
168 " apply {\n",
169 " ApplyRzArcTan2(limit, auxiliary, resource, target);\n",
170 "\n",
171 " let rotationAngle = 2.0 * ArcTan(2.0);\n",
172 " Rz(rotationAngle, target); // Rotate back to initial state\n",
173 " }\n",
174 "\n",
175 " return [M(auxiliary), M(resource), Measure([measurementBasis], [target])];\n",
176 "}\n",
177 "\n",
178 "/// # Summary\n",
179 "/// Apply Rz(arctan(2)) on qubits using repeat until success algorithm.\n",
180 "/// Updated to use for-loop sytle.\n",
181 "operation ApplyRzArcTan2 (\n",
182 " limit : Int,\n",
183 " auxiliary : Qubit,\n",
184 " resource : Qubit,\n",
185 " target : Qubit\n",
186 ") : Unit {\n",
187 " mutable (result1, result2) = (false, false);\n",
188 "\n",
189 " // Run Part 1 of the program.\n",
190 " for _ in 1..limit {\n",
191 " if not result1 or not result2 {\n",
192 " set result1 = ApplyAndMeasurePart1(auxiliary, resource) == Zero;\n",
193 "\n",
194 " // We’ll only run Part 2 if Part 1 returns Zero.\n",
195 " // Otherwise, we’ll skip and rerun Part 1 again.\n",
196 " if result1 {\n",
197 " set result2 = ApplyAndMeasurePart2(resource, target) == Zero;\n",
198 "\n",
199 " if not result2 {\n",
200 " Z(resource); // Reset resource\n",
201 " Adjoint Z(target); // Correct effective Z rotation on target\n",
202 " }\n",
203 " } else {\n",
204 " Z(auxiliary);\n",
205 " Reset(resource);\n",
206 " H(resource);\n",
207 " }\n",
208 " }\n",
209 " }\n",
210 "}\n",
211 "\n",
212 "/// # Summary\n",
213 "/// Apply part 1 of RUS circuit and measure\n",
214 "/// auxiliary qubit in Pauli X basis\n",
215 "operation ApplyAndMeasurePart1(auxiliary : Qubit, resource : Qubit) : Result {\n",
216 " within {\n",
217 " T(auxiliary);\n",
218 " }\n",
219 " apply {\n",
220 " CNOT(resource, auxiliary);\n",
221 " }\n",
222 " return Measure([PauliX], [auxiliary]);\n",
223 "}\n",
224 "\n",
225 "/// # Summary\n",
226 "/// Apply part 2 of RUS circuit and measure\n",
227 "/// resource qubit in Pauli X basis\n",
228 "operation ApplyAndMeasurePart2(resource : Qubit, target : Qubit) : Result {\n",
229 " T(target);\n",
230 " Z(target);\n",
231 " CNOT(target, resource);\n",
232 " T(resource);\n",
233 " return Measure([PauliX], [resource]);\n",
234 "}\n",
235 "\n",
236 "operation PreparePauliEigenstate(basis : Pauli, qubit : Qubit) : Unit {\n",
237 " if (basis == PauliX) {\n",
238 " H(qubit);\n",
239 " } elif (basis == PauliY) {\n",
240 " H(qubit);\n",
241 " S(qubit);\n",
242 " }\n",
243 "}"
244 ]
245 },
246 {
247 "cell_type": "markdown",
248 "metadata": {
249 "nteract": {
250 "transient": {
251 "deleting": false
252 }
253 }
254 },
255 "source": [
256 "## 3. Run the Experiment\n",
257 "We can now submit and wait on each of three experiments using different repeat limits, displaying the resulting histograms and saving them into variables for later analysis. We should see the percentage of resulting successes increase as more repeats are allowed to execute."
258 ]
259 },
260 {
261 "cell_type": "code",
262 "execution_count": null,
263 "metadata": {},
264 "outputs": [],
265 "source": [
266 "job0 = target.submit(qsharp.compile(\"AllocateQubitsAndApplyRzArcTan2(false, PauliX, PauliX, 0)\"), shots=100)\n",
267 "job0.wait_until_completed()\n",
268 "results0 = job0.get_results()"
269 ]
270 },
271 {
272 "cell_type": "code",
273 "execution_count": null,
274 "metadata": {},
275 "outputs": [],
276 "source": [
277 "job1 = target.submit(qsharp.compile(\"AllocateQubitsAndApplyRzArcTan2(false, PauliX, PauliX, 1)\"), shots=100)\n",
278 "job1.wait_until_completed()\n",
279 "results1 = job1.get_results()"
280 ]
281 },
282 {
283 "cell_type": "code",
284 "execution_count": null,
285 "metadata": {},
286 "outputs": [],
287 "source": [
288 "job2 = target.submit(qsharp.compile(\"AllocateQubitsAndApplyRzArcTan2(false, PauliX, PauliX, 2)\"), shots=100)\n",
289 "job2.wait_until_completed()\n",
290 "results2 = job2.get_results()"
291 ]
292 },
293 {
294 "cell_type": "markdown",
295 "metadata": {
296 "nteract": {
297 "transient": {
298 "deleting": false
299 }
300 }
301 },
302 "source": [
303 "## 4. Plot the Results\n",
304 "Finally, we plot the results. When run against the emulator or hardware, each successive experiment should show increasing probability of success, indicating that increasing the allowed runs of RUS further refines the effectiveness of the program."
305 ]
306 },
307 {
308 "cell_type": "code",
309 "execution_count": null,
310 "metadata": {
311 "jupyter": {
312 "outputs_hidden": false,
313 "source_hidden": false
314 },
315 "nteract": {
316 "transient": {
317 "deleting": false
318 }
319 }
320 },
321 "outputs": [],
322 "source": [
323 "from matplotlib import pyplot\n",
324 "\n",
325 "success = \"[0, 0, 0]\"\n",
326 "xticks = [0, 1, 2]\n",
327 "pyplot.title(\"Probability Success\")\n",
328 "pyplot.plot(xticks, [results0[success], results1[success], results2[success]])\n",
329 "pyplot.xticks([0, 1, 2])\n",
330 "pyplot.xlabel(\"Repeat Limit\")\n",
331 "pyplot.ylabel(\"Probability\")\n",
332 "\n",
333 "\n",
334 "pyplot.show()"
335 ]
336 }
337 ],
338 "metadata": {
339 "kernel_info": {
340 "name": "python3"
341 },
342 "kernelspec": {
343 "display_name": "Python 3 (ipykernel)",
344 "language": "python",
345 "name": "python3"
346 },
347 "language_info": {
348 "codemirror_mode": {
349 "name": "ipython",
350 "version": 3
351 },
352 "file_extension": ".py",
353 "mimetype": "text/x-python",
354 "name": "python",
355 "nbconvert_exporter": "python",
356 "pygments_lexer": "ipython3",
357 "version": "3.11.6"
358 },
359 "nteract": {
360 "version": "nteract-front-end@1.0.0"
361 }
362 },
363 "nbformat": 4,
364 "nbformat_minor": 0
365}
366