Skip to content

Commit 7ffb011

Browse files
authored
Renaming b -> c Fixes #14 (#19)
1 parent 209ae3f commit 7ffb011

File tree

1 file changed

+80
-80
lines changed

1 file changed

+80
-80
lines changed

apxb/apxb-exercise-solutions.ipynb renamed to apxc/apxc-exercise-solutions.ipynb

+80-80
Original file line numberDiff line numberDiff line change
@@ -2,164 +2,162 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {},
65
"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",
87
"----\n",
98
"> Copyright (c) Sarah Kaiser and Chris Granade.\n",
109
"> Code sample from the book \"Learn Quantum Computing with Python and Q#\" by\n",
1110
"> Sarah Kaiser and Chris Granade, published by Manning Publications Co.\n",
1211
"> Book ISBN 9781617296130.\n",
1312
"> Code licensed under the MIT License."
14-
]
13+
],
14+
"metadata": {}
1515
},
1616
{
1717
"cell_type": "markdown",
18-
"metadata": {},
1918
"source": [
2019
"\n",
2120
"### Preamble"
22-
]
21+
],
22+
"metadata": {}
2323
},
2424
{
2525
"cell_type": "code",
2626
"execution_count": 1,
27-
"metadata": {},
28-
"outputs": [],
2927
"source": [
3028
"import numpy as np"
31-
]
29+
],
30+
"outputs": [],
31+
"metadata": {}
3232
},
3333
{
3434
"cell_type": "markdown",
35-
"metadata": {},
3635
"source": [
3736
"\n",
3837
"### Exercise B.1 "
39-
]
38+
],
39+
"metadata": {}
4040
},
4141
{
4242
"cell_type": "markdown",
43-
"metadata": {},
4443
"source": [
4544
"**What would 25 meters West and 110 meters North be in feet?**"
46-
]
45+
],
46+
"metadata": {}
4747
},
4848
{
4949
"cell_type": "code",
5050
"execution_count": 2,
51-
"metadata": {},
51+
"source": [
52+
"directions_in_meters = np.array([[-25], [110]])\n",
53+
"directions_in_feet = 3.28 * directions_in_meters\n",
54+
"directions_in_feet"
55+
],
5256
"outputs": [
5357
{
58+
"output_type": "execute_result",
5459
"data": {
5560
"text/plain": [
5661
"array([[-82. ],\n",
5762
" [360.8]])"
5863
]
5964
},
60-
"execution_count": 2,
6165
"metadata": {},
62-
"output_type": "execute_result"
66+
"execution_count": 2
6367
}
6468
],
65-
"source": [
66-
"directions_in_meters = np.array([[-25], [110]])\n",
67-
"directions_in_feet = 3.28 * directions_in_meters\n",
68-
"directions_in_feet"
69-
]
69+
"metadata": {}
7070
},
7171
{
7272
"cell_type": "markdown",
73-
"metadata": {},
7473
"source": [
7574
"----\n",
7675
"### Exercise B.2"
77-
]
76+
],
77+
"metadata": {}
7878
},
7979
{
8080
"cell_type": "markdown",
81-
"metadata": {},
8281
"source": [
8382
"**Which of the following functions are linear?**\n",
8483
"\n",
8584
"- $f(x) = 2x$\n",
8685
"- $f(x) = x^2$\n",
8786
"- $f(x) = 2^x$"
88-
]
87+
],
88+
"metadata": {}
8989
},
9090
{
9191
"cell_type": "markdown",
92-
"metadata": {},
9392
"source": [
9493
"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",
9594
"Let's check this condition for each of the three functions above:\n",
9695
"\n",
9796
"- $f(ax + by) = 2(ax + by) = 2ax + 2by = a \\times 2x + b \\times 2y = af(x) + bf(y)$, thus this function is linear.\n",
9897
"- $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",
9998
"- $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": {}
101101
},
102102
{
103103
"cell_type": "markdown",
104-
"metadata": {},
105104
"source": [
106105
"----\n",
107106
"### Exercise B.3"
108-
]
107+
],
108+
"metadata": {}
109109
},
110110
{
111111
"cell_type": "markdown",
112-
"metadata": {},
113112
"source": [
114113
"**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",
115114
"Compute $g([[2], [-2]])$.**"
116-
]
115+
],
116+
"metadata": {}
117117
},
118118
{
119119
"cell_type": "code",
120120
"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+
],
122127
"outputs": [
123128
{
129+
"output_type": "execute_result",
124130
"data": {
125131
"text/plain": [
126132
"array([[15. ],\n",
127133
" [-7.6]])"
128134
]
129135
},
130-
"execution_count": 3,
131136
"metadata": {},
132-
"output_type": "execute_result"
137+
"execution_count": 3
133138
}
134139
],
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": {}
141141
},
142142
{
143143
"cell_type": "markdown",
144-
"metadata": {},
145144
"source": [
146145
"----\n",
147146
"### Exercise B.4"
148-
]
147+
],
148+
"metadata": {}
149149
},
150150
{
151151
"cell_type": "markdown",
152-
"metadata": {},
153152
"source": [
154153
"**Let $X$ be the matrix $[[0, 1], [1, 0]]$, and let $\\vec{y}$ be the vector $[[2], [3]]$.\n",
155154
"Using NumPy, compute $X\\vec{y}$ and $XX$.**"
156-
]
155+
],
156+
"metadata": {}
157157
},
158158
{
159159
"cell_type": "code",
160160
"execution_count": 4,
161-
"metadata": {},
162-
"outputs": [],
163161
"source": [
164162
"X = np.array([\n",
165163
" [0, 1],\n",
@@ -169,127 +167,129 @@
169167
" [2],\n",
170168
" [3]\n",
171169
"])"
172-
]
170+
],
171+
"outputs": [],
172+
"metadata": {}
173173
},
174174
{
175175
"cell_type": "code",
176176
"execution_count": 5,
177-
"metadata": {},
177+
"source": [
178+
"X @ y"
179+
],
178180
"outputs": [
179181
{
182+
"output_type": "execute_result",
180183
"data": {
181184
"text/plain": [
182185
"array([[3],\n",
183186
" [2]])"
184187
]
185188
},
186-
"execution_count": 5,
187189
"metadata": {},
188-
"output_type": "execute_result"
190+
"execution_count": 5
189191
}
190192
],
191-
"source": [
192-
"X @ y"
193-
]
193+
"metadata": {}
194194
},
195195
{
196196
"cell_type": "code",
197197
"execution_count": 6,
198-
"metadata": {},
198+
"source": [
199+
"X @ X"
200+
],
199201
"outputs": [
200202
{
203+
"output_type": "execute_result",
201204
"data": {
202205
"text/plain": [
203206
"array([[1, 0],\n",
204207
" [0, 1]])"
205208
]
206209
},
207-
"execution_count": 6,
208210
"metadata": {},
209-
"output_type": "execute_result"
211+
"execution_count": 6
210212
}
211213
],
212-
"source": [
213-
"X @ X"
214-
]
214+
"metadata": {}
215215
},
216216
{
217217
"cell_type": "markdown",
218-
"metadata": {},
219218
"source": [
220219
"----\n",
221220
"### Exercise B.5"
222-
]
221+
],
222+
"metadata": {}
223223
},
224224
{
225225
"cell_type": "markdown",
226-
"metadata": {},
227226
"source": [
228227
"**Given a vector $[[2], [3]]$, find a vector that points in the same direction but with length 1.**\n",
229228
"\n",
230229
"*HINT*: You can either do this by using an inner product, or the `np.linalg.norm` function."
231-
]
230+
],
231+
"metadata": {}
232232
},
233233
{
234234
"cell_type": "code",
235235
"execution_count": 7,
236-
"metadata": {},
237-
"outputs": [],
238236
"source": [
239237
"v = np.array([\n",
240238
" [2],\n",
241239
" [3]\n",
242240
"])"
243-
]
241+
],
242+
"outputs": [],
243+
"metadata": {}
244244
},
245245
{
246246
"cell_type": "markdown",
247-
"metadata": {},
248247
"source": [
249248
"We can try both ways suggested by the hint above to confirm that you get the same answer from each."
250-
]
249+
],
250+
"metadata": {}
251251
},
252252
{
253253
"cell_type": "code",
254254
"execution_count": 8,
255-
"metadata": {},
255+
"source": [
256+
"v / np.sqrt(v.transpose() @ v)"
257+
],
256258
"outputs": [
257259
{
260+
"output_type": "execute_result",
258261
"data": {
259262
"text/plain": [
260263
"array([[0.5547002 ],\n",
261264
" [0.83205029]])"
262265
]
263266
},
264-
"execution_count": 8,
265267
"metadata": {},
266-
"output_type": "execute_result"
268+
"execution_count": 8
267269
}
268270
],
269-
"source": [
270-
"v / np.sqrt(v.transpose() @ v)"
271-
]
271+
"metadata": {}
272272
},
273273
{
274274
"cell_type": "code",
275275
"execution_count": 9,
276-
"metadata": {},
276+
"source": [
277+
"v / np.linalg.norm(v)"
278+
],
277279
"outputs": [
278280
{
281+
"output_type": "execute_result",
279282
"data": {
280283
"text/plain": [
281284
"array([[0.5547002 ],\n",
282285
" [0.83205029]])"
283286
]
284287
},
285-
"execution_count": 9,
286288
"metadata": {},
287-
"output_type": "execute_result"
289+
"execution_count": 9
288290
}
289291
],
290-
"source": [
291-
"v / np.linalg.norm(v)"
292-
]
292+
"metadata": {}
293293
}
294294
],
295295
"metadata": {
@@ -313,4 +313,4 @@
313313
},
314314
"nbformat": 4,
315315
"nbformat_minor": 4
316-
}
316+
}

0 commit comments

Comments
 (0)