Skip to content

Commit

Permalink
improved version after CR1
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadav138 committed Jun 26, 2024
1 parent 330453a commit b2f228e
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions tutorials/QML_with_Classiq/QML_with_Classiq.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
"id": "be6ccf91-07a9-42a3-8945-33353d608ab3",
"metadata": {},
"source": [
"Welcome to the \"Quantum Machine Learning with Classiq\" tutorial. This guide is designed for users already familiar with the Classiq platform, aiming to explore the exciting realm of Quantum Machine Learning (QML) using Classiq. In this tutorial, we will cover three main methods to implement QML with Classiq:\n",
"Welcome to the \"Quantum Machine Learning with Classiq\" tutorial. This guide is designed for users already familiar with the fundamentals of the Classiq platform and Quantum Machine Learning (QML) concepts. Our aim is to showcase how to implement QML using Classiq. In this tutorial, we will cover three main methods to implement QML with Classiq:\n",
"\n",
"1. **Using the VQE Primitive**\n",
"2. **Using the PyTorch Integration**\n",
"3. **Using the QSVM Built-in App**\n",
"\n",
"\n",
"Each section will provide a brief explanation of the method followed by a simple, illustrative example to demonstrate the integration. /\n",
"Each section will provide a brief explanation of the method followed by a simple, illustrative example to demonstrate the integration. \\\n",
"These examples are intended to be straightforward to help you get started quickly."
]
},
Expand Down Expand Up @@ -97,7 +96,7 @@
"Let's start with a simple example by creating a VQE algorithm that estimates the minimal eigenvalue of the following 2x2 matrix:\n",
"\n",
"<body><center>\n",
"$ \\begin{bmatrix} 1 & -1 \\\\ -1 & 0 \\end{bmatrix} = \\frac{1}{2}I + \\frac{1}{2}Z - X $\n",
"$ H = \\frac{1}{2}I + \\frac{1}{2}Z - X = \\begin{bmatrix} 1 & -1 \\\\ -1 & 0 \\end{bmatrix} $\n",
"</center><body>"
]
},
Expand All @@ -111,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"id": "8601b362-1488-4a91-a18e-9f9a1185267a",
"metadata": {},
"outputs": [],
Expand All @@ -131,7 +130,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "aacdd2c8-b92e-4cfa-8314-ff4a9d2a5fef",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -182,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "e99e3173-0f36-4ad1-91e0-766fa680b2cc",
"metadata": {},
"outputs": [],
Expand All @@ -203,7 +202,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"id": "452706db-e41f-4d02-93aa-722c3b652267",
"metadata": {},
"outputs": [],
Expand All @@ -224,6 +223,14 @@
" save({\"result\": res})"
]
},
{
"cell_type": "markdown",
"id": "2cb58910-b98f-479a-959b-084b567effdb",
"metadata": {},
"source": [
"Note that for our `cmain` function, we are using the `@cfunc` decorator rather than `@qfunc` as we commonly do in the SDK."
]
},
{
"cell_type": "markdown",
"id": "ce9d46d1-8906-420d-b08e-ac9bf5747acb",
Expand Down Expand Up @@ -263,7 +270,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "aa96096f-f2b3-4648-9958-f6c68749f00f",
"metadata": {},
"outputs": [],
Expand All @@ -282,15 +289,15 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"id": "0a491c86-ba25-4099-a7dd-eeb13ae71c40",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/fc2a6eeb-dc3d-4264-8b61-6760ae2612bb?version=0.42.2\n"
"Opening: https://platform.classiq.io/circuit/49d3bfcb-b4f3-45e7-a8b8-9dc77e3ef56c?version=0.42.2\n"
]
}
],
Expand All @@ -308,14 +315,15 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"id": "8d6dc1f0-ad23-4f11-8c64-a827f0ebe97f",
"metadata": {},
"outputs": [],
"source": [
"res = execute(qprog)\n",
"# res.open_in_ide()\n",
"vqe_result = res.result()[0].value"
"vqe_result = res.result()[0].value\n",
"last_std_dev = vqe_result.intermediate_results[-1].standard_deviation"
]
},
{
Expand All @@ -328,22 +336,24 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"id": "f3bdc0d5-bd82-4797-b449-b7494101163b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimal energy: -0.6240234375\n",
"Optimal parameters: {'angles_0': 4.231154537560012, 'angles_1': 3.147445806511723, 'angles_2': 2.7306574045708336}\n",
"Eigenstate: {'0': (0.516334738808072+0j), '1': (0.856386850377795+0j)}\n"
"Optimal energy: -0.603515625\n",
"Standard Deviation of the last iteration: 0.5886752446817956\n",
"Optimal parameters: {'angles_0': -1.911377748595144, 'angles_1': 3.16467642787642, 'angles_2': -1.4600485696049208}\n",
"Eigenstate: {'1': (0.8172935519138763+0j), '0': (0.5762215285808054+0j)}\n"
]
}
],
"source": [
"print(f\"Optimal energy: {vqe_result.energy}\")\n",
"print(f\"Standard Deviation of the last iteration: {last_std_dev}\")\n",
"print(f\"Optimal parameters: {vqe_result.optimal_parameters}\")\n",
"print(f\"Eigenstate: {vqe_result.eigenstate}\")"
]
Expand Down

0 comments on commit b2f228e

Please sign in to comment.