{ "cells": [ { "cell_type": "markdown", "id": "f705780b", "metadata": {}, "source": [ "# 07 - Results & Visualization\n", "\n", "Deep dive into the Result object: counts, probabilities, histograms, DataFrames.\n", "\n", "**Concepts:** Result API, plotting, pandas, expectation values\n", "\n", "**Requires:** `pip install quantsdk[viz]`" ] }, { "cell_type": "code", "execution_count": null, "id": "d3a83311", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs" ] }, { "cell_type": "markdown", "id": "c4346d9e", "metadata": {}, "source": [ "## Basic Result Properties" ] }, { "cell_type": "code", "execution_count": null, "id": "49bf1190", "metadata": {}, "outputs": [], "source": [ "circuit = qs.Circuit(3).h(0).cx(0, 1).cx(0, 2).measure_all()\n", "result = qs.run(circuit, shots=5000, seed=42)\n", "\n", "print(f\"Counts: {result.counts}\")\n", "print(f\"Probabilities: {result.probabilities}\")\n", "print(f\"Most likely: {result.most_likely}\")\n", "print(f\"Shots: {result.shots}\")\n", "print(f\"Backend: {result.backend}\")" ] }, { "cell_type": "markdown", "id": "cfb9ad2c", "metadata": {}, "source": [ "## Top-K Results" ] }, { "cell_type": "code", "execution_count": null, "id": "ea7fe374", "metadata": {}, "outputs": [], "source": [ "# Get top-K most frequent outcomes\n", "print(\"Top 1:\", result.top_k(1))\n", "print(\"Top 2:\", result.top_k(2))" ] }, { "cell_type": "markdown", "id": "788b49a6", "metadata": {}, "source": [ "## Summary" ] }, { "cell_type": "code", "execution_count": null, "id": "a42aaefe", "metadata": {}, "outputs": [], "source": [ "print(result.summary())" ] }, { "cell_type": "markdown", "id": "b6ba4d0c", "metadata": {}, "source": [ "## Histogram Visualization\n", "\n", "Requires `matplotlib`." ] }, { "cell_type": "code", "execution_count": null, "id": "b2ec4e61", "metadata": {}, "outputs": [], "source": [ "# Plot a histogram of measurement results\n", "result.plot_histogram()" ] }, { "cell_type": "markdown", "id": "c5fbc16e", "metadata": {}, "source": [ "## Pandas DataFrame\n", "\n", "Requires `pandas`." ] }, { "cell_type": "code", "execution_count": null, "id": "3768f660", "metadata": {}, "outputs": [], "source": [ "df = result.to_pandas()\n", "print(df)\n", "print(f\"\\nDataFrame columns: {list(df.columns)}\")" ] }, { "cell_type": "markdown", "id": "efbe7f98", "metadata": {}, "source": [ "## Expectation Values" ] }, { "cell_type": "code", "execution_count": null, "id": "a53c5632", "metadata": {}, "outputs": [], "source": [ "# Z-basis expectation value for each qubit\n", "circuit = qs.Circuit(2).h(0).measure_all() # q0 in superposition, q1 in |0>\n", "result = qs.run(circuit, shots=5000, seed=42)\n", "\n", "# should be ~0 (superposition), should be ~1 (always |0>)\n", "exp_z0 = result.expectation_value(qubit=0)\n", "exp_z1 = result.expectation_value(qubit=1)\n", "print(f\" = {exp_z0:.4f} (expect ~0.0 for superposition)\")\n", "print(f\" = {exp_z1:.4f} (expect ~1.0 for |0>)\")" ] }, { "cell_type": "markdown", "id": "5c04137e", "metadata": {}, "source": [ "## Serialization" ] }, { "cell_type": "code", "execution_count": null, "id": "6d9b469b", "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "result_dict = result.to_dict()\n", "print(json.dumps(result_dict, indent=2))" ] }, { "cell_type": "markdown", "id": "1bd20a79", "metadata": {}, "source": [ "## Comparing Runs" ] }, { "cell_type": "code", "execution_count": null, "id": "cf00973b", "metadata": {}, "outputs": [], "source": [ "# Run the same circuit with different shot counts\n", "circuit = qs.Circuit(2).h(0).cx(0, 1).measure_all()\n", "\n", "for shots in [10, 100, 1000, 10000]:\n", " result = qs.run(circuit, shots=shots, seed=42)\n", " probs = result.probabilities\n", " p00 = probs.get('00', 0)\n", " p11 = probs.get('11', 0)\n", " print(f\"Shots={shots:5d}: P(00)={p00:.4f}, P(11)={p11:.4f}\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }