microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
be82821236a00686b004bc7fe619ad16904f7997

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/notebooks/memory_qubits.ipynb

194lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "f0c52178-5d77-43b2-8a0c-35691f96fdd9",
6 "metadata": {},
7 "source": [
8 "## Memory Qubits in Q#\n",
9 "\n",
10 "This notebook illustrates an advanced feature in Q#: memory qubits. Memory qubits can store a quantum state, but gates or operations cannot be applied to them. We call regular qubits that do support these operations \"compute qubits.\" This distinction makes it possible to design algorithms that use fewer compute qubits, because memory qubits can use more efficient error-correction codes (see, for example, [yoked surface codes](https://arxiv.org/pdf/2312.04522)).\n",
11 "\n",
12 "By default, all qubits are allocated as compute qubits. Users can turn a compute qubit into a memory qubit by calling `Std.Memory.Store`; the qubit is then \"stored in memory,\" and no operations can be applied to it. When an operation needs to be performed on a qubit, it must be \"loaded\" from memory by calling `Std.Memory.Load`.\n",
13 "\n",
14 "To make `Std.Memory.Load` and `Std.Memory.Store` affect resource estimation, call `Std.ResourceEstimation.EnableManualMemoryComputeArchitecture()`. This enables the \"manual\" memory/compute strategy, where users explicitly move qubits between memory and compute.\n",
15 "\n",
16 "### Example: GHZ state preparation and measurement\n",
17 "\n",
18 "In the example below, we prepare the [GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state) on 10 memory qubits. We move qubits to memory as soon as each has been used as the control qubit of a CNOT gate. As a result, this circuit needs only 2 compute qubits. It also uses 10 memory qubits (to store the state), so in total it uses 12 qubits.\n",
19 "\n",
20 "Then we demonstrate how to measure a memory register by moving qubits from memory to compute one-by-one before measuring them."
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": 1,
26 "id": "f596f9b9-0b8b-44cf-8a92-34e6b55ddbe1",
27 "metadata": {},
28 "outputs": [],
29 "source": [
30 "from qdk import qsharp"
31 ]
32 },
33 {
34 "cell_type": "code",
35 "execution_count": 2,
36 "id": "56f322d4-b77d-452a-a392-9ef7fbc6c04a",
37 "metadata": {
38 "vscode": {
39 "languageId": "qsharp"
40 }
41 },
42 "outputs": [],
43 "source": [
44 "%%qsharp\n",
45 "\n",
46 "// Prepares GHZ state (|0..0> + |1..1>)/sqrt(2) in a memory register of length n.\n",
47 "// Uses at most 2 compute qubits.\n",
48 "operation PrepareGhzStateInMemory(qs: Qubit[]) : Unit {\n",
49 " let n = Length(qs);\n",
50 " H(qs[0]);\n",
51 " for i in 1..n-1 {\n",
52 " CNOT(qs[i-1], qs[i]);\n",
53 " Std.Memory.Store(qs[i-1]);\n",
54 " }\n",
55 " Std.Memory.Store(qs[n-1]);\n",
56 "}\n",
57 "\n",
58 "// Measures the value in a memory-qubit register, interpreted as a little-endian integer.\n",
59 "// Uses a single compute qubit as a buffer.\n",
60 "operation MeasureMemory(qs : Qubit[]) : Int {\n",
61 " mutable results = [];\n",
62 " for q in qs {\n",
63 " Std.Memory.Load(q);\n",
64 " set results += [MResetZ(q)];\n",
65 " }\n",
66 " return Std.Convert.ResultArrayAsInt(results);\n",
67 "}\n",
68 "\n",
69 "operation Main() : Int {\n",
70 " Std.ResourceEstimation.EnableManualMemoryComputeArchitecture();\n",
71 " use qs = Qubit[10];\n",
72 " PrepareGhzStateInMemory(qs);\n",
73 " return MeasureMemory(qs);\n",
74 "}\n",
75 "\n",
76 "operation MainNoMemoryCompute() : Int {\n",
77 " use qs = Qubit[10];\n",
78 " PrepareGhzStateInMemory(qs);\n",
79 " return MeasureMemory(qs);\n",
80 "}"
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": 3,
86 "id": "764ac24b-c5d3-4fee-93ee-e64cdb7a3be6",
87 "metadata": {},
88 "outputs": [
89 {
90 "data": {
91 "text/plain": [
92 "Counter({1023: 12, 0: 8})"
93 ]
94 },
95 "execution_count": 3,
96 "metadata": {},
97 "output_type": "execute_result"
98 }
99 ],
100 "source": [
101 "from collections import Counter\n",
102 "Counter(qsharp.run(\"Main()\", shots=20))"
103 ]
104 },
105 {
106 "cell_type": "code",
107 "execution_count": 4,
108 "id": "79196eac-7f1e-4f21-9b4e-fecb95c3f5ea",
109 "metadata": {},
110 "outputs": [
111 {
112 "data": {
113 "text/plain": [
114 "{'numQubits': 12,\n",
115 " 'tCount': 0,\n",
116 " 'rotationCount': 0,\n",
117 " 'rotationDepth': 0,\n",
118 " 'cczCount': 0,\n",
119 " 'ccixCount': 0,\n",
120 " 'measurementCount': 10,\n",
121 " 'numComputeQubits': 2,\n",
122 " 'readFromMemoryCount': 10,\n",
123 " 'writeToMemoryCount': 10}"
124 ]
125 },
126 "execution_count": 4,
127 "metadata": {},
128 "output_type": "execute_result"
129 }
130 ],
131 "source": [
132 "qsharp.logical_counts(\"Main()\")"
133 ]
134 },
135 {
136 "cell_type": "markdown",
137 "id": "74e433db-45ce-4ff4-836b-35d4a24a912e",
138 "metadata": {},
139 "source": [
140 "`logical_counts` reports 2 compute qubits and 12 total qubits, which implies 10 memory qubits, as expected.\n",
141 "\n",
142 "For comparison, if we do not call `EnableManualMemoryComputeArchitecture`, we get 10 total qubits without a breakdown into memory and compute, which means 10 compute qubits:"
143 ]
144 },
145 {
146 "cell_type": "code",
147 "execution_count": 5,
148 "id": "cc72bc32-b2bf-4086-aa46-36488cdbe102",
149 "metadata": {},
150 "outputs": [
151 {
152 "data": {
153 "text/plain": [
154 "{'numQubits': 10,\n",
155 " 'tCount': 0,\n",
156 " 'rotationCount': 0,\n",
157 " 'rotationDepth': 0,\n",
158 " 'cczCount': 0,\n",
159 " 'ccixCount': 0,\n",
160 " 'measurementCount': 10}"
161 ]
162 },
163 "execution_count": 5,
164 "metadata": {},
165 "output_type": "execute_result"
166 }
167 ],
168 "source": [
169 "qsharp.logical_counts(\"MainNoMemoryCompute()\")"
170 ]
171 }
172 ],
173 "metadata": {
174 "kernelspec": {
175 "display_name": "Python 3 (ipykernel)",
176 "language": "python",
177 "name": "python3"
178 },
179 "language_info": {
180 "codemirror_mode": {
181 "name": "ipython",
182 "version": 3
183 },
184 "file_extension": ".py",
185 "mimetype": "text/x-python",
186 "name": "python",
187 "nbconvert_exporter": "python",
188 "pygments_lexer": "ipython3",
189 "version": "3.12.3"
190 }
191 },
192 "nbformat": 4,
193 "nbformat_minor": 5
194}
195