Skip to content

Commit 1767f40

Browse files
committed
Updated to Qiskit 1.0.2
1 parent ef15f8e commit 1767f40

11 files changed

+1973
-2249
lines changed

Adjoint_Differentiation.ipynb

Lines changed: 241 additions & 231 deletions
Large diffs are not rendered by default.

Classical_and_Quantum_Probabilities.ipynb

Lines changed: 558 additions & 473 deletions
Large diffs are not rendered by default.

Data_reuploading_Classifier.ipynb

Lines changed: 177 additions & 273 deletions
Large diffs are not rendered by default.

Evolution_in_Closed_and_Open_Systems.ipynb

Lines changed: 30 additions & 123 deletions
Large diffs are not rendered by default.

Gaussian_Transformation.ipynb

Lines changed: 119 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,34 @@
11
{
2-
"nbformat": 4,
3-
"nbformat_minor": 0,
4-
"metadata": {
5-
"colab": {
6-
"provenance": [],
7-
"authorship_tag": "ABX9TyOXzUywhEI6EA0KtJqWmdod",
8-
"include_colab_link": true
9-
},
10-
"kernelspec": {
11-
"name": "python3",
12-
"display_name": "Python 3"
13-
},
14-
"language_info": {
15-
"name": "python"
16-
}
17-
},
182
"cells": [
193
{
204
"cell_type": "markdown",
215
"metadata": {
22-
"id": "view-in-github",
23-
"colab_type": "text"
6+
"colab_type": "text",
7+
"id": "view-in-github"
248
},
259
"source": [
2610
"<a href=\"https://colab.research.google.com/github/MonitSharma/Learn-Quantum-Machine-Learning/blob/main/Gaussian_Transformation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
2711
]
2812
},
2913
{
3014
"cell_type": "markdown",
15+
"metadata": {
16+
"id": "Wo8gh7fA0CM9"
17+
},
3118
"source": [
3219
"# Gaussian Transformation\n",
3320
"\n",
3421
"Here we'll learn about the basic principles of continuous variable (CV) photonic devices.\n",
3522
"\n",
3623
"\n",
3724
"Read [this](https://strawberryfields.readthedocs.io/en/latest/) documentation to learn more on it."
38-
],
39-
"metadata": {
40-
"id": "Wo8gh7fA0CM9"
41-
}
25+
]
4226
},
4327
{
4428
"cell_type": "markdown",
29+
"metadata": {
30+
"id": "b2cDyZU00Uzh"
31+
},
4532
"source": [
4633
"## The quantum circuit\n",
4734
"----\n",
@@ -72,147 +59,147 @@
7259
"\n",
7360
"----\n",
7461
"Our aim here is to optimize the circuit parameters $(\\alpha, \\phi)$ such that the mean photon number is equal to one. The rotating gate is actually a *passive transformation*, meaning that it does not change the energy of the system. The displacement gate is an *active transformation*, which modifies the energy of the photonic system."
75-
],
76-
"metadata": {
77-
"id": "b2cDyZU00Uzh"
78-
}
62+
]
7963
},
8064
{
8165
"cell_type": "markdown",
66+
"metadata": {
67+
"id": "v1rphGsv17TA"
68+
},
8269
"source": [
8370
"## Constructing the QNode\n",
8471
"\n",
8572
"Import Pennylane and NumPy"
86-
],
87-
"metadata": {
88-
"id": "v1rphGsv17TA"
89-
}
73+
]
9074
},
9175
{
9276
"cell_type": "code",
93-
"source": [
94-
"import pennylane as qml\n",
95-
"from pennylane import numpy as np"
96-
],
77+
"execution_count": 1,
9778
"metadata": {
9879
"id": "X5PQCKxW0fv1"
9980
},
100-
"execution_count": 2,
101-
"outputs": []
81+
"outputs": [],
82+
"source": [
83+
"import pennylane as qml\n",
84+
"from pennylane import numpy as np"
85+
]
10286
},
10387
{
10488
"cell_type": "markdown",
105-
"source": [
106-
"Next, we instantiate a device which will be used to evaluate the circuit. Because our circuit contains only Gaussian operations, we can make use of the built-in `default.gaussian` device."
107-
],
10889
"metadata": {
10990
"id": "fIqx_6Ve2D1Y"
110-
}
91+
},
92+
"source": [
93+
"Next, we instantiate a device which will be used to evaluate the circuit. Because our circuit contains only Gaussian operations, we can make use of the built-in `default.gaussian` device."
94+
]
11195
},
11296
{
11397
"cell_type": "code",
114-
"source": [
115-
"dev_gaussian = qml.device(\"default.gaussian\", wires=1)"
116-
],
98+
"execution_count": 2,
11799
"metadata": {
118100
"id": "oeoFDYCd2BYe"
119101
},
120-
"execution_count": 3,
121-
"outputs": []
102+
"outputs": [],
103+
"source": [
104+
"dev_gaussian = qml.device(\"default.gaussian\", wires=1)"
105+
]
122106
},
123107
{
124108
"cell_type": "markdown",
125-
"source": [
126-
"After initializing the device, we can construct our quantum node. As before, we use the `qnode()` to convert our quantum function (encoded by the circuit above) into a quantum node running on the `default.gaussian` device."
127-
],
128109
"metadata": {
129110
"id": "aRy5muAV2LHr"
130-
}
111+
},
112+
"source": [
113+
"After initializing the device, we can construct our quantum node. As before, we use the `qnode()` to convert our quantum function (encoded by the circuit above) into a quantum node running on the `default.gaussian` device."
114+
]
131115
},
132116
{
133117
"cell_type": "code",
118+
"execution_count": 3,
119+
"metadata": {
120+
"id": "WeTUdD1w2JVo"
121+
},
122+
"outputs": [],
134123
"source": [
135124
"@qml.qnode(dev_gaussian)\n",
136125
"def mean_photon_gaussian(mag_alpha, phase_alpha, phi):\n",
137126
" qml.Displacement(mag_alpha, phase_alpha, wires=0)\n",
138127
" qml.Rotation(phi, wires=0)\n",
139128
" return qml.expval(qml.NumberOperator(0))"
140-
],
141-
"metadata": {
142-
"id": "WeTUdD1w2JVo"
143-
},
144-
"execution_count": 4,
145-
"outputs": []
129+
]
146130
},
147131
{
148132
"cell_type": "markdown",
133+
"metadata": {
134+
"id": "W2H_pYWr2TC5"
135+
},
149136
"source": [
150137
"Notice that we have broken up the complex number $α$\n",
151138
" into two real numbers `mag_alpha` and `phase_alpha`, which form a polar representation of $α$\n",
152139
". This is so that the notion of a gradient is clear and well-defined."
153-
],
154-
"metadata": {
155-
"id": "W2H_pYWr2TC5"
156-
}
140+
]
157141
},
158142
{
159143
"cell_type": "markdown",
144+
"metadata": {
145+
"id": "G2SWZbav2ZcG"
146+
},
160147
"source": [
161148
"## Optimization\n",
162149
"\n",
163150
"As in the [qubit rotation](https://colab.research.google.com/github/MonitSharma/Learn-Quantum-Machine-Learning/blob/main/Qubit_Rotation.ipynb) tutorial, let’s now use one of the built-in PennyLane optimizers in order to optimize the quantum circuit towards the desired output. We want the mean photon number to be exactly one, so we will use a squared-difference cost function:"
164-
],
165-
"metadata": {
166-
"id": "G2SWZbav2ZcG"
167-
}
151+
]
168152
},
169153
{
170154
"cell_type": "code",
171-
"source": [
172-
"def cost(params):\n",
173-
" return (mean_photon_gaussian(params[0], params[1], params[2]) - 1.0) ** 2\n"
174-
],
155+
"execution_count": 4,
175156
"metadata": {
176157
"id": "K9bFyeE52RNr"
177158
},
178-
"execution_count": 5,
179-
"outputs": []
159+
"outputs": [],
160+
"source": [
161+
"def cost(params):\n",
162+
" return (mean_photon_gaussian(params[0], params[1], params[2]) - 1.0) ** 2\n"
163+
]
180164
},
181165
{
182166
"cell_type": "markdown",
183-
"source": [
184-
"At the beginning of the optimization, we choose arbitrary small initial parameters:"
185-
],
186167
"metadata": {
187168
"id": "MtuCyrtk2mYZ"
188-
}
169+
},
170+
"source": [
171+
"At the beginning of the optimization, we choose arbitrary small initial parameters:"
172+
]
189173
},
190174
{
191175
"cell_type": "code",
192-
"source": [
193-
"init_params = np.array([0.015, 0.02, 0.005], requires_grad=True)\n",
194-
"print(cost(init_params))"
195-
],
176+
"execution_count": 5,
196177
"metadata": {
197178
"colab": {
198179
"base_uri": "https://localhost:8080/"
199180
},
200181
"id": "Q4d1C6dw2kcT",
201182
"outputId": "a0c748c6-9618-4036-8140-c368c6056f8c"
202183
},
203-
"execution_count": 6,
204184
"outputs": [
205185
{
206-
"output_type": "stream",
207186
"name": "stdout",
187+
"output_type": "stream",
208188
"text": [
209189
"0.9995500506249999\n"
210190
]
211191
}
192+
],
193+
"source": [
194+
"init_params = np.array([0.015, 0.02, 0.005], requires_grad=True)\n",
195+
"print(cost(init_params))"
212196
]
213197
},
214198
{
215199
"cell_type": "markdown",
200+
"metadata": {
201+
"id": "V6ngLFOe2qxz"
202+
},
216203
"source": [
217204
"When the gate parameters are near to zero, the gates are close to the identity transformation, which leaves the initial state largely unchanged. Since the initial state contains no photons, the mean photon number of the circuit output is approximately zero, and the cost is close to one.\n",
218205
"\n",
@@ -224,44 +211,22 @@
224211
"Now, let’s use the `GradientDescentOptimizer`, and update the circuit parameters over 100 optimization steps.\n",
225212
"\n",
226213
"\n"
227-
],
228-
"metadata": {
229-
"id": "V6ngLFOe2qxz"
230-
}
214+
]
231215
},
232216
{
233217
"cell_type": "code",
234-
"source": [
235-
"# initialise the optimizer\n",
236-
"opt = qml.GradientDescentOptimizer(stepsize=0.1)\n",
237-
"\n",
238-
"# set the number of steps\n",
239-
"steps = 20\n",
240-
"# set the initial parameter values\n",
241-
"params = init_params\n",
242-
"\n",
243-
"for i in range(steps):\n",
244-
" # update the circuit parameters\n",
245-
" params = opt.step(cost, params)\n",
246-
"\n",
247-
" print(\"Cost after step {:5d}: {:8f}\".format(i + 1, cost(params)))\n",
248-
"\n",
249-
"print(\"Optimized mag_alpha:{:8f}\".format(params[0]))\n",
250-
"print(\"Optimized phase_alpha:{:8f}\".format(params[1]))\n",
251-
"print(\"Optimized phi:{:8f}\".format(params[2]))"
252-
],
218+
"execution_count": 6,
253219
"metadata": {
254220
"colab": {
255221
"base_uri": "https://localhost:8080/"
256222
},
257223
"id": "h5Wi_2AA2olc",
258224
"outputId": "3c61c917-2ae0-42c9-92ff-70d4eb64538c"
259225
},
260-
"execution_count": 7,
261226
"outputs": [
262227
{
263-
"output_type": "stream",
264228
"name": "stdout",
229+
"output_type": "stream",
265230
"text": [
266231
"Cost after step 1: 0.999118\n",
267232
"Cost after step 2: 0.998273\n",
@@ -288,21 +253,65 @@
288253
"Optimized phi:0.005000\n"
289254
]
290255
}
256+
],
257+
"source": [
258+
"# initialise the optimizer\n",
259+
"opt = qml.GradientDescentOptimizer(stepsize=0.1)\n",
260+
"\n",
261+
"# set the number of steps\n",
262+
"steps = 20\n",
263+
"# set the initial parameter values\n",
264+
"params = init_params\n",
265+
"\n",
266+
"for i in range(steps):\n",
267+
" # update the circuit parameters\n",
268+
" params = opt.step(cost, params)\n",
269+
"\n",
270+
" print(\"Cost after step {:5d}: {:8f}\".format(i + 1, cost(params)))\n",
271+
"\n",
272+
"print(\"Optimized mag_alpha:{:8f}\".format(params[0]))\n",
273+
"print(\"Optimized phase_alpha:{:8f}\".format(params[1]))\n",
274+
"print(\"Optimized phi:{:8f}\".format(params[2]))"
291275
]
292276
},
293277
{
294278
"cell_type": "markdown",
279+
"metadata": {
280+
"id": "EFN1mc8824HM"
281+
},
295282
"source": [
296283
"The optimization converges after about 20 steps to a cost function value of zero.\n",
297284
"\n",
298285
"We observe that the two angular parameters phase_alpha and phi do not change during the optimization. Only the magnitude of the complex displacement $|α|$ affects the mean photon number of the circuit.\n",
299286
"\n",
300287
"\n",
301288
"Next we will learn how to utilize the extensive plugin ecosystem of Pennylane, build continuous variable quantum nodes."
302-
],
303-
"metadata": {
304-
"id": "EFN1mc8824HM"
305-
}
289+
]
306290
}
307-
]
308-
}
291+
],
292+
"metadata": {
293+
"colab": {
294+
"authorship_tag": "ABX9TyOXzUywhEI6EA0KtJqWmdod",
295+
"include_colab_link": true,
296+
"provenance": []
297+
},
298+
"kernelspec": {
299+
"display_name": "Python 3",
300+
"name": "python3"
301+
},
302+
"language_info": {
303+
"codemirror_mode": {
304+
"name": "ipython",
305+
"version": 3
306+
},
307+
"file_extension": ".py",
308+
"mimetype": "text/x-python",
309+
"name": "python",
310+
"nbconvert_exporter": "python",
311+
"pygments_lexer": "ipython3",
312+
"version": "3.12.2"
313+
}
314+
},
315+
"nbformat": 4,
316+
"nbformat_minor": 0
317+
}

0 commit comments

Comments
 (0)