{ "cells": [ { "cell_type": "markdown", "id": "2d51c41e", "metadata": {}, "source": [ "# 21 - Backend Comparison\n", "\n", "Compare the local simulator with the Aer backend.\n", "\n", "**Concepts:** Backend routing, qs.run, performance, consistency" ] }, { "cell_type": "code", "execution_count": null, "id": "1d873f7d", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs\n", "import time" ] }, { "cell_type": "markdown", "id": "ab3d7437", "metadata": {}, "source": [ "## Local Simulator" ] }, { "cell_type": "code", "execution_count": null, "id": "0f3c96ce", "metadata": {}, "outputs": [], "source": [ "circuit = qs.Circuit(4, name=\"test\")\n", "circuit.h(0).cx(0, 1).cx(1, 2).cx(2, 3).measure_all()\n", "\n", "# Run on local simulator (default)\n", "t0 = time.perf_counter()\n", "result_local = qs.run(circuit, shots=4000, seed=42, backend=\"local\")\n", "t_local = time.perf_counter() - t0\n", "\n", "print(\"Local Simulator Results:\")\n", "print(f\" Time: {t_local*1000:.1f} ms\")\n", "print(f\" Counts: {result_local.counts}\")\n", "print(f\" Most likely: {result_local.most_likely}\")" ] }, { "cell_type": "markdown", "id": "5f806028", "metadata": {}, "source": [ "## Aer Backend (if available)" ] }, { "cell_type": "code", "execution_count": null, "id": "201206ef", "metadata": {}, "outputs": [], "source": [ "try:\n", " t0 = time.perf_counter()\n", " result_aer = qs.run(circuit, shots=4000, seed=42, backend=\"aer\")\n", " t_aer = time.perf_counter() - t0\n", " \n", " print(\"Aer Backend Results:\")\n", " print(f\" Time: {t_aer*1000:.1f} ms\")\n", " print(f\" Counts: {result_aer.counts}\")\n", " print(f\" Most likely: {result_aer.most_likely}\")\n", "except Exception as e:\n", " print(f\"Aer backend not available: {e}\")\n", " print(\"Install: pip install 'quantsdk[ibm]'\")" ] }, { "cell_type": "markdown", "id": "7baa3f41", "metadata": {}, "source": [ "## Consistency Check" ] }, { "cell_type": "code", "execution_count": null, "id": "21d43f83", "metadata": {}, "outputs": [], "source": [ "# Both backends should give consistent results for simple circuits\n", "test_circuits = [\n", " (\"H|0>\", qs.Circuit(1).h(0).measure(0)),\n", " (\"Bell\", qs.Circuit(2).h(0).cx(0, 1).measure_all()),\n", " (\"GHZ-3\", qs.Circuit(3).h(0).cx(0, 1).cx(1, 2).measure_all()),\n", "]\n", "\n", "for name, circ in test_circuits:\n", " r = qs.run(circ, shots=4000, seed=42, backend=\"local\")\n", " print(f\"{name:8s}: {r.most_likely} | top_2={r.top_k(2)}\")" ] }, { "cell_type": "markdown", "id": "3ff29c15", "metadata": {}, "source": [ "## Scaling Benchmark" ] }, { "cell_type": "code", "execution_count": null, "id": "89698f8f", "metadata": {}, "outputs": [], "source": [ "# How does the local simulator scale with qubit count?\n", "for n in [4, 8, 12, 16, 20]:\n", " circuit = qs.Circuit(n)\n", " circuit.h(0)\n", " for i in range(n - 1):\n", " circuit.cx(i, i + 1)\n", " circuit.measure_all()\n", " \n", " t0 = time.perf_counter()\n", " result = qs.run(circuit, shots=1000, seed=42, backend=\"local\")\n", " elapsed = time.perf_counter() - t0\n", " \n", " print(f\"n={n:2d}: {elapsed*1000:8.1f} ms | {result.most_likely[:8]}... | states=2^{n}={2**n}\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }