Skip to content

Commit 2f9f552

Browse files
committed
finalizing
1 parent 61a28d2 commit 2f9f552

File tree

2 files changed

+120
-163
lines changed

2 files changed

+120
-163
lines changed

session_4/EX_6_QF_MixNN_V_U.ipynb

Lines changed: 71 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": 3,
24+
"execution_count": null,
2525
"source": [
26-
"is_colab = False\n",
26+
"is_colab = True\n",
2727
"import sys\n",
2828
"if is_colab:\n",
2929
" !pip install -q torch==1.9.0\n",
@@ -35,10 +35,10 @@
3535
"\n",
3636
"\n",
3737
"import torch\n",
38-
"from qiskit import QuantumCircuit, ClassicalRegister\n",
38+
"from qiskit import QuantumCircuit, ClassicalRegister\n",
3939
"import numpy as np\n",
4040
"import functools\n",
41-
"from qfnn.qf_fb.q_output import fire_ibmq,analyze,add_measure\n",
41+
"from qfnn.qf_fb.q_output import fire_ibmq, analyze, add_measure\n",
4242
"from qfnn.qf_circ.f_lyr_circ import F_LYR_Circ\n",
4343
"from qfnn.qf_circ.v_lyr_circ import V_LYR_Circ\n",
4444
"from qfnn.qf_net.utils import binarize\n",
@@ -53,11 +53,11 @@
5353
"# remember to change the path on your computer\n",
5454
"######################################################\n",
5555
"if is_colab:\n",
56-
" data_path = '/content/data' #mnist path\n",
57-
" resume_path = '/content/model/v16_u2/model_best.tar' #model path\n",
56+
" data_path = '/content/data' # mnist path\n",
57+
" resume_path = '/content/model/v16_u2/model_best.tar' # model path\n",
5858
"else:\n",
59-
" data_path = '/home/hzr/Software/quantum/qc_mnist/pytorch/data' #mnist path\n",
60-
" resume_path = '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar' #model path"
59+
" data_path = '/home/hzr/Software/quantum/qc_mnist/pytorch/data' # mnist path\n",
60+
" resume_path = '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar' # model path"
6161
],
6262
"outputs": [],
6363
"metadata": {
@@ -69,14 +69,14 @@
6969
},
7070
{
7171
"cell_type": "code",
72-
"execution_count": 4,
72+
"execution_count": null,
7373
"source": [
7474
"################ Zhirui on 12-30-2020 ################\n",
7575
"# Parameters of settings\n",
7676
"######################################################\n",
7777
"interest_num = [3,6]\n",
7878
"img_size = 4\n",
79-
"batch_size = 1# how many samples per batch to load\n",
79+
"batch_size = 1 # how many samples per batch to load\n",
8080
"inference_batch_size = 1\n",
8181
"isppd = False #is prepared data\n",
8282
"\n"
@@ -93,9 +93,9 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": 5,
96+
"execution_count": null,
9797
"source": [
98-
"train_loader, test_loader = load_data(interest_num,data_path,isppd,img_size,batch_size,inference_batch_size,False)\n",
98+
"train_loader, test_loader = load_data(interest_num, data_path, isppd, img_size, batch_size,inference_batch_size,False)\n",
9999
"\n",
100100
"\n",
101101
"for batch_idx, (data, target) in enumerate(test_loader):\n",
@@ -104,23 +104,7 @@
104104
" quantum_matrix = to_quantum_matrix(data)\n",
105105
" break"
106106
],
107-
"outputs": [
108-
{
109-
"name": "stderr",
110-
"output_type": "stream",
111-
"text": [
112-
"C:\\Users\\wjiang8\\AppData\\Roaming\\Python\\Python38\\site-packages\\torchvision\\datasets\\mnist.py:498: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at ..\\torch\\csrc\\utils\\tensor_numpy.cpp:180.)\n",
113-
" return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)\n"
114-
]
115-
},
116-
{
117-
"name": "stdout",
118-
"output_type": "stream",
119-
"text": [
120-
"Batch Id: 0, Target: tensor([1])\n"
121-
]
122-
}
123-
],
107+
"outputs": [],
124108
"metadata": {}
125109
},
126110
{
@@ -139,7 +123,7 @@
139123
},
140124
{
141125
"cell_type": "code",
142-
"execution_count": 6,
126+
"execution_count": null,
143127
"source": [
144128
"################ hzr on 12-30-2020 ################\n",
145129
"# Get the parameters of the trained model\n",
@@ -152,29 +136,47 @@
152136
"theta = checkpoint['state_dict']['fc0.theta']\n",
153137
"weight = checkpoint['state_dict']['fc1.weight']\n"
154138
],
155-
"outputs": [
156-
{
157-
"ename": "FileNotFoundError",
158-
"evalue": "[Errno 2] No such file or directory: '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar'",
159-
"output_type": "error",
160-
"traceback": [
161-
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
162-
"\u001B[1;31mFileNotFoundError\u001B[0m Traceback (most recent call last)",
163-
"\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_29852/3962706902.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m 4\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 5\u001B[0m \u001B[1;31m# Model initialization\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 6\u001B[1;33m \u001B[0mcheckpoint\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mtorch\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mload\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mresume_path\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmap_location\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;34m'cpu'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 7\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mcheckpoint\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'state_dict'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'fc0.theta'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 8\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mcheckpoint\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'state_dict'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'fc1.weight'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
164-
"\u001B[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\torch\\serialization.py\u001B[0m in \u001B[0;36mload\u001B[1;34m(f, map_location, pickle_module, **pickle_load_args)\u001B[0m\n\u001B[0;32m 592\u001B[0m \u001B[0mpickle_load_args\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'encoding'\u001B[0m\u001B[1;33m]\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;34m'utf-8'\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 593\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 594\u001B[1;33m \u001B[1;32mwith\u001B[0m \u001B[0m_open_file_like\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mf\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'rb'\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mopened_file\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 595\u001B[0m \u001B[1;32mif\u001B[0m \u001B[0m_is_zipfile\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mopened_file\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 596\u001B[0m \u001B[1;31m# The zipfile reader is going to advance the current file position.\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
165-
"\u001B[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\torch\\serialization.py\u001B[0m in \u001B[0;36m_open_file_like\u001B[1;34m(name_or_buffer, mode)\u001B[0m\n\u001B[0;32m 228\u001B[0m \u001B[1;32mdef\u001B[0m \u001B[0m_open_file_like\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mname_or_buffer\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmode\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 229\u001B[0m \u001B[1;32mif\u001B[0m \u001B[0m_is_path\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mname_or_buffer\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 230\u001B[1;33m \u001B[1;32mreturn\u001B[0m \u001B[0m_open_file\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mname_or_buffer\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmode\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 231\u001B[0m \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 232\u001B[0m \u001B[1;32mif\u001B[0m \u001B[1;34m'w'\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mmode\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
166-
"\u001B[1;32m~\\AppData\\Roaming\\Python\\Python38\\site-packages\\torch\\serialization.py\u001B[0m in \u001B[0;36m__init__\u001B[1;34m(self, name, mode)\u001B[0m\n\u001B[0;32m 209\u001B[0m \u001B[1;32mclass\u001B[0m \u001B[0m_open_file\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0m_opener\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 210\u001B[0m \u001B[1;32mdef\u001B[0m \u001B[0m__init__\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mname\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmode\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 211\u001B[1;33m \u001B[0msuper\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0m_open_file\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m__init__\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mopen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mname\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmode\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 212\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 213\u001B[0m \u001B[1;32mdef\u001B[0m \u001B[0m__exit__\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m*\u001B[0m\u001B[0margs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
167-
"\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar'"
168-
]
169-
}
170-
],
139+
"outputs": [],
171140
"metadata": {
172141
"collapsed": false,
173142
"pycharm": {
174143
"name": "#%%\n"
175144
}
176145
}
177146
},
147+
{
148+
"cell_type": "markdown",
149+
"source": [
150+
"### classical inference"
151+
],
152+
"metadata": {}
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"source": [
158+
"neural_in_layers = 'v:16,u:2'\n",
159+
"layers = []\n",
160+
"for item1 in neural_in_layers.split(\",\"):\n",
161+
" x= item1.split(\":\")\n",
162+
" layer =[]\n",
163+
" layer.append(x[0].strip())\n",
164+
" layer.append(int(x[1].strip()))\n",
165+
" layers.append(layer)\n",
166+
"print(layers)\n",
167+
"\n",
168+
"img_size = 4\n",
169+
"model = Net(img_size,layers,False,False)\n",
170+
"model.load_state_dict(checkpoint[\"state_dict\"])\n",
171+
"to_quantum_data = ToQuantumData(img_size)\n",
172+
"output_data = to_quantum_data(data)\n",
173+
"# output = model.forward(output_data,False)\n",
174+
"output = model(output_data, False)\n",
175+
"print(\"classical inference result:\",output)"
176+
],
177+
"outputs": [],
178+
"metadata": {}
179+
},
178180
{
179181
"cell_type": "markdown",
180182
"source": [
@@ -206,27 +208,37 @@
206208
{
207209
"cell_type": "markdown",
208210
"source": [
209-
"### build the network"
211+
"### build the network on Quantum Circuit"
210212
],
211-
"metadata": {}
213+
"metadata": {
214+
"collapsed": false,
215+
"pycharm": {
216+
"name": "#%% md\n"
217+
}
218+
}
212219
},
213220
{
214221
"cell_type": "code",
215-
"execution_count": null,
216222
"source": [
217223
"################ hzr on 12-30-2020 ################\n",
218224
"# Generate the circuit of v-layer\n",
219225
"######################################################\n",
220226
"\n",
221-
"#define your input and repeat number\n",
227+
"# define your input and repeat number\n",
222228
"vqc = V_LYR_Circ(4,2)\n",
223-
"#add v-layer to your circuit\n",
224-
"vqc.forward(circuit,inputs,'v10',np.array(theta,dtype=np.double))\n",
229+
"# add v-layer to your circuit\n",
230+
"vqc.forward(circuit, inputs, 'v10', np.array(theta, dtype=np.double))\n",
225231
"\n",
226232
"circuit.draw('text',fold=300)"
227233
],
228-
"outputs": [],
229-
"metadata": {}
234+
"metadata": {
235+
"collapsed": false,
236+
"pycharm": {
237+
"name": "#%%\n"
238+
}
239+
},
240+
"execution_count": null,
241+
"outputs": []
230242
},
231243
{
232244
"cell_type": "code",
@@ -248,11 +260,9 @@
248260
"#add f-layer to your circuit\n",
249261
"f_layer.forward(circuit,binarize(weight),inputs,f_layer_out_qubits,None,aux)\n",
250262
"\n",
263+
"#add measurement to your circuit if needed\n",
251264
"add_measure(circuit,f_layer_out_qubits,'reg')\n",
252-
"\n",
253265
"circuit.barrier()\n",
254-
"#add measurement to your circuit if needed\n",
255-
"\n",
256266
"\n",
257267
"circuit.draw('text',fold=300)\n",
258268
"\n"
@@ -270,9 +280,7 @@
270280
"source": [
271281
"### simulation\n"
272282
],
273-
"metadata": {
274-
"collapsed": false
275-
}
283+
"metadata": {}
276284
},
277285
{
278286
"cell_type": "code",
@@ -283,9 +291,9 @@
283291
"######################################################\n",
284292
"\n",
285293
"\n",
286-
"qc_shots=8192\n",
287-
"opt_counts = fire_ibmq(circuit,qc_shots,True)\n",
288-
"(opt_mycount,bits) = analyze(opt_counts)\n",
294+
"qc_shots= 10000 # 8192\n",
295+
"opt_counts = fire_ibmq(circuit, qc_shots, True)\n",
296+
"(opt_mycount, bits) = analyze(opt_counts)\n",
289297
"opt_class_prob=[]\n",
290298
"for b in range(bits):\n",
291299
" opt_class_prob.append(float(opt_mycount[b])/qc_shots)\n",
@@ -301,42 +309,6 @@
301309
"print(\"=\"*30)"
302310
],
303311
"outputs": [],
304-
"metadata": {
305-
"collapsed": false,
306-
"pycharm": {
307-
"name": "#%%\n"
308-
}
309-
}
310-
},
311-
{
312-
"cell_type": "markdown",
313-
"source": [
314-
"### classical inference"
315-
],
316-
"metadata": {}
317-
},
318-
{
319-
"cell_type": "code",
320-
"execution_count": null,
321-
"source": [
322-
"\n",
323-
"neural_in_layers = 'v:16,u:2'\n",
324-
"layers = []\n",
325-
"for item1 in neural_in_layers.split(\",\"):\n",
326-
" x= item1.split(\":\")\n",
327-
" layer =[]\n",
328-
" layer.append(x[0].strip())\n",
329-
" layer.append(int(x[1].strip()))\n",
330-
" layers.append(layer)\n",
331-
"\n",
332-
"model = Net(img_size,layers,False,False)\n",
333-
"model.load_state_dict(checkpoint[\"state_dict\"])\n",
334-
"to_quantum_data = ToQuantumData(img_size)\n",
335-
"output_data = to_quantum_data(data)\n",
336-
"output = model.forward(output_data,False)\n",
337-
"print(\"classical inference result:\",output)"
338-
],
339-
"outputs": [],
340312
"metadata": {}
341313
}
342314
],
@@ -357,15 +329,6 @@
357329
"nbconvert_exporter": "python",
358330
"file_extension": ".py"
359331
},
360-
"pycharm": {
361-
"stem_cell": {
362-
"cell_type": "raw",
363-
"source": [],
364-
"metadata": {
365-
"collapsed": false
366-
}
367-
}
368-
},
369332
"interpreter": {
370333
"hash": "f24048f0d5bdb0ff49c5e7c8a9899a65bc3ab13b0f32660a2227453ca6b95fd8"
371334
}

0 commit comments

Comments
 (0)