You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apxc/apxc-exercise-solutions.ipynb
+80-80
Original file line number
Diff line number
Diff line change
@@ -2,164 +2,162 @@
2
2
"cells": [
3
3
{
4
4
"cell_type": "markdown",
5
-
"metadata": {},
6
5
"source": [
7
-
"# [Learn Quantum Computing with Python and Q#](https://www.manning.com/books/learn-quantum-computing-with-python-and-q-sharp?a_aid=learn-qc-granade&a_bid=ee23f338)<br>Appendix B Exercise Solutions\n",
6
+
"# [Learn Quantum Computing with Python and Q#](https://www.manning.com/books/learn-quantum-computing-with-python-and-q-sharp?a_aid=learn-qc-granade&a_bid=ee23f338)<br>Appendix C Exercise Solutions\n",
8
7
"----\n",
9
8
"> Copyright (c) Sarah Kaiser and Chris Granade.\n",
10
9
"> Code sample from the book \"Learn Quantum Computing with Python and Q#\" by\n",
11
10
"> Sarah Kaiser and Chris Granade, published by Manning Publications Co.\n",
12
11
"> Book ISBN 9781617296130.\n",
13
12
"> Code licensed under the MIT License."
14
-
]
13
+
],
14
+
"metadata": {}
15
15
},
16
16
{
17
17
"cell_type": "markdown",
18
-
"metadata": {},
19
18
"source": [
20
19
"\n",
21
20
"### Preamble"
22
-
]
21
+
],
22
+
"metadata": {}
23
23
},
24
24
{
25
25
"cell_type": "code",
26
26
"execution_count": 1,
27
-
"metadata": {},
28
-
"outputs": [],
29
27
"source": [
30
28
"import numpy as np"
31
-
]
29
+
],
30
+
"outputs": [],
31
+
"metadata": {}
32
32
},
33
33
{
34
34
"cell_type": "markdown",
35
-
"metadata": {},
36
35
"source": [
37
36
"\n",
38
37
"### Exercise B.1 "
39
-
]
38
+
],
39
+
"metadata": {}
40
40
},
41
41
{
42
42
"cell_type": "markdown",
43
-
"metadata": {},
44
43
"source": [
45
44
"**What would 25 meters West and 110 meters North be in feet?**"
"**Which of the following functions are linear?**\n",
84
83
"\n",
85
84
"- $f(x) = 2x$\n",
86
85
"- $f(x) = x^2$\n",
87
86
"- $f(x) = 2^x$"
88
-
]
87
+
],
88
+
"metadata": {}
89
89
},
90
90
{
91
91
"cell_type": "markdown",
92
-
"metadata": {},
93
92
"source": [
94
93
"Recall that a function $f$ is linear if and only if for all scalars $a$ and $b$, $f(ax + by) = af(x) + bf(y)$.\n",
95
94
"Let's check this condition for each of the three functions above:\n",
96
95
"\n",
97
96
"- $f(ax + by) = 2(ax + by) = 2ax + 2by = a \\times 2x + b \\times 2y = af(x) + bf(y)$, thus this function is linear.\n",
98
97
"- $f(ax + by) = (ax + by)^2 = a^2 x^2 + 2abxy + b^2 y^2$, but $af(x) + bf(y) = ax^2 + by^2$. Since $a \\ne a^2$ for all $a$, and since the second expression is missing the $2abxy$ term, you can conclude that this function is **not** linear.\n",
99
98
"- $f(ax + by) = 2^{ax + by} = 2^{ax} \\times 2^{by}$, but $af(x) + bf(y) = a2^x + b2^y$. These two expressions aren't the same, so you can conclude that this function is **not** linear."
100
-
]
99
+
],
100
+
"metadata": {}
101
101
},
102
102
{
103
103
"cell_type": "markdown",
104
-
"metadata": {},
105
104
"source": [
106
105
"----\n",
107
106
"### Exercise B.3"
108
-
]
107
+
],
108
+
"metadata": {}
109
109
},
110
110
{
111
111
"cell_type": "markdown",
112
-
"metadata": {},
113
112
"source": [
114
113
"**Suppose that you have a linear function $g$ such that $g([[1], [0]]) = [[2.3], [-3.1]]$ and $g([[0], [1]]) = [[-5.2], [0.7]]$.\n",
115
114
"Compute $g([[2], [-2]])$.**"
116
-
]
115
+
],
116
+
"metadata": {}
117
117
},
118
118
{
119
119
"cell_type": "code",
120
120
"execution_count": 3,
121
-
"metadata": {},
121
+
"source": [
122
+
"g_horizontal = np.array([[2.3], [-3.1]])\n",
123
+
"g_vertical = np.array([[-5.2], [0.7]])\n",
124
+
"\n",
125
+
"2 * g_horizontal + (-2) * g_vertical"
126
+
],
122
127
"outputs": [
123
128
{
129
+
"output_type": "execute_result",
124
130
"data": {
125
131
"text/plain": [
126
132
"array([[15. ],\n",
127
133
" [-7.6]])"
128
134
]
129
135
},
130
-
"execution_count": 3,
131
136
"metadata": {},
132
-
"output_type": "execute_result"
137
+
"execution_count": 3
133
138
}
134
139
],
135
-
"source": [
136
-
"g_horizontal = np.array([[2.3], [-3.1]])\n",
137
-
"g_vertical = np.array([[-5.2], [0.7]])\n",
138
-
"\n",
139
-
"2 * g_horizontal + (-2) * g_vertical"
140
-
]
140
+
"metadata": {}
141
141
},
142
142
{
143
143
"cell_type": "markdown",
144
-
"metadata": {},
145
144
"source": [
146
145
"----\n",
147
146
"### Exercise B.4"
148
-
]
147
+
],
148
+
"metadata": {}
149
149
},
150
150
{
151
151
"cell_type": "markdown",
152
-
"metadata": {},
153
152
"source": [
154
153
"**Let $X$ be the matrix $[[0, 1], [1, 0]]$, and let $\\vec{y}$ be the vector $[[2], [3]]$.\n",
155
154
"Using NumPy, compute $X\\vec{y}$ and $XX$.**"
156
-
]
155
+
],
156
+
"metadata": {}
157
157
},
158
158
{
159
159
"cell_type": "code",
160
160
"execution_count": 4,
161
-
"metadata": {},
162
-
"outputs": [],
163
161
"source": [
164
162
"X = np.array([\n",
165
163
" [0, 1],\n",
@@ -169,127 +167,129 @@
169
167
" [2],\n",
170
168
" [3]\n",
171
169
"])"
172
-
]
170
+
],
171
+
"outputs": [],
172
+
"metadata": {}
173
173
},
174
174
{
175
175
"cell_type": "code",
176
176
"execution_count": 5,
177
-
"metadata": {},
177
+
"source": [
178
+
"X @ y"
179
+
],
178
180
"outputs": [
179
181
{
182
+
"output_type": "execute_result",
180
183
"data": {
181
184
"text/plain": [
182
185
"array([[3],\n",
183
186
" [2]])"
184
187
]
185
188
},
186
-
"execution_count": 5,
187
189
"metadata": {},
188
-
"output_type": "execute_result"
190
+
"execution_count": 5
189
191
}
190
192
],
191
-
"source": [
192
-
"X @ y"
193
-
]
193
+
"metadata": {}
194
194
},
195
195
{
196
196
"cell_type": "code",
197
197
"execution_count": 6,
198
-
"metadata": {},
198
+
"source": [
199
+
"X @ X"
200
+
],
199
201
"outputs": [
200
202
{
203
+
"output_type": "execute_result",
201
204
"data": {
202
205
"text/plain": [
203
206
"array([[1, 0],\n",
204
207
" [0, 1]])"
205
208
]
206
209
},
207
-
"execution_count": 6,
208
210
"metadata": {},
209
-
"output_type": "execute_result"
211
+
"execution_count": 6
210
212
}
211
213
],
212
-
"source": [
213
-
"X @ X"
214
-
]
214
+
"metadata": {}
215
215
},
216
216
{
217
217
"cell_type": "markdown",
218
-
"metadata": {},
219
218
"source": [
220
219
"----\n",
221
220
"### Exercise B.5"
222
-
]
221
+
],
222
+
"metadata": {}
223
223
},
224
224
{
225
225
"cell_type": "markdown",
226
-
"metadata": {},
227
226
"source": [
228
227
"**Given a vector $[[2], [3]]$, find a vector that points in the same direction but with length 1.**\n",
229
228
"\n",
230
229
"*HINT*: You can either do this by using an inner product, or the `np.linalg.norm` function."
231
-
]
230
+
],
231
+
"metadata": {}
232
232
},
233
233
{
234
234
"cell_type": "code",
235
235
"execution_count": 7,
236
-
"metadata": {},
237
-
"outputs": [],
238
236
"source": [
239
237
"v = np.array([\n",
240
238
" [2],\n",
241
239
" [3]\n",
242
240
"])"
243
-
]
241
+
],
242
+
"outputs": [],
243
+
"metadata": {}
244
244
},
245
245
{
246
246
"cell_type": "markdown",
247
-
"metadata": {},
248
247
"source": [
249
248
"We can try both ways suggested by the hint above to confirm that you get the same answer from each."
0 commit comments