{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Well-Studied Task Execution Quality Metrics\n", "\n", "This tutorial covers benchmarks for algorithm-specific and task-specific metrics." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from qcmet import QFT\n", "from qcmet.devices import IdealSimulator, NoisySimulator, AerSimulator\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The well-studied task execution quality metrics consist of:\n", "\n", "- Variational quantum eigensolver (VQE)\n", " - Specific implementation for the 1D Fermi-Hubbard model (VQE1DFermiHubbard)\n", "\n", "- Hamiltonian simulation\n", " - Specific implementation for the 1D Fermi-Hubbard model (Simulation1DFermiHubbard)\n", "\n", "- Quantum fourier transform (QFT)\n", "\n", "- QScore\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quantum Fourier Transform (QFT)\n", "\n", "The QFT benchmark measures how well a quantum computer can implement the Quantum Fourier Transform algorithm.\n", "\n", "The QFT is a key subroutine in many quantum algorithms including:\n", "- Shor's algorithm for factoring\n", "- Quantum phase estimation\n", "- Hidden subgroup problem solutions" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated 1 QFT circuits\n" ] } ], "source": [ "# Setup QFT benchmark\n", "qft = QFT(\n", " qubits=4, # QFT on 4 qubits\n", ")\n", "\n", "# Generate circuits\n", "qft.generate_circuits()\n", "print(f\"Generated {len(qft.circuits)} QFT circuits\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "QFT circuit structure:\n", " ┌───┐┌───┐ »\n", " q_0: ┤ X ├┤ H ├─■────────■─────────────■──────────────────────────────»\n", " ├───┤└───┘ │P(π/2) │ ┌───┐ │ »\n", " q_1: ┤ X ├──────■────────┼───────┤ H ├─┼────────■────────■────────────»\n", " ├───┤ │P(π/4) └───┘ │ │P(π/2) │ ┌───┐»\n", " q_2: ┤ X ├───────────────■─────────────┼────────■────────┼───────┤ H ├»\n", " └───┘ │P(π/8) │P(π/4) └───┘»\n", " q_3: ──────────────────────────────────■─────────────────■────────────»\n", " »\n", "meas: 4/═════════════════════════════════════════════════════════════════»\n", " »\n", "« ┌───────┐ »\n", "« q_0: ───────────────X──┤ Rz(π) ├─────X───────────────────────────────»\n", "« │ ┌┴───────┴┐ │ »\n", "« q_1: ───────────X───┼─┤ Rz(π/2) ├─X──┼──────────────────────■────────»\n", "« │ │ ├─────────┤ │ │ ┌───┐ │ »\n", "« q_2: ─■─────────X───┼─┤ Rz(π/4) ├─X──┼───────■────────┤ H ├─┼────────»\n", "« │P(π/2) ┌───┐ │ ├─────────┤ │ ┌───┐ │P(-π/2) └───┘ │P(-π/4) »\n", "« q_3: ─■───────┤ H ├─X─┤ Rz(π/8) ├────X─┤ H ├─■──────────────■────────»\n", "« └───┘ └─────────┘ └───┘ »\n", "«meas: 4/════════════════════════════════════════════════════════════════»\n", "« »\n", "« ┌───┐ ░ ┌─┐ \n", "« q_0: ───────────■──────────────■─────────■────────┤ H ├─░─┤M├─────────\n", "« │ ┌───┐ │ │P(-π/2) └───┘ ░ └╥┘┌─┐ \n", "« q_1: ─■─────────┼────────┤ H ├─┼─────────■──────────────░──╫─┤M├──────\n", "« │P(-π/2) │ └───┘ │P(-π/4) ░ ║ └╥┘┌─┐ \n", "« q_2: ─■─────────┼──────────────■────────────────────────░──╫──╫─┤M├───\n", "« │P(-π/8) ░ ║ ║ └╥┘┌─┐\n", "« q_3: ───────────■───────────────────────────────────────░──╫──╫──╫─┤M├\n", "« ░ ║ ║ ║ └╥┘\n", "«meas: 4/══════════════════════════════════════════════════════╩══╩══╩══╩═\n", "« 0 1 2 3 \n" ] } ], "source": [ "# Look at QFT circuit\n", "print(\"QFT circuit structure:\")\n", "print(qft.circuits[0])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "QFT Results:\n", " fidelity: [1.0]\n", " normalized_fidelity: [1.0]\n" ] } ], "source": [ "# Run on device\n", "device = AerSimulator()\n", "qft.run(device, num_shots=2048)\n", "\n", "# Analyze\n", "results_qft = qft.analyze()\n", "print(\"\\nQFT Results:\")\n", "for key, value in results_qft.items():\n", " print(f\" {key}: {value}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparing QFT Performance Across Simulators" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Testing QFT on Ideal simulator...\n", " Fidelity: [1.0]\n", "\n", "Testing QFT on Noisy simulator...\n", " Fidelity: [0.9926757812499999]\n" ] } ], "source": [ "# Test QFT on different devices\n", "devices = {\n", " 'Ideal': IdealSimulator(),\n", " 'Noisy': NoisySimulator(),\n", "}\n", "\n", "qft_comparison = {}\n", "\n", "for device_name, device in devices.items():\n", " print(f\"\\nTesting QFT on {device_name} simulator...\")\n", "\n", " qft_test = QFT(qubits=4)\n", " qft_test.generate_circuits()\n", " qft_test.run(device, num_shots=2048)\n", " result = qft_test.analyze()\n", "\n", " qft_comparison[device_name] = result\n", " print(f\" Fidelity: {result.get('fidelity', 'N/A')}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What the Numbers Mean\n", "\n", "**Fidelity**: Measure of how close the output state from your real QFT circuit is to the ideal QFT output\n", "- The higher the fidelity, the better the performance of QFT by the device.\n", "\n", "**Normalised Fidelity**: Measure of how close the output state from your real QFT circuit is to the ideal QFT output normalized against a fully depolarized output distribution.\n", "- The higher the normalised fidelity, the better the performance of QFT by the device.\n", "- F will be between 0 and 1. \n", "- F = 1 indicates that the output states are identical to the ideal states.\n", "- F = 0 indicates that the output states are orthogonal to the ideal states.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other Benchmarks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "QCMet furthermore implements the QScore, VQE (1D Fermi-Hubbard), and Hamiltonian simulation (1D Fermi-Hubbard) as well studied task benchmarks. See the respective implementations for further details." ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.10" } }, "nbformat": 4, "nbformat_minor": 4 }