microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/estimation/estimation-random-circuit.ipynb
155lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "# Resource Estimation of Random Circuit Used in State Vector Simulation\n", |
| 8 | "\n", |
| 9 | "This notebook gives the resource estimation of random quantum circuits used for the full state vector simulation in\n", |
| 10 | "[Simulating 44-Qubit quantum circuits using AWS ParallelCluster](https://aws.amazon.com/blogs/hpc/simulating-44-qubit-quantum-circuits-using-aws-parallelcluster/)." |
| 11 | ] |
| 12 | }, |
| 13 | { |
| 14 | "cell_type": "code", |
| 15 | "execution_count": null, |
| 16 | "metadata": {}, |
| 17 | "outputs": [], |
| 18 | "source": [ |
| 19 | "import qsharp" |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "cell_type": "markdown", |
| 24 | "metadata": {}, |
| 25 | "source": [ |
| 26 | "## Generating a random circuit\n", |
| 27 | "\n", |
| 28 | "Let's now create a random Q# program. Note that the generated circuit will be different on each run, so resource estimates produced for it will vary slightly." |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "cell_type": "code", |
| 33 | "execution_count": null, |
| 34 | "metadata": { |
| 35 | "microsoft": { |
| 36 | "language": "qsharp" |
| 37 | }, |
| 38 | "vscode": { |
| 39 | "languageId": "qsharp" |
| 40 | } |
| 41 | }, |
| 42 | "outputs": [], |
| 43 | "source": [ |
| 44 | "%%qsharp\n", |
| 45 | "open Microsoft.Quantum.Math;\n", |
| 46 | "open Microsoft.Quantum.Random;\n", |
| 47 | "\n", |
| 48 | "operation RunRandomCircuit(nQubits : Int, nGates : Int) : Unit {\n", |
| 49 | " use qs = Qubit[nQubits];\n", |
| 50 | "\n", |
| 51 | " for _ in 1 .. nGates {\n", |
| 52 | " let rnd = DrawRandomDouble(0., 1.);\n", |
| 53 | " if rnd < 0.5 {\n", |
| 54 | " // CZ\n", |
| 55 | " let controlInd = DrawRandomInt(0, nQubits - 1);\n", |
| 56 | " // Make sure target qubit index is distinct from control qubit index\n", |
| 57 | " mutable targetInd = DrawRandomInt(0, nQubits - 2);\n", |
| 58 | " if targetInd >= controlInd {\n", |
| 59 | " set targetInd += 1;\n", |
| 60 | " }\n", |
| 61 | " CZ(qs[controlInd], qs[targetInd]);\n", |
| 62 | " } elif rnd < 0.625 {\n", |
| 63 | " // H\n", |
| 64 | " let ind = DrawRandomInt(0, nQubits - 1);\n", |
| 65 | " H(qs[ind]);\n", |
| 66 | " } else {\n", |
| 67 | " // R\n", |
| 68 | " let ind = DrawRandomInt(0, nQubits - 1);\n", |
| 69 | " let gate = [Rx, Ry, Rz][DrawRandomInt(0, 2)];\n", |
| 70 | " let angle = DrawRandomDouble(0., PI());\n", |
| 71 | " gate(angle, qs[ind]);\n", |
| 72 | " }\n", |
| 73 | " }\n", |
| 74 | "}" |
| 75 | ] |
| 76 | }, |
| 77 | { |
| 78 | "cell_type": "markdown", |
| 79 | "metadata": {}, |
| 80 | "source": [ |
| 81 | "## Running the Resource Estimator\n", |
| 82 | "\n", |
| 83 | "Now we can estimate the resources required for the generated Q# program." |
| 84 | ] |
| 85 | }, |
| 86 | { |
| 87 | "cell_type": "code", |
| 88 | "execution_count": null, |
| 89 | "metadata": {}, |
| 90 | "outputs": [], |
| 91 | "source": [ |
| 92 | "n_qubits = 43\n", |
| 93 | "n_gates = 1000\n", |
| 94 | "qsharp.set_classical_seed(12)\n", |
| 95 | "result = qsharp.estimate(f\"RunRandomCircuit({n_qubits}, {n_gates})\")\n", |
| 96 | "result" |
| 97 | ] |
| 98 | }, |
| 99 | { |
| 100 | "cell_type": "code", |
| 101 | "execution_count": null, |
| 102 | "metadata": { |
| 103 | "jupyter": { |
| 104 | "outputs_hidden": false, |
| 105 | "source_hidden": false |
| 106 | }, |
| 107 | "nteract": { |
| 108 | "transient": { |
| 109 | "deleting": false |
| 110 | } |
| 111 | } |
| 112 | }, |
| 113 | "outputs": [], |
| 114 | "source": [ |
| 115 | "logical_qubits = result['physicalCounts']['breakdown']['algorithmicLogicalQubits']\n", |
| 116 | "logical_depth = result['physicalCounts']['breakdown']['algorithmicLogicalDepth']\n", |
| 117 | "# We take the runtime of the circuit from the blog post https://aws.amazon.com/blogs/hpc/simulating-44-qubit-quantum-circuits-using-aws-parallelcluster/, figure 4.\n", |
| 118 | "target_runtime = 5800\n", |
| 119 | "target_rqops = logical_qubits * logical_depth / target_runtime\n", |
| 120 | "\n", |
| 121 | "print(f\"Logical qubits = {logical_qubits}\")\n", |
| 122 | "print(f\"Logical depth = {logical_depth}\")\n", |
| 123 | "print(f\"Target runtime = {target_runtime} seconds\")\n", |
| 124 | "print(f\"Target rQOPS = {target_rqops}\")\n", |
| 125 | "\n", |
| 126 | "print(f\"Execution time on hardware = {result['physicalCounts']['runtime'] * 1e-9} seconds\")\n", |
| 127 | "print(f\"rQOPS for execution on hardware = {result['physicalCounts']['rqops']}\")" |
| 128 | ] |
| 129 | } |
| 130 | ], |
| 131 | "metadata": { |
| 132 | "kernel_info": { |
| 133 | "name": "python3" |
| 134 | }, |
| 135 | "kernelspec": { |
| 136 | "display_name": "Python 3 (ipykernel)", |
| 137 | "language": "python", |
| 138 | "name": "python3" |
| 139 | }, |
| 140 | "language_info": { |
| 141 | "codemirror_mode": { |
| 142 | "name": "ipython", |
| 143 | "version": 3 |
| 144 | }, |
| 145 | "file_extension": ".py", |
| 146 | "mimetype": "text/x-python", |
| 147 | "name": "python", |
| 148 | "nbconvert_exporter": "python", |
| 149 | "pygments_lexer": "ipython3", |
| 150 | "version": "3.11.7" |
| 151 | } |
| 152 | }, |
| 153 | "nbformat": 4, |
| 154 | "nbformat_minor": 1 |
| 155 | } |
| 156 | |