Skip to content

Commit

Permalink
finalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
weiwenjiang committed Oct 7, 2021
1 parent 61a28d2 commit 2f9f552
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 163 deletions.
179 changes: 71 additions & 108 deletions session_4/EX_6_QF_MixNN_V_U.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"source": [
"is_colab = False\n",
"is_colab = True\n",
"import sys\n",
"if is_colab:\n",
" !pip install -q torch==1.9.0\n",
Expand All @@ -35,10 +35,10 @@
"\n",
"\n",
"import torch\n",
"from qiskit import QuantumCircuit, ClassicalRegister\n",
"from qiskit import QuantumCircuit, ClassicalRegister\n",
"import numpy as np\n",
"import functools\n",
"from qfnn.qf_fb.q_output import fire_ibmq,analyze,add_measure\n",
"from qfnn.qf_fb.q_output import fire_ibmq, analyze, add_measure\n",
"from qfnn.qf_circ.f_lyr_circ import F_LYR_Circ\n",
"from qfnn.qf_circ.v_lyr_circ import V_LYR_Circ\n",
"from qfnn.qf_net.utils import binarize\n",
Expand All @@ -53,11 +53,11 @@
"# remember to change the path on your computer\n",
"######################################################\n",
"if is_colab:\n",
" data_path = '/content/data' #mnist path\n",
" resume_path = '/content/model/v16_u2/model_best.tar' #model path\n",
" data_path = '/content/data' # mnist path\n",
" resume_path = '/content/model/v16_u2/model_best.tar' # model path\n",
"else:\n",
" data_path = '/home/hzr/Software/quantum/qc_mnist/pytorch/data' #mnist path\n",
" resume_path = '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar' #model path"
" data_path = '/home/hzr/Software/quantum/qc_mnist/pytorch/data' # mnist path\n",
" resume_path = '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar' # model path"
],
"outputs": [],
"metadata": {
Expand All @@ -69,14 +69,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"source": [
"################ Zhirui on 12-30-2020 ################\n",
"# Parameters of settings\n",
"######################################################\n",
"interest_num = [3,6]\n",
"img_size = 4\n",
"batch_size = 1# how many samples per batch to load\n",
"batch_size = 1 # how many samples per batch to load\n",
"inference_batch_size = 1\n",
"isppd = False #is prepared data\n",
"\n"
Expand All @@ -93,9 +93,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"source": [
"train_loader, test_loader = load_data(interest_num,data_path,isppd,img_size,batch_size,inference_batch_size,False)\n",
"train_loader, test_loader = load_data(interest_num, data_path, isppd, img_size, batch_size,inference_batch_size,False)\n",
"\n",
"\n",
"for batch_idx, (data, target) in enumerate(test_loader):\n",
Expand All @@ -104,23 +104,7 @@
" quantum_matrix = to_quantum_matrix(data)\n",
" break"
],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"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",
" return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Batch Id: 0, Target: tensor([1])\n"
]
}
],
"outputs": [],
"metadata": {}
},
{
Expand All @@ -139,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"source": [
"################ hzr on 12-30-2020 ################\n",
"# Get the parameters of the trained model\n",
Expand All @@ -152,29 +136,47 @@
"theta = checkpoint['state_dict']['fc0.theta']\n",
"weight = checkpoint['state_dict']['fc1.weight']\n"
],
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar'",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mFileNotFoundError\u001B[0m Traceback (most recent call last)",
"\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",
"\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",
"\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",
"\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",
"\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: '/home/hzr/Software/quantum/QuantumFlow_Tutorial/model/v16_u2/model_best.tar'"
]
}
],
"outputs": [],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"### classical inference"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"neural_in_layers = 'v:16,u:2'\n",
"layers = []\n",
"for item1 in neural_in_layers.split(\",\"):\n",
" x= item1.split(\":\")\n",
" layer =[]\n",
" layer.append(x[0].strip())\n",
" layer.append(int(x[1].strip()))\n",
" layers.append(layer)\n",
"print(layers)\n",
"\n",
"img_size = 4\n",
"model = Net(img_size,layers,False,False)\n",
"model.load_state_dict(checkpoint[\"state_dict\"])\n",
"to_quantum_data = ToQuantumData(img_size)\n",
"output_data = to_quantum_data(data)\n",
"# output = model.forward(output_data,False)\n",
"output = model(output_data, False)\n",
"print(\"classical inference result:\",output)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
Expand Down Expand Up @@ -206,27 +208,37 @@
{
"cell_type": "markdown",
"source": [
"### build the network"
"### build the network on Quantum Circuit"
],
"metadata": {}
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"################ hzr on 12-30-2020 ################\n",
"# Generate the circuit of v-layer\n",
"######################################################\n",
"\n",
"#define your input and repeat number\n",
"# define your input and repeat number\n",
"vqc = V_LYR_Circ(4,2)\n",
"#add v-layer to your circuit\n",
"vqc.forward(circuit,inputs,'v10',np.array(theta,dtype=np.double))\n",
"# add v-layer to your circuit\n",
"vqc.forward(circuit, inputs, 'v10', np.array(theta, dtype=np.double))\n",
"\n",
"circuit.draw('text',fold=300)"
],
"outputs": [],
"metadata": {}
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
Expand All @@ -248,11 +260,9 @@
"#add f-layer to your circuit\n",
"f_layer.forward(circuit,binarize(weight),inputs,f_layer_out_qubits,None,aux)\n",
"\n",
"#add measurement to your circuit if needed\n",
"add_measure(circuit,f_layer_out_qubits,'reg')\n",
"\n",
"circuit.barrier()\n",
"#add measurement to your circuit if needed\n",
"\n",
"\n",
"circuit.draw('text',fold=300)\n",
"\n"
Expand All @@ -270,9 +280,7 @@
"source": [
"### simulation\n"
],
"metadata": {
"collapsed": false
}
"metadata": {}
},
{
"cell_type": "code",
Expand All @@ -283,9 +291,9 @@
"######################################################\n",
"\n",
"\n",
"qc_shots=8192\n",
"opt_counts = fire_ibmq(circuit,qc_shots,True)\n",
"(opt_mycount,bits) = analyze(opt_counts)\n",
"qc_shots= 10000 # 8192\n",
"opt_counts = fire_ibmq(circuit, qc_shots, True)\n",
"(opt_mycount, bits) = analyze(opt_counts)\n",
"opt_class_prob=[]\n",
"for b in range(bits):\n",
" opt_class_prob.append(float(opt_mycount[b])/qc_shots)\n",
Expand All @@ -301,42 +309,6 @@
"print(\"=\"*30)"
],
"outputs": [],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"### classical inference"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"\n",
"neural_in_layers = 'v:16,u:2'\n",
"layers = []\n",
"for item1 in neural_in_layers.split(\",\"):\n",
" x= item1.split(\":\")\n",
" layer =[]\n",
" layer.append(x[0].strip())\n",
" layer.append(int(x[1].strip()))\n",
" layers.append(layer)\n",
"\n",
"model = Net(img_size,layers,False,False)\n",
"model.load_state_dict(checkpoint[\"state_dict\"])\n",
"to_quantum_data = ToQuantumData(img_size)\n",
"output_data = to_quantum_data(data)\n",
"output = model.forward(output_data,False)\n",
"print(\"classical inference result:\",output)"
],
"outputs": [],
"metadata": {}
}
],
Expand All @@ -357,15 +329,6 @@
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
}
},
"interpreter": {
"hash": "f24048f0d5bdb0ff49c5e7c8a9899a65bc3ab13b0f32660a2227453ca6b95fd8"
}
Expand Down
Loading

0 comments on commit 2f9f552

Please sign in to comment.