microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/notebooks/iterative_phase_estimation.ipynb
386lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "# Iterative Phase Estimation via the Cloud\n", |
| 8 | "\n", |
| 9 | "This sample code and notebook was written by members of KPMG Quantum team in Australia. It aims to demonstrate expanded capabilities of targets that support integrated hybrid computing. It makes use of bounded loops, classical function calls, nested conditional if statements, mid-program measurements and qubit reuse.\n", |
| 10 | "\n", |
| 11 | "## Connect to Azure Quantum and set up target\n", |
| 12 | "\n", |
| 13 | "First, we must configure the qsharp module to connect the azure workspace and specify a target. The Quantinuum H2 emulator target `quantinuum.sim.h2-1e` is chosen by default, which performs hardware-modeled noisy simulation. Running the experiments in this notebook against that target will consume approximately 25 HQC.\n", |
| 14 | "\n", |
| 15 | "We also configure the target profile as `Adaptive_RI` to indicate we use the QIR profile with support for mid-circuit measurement, measurement-based control flow, and classical integer computation as part of compilation.\n", |
| 16 | "\n", |
| 17 | "Replace the `subscription_id`, `resource_group`, `name`, and `location` connection parameters with the values for your configured Azure Quantum Workspace, or try copying the Python code for connecting to your workspace from the VS Code Microsoft Quantum Development Kit Extension." |
| 18 | ] |
| 19 | }, |
| 20 | { |
| 21 | "cell_type": "code", |
| 22 | "execution_count": null, |
| 23 | "metadata": {}, |
| 24 | "outputs": [], |
| 25 | "source": [ |
| 26 | "from qdk import qsharp\n", |
| 27 | "\n", |
| 28 | "qsharp.init(target_profile=qsharp.TargetProfile.Adaptive_RI)" |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "cell_type": "code", |
| 33 | "execution_count": null, |
| 34 | "metadata": {}, |
| 35 | "outputs": [], |
| 36 | "source": [ |
| 37 | "import azure.quantum\n", |
| 38 | "\n", |
| 39 | "workspace = azure.quantum.Workspace(\n", |
| 40 | " subscription_id = \"SUBSCRIPTION ID\",\n", |
| 41 | " resource_group = \"RESOURCE GROUP\",\n", |
| 42 | " name = \"WORKSPACE NAME\",\n", |
| 43 | " location = \"LOCATION\",\n", |
| 44 | ")" |
| 45 | ] |
| 46 | }, |
| 47 | { |
| 48 | "cell_type": "code", |
| 49 | "execution_count": null, |
| 50 | "metadata": {}, |
| 51 | "outputs": [], |
| 52 | "source": [ |
| 53 | "target = workspace.get_targets(\"quantinuum.sim.h2-1e\")" |
| 54 | ] |
| 55 | }, |
| 56 | { |
| 57 | "cell_type": "markdown", |
| 58 | "metadata": {}, |
| 59 | "source": [ |
| 60 | "## Two Dimensional Inner Product Calculation Using Iterative Phase Estimation on Three Qubits\n", |
| 61 | "\n", |
| 62 | "This notebook demonstrates an iterative phase estimation within Q#. It will use iterative phase estimation to calculate an inner product between two 2-dimensional vectors encoded on a target qubit and an ancilla qubit. An additional control qubit is also initialised which will be the only qubit used for measurement.\n", |
| 63 | "\n", |
| 64 | "The circuit begins by encoding the pair of vectors on the target qubit and the ancilla qubit. It then applies an Oracle operator to the entire register, controlled off the control qubit (which is set up in the $\\ket +$ state). The controlled Oracle operator generates a phase on the $\\ket 1$ state of the control qubit. This can then be read by applying a H gate to the control qubit to make the phase observable when measuring." |
| 65 | ] |
| 66 | }, |
| 67 | { |
| 68 | "cell_type": "markdown", |
| 69 | "metadata": {}, |
| 70 | "source": [ |
| 71 | "## Encoding vectors\n", |
| 72 | "\n", |
| 73 | "The vectors v and c are to be encoded onto the target qubit and the ancilla qubit. The vector $v = (cos(\\frac{\\theta_1}{2}),sin(\\frac{\\theta_1}{2}))$ can be represented by the quantum state $\\ket v = cos(\\frac{\\theta_1}{2})\\ket 0 + sin(\\frac{\\theta_1}{2})\\ket 1$, similarly $c$ can be constructed using $\\theta_2$. \n", |
| 74 | "\n", |
| 75 | "A Y rotation applied to a target qubit in the $\\ket 0$ state:\n", |
| 76 | "\n", |
| 77 | "$$RY(\\theta)\\ket 0 = e^{iY\\theta/2}\\ket 0 = cos(\\frac{\\theta}{2})\\ket 0 + sin(\\frac{\\theta}{2})\\ket 1$$\n", |
| 78 | "\n", |
| 79 | "**Note**: <u> A factor of 2 </u> is present here on theta. An application of a $RY(2\\pi)$ gate on $\\ket 0$ gives the state $-\\ket 0$ and would encode the vector $(-1,0)$. This phase cannot be considered a global phase and removed as the entire register will be entangled.\n", |
| 80 | "\n", |
| 81 | "The register of the target qubit and ancilla qubit is,\n", |
| 82 | "\n", |
| 83 | "$$\\ket \\Psi = \\ket {\\Psi_\\text{Target qubit}}\\ket {\\Psi_\\text{Ancilla qubit}}$$\n", |
| 84 | "The state to be created is on the target qubit and the ancilla qubit is,\n", |
| 85 | "\n", |
| 86 | "$$\\ket{\\Psi}=\\frac{1}{\\sqrt{2}}(\\ket{v}\\ket{+}+\\ket{c}\\ket{-}),$$\n", |
| 87 | "\n", |
| 88 | "\n", |
| 89 | "which also takes the form,\n", |
| 90 | "\n", |
| 91 | "$$\\ket{\\Psi} = \\frac{1}{2}(\\ket{v}+\\ket{c})\\ket{0}+\\frac{1}{2}(\\ket{v}-\\ket{c})\\ket{1}.$$\n" |
| 92 | ] |
| 93 | }, |
| 94 | { |
| 95 | "cell_type": "code", |
| 96 | "execution_count": null, |
| 97 | "metadata": { |
| 98 | "vscode": { |
| 99 | "languageId": "qsharp" |
| 100 | } |
| 101 | }, |
| 102 | "outputs": [], |
| 103 | "source": [ |
| 104 | "%%qsharp\n", |
| 105 | "\n", |
| 106 | "operation StateInitialisation(\n", |
| 107 | " TargetReg : Qubit,\n", |
| 108 | " AncilReg : Qubit,\n", |
| 109 | " theta1 : Double,\n", |
| 110 | " theta2 : Double\n", |
| 111 | ") : Unit is Adj + Ctl {\n", |
| 112 | " H(AncilReg);\n", |
| 113 | " // Arbitrary controlled rotation based on theta. This is vector v.\n", |
| 114 | " Controlled R([AncilReg], (PauliY, theta1, TargetReg));\n", |
| 115 | " // X gate on ancilla to change from |+〉 to |-〉.\n", |
| 116 | " X(AncilReg);\n", |
| 117 | " // Arbitrary controlled rotation based on theta. This is vector c.\n", |
| 118 | " Controlled R([AncilReg], (PauliY, theta2, TargetReg));\n", |
| 119 | " X(AncilReg);\n", |
| 120 | " H(AncilReg);\n", |
| 121 | "}" |
| 122 | ] |
| 123 | }, |
| 124 | { |
| 125 | "cell_type": "markdown", |
| 126 | "metadata": {}, |
| 127 | "source": [ |
| 128 | "## The Oracle\n", |
| 129 | "\n", |
| 130 | "An oracle G needs to be constructed such that it generates an eigenphase on the state encoded on the target qubit and the ancilla qubit. The construction of this oracle is unimportant to the demonstration within this notebook, but the operation it applies is,\n", |
| 131 | "\n", |
| 132 | "$$G\\ket \\Psi = e^{2\\pi i\\phi} \\ket \\Psi.$$\n", |
| 133 | "\n", |
| 134 | "where the inner product $\\braket {v|c}$ is contained within the phase $\\phi$, which is bound between [0,1]. When applied controlled on the control qubit which begins in that state $\\ket{\\Psi_\\text{Control Qubit}} = \\ket +$,\n", |
| 135 | "\n", |
| 136 | "$$\\begin{aligned}\n", |
| 137 | " \\text{Controlled }G \\ket{\\Psi_\\text{Control Qubit}} \\ket \\Psi & = \\frac {1}{\\sqrt{2}} (\\ket 0 \\ket \\Psi + e^{2\\pi i\\phi}\\ket 1 \\ket \\Psi )\\\\\n", |
| 138 | " & =\\frac {1}{\\sqrt{2}} (\\ket 0 + e^{2\\pi i\\phi}\\ket 1) \\ket \\Psi\n", |
| 139 | "\\end{aligned}$$\n", |
| 140 | "\n", |
| 141 | "Now the control qubit contains the phase $\\phi$ which relates to the inner product $\\braket {v|c}$\n", |
| 142 | "\n", |
| 143 | "$$\\ket{\\Psi_\\text{Control Qubit}} = \\frac {1}{\\sqrt{2}} (\\ket 0 + e^{2\\pi i\\phi}\\ket 1)$$" |
| 144 | ] |
| 145 | }, |
| 146 | { |
| 147 | "cell_type": "code", |
| 148 | "execution_count": null, |
| 149 | "metadata": { |
| 150 | "vscode": { |
| 151 | "languageId": "qsharp" |
| 152 | } |
| 153 | }, |
| 154 | "outputs": [], |
| 155 | "source": [ |
| 156 | "%%qsharp\n", |
| 157 | "\n", |
| 158 | "operation GOracle(\n", |
| 159 | " TargetReg : Qubit,\n", |
| 160 | " AncilReg : Qubit,\n", |
| 161 | " theta1 : Double,\n", |
| 162 | " theta2 : Double\n", |
| 163 | ") : Unit is Adj + Ctl {\n", |
| 164 | " Z(AncilReg);\n", |
| 165 | " within {\n", |
| 166 | " Adjoint StateInitialisation(TargetReg, AncilReg, theta1, theta2);\n", |
| 167 | " X(AncilReg);\n", |
| 168 | " X(TargetReg);\n", |
| 169 | " } apply {\n", |
| 170 | " Controlled Z([AncilReg], TargetReg);\n", |
| 171 | " }\n", |
| 172 | "}" |
| 173 | ] |
| 174 | }, |
| 175 | { |
| 176 | "cell_type": "markdown", |
| 177 | "metadata": {}, |
| 178 | "source": [ |
| 179 | "## Iteration\n", |
| 180 | "\n", |
| 181 | "Now for the iterative part of the circuit. For n measurements, consider that the phase can be represented as a binary value $\\phi$, and that applying $2^n$ oracles makes the nth binary point of the phase observable (through simple binary multiplication, and modulus $2\\pi$). The value of the control qubit can be readout, placed in a classical register and the qubit reset for use in the next iteration. The next iteration applies $2^{n-1}$ oracles, correcting phase on the control qubit dependent on the nth measurement. The state on the control qubit can be represented as,\n", |
| 182 | "\n", |
| 183 | "$$ \\ket {\\Psi_{\\text{Control Qubit}}} = \\ket 0 + e^{2\\pi i\\phi}\\ket 1 $$\n", |
| 184 | "\n", |
| 185 | "where $\\phi = 0.\\phi_0\\phi_1\\phi_2\\phi_3$...\n", |
| 186 | "\n", |
| 187 | "Applying $2^n$ controlled oracles gives the state on the control qubit,\n", |
| 188 | "\n", |
| 189 | "$$ G^{2^n}\\ket {\\Psi_{\\text{Control Qubit}}} = \\ket 0 + e^{2\\pi i 0.\\phi_n\\phi_{n+1}\\phi_{n+2}\\phi_{n+3}...}\\ket 1 $$\n", |
| 190 | "\n", |
| 191 | "Consider that the phase has no terms deeper than $\\phi_n$ (ie, terms $\\phi_{n+1},\\phi_{n+2}, \\text{etc}$),\n", |
| 192 | "\n", |
| 193 | "$$ G^{2^n}\\ket {\\Psi_{\\text{Control Qubit}}} = \\ket 0 + e^{2\\pi i 0.\\phi_n}\\ket 1 $$\n", |
| 194 | "\n", |
| 195 | "Now the value $\\phi_n$ can be observed with a H gate and a measurement projecting along the Z axis. Resetting the control qubit and applying the oracle $2^{n-1}$ times,\n", |
| 196 | "\n", |
| 197 | "$$ G^{2^{n-1}}\\ket {\\Psi_{\\text{Control Qubit}}} = \\ket 0 + e^{2\\pi i 0.\\phi_{n-1}\\phi_n}\\ket 1 $$\n", |
| 198 | "\n", |
| 199 | "Using the previous measured value for $\\phi_n$, the additional binary point can be rotated out.\n", |
| 200 | "\n", |
| 201 | "$$ RZ(-2\\pi \\times 0.0\\phi_n)G^{n-1}\\ket {\\Psi_{\\text{Control Qubit}}} = \\ket 0 + e^{2\\pi i 0.\\phi_{n-1}}\\ket 1 $$\n", |
| 202 | "\n", |
| 203 | "This process is iteratively applied for some bit precision n to obtain the phase $0.\\phi_0\\phi_1\\phi_2...\\phi_{n}$. The value is stored as a binary value $x = \\phi_0\\phi_1\\phi_2...\\phi_{n}$ as only integers are manipulatable at runtime currently.\n", |
| 204 | "\n", |
| 205 | "As the readout tells nothing of either vector, only the inner product between them, the states on the target qubit and ancilla qubit <u>remain in the same state</u> throughout the process!" |
| 206 | ] |
| 207 | }, |
| 208 | { |
| 209 | "cell_type": "code", |
| 210 | "execution_count": null, |
| 211 | "metadata": { |
| 212 | "vscode": { |
| 213 | "languageId": "qsharp" |
| 214 | } |
| 215 | }, |
| 216 | "outputs": [], |
| 217 | "source": [ |
| 218 | "%%qsharp\n", |
| 219 | "\n", |
| 220 | "import Std.Math.*;\n", |
| 221 | "import Std.Convert.*;\n", |
| 222 | "\n", |
| 223 | "operation IterativePhaseEstimation(\n", |
| 224 | " TargetReg : Qubit,\n", |
| 225 | " AncilReg : Qubit,\n", |
| 226 | " theta1 : Double,\n", |
| 227 | " theta2 : Double,\n", |
| 228 | " Measurements : Int\n", |
| 229 | ") : Int {\n", |
| 230 | " use ControlReg = Qubit();\n", |
| 231 | " mutable MeasureControlReg = [Zero, size = Measurements];\n", |
| 232 | " mutable bitValue = 0;\n", |
| 233 | " //Apply to initialise state, this is defined by the angles theta1 and theta2\n", |
| 234 | " StateInitialisation(TargetReg, AncilReg, theta1, theta2);\n", |
| 235 | " for index in 0..Measurements - 1 {\n", |
| 236 | " H(ControlReg);\n", |
| 237 | " //Don't apply rotation on first set of oracles\n", |
| 238 | " if index > 0 {\n", |
| 239 | " //Loop through previous results\n", |
| 240 | " for index2 in 0..index - 1 {\n", |
| 241 | " if MeasureControlReg[Measurements - 1 - index2] == One {\n", |
| 242 | " //Rotate control qubit dependent on previous measurements and number of measurements\n", |
| 243 | " let angle = -IntAsDouble(2^(index2)) * PI() / (2.0^IntAsDouble(index));\n", |
| 244 | " R(PauliZ, angle, ControlReg);\n", |
| 245 | " }\n", |
| 246 | " }\n", |
| 247 | " }\n", |
| 248 | "\n", |
| 249 | " let powerIndex = (1 <<< (Measurements - 1 - index));\n", |
| 250 | " //Apply a number of oracles equal to 2^index, where index is the number or measurements left\n", |
| 251 | " for _ in 1..powerIndex {\n", |
| 252 | " Controlled GOracle([ControlReg], (TargetReg, AncilReg, theta1, theta2));\n", |
| 253 | " }\n", |
| 254 | " H(ControlReg);\n", |
| 255 | " //Make a measurement mid circuit\n", |
| 256 | " set MeasureControlReg w/= (Measurements - 1 - index) <- MResetZ(ControlReg);\n", |
| 257 | " if MeasureControlReg[Measurements - 1 - index] == One {\n", |
| 258 | " //Assign bitValue based on previous measurement\n", |
| 259 | " set bitValue += 2^(index);\n", |
| 260 | " }\n", |
| 261 | " }\n", |
| 262 | " return bitValue;\n", |
| 263 | "}" |
| 264 | ] |
| 265 | }, |
| 266 | { |
| 267 | "cell_type": "markdown", |
| 268 | "metadata": {}, |
| 269 | "source": [ |
| 270 | "Finally to calculate the inner product from the measured value,\n", |
| 271 | "\n", |
| 272 | "$$\\braket {v|c} = -cos(2\\pi x / 2^n)$$\n", |
| 273 | "\n", |
| 274 | "where $x = \\phi_0\\phi_1\\phi_2...\\phi_{n}$. The denominator within the cosine function is to shift the binary point to match the original value $\\phi$.\n", |
| 275 | "\n", |
| 276 | "**Note**: For inner product that are not -1 or 1, the solutions are paired with a value difference of $2^{n-1}$. For example for n=3 measurements, the measured bit value of 2 would also have a pair solution of 6. Either of these values produce the same value of the inner product when input as the variable to the even function cosine (resulting in an inner product of 0 in this example).\n", |
| 277 | "\n", |
| 278 | "**Note**: For inner product solutions between the discrete bit precision, a distribution of results will be produced based on where the inner product lies between the discrete bit value. " |
| 279 | ] |
| 280 | }, |
| 281 | { |
| 282 | "cell_type": "code", |
| 283 | "execution_count": null, |
| 284 | "metadata": { |
| 285 | "vscode": { |
| 286 | "languageId": "qsharp" |
| 287 | } |
| 288 | }, |
| 289 | "outputs": [], |
| 290 | "source": [ |
| 291 | "%%qsharp\n", |
| 292 | "\n", |
| 293 | "operation QuantumInnerProduct(theta1 : Double, theta2 : Double, iterationCount : Int) : Int {\n", |
| 294 | " //Create target register\n", |
| 295 | " use TargetReg = Qubit();\n", |
| 296 | " //Create ancilla register\n", |
| 297 | " use AncilReg = Qubit();\n", |
| 298 | " //Run iterative phase estimation\n", |
| 299 | " let Results = IterativePhaseEstimation(TargetReg, AncilReg, theta1, theta2, iterationCount);\n", |
| 300 | " Reset(TargetReg);\n", |
| 301 | " Reset(AncilReg);\n", |
| 302 | " return Results;\n", |
| 303 | "}" |
| 304 | ] |
| 305 | }, |
| 306 | { |
| 307 | "cell_type": "code", |
| 308 | "execution_count": null, |
| 309 | "metadata": { |
| 310 | "vscode": { |
| 311 | "languageId": "qsharp" |
| 312 | } |
| 313 | }, |
| 314 | "outputs": [], |
| 315 | "source": [ |
| 316 | "%%qsharp\n", |
| 317 | "\n", |
| 318 | "operation PerformMeasurements(theta1 : Double, theta2 : Double, n : Int) : Int {\n", |
| 319 | " let measurementCount = n + 1;\n", |
| 320 | " return QuantumInnerProduct(theta1, theta2, measurementCount);\n", |
| 321 | "}" |
| 322 | ] |
| 323 | }, |
| 324 | { |
| 325 | "cell_type": "markdown", |
| 326 | "metadata": {}, |
| 327 | "source": [ |
| 328 | "Set the parameters and call the `PerformMeasurements` operation to simulate it." |
| 329 | ] |
| 330 | }, |
| 331 | { |
| 332 | "cell_type": "code", |
| 333 | "execution_count": null, |
| 334 | "metadata": { |
| 335 | "vscode": { |
| 336 | "languageId": "qsharp" |
| 337 | } |
| 338 | }, |
| 339 | "outputs": [], |
| 340 | "source": [ |
| 341 | "%%qsharp\n", |
| 342 | "\n", |
| 343 | "PerformMeasurements(PI() / 7.0, PI() / 5.0, 4)" |
| 344 | ] |
| 345 | }, |
| 346 | { |
| 347 | "cell_type": "markdown", |
| 348 | "metadata": {}, |
| 349 | "source": [ |
| 350 | "Submit the job to the target. The circuit is approximately 0.4 EHQC each shot. The number of shots specified is 128, but this can be increased to reduce the variance of the result, up to some stable distribution. The total EHQCs for a job can be viewed within the Azure portal under \"Job management\". Selecting the desired job, the cost estimation can be viewed." |
| 351 | ] |
| 352 | }, |
| 353 | { |
| 354 | "cell_type": "code", |
| 355 | "execution_count": null, |
| 356 | "metadata": {}, |
| 357 | "outputs": [], |
| 358 | "source": [ |
| 359 | "job = target.submit(qsharp.compile(\"PerformMeasurements(PI() / 7.0, PI() / 5.0, 4)\"), shots=128)\n", |
| 360 | "job.wait_until_completed()\n", |
| 361 | "job.get_results()" |
| 362 | ] |
| 363 | } |
| 364 | ], |
| 365 | "metadata": { |
| 366 | "kernelspec": { |
| 367 | "display_name": "Python 3", |
| 368 | "language": "python", |
| 369 | "name": "python3" |
| 370 | }, |
| 371 | "language_info": { |
| 372 | "codemirror_mode": { |
| 373 | "name": "ipython", |
| 374 | "version": 3 |
| 375 | }, |
| 376 | "file_extension": ".py", |
| 377 | "mimetype": "text/x-python", |
| 378 | "name": "python", |
| 379 | "nbconvert_exporter": "python", |
| 380 | "pygments_lexer": "ipython3", |
| 381 | "version": "3.11.0" |
| 382 | } |
| 383 | }, |
| 384 | "nbformat": 4, |
| 385 | "nbformat_minor": 2 |
| 386 | } |
| 387 | |