microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/qdk_package

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/notebooks/qdk.ipynb

243lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "746adee2",
6 "metadata": {},
7 "source": [
8 "# Basic OpenQASM code"
9 ]
10 },
11 {
12 "cell_type": "code",
13 "execution_count": null,
14 "id": "b5f1355d",
15 "metadata": {},
16 "outputs": [],
17 "source": [
18 "from qdk.openqasm import run, DepolarizingNoise, compile\n",
19 "from qdk.widgets import Histogram\n",
20 "\n",
21 "source = \"\"\"\n",
22 " include \"stdgates.inc\";\n",
23 " bit[2] c;\n",
24 " qubit[2] q;\n",
25 " h q[0];\n",
26 " cx q[0], q[1];\n",
27 " c = measure q;\n",
28 "\"\"\"\n",
29 "\n",
30 "Histogram(run(source, noise=DepolarizingNoise(0.01), as_bitstring=True))"
31 ]
32 },
33 {
34 "cell_type": "code",
35 "execution_count": null,
36 "id": "c20b9ab1",
37 "metadata": {},
38 "outputs": [],
39 "source": [
40 "print(compile(source, as_bitstring=True))"
41 ]
42 },
43 {
44 "cell_type": "markdown",
45 "id": "8bee6b70",
46 "metadata": {},
47 "source": [
48 "## QASM and code"
49 ]
50 },
51 {
52 "cell_type": "code",
53 "execution_count": null,
54 "id": "253f3b44",
55 "metadata": {},
56 "outputs": [],
57 "source": [
58 "from qdk.openqasm import import_openqasm, code\n",
59 "\n",
60 "source = \"\"\"\n",
61 " include \"stdgates.inc\";\n",
62 " bit[2] c;\n",
63 " qubit[2] q;\n",
64 " h q[0];\n",
65 " cx q[0], q[1];\n",
66 " c = measure q;\n",
67 "\"\"\"\n",
68 "\n",
69 "import_openqasm(source, name=\"bell\")\n",
70 "\n",
71 "code.bell()\n"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": null,
77 "id": "ae62fc8c",
78 "metadata": {},
79 "outputs": [],
80 "source": [
81 "from qdk.openqasm import circuit\n",
82 "from qdk.widgets import Circuit\n",
83 "\n",
84 "display(Circuit(circuit(code.bell)))"
85 ]
86 },
87 {
88 "cell_type": "markdown",
89 "id": "ad272e34",
90 "metadata": {},
91 "source": [
92 "## Qiskit integration"
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": null,
98 "id": "1ce82c8e",
99 "metadata": {},
100 "outputs": [],
101 "source": [
102 "from qiskit import QuantumCircuit\n",
103 "import numpy as np\n",
104 "\n",
105 "circuit = QuantumCircuit(2, 2)\n",
106 "circuit.name = \"state_prep\"\n",
107 "\n",
108 "# State vector to initialize: |ψ⟩ = (|0⟩ - |1⟩) / √2\n",
109 "circuit.initialize([1 / np.sqrt(2), -1 / np.sqrt(2)], 0)\n",
110 "circuit.h(0)\n",
111 "circuit.measure(0, 0)\n",
112 "\n",
113 "circuit.prepare_state([1 / np.sqrt(2), -1 / np.sqrt(2)], 1)\n",
114 "circuit.h(1)\n",
115 "circuit.measure(1, 1)\n",
116 "\n",
117 "circuit.draw(output=\"text\")"
118 ]
119 },
120 {
121 "cell_type": "code",
122 "execution_count": null,
123 "id": "b54a8930",
124 "metadata": {},
125 "outputs": [],
126 "source": [
127 "from qdk.qiskit import QSharpBackend, TargetProfile\n",
128 "\n",
129 "backend = QSharpBackend(target_profile=TargetProfile.Adaptive_RI)\n",
130 "job = backend.run(circuit)\n",
131 "counts = job.result().get_counts()\n",
132 "print(counts)"
133 ]
134 },
135 {
136 "cell_type": "code",
137 "execution_count": null,
138 "id": "a728e279",
139 "metadata": {},
140 "outputs": [],
141 "source": [
142 "print(backend.qir(circuit))"
143 ]
144 },
145 {
146 "cell_type": "markdown",
147 "id": "b843134d",
148 "metadata": {},
149 "source": [
150 "## Resource estimation\n"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": null,
156 "id": "57c6fd03",
157 "metadata": {},
158 "outputs": [],
159 "source": [
160 "from qdk.qsharp import init, eval, estimate\n",
161 "from qdk.widgets import EstimatesOverview\n",
162 "\n",
163 "init()\n",
164 "with open(\"../estimation/EkeraHastadFactoring.qs\", \"r\") as f:\n",
165 " eval(f.read())\n",
166 "\n",
167 "rsa_number = 1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139\n",
168 "\n",
169 "estimates = estimate(f\"EkeraHastad({rsa_number.bit_length()}, {rsa_number}L, 7L)\", [\n",
170 " {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_ns_e3\"}},\n",
171 " {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_ns_e4\"}},\n",
172 " {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_us_e3\"}},\n",
173 " {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_us_e4\"}},\n",
174 " {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_maj_ns_e4\"}, \"qecScheme\": {\"name\": \"floquet_code\"}},\n",
175 " {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_maj_ns_e6\"}, \"qecScheme\": {\"name\": \"floquet_code\"}}\n",
176 "])\n",
177 "\n",
178 "EstimatesOverview(\n",
179 " estimates,\n",
180 " colors=[\"#1f77b4\", \"#ff7f0e\", \"blue\", \"red\", \"green\", \"yellow\"],\n",
181 " runNames=[\"Gate ns e3, surface\", \"Gate ns e4, surface\", \"Gate us e3, surface\", \"Gate us e4, surface\", \"Majorana ns e4, floquet\", \"Majorana ns e6, floquet\"]\n",
182 ")"
183 ]
184 },
185 {
186 "cell_type": "markdown",
187 "id": "241dab0c",
188 "metadata": {},
189 "source": [
190 "## Azure submission"
191 ]
192 },
193 {
194 "cell_type": "code",
195 "execution_count": null,
196 "id": "822a19b1",
197 "metadata": {},
198 "outputs": [],
199 "source": [
200 "from qdk.qsharp import init, TargetProfile, eval, compile\n",
201 "from qdk.azure import Workspace\n",
202 "\n",
203 "init(target_profile=TargetProfile.Base)\n",
204 "\n",
205 "eval(\"operation Main() : Result { use q = Qubit(); H(q); return M(q);}\")\n",
206 "operation = compile(\"Main()\")\n",
207 "\n",
208 "workspace = Workspace(\n",
209 " subscription_id = \"916dfd6d-030c-4bd9-b579-7bb6d1926e97\",\n",
210 " resource_group = \"billti-team\",\n",
211 " name = \"billti-quantum01\",\n",
212 " location = \"westus2\",\n",
213 ")\n",
214 "\n",
215 "target = workspace.get_targets(\"quantinuum.sim.h2-2e\")\n",
216 "print(target)\n",
217 "# job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n",
218 "# print(job.get_results())\n"
219 ]
220 }
221 ],
222 "metadata": {
223 "kernelspec": {
224 "display_name": ".venv",
225 "language": "python",
226 "name": "python3"
227 },
228 "language_info": {
229 "codemirror_mode": {
230 "name": "ipython",
231 "version": 3
232 },
233 "file_extension": ".py",
234 "mimetype": "text/x-python",
235 "name": "python",
236 "nbconvert_exporter": "python",
237 "pygments_lexer": "ipython3",
238 "version": "3.13.7"
239 }
240 },
241 "nbformat": 4,
242 "nbformat_minor": 5
243}