{ "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 3\n", "These code samples were written by Mariia Mykhailova." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3-1: Creating a multi-qubit state that can be expressed in terms of its qubits" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"SeparableState\"]", "text/html": [ "" ], "text/plain": [ "SeparableState" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 3-1: Creating a multi-qubit state that can be expressed in terms of its qubits\n", "\n", "// open namespace which defines diagnostic routines\n", "open Microsoft.Quantum.Diagnostics;\n", "\n", "operation SeparableState () : Unit {\n", " // allocate the qubits\n", " use (q1, q2, q3) = (Qubit(), Qubit(), Qubit());\n", " // put each of the qubits q2 and q3 into superposition of 0 and 1\n", " H(q2);\n", " H(q3);\n", "\n", " // output the wave function of the three-qubit state\n", " DumpMachine();\n", "\n", " // make sure the qubits are back to the 0 state\n", " ResetAll([q1, q2, q3]);\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/json": "{\"div_id\":\"dump-machine-div-5dd3c8a4-ada4-4d15-8c63-8a02a70e1821\",\"qubit_ids\":[0,1,2],\"n_qubits\":3,\"amplitudes\":[{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0}]}", "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", " \r\n", " \r\n", " \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", " \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", " \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", " \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", " \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", " \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", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "
Qubit IDs0, 1, 2
Basis state (little endian)AmplitudeMeas. Pr.Phase
$\\left|0\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|1\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|2\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|3\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|4\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|5\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|6\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|7\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
" ], "text/plain": [ "|0⟩\t0.5000000000000001 + 0𝑖\n", "|1⟩\t0 + 0𝑖\n", "|2⟩\t0.5000000000000001 + 0𝑖\n", "|3⟩\t0 + 0𝑖\n", "|4⟩\t0.5000000000000001 + 0𝑖\n", "|5⟩\t0 + 0𝑖\n", "|6⟩\t0.5000000000000001 + 0𝑖\n", "|7⟩\t0 + 0𝑖" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate SeparableState" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3-2: Make a Bell pair" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"PrepareAndMeasureBellPair\",\"PrepareMultipleBellPairs\"]", "text/html": [ "" ], "text/plain": [ "PrepareAndMeasureBellPair, PrepareMultipleBellPairs" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 3-2: Make a Bell pair\n", "\n", "operation PrepareAndMeasureBellPair () : Unit {\n", " // allocate the qubits\n", " use (a, b) = (Qubit(), Qubit());\n", " // put qubit a in superposition\n", " H(a);\n", "\n", " // entangle qubits a and b\n", " CNOT(a, b);\n", "\n", " // measure both qubits and output the results\n", " Message($\"Measurement results: {M(a)}, {M(b)}\");\n", "\n", " // make sure the qubits are back to the 0 state\n", " ResetAll([a, b]);\n", "}\n", "\n", "operation PrepareMultipleBellPairs () : Unit {\n", " // repeat the experiment multiple times to observe the correlation between measurement results:\n", " // the two bits will be random but always the same\n", " for i in 1..10 {\n", " PrepareAndMeasureBellPair();\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Measurement results: Zero, Zero\n", "Measurement results: One, One\n", "Measurement results: One, One\n", "Measurement results: Zero, Zero\n", "Measurement results: One, One\n", "Measurement results: Zero, Zero\n", "Measurement results: Zero, Zero\n", "Measurement results: Zero, Zero\n", "Measurement results: Zero, Zero\n", "Measurement results: Zero, Zero\n" ] }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate PrepareMultipleBellPairs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3-3: Phase kickback" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"PhaseKickback\"]", "text/html": [ "" ], "text/plain": [ "PhaseKickback" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 3-3: Phase kickback\n", "\n", "// open namespace which defines diagnostic routines\n", "open Microsoft.Quantum.Diagnostics;\n", "\n", "operation PhaseKickback () : Unit {\n", " // allocate two registers, control and target\n", " use (reg1, reg2) = (Qubit[2], Qubit());\n", " // put each qubit of the control register into superposition of 0 and 1\n", " ApplyToEach(H, reg1);\n", "\n", " // initialize the target qubit into 1 state\n", " X(reg2);\n", "\n", " // output the wave function of the three-qubit state BEFORE phase kickback\n", " DumpMachine();\n", "\n", " // apply phase rotations controlled on the first register\n", " Controlled T(reg1[0..0], reg2);\n", " Controlled S(reg1[1..1], reg2);\n", "\n", " // output the wave function of the three-qubit state AFTER phase kickback\n", " DumpMachine();\n", "\n", " // make sure the qubits are back to the 0 state\n", " ResetAll(reg1 + [reg2]);\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/json": "{\"div_id\":\"dump-machine-div-c2039c8d-22a6-4cd4-9cd9-7ffc912681a7\",\"qubit_ids\":[0,1,2],\"n_qubits\":3,\"amplitudes\":[{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0}]}", "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", " \r\n", " \r\n", " \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", " \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", " \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", " \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", " \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", " \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", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "
Qubit IDs0, 1, 2
Basis state (little endian)AmplitudeMeas. Pr.Phase
$\\left|0\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|1\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|2\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|3\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|4\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|5\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|6\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|7\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
" ], "text/plain": [ "|0⟩\t0 + 0𝑖\n", "|1⟩\t0 + 0𝑖\n", "|2⟩\t0 + 0𝑖\n", "|3⟩\t0 + 0𝑖\n", "|4⟩\t0.5000000000000001 + 0𝑖\n", "|5⟩\t0.5000000000000001 + 0𝑖\n", "|6⟩\t0.5000000000000001 + 0𝑖\n", "|7⟩\t0.5000000000000001 + 0𝑖" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/json": "{\"div_id\":\"dump-machine-div-2bc96a0e-e9c0-43d0-b77e-d7c15657bb04\",\"qubit_ids\":[0,1,2],\"n_qubits\":3,\"amplitudes\":[{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.0,\"Magnitude\":0.0,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.35355339059327384,\"Imaginary\":0.35355339059327384,\"Magnitude\":0.5000000000000001,\"Phase\":0.7853981633974483},{\"Real\":0.0,\"Imaginary\":0.5000000000000001,\"Magnitude\":0.5000000000000001,\"Phase\":1.5707963267948966},{\"Real\":-0.35355339059327384,\"Imaginary\":0.35355339059327384,\"Magnitude\":0.5000000000000001,\"Phase\":2.356194490192345}]}", "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", " \r\n", " \r\n", " \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", " \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", " \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", " \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", " \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", " \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", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "
Qubit IDs0, 1, 2
Basis state (little endian)AmplitudeMeas. Pr.Phase
$\\left|0\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|1\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|2\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|3\\right\\rangle$$0.0000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|4\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|5\\right\\rangle$$0.3536 + 0.3536 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|6\\right\\rangle$$0.0000 + 0.5000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|7\\right\\rangle$$-0.3536 + 0.3536 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
" ], "text/plain": [ "|0⟩\t0 + 0𝑖\n", "|1⟩\t0 + 0𝑖\n", "|2⟩\t0 + 0𝑖\n", "|3⟩\t0 + 0𝑖\n", "|4⟩\t0.5000000000000001 + 0𝑖\n", "|5⟩\t0.35355339059327384 + 0.35355339059327384𝑖\n", "|6⟩\t0 + 0.5000000000000001𝑖\n", "|7⟩\t-0.35355339059327384 + 0.35355339059327384𝑖" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate PhaseKickback" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3-4: The swap test" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"SwapTest\",\"RunSwapTest\"]", "text/html": [ "" ], "text/plain": [ "SwapTest, RunSwapTest" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 3-4: The swap test\n", "\n", "// open namespace which defines type conversion functions\n", "open Microsoft.Quantum.Convert;\n", "// open namespace which defines MResetZ\n", "open Microsoft.Quantum.Measurement;\n", "\n", "// Returns True if the states of the input qubits are equal\n", "operation SwapTest (input1 : Qubit, input2 : Qubit) : Bool {\n", " // allocate the output qubit\n", " use output = Qubit();\n", " // initialize the output qubit\n", " H(output);\n", "\n", " // swap the input states conditioned on the output state\n", " Controlled SWAP([output], (input1, input2));\n", "\n", " // extract the result and return the output qubit back to 0 state\n", " H(output);\n", " X(output);\n", "\n", " return M(output) == One;\n", "}\n", "\n", "operation RunSwapTest () : Unit {\n", " let attempts = 100;\n", " mutable reportedEqual = 0;\n", " // repeat the test multiple times to observe the probability of the states being reported equal\n", " for i in 1..attempts {\n", " // allocate qubits to be tested\n", " use (input1, input2) = (Qubit(), Qubit());\n", " // initialize the qubits in the states we want to compare\n", " // leave input1 in the |0⟩ state and rotate input2 - the larger the angle, the further away are the states\n", " // try replacing 0.1 with 0.0 (when the states will be equal) \n", " // and with 0.2, 0.3, ... up to 1.0 when the state is rotated all the way to the -|0⟩\n", " Ry(2.0 * 3.14 * 0.1, input2);\n", "\n", " // some other suggested state initializations:\n", " // X(input1); H(input1); H(input2); Z(input2); // same state |-⟩, prepared in different ways\n", " // X(input1); H(input1); H(input2); // orthogonal states: input1 in the |-⟩ state, input2 in the |+⟩ state\n", "\n", " if (SwapTest(input1, input2)) {\n", " set reportedEqual += 1;\n", " }\n", "\n", " ResetAll([input1, input2]);\n", " }\n", " Message($\"The states were reported equal {IntAsDouble(reportedEqual) / IntAsDouble(attempts) * 100.0 }% of the time\");\n", "}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The states were reported equal 97% of the time\r\n" ] }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate RunSwapTest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3-5: Custom conditional phase" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"CustomConditionalPhase\"]", "text/html": [ "" ], "text/plain": [ "CustomConditionalPhase" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 3-5: Custom conditional phase\n", "\n", "// open namespace which defines diagnostic routines\n", "open Microsoft.Quantum.Diagnostics;\n", "\n", "operation CustomConditionalPhase () : Unit {\n", " // allocate two qubits\n", " use (q1, q2) = (Qubit(), Qubit());\n", " // put each of the qubits into superposition of 0 and 1\n", " H(q1);\n", " H(q2);\n", "\n", " // apply phases\n", " T(q2);\n", " CNOT(q1, q2);\n", " Adjoint T(q2);\n", " CNOT(q1, q2);\n", " T(q1);\n", "\n", " // output the wave function of the two-qubit state\n", " DumpMachine();\n", "\n", " // apply a single gate equivalent to the earlier sequence of gates\n", " Controlled S([q1], q2);\n", "\n", " // output the wave function of the two-qubit state\n", " DumpMachine();\n", "\n", " // make sure the qubits are back to the 0 state\n", " ResetAll([q1, q2]);\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/json": "{\"div_id\":\"dump-machine-div-d5e4c49e-48e6-4551-b3d6-2bb3598e6087\",\"qubit_ids\":[0,1],\"n_qubits\":2,\"amplitudes\":[{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.0,\"Imaginary\":0.5000000000000001,\"Magnitude\":0.5000000000000001,\"Phase\":1.5707963267948966}]}", "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", " \r\n", " \r\n", " \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", " \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", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "
Qubit IDs0, 1
Basis state (little endian)AmplitudeMeas. Pr.Phase
$\\left|0\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|1\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|2\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|3\\right\\rangle$$0.0000 + 0.5000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
" ], "text/plain": [ "|0⟩\t0.5000000000000001 + 0𝑖\n", "|1⟩\t0.5000000000000001 + 0𝑖\n", "|2⟩\t0.5000000000000001 + 0𝑖\n", "|3⟩\t0 + 0.5000000000000001𝑖" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/json": "{\"div_id\":\"dump-machine-div-19a81b5e-cba8-4cac-8ad0-b92cb7746ede\",\"qubit_ids\":[0,1],\"n_qubits\":2,\"amplitudes\":[{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":0.0},{\"Real\":-0.5000000000000001,\"Imaginary\":0.0,\"Magnitude\":0.5000000000000001,\"Phase\":3.141592653589793}]}", "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", " \r\n", " \r\n", " \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", " \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", " \r\n", " \r\n", " \r\n", " \r\n", " \r\n", "
Qubit IDs0, 1
Basis state (little endian)AmplitudeMeas. Pr.Phase
$\\left|0\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|1\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|2\\right\\rangle$$0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
$\\left|3\\right\\rangle$$-0.5000 + 0.0000 i$\r\n", " \r\n", " \r\n", "

\r\n", "

\r\n", "
\r\n", " ↑\r\n", "
" ], "text/plain": [ "|0⟩\t0.5000000000000001 + 0𝑖\n", "|1⟩\t0.5000000000000001 + 0𝑖\n", "|2⟩\t0.5000000000000001 + 0𝑖\n", "|3⟩\t-0.5000000000000001 + 0𝑖" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate CustomConditionalPhase" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3-6: Remote-controlled randomness" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "application/json": "[\"RemoteControlledRandomness\"]", "text/html": [ "" ], "text/plain": [ "RemoteControlledRandomness" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Example 3-6: Remote-controlled randomness\n", "\n", "// open namespace which defines type conversion functions\n", "open Microsoft.Quantum.Convert;\n", "// open namespace which defines MResetZ\n", "open Microsoft.Quantum.Measurement;\n", "\n", "operation RemoteControlledRandomness () : Unit {\n", " let attempts = 1000;\n", " mutable result = [0, 0, 0, 0];\n", " for i in 1 .. attempts {\n", " // allocate two qubits\n", " use (a, b) = (Qubit(), Qubit());\n", " H(a);\n", " // measuring a now will give us 0 or 1 with 50% probability\n", "\n", " H(b);\n", " T(b);\n", " H(b);\n", " // measuring b now will give us 0 with 85% probability and 1 with 15% probability\n", "\n", " // entangle a and b\n", " CNOT(a, b);\n", " // now, you can read *either* qubit and get 0 or 1 with 50% probability;\n", " // if the result is 0, then the probability of the *remaining* qubit measuring 1 is 15%, else it's 85%;\n", " // if the result is 1, the probabilities are the other way around\n", "\n", " // let's measure qubit a first\n", " // (we'll convert measurement results into integer to keep statistics in an array)\n", " let index = (M(a) == One ? 1 | 0) * 2 + (M(b) == One ? 1 | 0);\n", " set result w/= index <- result[index] + 1;\n", " }\n", " Message($\"Overall measurement counts (out of {attempts}): {result}\");\n", " let a0b0_percentage = IntAsDouble(result[0]) / IntAsDouble(result[0] + result[1]) * 100.0;\n", " Message($\"When a was measured to be 0, b was measured 0 {a0b0_percentage}% of times\");\n", " let a1b0_percentage = IntAsDouble(result[2]) / IntAsDouble(result[2] + result[3]) * 100.0;\n", " Message($\"When a was measured to be 1, b was measured 0 {a1b0_percentage}% of times\");\n", "}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overall measurement counts (out of 1000): [446,73,73,408]\n", "When a was measured to be 0, b was measured 0 85.9344894026975% of times\n", "When a was measured to be 1, b was measured 0 15.176715176715177% of times\n" ] }, { "data": { "application/json": "{\"@type\":\"tuple\"}", "text/plain": [ "()" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%simulate RemoteControlledRandomness" ] } ], "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 }