{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![(book cover)](https://covers.oreillystatic.com/images/0636920167433/cat.gif \"(book cover)\")\n", "# Programming Quantum Computers by O'Reilly Media - [book info](http://shop.oreilly.com/product/0636920167433.do) - [all code samples](https://oreilly-qc.github.io)\n", "\n", "## Code samples for Chapter 8\n", "These code samples were written by Mariia Mykhailova." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 8-1: Using the phase estimation primitive" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"HPower\",\"UsingQPE\"]", "text/html": [ "" ], "text/plain": [ "HPower, UsingQPE" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 8-1: Using the phase estimation primitive\n", "\n", "open Microsoft.Quantum.Arithmetic;\n", "open Microsoft.Quantum.Characterization;\n", "open Microsoft.Quantum.Convert;\n", "open Microsoft.Quantum.Diagnostics;\n", "open Microsoft.Quantum.Math;\n", "open Microsoft.Quantum.Oracles;\n", "\n", "// Helper operation to define powers of H gate\n", "operation HPower (power : Int, register : Qubit[]) : Unit is Adj + Ctl {\n", " // We know that H² = I, so we just need to apply H if power is odd\n", " if (power % 2 == 1) {\n", " H(register[0]);\n", " }\n", "}\n", "\n", "operation UsingQPE () : Unit {\n", " let precision = 4;\n", " // Allocate qubits to hold the eigenstate of H and the phase (in a big endian register)\n", " use (eigenstate, phaseRegister) = (Qubit[1], Qubit[precision]);\n", " // Prepare the eigenstate of H gate corresponding to eigenphase of 180°\n", " // (for 0° we'd use 0.25 * PI() as rotation angle)\n", " Ry(-0.75 * PI(), eigenstate[0]);\n", "\n", " // Call library implementation of quantum phase estimation\n", " let phaseRegisterBE = BigEndian(phaseRegister);\n", " let powerUnitary = DiscreteOracle(HPower);\n", " QuantumPhaseEstimation(powerUnitary, eigenstate, phaseRegisterBE);\n", "\n", " // Read out the phase\n", " let phase = IntAsDouble(MeasureInteger(BigEndianAsLittleEndian(phaseRegisterBE))) / IntAsDouble(1 <<< precision);\n", " Message($\"Estimated phase = {phase * 360.0}°\");\n", "\n", " ResetAll(eigenstate + phaseRegister);\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Estimated phase = 180°\n" ] }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate UsingQPE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 8-2: Implementation of the phase estimation primitive" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/json": "[\"ImplementingQPE\",\"QPE\",\"RotatePower\"]", "text/html": [ "" ], "text/plain": [ "ImplementingQPE, QPE, RotatePower" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 8-2: Implementation of the phase estimation primitive\n", "\n", "open Microsoft.Quantum.Arithmetic;\n", "open Microsoft.Quantum.Characterization;\n", "open Microsoft.Quantum.Convert;\n", "open Microsoft.Quantum.Diagnostics;\n", "open Microsoft.Quantum.Math;\n", "open Microsoft.Quantum.Oracles;\n", "\n", "operation QPE (powerUnitary : ((Int, Qubit[]) => Unit is Adj+Ctl), \n", " eigenstate : Qubit[],\n", " phaseRegister : Qubit[]) : Unit {\n", " ApplyToEach(H, phaseRegister);\n", " \n", " // Apply conditional powers of U\n", " for i in 0 .. Length(phaseRegister) - 1 {\n", " Controlled powerUnitary([phaseRegister[i]], (1 <<< i, eigenstate));\n", " }\n", " \n", " QFTLE(LittleEndian(phaseRegister));\n", "}\n", "\n", "// Helper operation to define powers of the rotation gate\n", "operation RotatePower (power : Int, register : Qubit[]) : Unit is Adj + Ctl {\n", " // To apply higher powers of the rotation gate, we can rotate by multiples of the angle\n", " R1(-PI() * 5.0/6.0 * IntAsDouble(power), register[0]);\n", "}\n", "\n", "operation ImplementingQPE () : Unit {\n", " let precision = 3;\n", " // Allocate qubits to hold the eigenstate of H and the phase (in a big endian register)\n", " use (eigenstate, phaseRegister) = (Qubit[1], Qubit[precision]);\n", " // Prepare the eigenstate of the rotation gate corresponding to eigenphase of 150°;\n", " // for R1 gate, that is simply a |1⟩\n", " X(eigenstate[0]);\n", "\n", " // Call our implementation of quantum phase estimation\n", " QPE(RotatePower, eigenstate, phaseRegister);\n", "\n", " // Inspect the state we obtain after applying QPE\n", " DumpRegister((), phaseRegister);\n", "\n", " // Read out the phase\n", " let phase = IntAsDouble(MeasureInteger(LittleEndian(phaseRegister))) / IntAsDouble(1 <<< precision);\n", " Message($\"Estimated phase = {phase * 360.0}°\");\n", "\n", " ResetAll(eigenstate + phaseRegister);\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/json": "{\"QubitIds\":[1,2,3],\"NQubits\":3,\"Amplitudes\":[{\"Real\":0.10825317547305487,\"Imaginary\":0.029006350946109552,\"Magnitude\":0.1120719340210067,\"Phase\":0.2617993877991484},{\"Real\":0.13528279153744854,\"Imaginary\":-0.017810317386500216,\"Magnitude\":0.1364501414127982,\"Phase\":-0.13089969389957543},{\"Real\":0.18749999999999992,\"Imaginary\":-0.10825317547305492,\"Magnitude\":0.21650635094610965,\"Phase\":-0.5235987755982995},{\"Real\":0.5048822514075066,\"Imaginary\":-0.6579753603314554,\"Magnitude\":0.8293597907962631,\"Phase\":-0.9162978572970232},{\"Real\":-0.10825317547305481,\"Imaginary\":0.4040063509461098,\"Magnitude\":0.4182581518689041,\"Phase\":1.832595714594046},{\"Real\":0.023210857516441776,\"Imaginary\":0.1763039664403905,\"Magnitude\":0.17782528641692918,\"Phase\":1.4398966328953222},{\"Real\":0.06250000000000003,\"Imaginary\":0.10825317547305492,\"Magnitude\":0.1250000000000001,\"Phase\":1.0471975511965979},{\"Real\":0.08662409953860287,\"Imaginary\":0.06646900938534578,\"Magnitude\":0.1091872878568882,\"Phase\":0.6544984694978735}]}", "text/html": [ "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \n", "\r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "
Qubit IDs1, 2, 3
Basis state (little endian)AmplitudeMeas. Pr.Phase
$\\left|0\\right\\rangle$$0.1083 + 0.0290 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|1\\right\\rangle$$0.1353 -0.0178 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|2\\right\\rangle$$0.1875 -0.1083 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|3\\right\\rangle$$0.5049 -0.6580 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|4\\right\\rangle$$-0.1083 + 0.4040 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|5\\right\\rangle$$0.0232 + 0.1763 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|6\\right\\rangle$$0.0625 + 0.1083 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
$\\left|7\\right\\rangle$$0.0866 + 0.0665 i$\r\n", " \r\n", " \r\n", " ↑\r\n", "
\r\n", " " ], "text/plain": [ "|0⟩\t0.10825317547305487 + 0.029006350946109552𝑖\n", "|1⟩\t0.13528279153744854 + -0.017810317386500216𝑖\n", "|2⟩\t0.18749999999999992 + -0.10825317547305492𝑖\n", "|3⟩\t0.5048822514075066 + -0.6579753603314554𝑖\n", "|4⟩\t-0.10825317547305481 + 0.4040063509461098𝑖\n", "|5⟩\t0.023210857516441776 + 0.1763039664403905𝑖\n", "|6⟩\t0.06250000000000003 + 0.10825317547305492𝑖\n", "|7⟩\t0.08662409953860287 + 0.06646900938534578𝑖" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Estimated phase = 90°\n" ] }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate ImplementingQPE" ] } ], "metadata": { "kernelspec": { "display_name": "Q#", "language": "qsharp", "name": "iqsharp" }, "language_info": { "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", "version": "0.14" } }, "nbformat": 4, "nbformat_minor": 2 }