{ "cells": [ { "cell_type": "markdown", "id": "ccda62f3", "metadata": {}, "source": [ "# 14 - Quantum Phase Estimation\n", "\n", "Estimate the eigenvalue of a unitary operator.\n", "\n", "**Concepts:** Phase estimation, inverse QFT, eigenvalues" ] }, { "cell_type": "code", "execution_count": null, "id": "ce09b30c", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs\n", "import math" ] }, { "cell_type": "markdown", "id": "c9c2a4fb", "metadata": {}, "source": [ "## Phase Estimation for T Gate\n", "\n", "The T gate has eigenvalue $e^{i\\pi/4}$, so the phase is $\\phi = 1/8$.\n", "\n", "With 3 counting qubits we can represent $\\phi = 0.001_2 = 1/8$ exactly." ] }, { "cell_type": "code", "execution_count": null, "id": "59405cd1", "metadata": {}, "outputs": [], "source": [ "n_count = 3 # counting qubits\n", "circuit = qs.Circuit(n_count + 1, name=\"QPE-T\")\n", "\n", "# Prepare eigenstate |1> on target qubit\n", "circuit.x(n_count)\n", "\n", "# Hadamard on counting qubits\n", "for i in range(n_count):\n", " circuit.h(i)\n", "\n", "# Controlled-U^(2^k) operations (textbook convention: qubit 0 = MSB)\n", "# T gate: phase = pi/4, so T^(2^k) = Phase(pi/4 * 2^k)\n", "for k in range(n_count):\n", " power = n_count - 1 - k # qubit 0 gets highest power\n", " angle = math.pi / 4 * (2 ** power)\n", " circuit.cp(k, n_count, angle)\n", "\n", "# Inverse QFT on counting qubits\n", "circuit.swap(0, 2)\n", "circuit.h(0)\n", "circuit.cp(0, 1, -math.pi / 2)\n", "circuit.h(1)\n", "circuit.cp(0, 2, -math.pi / 4)\n", "circuit.cp(1, 2, -math.pi / 2)\n", "circuit.h(2)\n", "\n", "# Measure counting qubits\n", "for i in range(n_count):\n", " circuit.measure(i)\n", "\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"QPE for T gate (phase = 1/8):\")\n", "print(f\"Result: {result.counts}\")\n", "print(f\"Most likely: {result.most_likely}\")\n", "\n", "# Convert binary to phase\n", "measured = result.most_likely\n", "phase = int(measured, 2) / (2 ** n_count)\n", "print(f\"Estimated phase: {phase} (expected: 0.125 = 1/8)\")" ] }, { "cell_type": "markdown", "id": "2b5d652d", "metadata": {}, "source": [ "## Phase Estimation for S Gate\n", "\n", "S gate: eigenvalue $e^{i\\pi/2}$, phase $\\phi = 1/4$." ] }, { "cell_type": "code", "execution_count": null, "id": "42d83cba", "metadata": {}, "outputs": [], "source": [ "n_count = 3\n", "circuit = qs.Circuit(n_count + 1, name=\"QPE-S\")\n", "\n", "circuit.x(n_count)\n", "for i in range(n_count):\n", " circuit.h(i)\n", "\n", "# S gate: phase = pi/2\n", "for k in range(n_count):\n", " power = n_count - 1 - k\n", " angle = math.pi / 2 * (2 ** power)\n", " circuit.cp(k, n_count, angle)\n", "\n", "# Inverse QFT\n", "circuit.swap(0, 2)\n", "circuit.h(0)\n", "circuit.cp(0, 1, -math.pi / 2)\n", "circuit.h(1)\n", "circuit.cp(0, 2, -math.pi / 4)\n", "circuit.cp(1, 2, -math.pi / 2)\n", "circuit.h(2)\n", "\n", "for i in range(n_count):\n", " circuit.measure(i)\n", "\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "measured = result.most_likely\n", "phase = int(measured, 2) / (2 ** n_count)\n", "print(f\"QPE for S gate: measured={measured}, phase={phase} (expected: 0.25)\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }