MyRepo / QSVC (1).ipynb
QSVC (1).ipynb
Raw
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5112e9dc-6cd4-4976-95ee-26cda7cf6c4b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Classical SVC Accuracy: 0.45\n",
      "QSVC Classification Accuracy: 0.85\n"
     ]
    }
   ],
   "source": [
    "from qiskit_machine_learning.kernels import FidelityQuantumKernel\n",
    "from qiskit.circuit.library import ZZFeatureMap\n",
    "from qiskit.algorithms.state_fidelities import ComputeUncompute\n",
    "from qiskit.primitives import Sampler\n",
    "from qiskit_machine_learning.algorithms import QSVC\n",
    "from sklearn.svm import SVC\n",
    "from qiskit_machine_learning.datasets import ad_hoc_data\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "adhoc_dimension = 3  # Change to 2 or 3\n",
    "\n",
    "# Generate the ad hoc dataset\n",
    "train_features, train_labels, test_features, test_labels, _ = ad_hoc_data(\n",
    "    training_size=100,\n",
    "    test_size=10,\n",
    "    n=adhoc_dimension,\n",
    "    gap=0.3,\n",
    "    plot_data=False,\n",
    "    one_hot=False,\n",
    "    include_sample_total=True,\n",
    ")\n",
    "\n",
    "# Classical SVC\n",
    "svc_classifier = SVC(kernel='linear')\n",
    "svc_classifier.fit(train_features, train_labels)\n",
    "svc_predictions = svc_classifier.predict(test_features)\n",
    "svc_accuracy = accuracy_score(test_labels, svc_predictions)\n",
    "print(\"Classical SVC Accuracy:\", svc_accuracy)\n",
    "\n",
    "# Quantum SVC with FidelityQuantumKernel\n",
    "adhoc_feature_map = ZZFeatureMap(feature_dimension=adhoc_dimension, reps=2, entanglement=\"linear\")\n",
    "sampler = Sampler()\n",
    "fidelity = ComputeUncompute(sampler=sampler)\n",
    "adhoc_kernel = FidelityQuantumKernel(fidelity=fidelity, feature_map=adhoc_feature_map)\n",
    "\n",
    "# Reuse the same ad hoc feature map for both classical and quantum SVM\n",
    "qsvc = QSVC(quantum_kernel=adhoc_kernel)\n",
    "qsvc.fit(train_features, train_labels)\n",
    "qsvc_predictions = qsvc.predict(test_features)\n",
    "qsvc_accuracy = accuracy_score(test_labels, qsvc_predictions)\n",
    "print(\"QSVC Classification Accuracy:\", qsvc_accuracy)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "020b10065bc846f2a7ef4649d156debd": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_cd7352fed70f4cdcb0ffeec449b103a1",
       "style": "IPY_MODEL_1121fb3b8f8f46ffaeb96e2d6b6cff64",
       "value": "<h5>Message</h5>"
      }
     },
     "077ef63296a04aadab8f3a359104eca5": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_81ccf0fbb3d34a2ebb76ae56c4de2279",
       "style": "IPY_MODEL_b3fc554ba7dc4fdd9d9e8f95233f9aa9",
       "value": "<h5>Job ID</h5>"
      }
     },
     "1121fb3b8f8f46ffaeb96e2d6b6cff64": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "1f381e453eac4711b6ddf89d4e4592fe": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "56c701f487254feba60c06cbaaf16cb1": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "width": "145px"
      }
     },
     "636ecc3ab21040009a4e2ad1035badca": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "68d3253c2ef0452eae263075812c2adb": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "6a964afac4114d418f7140d6a65c3ebe": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_077ef63296a04aadab8f3a359104eca5",
        "IPY_MODEL_962282d76059499bb08ad09a3613a836",
        "IPY_MODEL_e57c69a8a0344f42946a001b3466cbaf",
        "IPY_MODEL_6d23f5ec95154000937f71c50e677458",
        "IPY_MODEL_020b10065bc846f2a7ef4649d156debd"
       ],
       "layout": "IPY_MODEL_b8b0986531ca4ecbaee60f86b96c4d76"
      }
     },
     "6d23f5ec95154000937f71c50e677458": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_6d7e3675e9924261a761a50153c2d9e4",
       "style": "IPY_MODEL_636ecc3ab21040009a4e2ad1035badca",
       "value": "<h5>Queue</h5>"
      }
     },
     "6d7e3675e9924261a761a50153c2d9e4": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "width": "70px"
      }
     },
     "81ccf0fbb3d34a2ebb76ae56c4de2279": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "width": "190px"
      }
     },
     "9172304d47b9402dab5b2208a269615b": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "GridBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_de242590eef243dda569e6704ab6e684"
       ],
       "layout": "IPY_MODEL_c7cdfe6317054360907806ea3376de7f"
      }
     },
     "962282d76059499bb08ad09a3613a836": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_56c701f487254feba60c06cbaaf16cb1",
       "style": "IPY_MODEL_1f381e453eac4711b6ddf89d4e4592fe",
       "value": "<h5>Backend</h5>"
      }
     },
     "9ce68d077eb54438a8f0d25b79fad252": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "ButtonStyleModel",
      "state": {
       "font_family": null,
       "font_size": null,
       "font_style": null,
       "font_variant": null,
       "font_weight": null,
       "text_color": null,
       "text_decoration": null
      }
     },
     "b3fc554ba7dc4fdd9d9e8f95233f9aa9": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "b8b0986531ca4ecbaee60f86b96c4d76": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "margin": "0px 0px 0px 37px",
       "width": "600px"
      }
     },
     "c7cdfe6317054360907806ea3376de7f": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "grid_template_areas": "\n                                       \". . . . right \"\n                                        ",
       "grid_template_columns": "20% 20% 20% 20% 20%",
       "width": "100%"
      }
     },
     "cd7352fed70f4cdcb0ffeec449b103a1": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "d14eb33689ad405489db9b156e260a84": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "de242590eef243dda569e6704ab6e684": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "ButtonModel",
      "state": {
       "button_style": "primary",
       "description": "Clear",
       "layout": "IPY_MODEL_feabff71af684f8c8013e2a123728bca",
       "style": "IPY_MODEL_9ce68d077eb54438a8f0d25b79fad252",
       "tooltip": null
      }
     },
     "df32baf4cd974c639427c4439911966e": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "width": "95px"
      }
     },
     "dfbe6cc96a2f4aa9935d8d49cebb6b19": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "margin": "0px 0px 10px 0px"
      }
     },
     "e57c69a8a0344f42946a001b3466cbaf": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_df32baf4cd974c639427c4439911966e",
       "style": "IPY_MODEL_d14eb33689ad405489db9b156e260a84",
       "value": "<h5>Status</h5>"
      }
     },
     "e70fb09cd91f49349724f3393b835657": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_dfbe6cc96a2f4aa9935d8d49cebb6b19",
       "style": "IPY_MODEL_68d3253c2ef0452eae263075812c2adb",
       "value": "<p style='font-family: IBM Plex Sans, Arial, Helvetica, sans-serif; font-size: 20px; font-weight: medium;'>Circuit Properties</p>"
      }
     },
     "feabff71af684f8c8013e2a123728bca": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "grid_area": "right",
       "padding": "0px 0px 0px 0px",
       "width": "70px"
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}