Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions algorithms/QML/quantum_autoencoder/quantum_autoencoder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,12 @@
"id": "40",
"metadata": {},
"outputs": [],
"source": "trained_w = encoder_train_network.qlayer.weight.tolist()\ninput_data = train_dataset.data.tolist()\nbatch_data = [{\"w\": trained_w, \"input_data\": data} for data in input_data]\nresults_validator = sample(qprog_validator, parameters=batch_data)"
"source": [
"trained_w = encoder_train_network.qlayer.weight.tolist()\n",
"input_data = train_dataset.data.tolist()\n",
"batch_data = [{\"w\": trained_w, \"input_data\": data} for data in input_data]\n",
"results_validator = sample(qprog_validator, parameters=batch_data)"
]
},
{
"cell_type": "markdown",
Expand All @@ -685,7 +690,12 @@
]
}
],
"source": "for data, res in zip(input_data, results_validator):\n df = res\n output = df.loc[df[\"probability\"].idxmax(), \"decoded\"]\n print(\"input =\", data, \", output =\", output)"
"source": [
"for data, res in zip(input_data, results_validator):\n",
" df = res\n",
" output = df.loc[df[\"probability\"].idxmax(), \"decoded\"]\n",
" print(\"input =\", data, \", output =\", output)"
]
},
{
"cell_type": "markdown",
Expand All @@ -709,7 +719,21 @@
"id": "45",
"metadata": {},
"outputs": [],
"source": "import random\n\ninput_anomaly_data = [\n [0, 0, 1, 1],\n [0, 0, 0, 1],\n [0, 1, 1, 1],\n [1, 0, 1, 0],\n [1, 1, 1, 1],\n]\nrandom.shuffle(input_anomaly_data)\n\nbatch_data = [{\"w\": trained_w, \"input_data\": data} for data in input_anomaly_data]\nresults_anomaly = sample(qprog_ae_network, parameters=batch_data)"
"source": [
"import random\n",
"\n",
"input_anomaly_data = [\n",
" [0, 0, 1, 1],\n",
" [0, 0, 0, 1],\n",
" [0, 1, 1, 1],\n",
" [1, 0, 1, 0],\n",
" [1, 1, 1, 1],\n",
"]\n",
"random.shuffle(input_anomaly_data)\n",
"\n",
"batch_data = [{\"w\": trained_w, \"input_data\": data} for data in input_anomaly_data]\n",
"results_anomaly = sample(qprog_ae_network, parameters=batch_data)"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -737,7 +761,17 @@
]
}
],
"source": "tolerance = 1e-2\nfor data, res in zip(input_anomaly_data, results_anomaly):\n # The probabiliy of the test qubit\n alpha_sqaured = res.loc[res[\"test\"] == 0, \"counts\"].sum() / num_shots\n output = 1 - alpha_sqaured\n if abs(output) > tolerance:\n print(f\"input= {data}, loss= {output} ----> ANOMALY DETECTED\")\n else:\n print(f\"input= {data}, loss= {output}\")"
"source": [
"tolerance = 1e-2\n",
"for data, res in zip(input_anomaly_data, results_anomaly):\n",
" # The probabiliy of the test qubit\n",
" alpha_sqaured = res.loc[res[\"test\"] == 0, \"counts\"].sum() / num_shots\n",
" output = 1 - alpha_sqaured\n",
" if abs(output) > tolerance:\n",
" print(f\"input= {data}, loss= {output} ----> ANOMALY DETECTED\")\n",
" else:\n",
" print(f\"input= {data}, loss= {output}\")"
]
},
{
"cell_type": "markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@
"id": "17",
"metadata": {},
"outputs": [],
"source": "result = sample(qprog_qpe)"
"source": [
"result = sample(qprog_qpe)"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -305,7 +307,14 @@
"output_type": "display_data"
}
],
"source": "import matplotlib.pyplot as plt\n\nphases_counts = dict(zip(result[\"phase_reg\"], result[\"counts\"]))\nplt.bar(phases_counts.keys(), phases_counts.values(), width=0.1)\nplt.xticks(rotation=90)\nprint(\"phase with max probability: \", max(phases_counts, key=phases_counts.get))"
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"phases_counts = dict(zip(result[\"phase_reg\"], result[\"counts\"]))\n",
"plt.bar(phases_counts.keys(), phases_counts.values(), width=0.1)\n",
"plt.xticks(rotation=90)\n",
"print(\"phase with max probability: \", max(phases_counts, key=phases_counts.get))"
]
},
{
"cell_type": "markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,14 @@
]
}
],
"source": "qprog = synthesize(main)\nshow(qprog)\n\n# Post-select qsvt_aux == 0 on the statevector simulator.\n# Note: filtering is possible for QBit and QNum, but not for QStruct.\nresult = calculate_state_vector(qprog, filters={\"qsvt_aux\": 0})"
"source": [
"qprog = synthesize(main)\n",
"show(qprog)\n",
"\n",
"# Post-select qsvt_aux == 0 on the statevector simulator.\n",
"# Note: filtering is possible for QBit and QNum, but not for QStruct.\n",
"result = calculate_state_vector(qprog, filters={\"qsvt_aux\": 0})"
]
},
{
"attachments": {
Expand Down Expand Up @@ -558,7 +565,11 @@
"output_type": "execute_result"
}
],
"source": "df = result\ndf_filtered = df[(df[\"qsvt_state.block\"] == 0)].sort_values(\"qsvt_state.state\")\ndf_filtered"
"source": [
"df = result\n",
"df_filtered = df[(df[\"qsvt_state.block\"] == 0)].sort_values(\"qsvt_state.state\")\n",
"df_filtered"
]
},
{
"cell_type": "markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,48 @@
"id": "4",
"metadata": {},
"outputs": [],
"source": "def get_be_state(rhs_vec, be_qfunc, block_size, data_size):\n \"\"\"\n Apply a block-encoding qfunc to an initial state and return the post-selected output.\n\n Parameters\n ----------\n rhs_vec : list[real]\n Amplitudes of the initial data state |\\psi>. Length must be 2**data_size.\n be_qfunc : qfunc\n A Qmod qfunc with signature be_qfunc(block: QNum, data: QNum) that applies\n the block-encoding for the matrix A/s.\n block_size : int\n Number of qubits in the block variable.\n data_size : int\n Number of qubits in the data variable.\n\n Returns\n -------\n array\n The post-selected data variable state equal to (A/s)|\\psi>, obtained\n by projecting the block register onto 0 after applying the block encoding.\n qprog\n Thr resulting quantum program\n \"\"\"\n\n @qfunc\n def main(\n data: Output[QNum[data_size]],\n block: Output[QNum[block_size]],\n ):\n\n allocate(block)\n prepare_amplitudes(rhs_vec, 0.0, data)\n be_qfunc(block, data)\n\n qprog = synthesize(main, preferences=Preferences(timeout_seconds=2000))\n # Post-select block == 0 on the statevector simulator.\n results = calculate_state_vector(qprog, filters={\"block\": 0})\n resulting_state = get_projected_state_vector(results, \"data\", data_size)\n return resulting_state, qprog"
"source": [
"def get_be_state(rhs_vec, be_qfunc, block_size, data_size):\n",
" \"\"\"\n",
" Apply a block-encoding qfunc to an initial state and return the post-selected output.\n",
"\n",
" Parameters\n",
" ----------\n",
" rhs_vec : list[real]\n",
" Amplitudes of the initial data state |\\psi>. Length must be 2**data_size.\n",
" be_qfunc : qfunc\n",
" A Qmod qfunc with signature be_qfunc(block: QNum, data: QNum) that applies\n",
" the block-encoding for the matrix A/s.\n",
" block_size : int\n",
" Number of qubits in the block variable.\n",
" data_size : int\n",
" Number of qubits in the data variable.\n",
"\n",
" Returns\n",
" -------\n",
" array\n",
" The post-selected data variable state equal to (A/s)|\\psi>, obtained\n",
" by projecting the block register onto 0 after applying the block encoding.\n",
" qprog\n",
" Thr resulting quantum program\n",
" \"\"\"\n",
"\n",
" @qfunc\n",
" def main(\n",
" data: Output[QNum[data_size]],\n",
" block: Output[QNum[block_size]],\n",
" ):\n",
"\n",
" allocate(block)\n",
" prepare_amplitudes(rhs_vec, 0.0, data)\n",
" be_qfunc(block, data)\n",
"\n",
" qprog = synthesize(main, preferences=Preferences(timeout_seconds=2000))\n",
" # Post-select block == 0 on the statevector simulator.\n",
" results = calculate_state_vector(qprog, filters={\"block\": 0})\n",
" resulting_state = get_projected_state_vector(results, \"data\", data_size)\n",
" return resulting_state, qprog"
]
},
{
"cell_type": "code",
Expand Down
Loading
Loading