|
21 | 21 | },
|
22 | 22 | {
|
23 | 23 | "cell_type": "code",
|
24 |
| - "execution_count": 3, |
| 24 | + "execution_count": null, |
25 | 25 | "source": [
|
26 |
| - "is_colab = False\n", |
| 26 | + "is_colab = True\n", |
27 | 27 | "import sys\n",
|
28 | 28 | "if is_colab:\n",
|
29 | 29 | " !pip install -q torch==1.9.0\n",
|
|
35 | 35 | "\n",
|
36 | 36 | "\n",
|
37 | 37 | "import torch\n",
|
38 |
| - "from qiskit import QuantumCircuit, ClassicalRegister\n", |
| 38 | + "from qiskit import QuantumCircuit, ClassicalRegister\n", |
39 | 39 | "import numpy as np\n",
|
40 | 40 | "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", |
42 | 42 | "from qfnn.qf_circ.f_lyr_circ import F_LYR_Circ\n",
|
43 | 43 | "from qfnn.qf_circ.v_lyr_circ import V_LYR_Circ\n",
|
44 | 44 | "from qfnn.qf_net.utils import binarize\n",
|
|
53 | 53 | "# remember to change the path on your computer\n",
|
54 | 54 | "######################################################\n",
|
55 | 55 | "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", |
58 | 58 | "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" |
61 | 61 | ],
|
62 | 62 | "outputs": [],
|
63 | 63 | "metadata": {
|
|
69 | 69 | },
|
70 | 70 | {
|
71 | 71 | "cell_type": "code",
|
72 |
| - "execution_count": 4, |
| 72 | + "execution_count": null, |
73 | 73 | "source": [
|
74 | 74 | "################ Zhirui on 12-30-2020 ################\n",
|
75 | 75 | "# Parameters of settings\n",
|
76 | 76 | "######################################################\n",
|
77 | 77 | "interest_num = [3,6]\n",
|
78 | 78 | "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", |
80 | 80 | "inference_batch_size = 1\n",
|
81 | 81 | "isppd = False #is prepared data\n",
|
82 | 82 | "\n"
|
|
93 | 93 | },
|
94 | 94 | {
|
95 | 95 | "cell_type": "code",
|
96 |
| - "execution_count": 5, |
| 96 | + "execution_count": null, |
97 | 97 | "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", |
99 | 99 | "\n",
|
100 | 100 | "\n",
|
101 | 101 | "for batch_idx, (data, target) in enumerate(test_loader):\n",
|
|
104 | 104 | " quantum_matrix = to_quantum_matrix(data)\n",
|
105 | 105 | " break"
|
106 | 106 | ],
|
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": [], |
124 | 108 | "metadata": {}
|
125 | 109 | },
|
126 | 110 | {
|
|
139 | 123 | },
|
140 | 124 | {
|
141 | 125 | "cell_type": "code",
|
142 |
| - "execution_count": 6, |
| 126 | + "execution_count": null, |
143 | 127 | "source": [
|
144 | 128 | "################ hzr on 12-30-2020 ################\n",
|
145 | 129 | "# Get the parameters of the trained model\n",
|
|
152 | 136 | "theta = checkpoint['state_dict']['fc0.theta']\n",
|
153 | 137 | "weight = checkpoint['state_dict']['fc1.weight']\n"
|
154 | 138 | ],
|
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": [], |
171 | 140 | "metadata": {
|
172 | 141 | "collapsed": false,
|
173 | 142 | "pycharm": {
|
174 | 143 | "name": "#%%\n"
|
175 | 144 | }
|
176 | 145 | }
|
177 | 146 | },
|
| 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 | + }, |
178 | 180 | {
|
179 | 181 | "cell_type": "markdown",
|
180 | 182 | "source": [
|
|
206 | 208 | {
|
207 | 209 | "cell_type": "markdown",
|
208 | 210 | "source": [
|
209 |
| - "### build the network" |
| 211 | + "### build the network on Quantum Circuit" |
210 | 212 | ],
|
211 |
| - "metadata": {} |
| 213 | + "metadata": { |
| 214 | + "collapsed": false, |
| 215 | + "pycharm": { |
| 216 | + "name": "#%% md\n" |
| 217 | + } |
| 218 | + } |
212 | 219 | },
|
213 | 220 | {
|
214 | 221 | "cell_type": "code",
|
215 |
| - "execution_count": null, |
216 | 222 | "source": [
|
217 | 223 | "################ hzr on 12-30-2020 ################\n",
|
218 | 224 | "# Generate the circuit of v-layer\n",
|
219 | 225 | "######################################################\n",
|
220 | 226 | "\n",
|
221 |
| - "#define your input and repeat number\n", |
| 227 | + "# define your input and repeat number\n", |
222 | 228 | "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", |
225 | 231 | "\n",
|
226 | 232 | "circuit.draw('text',fold=300)"
|
227 | 233 | ],
|
228 |
| - "outputs": [], |
229 |
| - "metadata": {} |
| 234 | + "metadata": { |
| 235 | + "collapsed": false, |
| 236 | + "pycharm": { |
| 237 | + "name": "#%%\n" |
| 238 | + } |
| 239 | + }, |
| 240 | + "execution_count": null, |
| 241 | + "outputs": [] |
230 | 242 | },
|
231 | 243 | {
|
232 | 244 | "cell_type": "code",
|
|
248 | 260 | "#add f-layer to your circuit\n",
|
249 | 261 | "f_layer.forward(circuit,binarize(weight),inputs,f_layer_out_qubits,None,aux)\n",
|
250 | 262 | "\n",
|
| 263 | + "#add measurement to your circuit if needed\n", |
251 | 264 | "add_measure(circuit,f_layer_out_qubits,'reg')\n",
|
252 |
| - "\n", |
253 | 265 | "circuit.barrier()\n",
|
254 |
| - "#add measurement to your circuit if needed\n", |
255 |
| - "\n", |
256 | 266 | "\n",
|
257 | 267 | "circuit.draw('text',fold=300)\n",
|
258 | 268 | "\n"
|
|
270 | 280 | "source": [
|
271 | 281 | "### simulation\n"
|
272 | 282 | ],
|
273 |
| - "metadata": { |
274 |
| - "collapsed": false |
275 |
| - } |
| 283 | + "metadata": {} |
276 | 284 | },
|
277 | 285 | {
|
278 | 286 | "cell_type": "code",
|
|
283 | 291 | "######################################################\n",
|
284 | 292 | "\n",
|
285 | 293 | "\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", |
289 | 297 | "opt_class_prob=[]\n",
|
290 | 298 | "for b in range(bits):\n",
|
291 | 299 | " opt_class_prob.append(float(opt_mycount[b])/qc_shots)\n",
|
|
301 | 309 | "print(\"=\"*30)"
|
302 | 310 | ],
|
303 | 311 | "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": [], |
340 | 312 | "metadata": {}
|
341 | 313 | }
|
342 | 314 | ],
|
|
357 | 329 | "nbconvert_exporter": "python",
|
358 | 330 | "file_extension": ".py"
|
359 | 331 | },
|
360 |
| - "pycharm": { |
361 |
| - "stem_cell": { |
362 |
| - "cell_type": "raw", |
363 |
| - "source": [], |
364 |
| - "metadata": { |
365 |
| - "collapsed": false |
366 |
| - } |
367 |
| - } |
368 |
| - }, |
369 | 332 | "interpreter": {
|
370 | 333 | "hash": "f24048f0d5bdb0ff49c5e7c8a9899a65bc3ab13b0f32660a2227453ca6b95fd8"
|
371 | 334 | }
|
|
0 commit comments