Skip to content
Merged
Changes from 7 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
84 changes: 57 additions & 27 deletions docs/tutorials/projected-quantum-kernels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "fe8a02d3-994a-45fe-823d-5e68ded0d717",
"metadata": {},
"outputs": [],
Expand All @@ -79,6 +79,8 @@
"\n",
"# Standard libraries\n",
"import os\n",
"import urllib.request\n",
"from pathlib import Path\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
Expand Down Expand Up @@ -153,7 +155,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "f1ce0222-e7c7-451c-a11c-e61425a6bb8e",
"metadata": {},
"outputs": [],
Expand All @@ -165,12 +167,10 @@
" # Read from the csv files\n",
" train_data = pd.read_csv(\n",
" os.path.join(dir_root, args[\"file_train_data\"]),\n",
" encoding=\"unicode_escape\",\n",
" sep=\",\",\n",
" )\n",
" test_data = pd.read_csv(\n",
" os.path.join(dir_root, args[\"file_test_data\"]),\n",
" encoding=\"unicode_escape\",\n",
" sep=\",\",\n",
" )\n",
"\n",
Expand Down Expand Up @@ -320,29 +320,52 @@
"execution_count": null,
"id": "84495ee1-880a-48cb-a904-d83396e8b29e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" train_data.csv ... OK (5,532 bytes)\n",
" test_data.csv ... OK (2,420 bytes)\n",
" projections_train.csv ... OK (779,903 bytes)\n",
" projections_test.csv ... OK (335,604 bytes)\n",
"\n",
"All files saved to data_tutorial/pqk/\n"
]
}
],
"source": [
"## Download dataset\n",
"\n",
"# Create data directory if it doesn't exist\n",
"!mkdir -p data_tutorial/pqk\n",
"\n",
"# Download the training and test sets from the official Qiskit documentation repo\n",
"!wget -q --show-progress -O data_tutorial/pqk/train_data.csv \\\n",
" https://raw.githubusercontent.com/Qiskit/documentation/main/datasets/tutorials/pqk/train_data.csv\n",
"def download_pqk_dataset(data_dir=\"data_tutorial/pqk\"):\n",
" \"\"\"Download the four CSV files from the Qiskit documentation repo.\"\"\"\n",
" data_dir = Path(data_dir)\n",
" data_dir.mkdir(parents=True, exist_ok=True)\n",
"\n",
"!wget -q --show-progress -O data_tutorial/pqk/test_data.csv \\\n",
" https://raw.githubusercontent.com/Qiskit/documentation/main/datasets/tutorials/pqk/test_data.csv\n",
" base_url = (\n",
" \"https://raw.githubusercontent.com/Qiskit/documentation/main/\"\n",
" \"datasets/tutorials/pqk\"\n",
" )\n",
" files = [\n",
" \"train_data.csv\",\n",
" \"test_data.csv\",\n",
" \"projections_train.csv\",\n",
" \"projections_test.csv\",\n",
" ]\n",
"\n",
" for filename in files:\n",
" url = f\"{base_url}/{filename}\"\n",
" dest = data_dir / filename\n",
" print(f\" {filename} ...\", end=\" \", flush=True)\n",
" urllib.request.urlretrieve(url, dest)\n",
" print(f\"OK ({dest.stat().st_size:,} bytes)\")\n",
"\n",
"!wget -q --show-progress -O data_tutorial/pqk/projections_train.csv \\\n",
" https://raw.githubusercontent.com/Qiskit/documentation/main/datasets/tutorials/pqk/projections_train.csv\n",
" print(f\"\\nAll files saved to {data_dir}/\")\n",
" return data_dir\n",
"\n",
"!wget -q --show-progress -O data_tutorial/pqk/projections_test.csv \\\n",
" https://raw.githubusercontent.com/Qiskit/documentation/main/datasets/tutorials/pqk/projections_test.csv\n",
"\n",
"# Check the files have been downloaded\n",
"!echo \"Dataset files downloaded:\"\n",
"!ls -lh data_tutorial/pqk/*.csv"
"DATA_DIR = download_pqk_dataset()"
]
},
{
Expand Down Expand Up @@ -371,7 +394,9 @@
" \"min_label_value\": -1,\n",
" \"encoder\": \"one-hot\",\n",
"}\n",
"dir_root = \"./\"\n",
"\n",
"# dir_root points to the folder where the downloaded CSVs live\n",
"dir_root = str(DATA_DIR)\n",
"\n",
"# Preprocess data\n",
"train_data, test_data, train_labels, test_labels, num_class, num_motifs = (\n",
Expand All @@ -394,7 +419,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "d40d9a0f-67d0-4704-8a94-cc0a466ffc92",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -712,7 +737,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "7a56eda8-3fb2-43e9-8a82-ec64be05b699",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -1191,6 +1216,11 @@
"metadata": {},
"outputs": [],
"source": [
"# ---------------------------------------------------------------------------\n",
"# Load projections — either from the hardware run above, or from the\n",
"# pre-computed CSVs that were downloaded alongside the motif data.\n",
"# ---------------------------------------------------------------------------\n",
"\n",
"if run_experiment:\n",
" projections_train = np.array(projections_train).reshape(\n",
" len(projections_train), -1\n",
Expand All @@ -1199,8 +1229,8 @@
" len(projections_test), -1\n",
" )\n",
"else:\n",
" projections_train = np.loadtxt(\"projections_train.txt\")\n",
" projections_test = np.loadtxt(\"projections_test.txt\")"
" projections_train = np.loadtxt(DATA_DIR / \"projections_train.csv\")\n",
" projections_test = np.loadtxt(DATA_DIR / \"projections_test.csv\")"
]
},
{
Expand Down Expand Up @@ -1520,6 +1550,7 @@
}
],
"metadata": {
"hours": 2.5,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
Expand All @@ -1537,15 +1568,14 @@
"pygments_lexer": "ipython3",
"version": "3"
},
"qpuSeconds": 4800,
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
},
"hours": 2.5,
"qpuSeconds": 4800
}
},
"nbformat": 4,
"nbformat_minor": 4
Expand Down
Loading