Add Single Qubit Systems Measurements tutorial (#570)

This commit is contained in:
Pratik Sathe 2020-12-18 18:19:13 -08:00 коммит произвёл GitHub
Родитель 754eb20e7e
Коммит 5eb28be678
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 1344 добавлений и 0 удалений

Просмотреть файл

@ -60,6 +60,7 @@ RUN cd ${HOME} && \
./scripts/prebuild-kata.sh tutorials/Qubit Qubit.ipynb && \
./scripts/prebuild-kata.sh tutorials/RandomNumberGeneration RandomNumberGenerationTutorial.ipynb && \
./scripts/prebuild-kata.sh tutorials/SingleQubitGates SingleQubitGates.ipynb && \
./scripts/prebuild-kata.sh tutorials/SingleQubitSystemMeasurements SingleQubitSystemMeasurements.ipynb && \
# To improve performance when loading packages at IQ# kernel initialization time,
# we remove all online sources for NuGet such that IQ# Package Loading and NuGet dependency
# resolution won't attempt to resolve package dependencies again (as it was already done

Просмотреть файл

@ -52,6 +52,8 @@ Here is the learning path we suggest you to follow if you are starting to learn
#### Quantum Computing Concepts: Measurements
* **[Single-qubit measurements (tutorial)](./tutorials/SingleQubitSystemMeasurements/)**.
Learn what quantum measurement is and how to use it for single-qubit systems.
* **[Measurements](./Measurements/)**.
Learn to distinguish quantum states using measurements.
* **[Distinguish unitaries](./DistinguishUnitaries/)**.

Просмотреть файл

@ -42,6 +42,8 @@
"\n",
"#### Quantum Computing Concepts: Measurements\n",
"\n",
"* **[Single-qubit measurements (tutorial)](./tutorials/SingleQubitSystemMeasurements/SingleQubitSystemMeasurements.ipynb)**.\n",
" Learn what quantum measurement is and how to use it for single-qubit systems.\n",
"* **[Measurements](./Measurements/Measurements.ipynb)**.\n",
" Learn to distinguish quantum states using measurements.\n",
"* **[Distinguish unitaries](./DistinguishUnitaries/DistinguishUnitaries.ipynb)**\\*.\n",

27
tutorials/SingleQubitSystemMeasurements/.vscode/launch.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/SingleQubitSystemMeasurements.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}

42
tutorials/SingleQubitSystemMeasurements/.vscode/tasks.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/SingleQubitSystemMeasurements.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SingleQubitSystemMeasurements.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/SingleQubitSystemMeasurements.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}

Просмотреть файл

@ -0,0 +1,8 @@
# Welcome!
This tutorial introduces the basics of quantum measurements for single qubit systems.
You can run the tutorial online [here](https://mybinder.org/v2/gh/Microsoft/QuantumKatas/main?filepath=tutorials/SingleQubitSystemMeasurements/SingleQubitSystemMeasurements.ipynb).
Alternatively, you can install Jupyter and Q# on your machine, as described [here](https://docs.microsoft.com/quantum/install-guide#develop-with-jupyter-notebooks), and run the tutorial locally by navigating to this folder and starting the notebook from the command line using the following command:
jupyter notebook SingleQubitSystemMeasurements.ipynb

Просмотреть файл

@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all tasks.
// You should not modify anything in this file.
// We recommend that you try to solve the tasks yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.SingleQubitSystemMeasurements {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
// Exercise 2.
operation IsQubitZero_Reference (q : Qubit) : Bool {
return M(q) == Zero;
}
// Exercise 3.
operation IsQubitMinus_Reference (q : Qubit) : Bool {
return Measure([PauliX], [q]) == One;
}
// Exercise 5.
operation IsQubitPsiPlus_Reference (q : Qubit) : Bool {
Ry(-2.0 * ArcTan2(0.8, 0.6), q);
return M(q) == Zero;
}
// Exercise 6.
operation IsQubitA_Reference (alpha : Double, q : Qubit) : Bool {
Rx(-2.0 * alpha, q);
return M(q) == Zero;
}
// Exercise 7.
operation MeasureInABBasis_Reference (alpha : Double, q : Qubit) : Result {
Rx(-2.0 * alpha, q);
let measurementResult = M(q);
Rx(2.0 * alpha, q);
return measurementResult;
}
}

Просмотреть файл

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.20102604">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<RootNamespace>Quantum.Kata.SingleQubitSystemMeasurements</RootNamespace>
<IQSharpLoadAutomatically>true</IQSharpLoadAutomatically>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Katas" Version="0.13.20102604" />
<PackageReference Include="Microsoft.Quantum.Xunit" Version="0.13.20102604" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,570 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Measurements for single-qubit systems\n",
"\n",
"This tutorial introduces you to measurements done on single-qubit systems. The concept of a measurement is a central part of quantum mechanics, as well as quantum algorithms. Single-qubit measurements, as their name implies are measurements on single qubits. The outcomes of a measurement in quantum mechanics are probabilistic, and in general, change the state of the qubit depending on the outcome of the measurement. \n",
"\n",
"We recommend to go through the [tutorial that introduces single qubit gates](../SingleQubitGates/SingleQubitGates.ipynb) before starting this one.\n",
"\n",
"This tutorial covers the following topics:\n",
"\n",
"* Computational basis measurements\n",
"* Pauli basis measurements\n",
"* Measurements in arbitrary orthogonal bases\n",
"* Representing measurements as projector operators\n",
"$\\newcommand{\\ket}[1]{\\left|#1\\right>}$\n",
"$\\newcommand{\\bra}[1]{\\left<#1\\right|}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Computational basis measurements\n",
"In this section, we will discuss the simplest type of qubit measurements - measurements in the computational basis. (This is the \"default\" type of measurements - unless otherwise specified, \"measurement\" refers to this type.)\n",
"\n",
"The state $\\ket{\\psi}$ of a single qubit can always be expressed in [Dirac notation](../Qubit/Qubit.ipynb#Dirac-Notation) as \n",
"$$\\ket{\\psi} = \\alpha \\ket{0} + \\beta \\ket{1},$$\n",
"where $\\alpha$ and $\\beta$ are complex numbers, and the state is normalized, i.e., $|\\alpha|^2 + |\\beta|^2 = 1$. \n",
"\n",
"We can examine the qubit to get some information about its state - *measure* its state. Similar to the classical case of examining a bit, the outcome of a measurement can be $0$ or $1$. But, unlike the classical case, quantum measurement is a probabilistic process. \n",
"The probabilities of the measurement outcomes being $0$ and $1$ are $|\\alpha|^2$ and $|\\beta|^2$, respectively. Additionally, the state of the qubit is modified by the measurement: if the outcome of the measurement is $0$, then the post-measurement state of the qubit is $\\ket{0}$, and if the outcome is $1$, the state is $\\ket{1}$. In quantum mechanics, this is referred to as the [collapse of the wave function](https://en.wikipedia.org/wiki/Wave_function_collapse).\n",
"\n",
"Computational basis measurement outcomes and their probabilities are summarized in the table below:\n",
"<table style=\"border:1px solid\">\n",
" <col width=150>\n",
" <col width=150>\n",
" <col width=150>\n",
" <tr>\n",
" <th style=\"text-align:center; border:1px solid\">Measurement outcome</th>\n",
" <th style=\"text-align:center; border:1px solid\">Probability of outcome</th>\n",
" <th style=\"text-align:center; border:1px solid\">State after measurement</th>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:center; border:1px solid\">$0$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$|\\alpha|^2$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$\\ket 0$</td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:center; border:1px solid\">$1$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$|\\beta|^2$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$\\ket 1$</td>\n",
" </tr>\n",
" \n",
"</table>\n",
"\n",
"> Unlike quantum gates which are unitary and reversible operations, measurements are neither unitary nor reversible. Since the outcomes of a measurement are probabilistic, any two isolated qubits which are initially prepared in identical superposition states are in general not guaranteed to have the same measurement outcomes after each qubit has been measured separately. As we will see below, measurements are modeled by projection operators instead of unitary operators.\n",
">\n",
"> Additionally, the assumption of the wave function being **normalized** is important, since the probability outcomes must sum up to $1$. If the wave function is not normalized, it is important to normalize it first in order to obtain the correct measurement probabilities."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 1</span>: The probability outcomes for a specific state\n",
"\n",
"The qubit is in the following state:\n",
"$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1 \\equiv \\begin{bmatrix} 0.6 \\\\ 0.8\\end{bmatrix}.$$\n",
"\n",
"If this qubit is measured in the computational basis, what are the outcome probabilities?\n",
"\n",
"*Can't come up with a solution? See the explained solution in the [Single Qubit System Measurements Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-1:-The-probability-outcomes-for-a-specific-state).*\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Demo: Implementing measurement in Q# using the M operation</span>\n",
"\n",
"In this demo, we prepare a qubit in the state we've seen in Exercise 1, and then measure it in the computational basis. In Q#, single-qubit measurements in the computational basis can be implemented using the [M operation](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.m). It will return the constant `Zero` if measurement result was $0$ or the constant `One` if the measurement result was $1$. `Zero` and `One` are constants of type `Result`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Run this cell using Ctrl+Enter (⌘+Enter on Mac)\n",
"// Run the next cell to see the output\n",
"\n",
"open Microsoft.Quantum.Diagnostics;\n",
"open Microsoft.Quantum.Math;\n",
"\n",
"operation SimpleMeasurementDemo () : Unit {\n",
" using (q = Qubit()) {\n",
" // Prepare the qubit in the superposition state\n",
" // |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n",
" Ry(2.0 * ArcTan2(0.8, 0.6), q);\n",
"\n",
" Message(\"Qubit in state |𝜓❭:\");\n",
" DumpMachine();\n",
" \n",
" Message(\"Measuring the qubit...\");\n",
" let outcome = (M(q) == One ? 1 | 0);\n",
"\n",
" Message($\"The measurement outcome is {outcome}.\");\n",
" Message(\"Post-measurement state of the qubit:\");\n",
" DumpMachine();\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%simulate SimpleMeasurementDemo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> If you run the cell above multiple times, you will notice that whenever the measurement outcome is $1$, the post-measurement state of the qubit is $\\ket 1$, and similarly for $0$. This is in line with our expectation that after the measurement the wave function 'collapses' to the corresponding state."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Demo: Measurement statistics</span>\n",
"\n",
"The following cell contains code demonstrating that the theoretical and experimental values of the probability outcomes indeed match with each other. We repeatedly prepare the same state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ and measure it in the computational basis $100$ times. At the end, we expect $0$ to be measured approximately $36$ times, and $1$ to be measured approximately $64$ times. Note that since measurements are probabilistic, we do not expect the results to match these values exactly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"open Microsoft.Quantum.Diagnostics;\n",
"open Microsoft.Quantum.Math;\n",
"\n",
"operation MeasumentStatisticsDemo () : Unit {\n",
" mutable countZero = 0;\n",
" let numRuns = 100;\n",
" using (q = Qubit()) {\n",
" for (i in 1 .. numRuns) {\n",
" // Prepare the qubit in the superposition state\n",
" // |𝜓❭ = 0.6 |0❭ + 0.8 |1❭\n",
" Ry(2.0 * ArcTan2(0.8, 0.6), q);\n",
" \n",
" // Measure in the computational basis, and update the counts according to the outcomes\n",
" if (M(q) == Zero) {\n",
" set countZero += 1;\n",
" } \n",
" // Reset the qubit for use in the next iteration\n",
" Reset(q);\n",
" }\n",
" }\n",
" let countOne = numRuns - countZero;\n",
" \n",
" Message($\"Simulated probability of measuring 0 is 0.{countZero}.\");\n",
" Message($\"Theoretical probability of measuring 0 is 0.36.\");\n",
" \n",
" Message($\"Simulated probability of measuring 1 is 0.{countOne}.\");\n",
" Message($\"Theoretical probability of measuring 0 is 0.64.\");\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%simulate MeasumentStatisticsDemo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Measurements can be used to distinguish orthogonal states. We start with an exercise for distinguishing between the computational basis states, and discuss the general case of arbitrary basis measurements later in the tutorial.\n",
"\n",
"### <span style=\"color:blue\">Exercise 2</span>: Distinguish $|0\\rangle$ and $|1\\rangle$\n",
"\n",
"**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|1\\rangle$ state.\n",
"\n",
"**Output:** `true` if the qubit was in the $|0\\rangle$ state, or `false` if it was in the $|1\\rangle$ state. The state of the qubit at the end of the operation does not matter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T2_IsQubitZero\n",
"\n",
"operation IsQubitZero (q : Qubit) : Bool {\n",
" // The operation M will measure a qubit in the computational basis (|0⟩ and |1⟩ basis)\n",
" // and return Zero if the observed state was |0⟩ or One if the state was |1⟩.\n",
" // Measuring a basis state will yield the result matching that state deterministically.\n",
" // To answer the question, you need to perform the measurement and check whether the result\n",
" // equals One - either directly or using library function IsResultOne.\n",
" //\n",
" // Type the following: return M(q) == Zero;\n",
" // Then run the cell using Ctrl+Enter (⌘+Enter on macOS).\n",
"\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Single Qubit System Measurements Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-2:-Distinguish-$|0\\rangle$-and-$|1\\rangle$).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Measurements in the Pauli bases\n",
"So far, we have discussed measurements done in the computational basis, i.e., the $\\{ \\ket 0, \\ket 1\\}$ basis. \n",
"\n",
"It is also possible to implement measurements in other orthogonal bases, such as the [Pauli X basis](../SingleQubitGates/SingleQubitGates.ipynb#Pauli-Gates), which consists of the two vectors $\\ket + = \\frac1{\\sqrt2} \\big(\\ket 0 +\\ket 1\\big)$, and $\\ket - = \\frac1{\\sqrt2} \\big(\\ket 0 -\\ket 1\\big)$. Q# has a built-in operation [Measure](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure) for measurements in the Pauli bases. \n",
"\n",
"> The `Measure` operation can be used for measuring multiple qubits in a multi-qubit system; however, in this tutorial we only consider measurements for single-qubit systems.\n",
"\n",
"The eigenvalues of a Pauli matrix are $\\pm 1$, with one eigenvector corresponding to each eigenvalue. For any chosen Pauli basis, the `Measure` operation returns `Zero` if the measurement outcome corresponds to the eigenvalue $+1$, and returns `One` if the measurement outcome corresponds to the eigenvalue $-1$. As in the case of the computational basis measurements, the wave function of the qubit collapses to the corresponding state after the measurement is executed. \n",
"\n",
"The probabilities of the outcomes are defined using a similar rule: to measure a state $\\ket \\psi$ in a Pauli basis $\\{ \\ket {b_0}, \\ket {b_1}\\}$, we represent it as a linear combination of the basis vectors\n",
"$$\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}$$\n",
"\n",
"The probabilities of outcomes $0$ and $1$ will be defined as $|c_0|^2$ and $|c_1|^2$, respectively.\n",
"\n",
"> Computational basis measurement is often referred to as measurement in Pauli Z basis. Indeed, the eigenvectors of the Z gate are $\\ket 0$ and $\\ket 1$, with eigenvalues $+1$ and $-1$, respectively."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 3</span>: Distinguish $|+\\rangle$ and $|-\\rangle$ using the `Measure` operation\n",
"\n",
"**Input**: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.\n",
"\n",
"**Output**: `true` if the qubit is in the $\\ket -$ state, or `false` if it was in the $\\ket +$ state. \n",
"\n",
"> To perform a single-qubit measurement in a certain Pauli basis using the `Measure` operation, \n",
"> you need to pass it two parameters: first, an array of one `Pauli` constant (`PauliX`, `PauliY` or `PauliZ`), and second, an array of one qubit you want to measure."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T3_IsQubitMinus\n",
"\n",
"operation IsQubitMinus (q : Qubit) : Bool {\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Single Qubit System Measurements Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-3:-Distinguish-$|+\\rangle$-and-$|-\\rangle$-using-the-Measure-operation).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Measurements in arbitrary orthogonal bases\n",
"It is possible to measure a qubit in orthogonal bases other than the Pauli bases. Suppose one wants to measure a qubit in an orthonormal basis $\\ket {b_0}$ and $\\ket {b_1}$. Let the state of the qubit be represented by the normalized vector $\\ket \\psi$. Then, one can always express the state in terms of the basis vectors $\\ket{b_0}$ and $\\ket{b_1}$, i.e., there exist complex numbers $c_0, c_1$, such that \n",
"$$\n",
"\\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1}.\n",
"$$\n",
"The rule for obtaining the probabilities of measurement outcomes is exactly the same as that for the computation basis measurement. For a measurement in a $\\{ b_0, b_1\\}$ basis we get\n",
"- Outcome $b_0$ with probability $|c_0|^2$, and the post-measurement state of the qubit $\\ket {b_0}$;\n",
"- Outcome $b_1$ with probability $|c_1|^2$, and the post-measurement state of the qubit $\\ket {b_1}$.\n",
"\n",
"This can be summarized in the following table:\n",
"<table style=\"border:1px solid\">\n",
" <col width=150>\n",
" <col width=150>\n",
" <col width=150>\n",
" <tr>\n",
" <th style=\"text-align:center; border:1px solid\">Measurement outcome</th>\n",
" <th style=\"text-align:center; border:1px solid\">Probability of outcome</th>\n",
" <th style=\"text-align:center; border:1px solid\">State after measurement</th>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:center; border:1px solid\">$b_0$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$|c_0|^2$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$\\ket{b_0}$</td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:center; border:1px solid\">$b_1$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$|c_1|^2$</td>\n",
" <td style=\"text-align:center; border:1px solid\">$\\ket{b_1}$</td>\n",
" </tr>\n",
" \n",
"</table>\n",
"\n",
"As before, the assumption of $\\ket \\psi$ being normalized is important, since it guarantees that the two probabilities add to $1$.\n",
"\n",
"> As you may recall, a [global phase](../Qubit/Qubit.ipynb#Relative-and-Global-Phase) is said to be hidden or unobservable. \n",
"This is explained by the fact that global phases have no impact on quantum measurements. For example, consider two isolated qubits which are in (normalized) states $\\ket \\psi$ and $e^{i\\theta}\\ket \\psi$. \n",
"If both are measured in an orthogonal basis $\\{ \\ket{b_0},\\ket{b_1}\\}$, the probabilities of measuring $b_0$ or $b_1$ are identical in both cases, since $|\\bra{b_i}\\ket{\\psi}|^2 = |\\bra{b_i}e^{i\\theta}\\ket{\\psi}|^2 $. \n",
"Similarly, for either qubit, if $b_i$ is the measurement outcome, the post-measurement state of the qubit is $\\ket{b_i}$ for both qubits. Hence, the measurements are independent of the global phase $\\theta$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> ### Measurements as projection operations\n",
"Quantum measurements are modeled by orthogonal projection operators. An orthogonal projection operator is a matrix $P$ which satisfies the following property:\n",
"$$\n",
"P^2 = P^\\dagger = P.\n",
"$$\n",
"(As usual, the $\\dagger$ symbol denotes conjugate transposition.) \n",
">\n",
"> Using the [ket-bra representation](../SingleQubitGates/SingleQubitGates.ipynb#Ket-bra-Representation), one can represent a projection matrix in the Dirac notation.\n",
"For example, one may construct a projector onto the $\\ket{0}$ subspace as:\n",
"$$\n",
"P = \\ket 0 \\bra 0 \\equiv \\begin{bmatrix} 1 & 0 \\\\ 0 & 0\\end{bmatrix}.\n",
"$$\n",
">\n",
">A measurement in an orthogonal basis $\\{ \\ket{b_0}, \\ket{b_1}\\}$ is described by a pair of projectors $P_0 = \\ket{b_0}\\bra{b_0}$ and $P_1 = \\ket{b_1}\\bra{b_1}$. Since $\\ket{b_0}$ and $\\ket{b_1}$ are orthogonal, their projectors are also orthogonal, i.e., $P_0 P_1 = P_1 P_0 = 0$. The rules for measurements in this basis can then be summarized as follows: \n",
"- Measuring a qubit in a state $\\ket \\psi$ is done by picking one of these projection operators at random.\n",
"- Projection $P_0$ is chosen with probability $|P_0 \\ket{\\psi}|^2$, and the projector $P_1$ is chosen with probability $|P_1\\ket{\\psi}|^2$.\n",
"- If projector $P_0$ is chosen, the post-measurement state of the qubit is given by\n",
"$$\n",
"\\frac1{|P_0 \\ket{\\psi}|}P_0 \\ket\\psi,\n",
"$$\n",
"and similarly for $P_1$.\n",
">\n",
">Although this formalism looks different from the previous sections, it is in fact equivalent. If $\\ket \\psi = c_0 \\ket{b_0} + c_1 \\ket{b_1}$, we have \n",
"$$\n",
"P_0 \\ket \\psi = c_0 \\ket{b_0}, \\text{so that } | P_0\\ket \\psi| = c_0,\n",
"$$\n",
"and similarly, \n",
"$$\n",
"P_1 \\ket \\psi = c_1 \\ket{b_1}, \\text{so that } |P_1\\ket \\psi| = c_1.\n",
"$$\n",
">\n",
">Thus, as before, the probability of measuring $b_0$ is $|P_0\\ket\\psi|^2 = |c_0|^2$, and the probability of measuring $b_1$ is $|P_1\\ket\\psi|^2 = |c_1|^2$. Similarly, one can verify that the post-measurement outcomes are also $\\ket{b_0}$ and $\\ket{b_1}$ respectively (up to unobservable global phases).\n",
">\n",
">Although the projector formalism for single-qubit systems may seem superfluous, its importance will become clear later, while considering measurements for multi-qubit systems."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Arbitrary basis measurements implementation\n",
"In the previous section, we discussed measurements in Pauli bases using the built-in `Measure` operation. We will now show that using just unitary rotation matrices and computation basis measurements it is always possible to measure a qubit in any orthogonal basis. \n",
"\n",
"Consider a state $ \\ket \\psi = c_0 \\ket {b_0} + c_1 \\ket {b_1} $ which we would like to measure in an orthonormal basis $\\{ \\ket{b_0}, \\ket{b_1}\\}$. First, we construct the following unitary matrix:\n",
"$$\n",
"U = \\ket{0} \\bra{b_0} + \\ket{1} \\bra{b_1}\n",
"$$\n",
"\n",
"The conjugate transpose of this unitary is the operator \n",
"$$\n",
"U^\\dagger = \\ket{b_0} \\bra{0} + \\ket{b_1} \\bra{1}\n",
"$$\n",
"\n",
"(One may verify that $U$ is indeed a unitary matrix, by checking that $U^\\dagger U = U U^\\dagger = I$.)\n",
"\n",
"Note that the effect of these matrices on the two bases is the following:\n",
"\\begin{align}\n",
"U\\ket{b_0} &= \\ket{0}; & U\\ket{b_1} &= \\ket{1}\\\\\n",
"U^\\dagger \\ket{0} &= \\ket{b_0}; & U^\\dagger \\ket 1 &= \\ket{b_1}.\n",
"\\end{align}\n",
"\n",
"In order to implement a measurement in the $\\{ \\ket{b_0}, \\ket{b_1}\\}$ basis, we do the following:\n",
"\n",
"1. Apply $U$ to $\\ket \\psi$. \n",
" The resulting state is $U\\ket \\psi = c_0 \\ket 0 + c_1 \\ket 1 $.\n",
"2. Measure the state $U\\ket{\\psi}$ in the computational basis. \n",
" The outcomes $0$ and $1$ occur with probabilities $|c_0|^2$ and $|c_1|^2$.\n",
"3. Apply $U^\\dagger$ to the post-measurement state. \n",
" This transforms the states $\\ket 0$ and $\\ket 1$ to the states $\\ket{b_0}$ and $\\ket{b_1}$, respectively.\n",
"\n",
"Thus, $b_0$ and $b_1$ are measured with probabilities $|c_0|^2$ and $|c_1|^2$, respectively, with the end state being $\\ket{b_0}$ and $\\ket{b_1}$ - which is exactly the measurement we want to implement. \n",
"\n",
"This procedure can be used to distinguish arbitrary orthogonal states as well, as will become clear from the following exercises."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 4</span>: The outcome probabilities for a measurement in a specified basis\n",
"<ol>\n",
"<li>What are the outcome probabilities of measuring a qubit in the $\\ket{0}$ state in the Pauli X basis, i.e., the $\\{ \\ket +, \\ket -\\}$ basis?</li>\n",
"<li>What are the outcome probabilities of measuring a qubit in the $0.6\\ket{0} + 0.8 \\ket{1}$ state in the Pauli Y basis, i.e., the $\\{ \\ket i, \\ket{-i}\\}$ basis?</li>\n",
"</ol> \n",
"\n",
"*Can't come up with a solution? See the explained solution in the [Single Qubit Measurement Tutorial Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-4:-The-outcome-probabilities-for-a-measurement-in-a-specified-basis).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 5</span>: Distinguishing orthogonal states - 1\n",
"\n",
"**Input:** A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = -0.8\\ket 0 + 0.6 \\ket 1$.\n",
"\n",
"**Output:** `true` if the qubit was in the $\\ket {\\psi_+}$ state, or `false` if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter.\n",
"\n",
"<br/>\n",
"<details>\n",
" <summary><strong>Need a hint? Click here</strong></summary>\n",
" A suitable <a href=\"../SingleQubitGates/SingleQubitGates.ipynb#Rotation-Gates\">$R_y$ rotation</a> can be used to go from the computational basis $\\{ \\ket 0, \\ket 1 \\}$ to the $\\{ \\ket{\\psi_+}, \\ket{\\psi_-} \\}$ basis and vice versa. \n",
" \n",
"</details>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T5_IsQubitPsiPlus\n",
"\n",
"open Microsoft.Quantum.Math;\n",
"\n",
"operation IsQubitPsiPlus (q : Qubit) : Bool {\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Single Qubit Measurement Tutorial Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-5:-Distinguishing-orthogonal-states---1).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 6</span>: Distinguishing orthogonal states - 2\n",
"\n",
"**Inputs:** \n",
"\n",
"1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
"2. A qubit which is guaranteed to be in either the $|A\\rangle$ or the $|B\\rangle$ state, where $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$.\n",
"\n",
"**Output:** `true` if the qubit was in the $|A\\rangle$ state, or `false` if it was in the $|B\\rangle$ state. The state of the qubit at the end of the operation does not matter.\n",
"\n",
"<br/>\n",
"<details>\n",
" <summary><strong>Need a hint? Click here</strong></summary>\n",
" An <a href=\"../SingleQubitGates/SingleQubitGates.ipynb#Rotation-Gates\">$R_x$ rotation</a> can be used to go from the computational basis $\\{ \\ket 0, \\ket 1 \\}$ to the $\\{ \\ket{A}, \\ket{B} \\}$ basis and vice versa. \n",
" \n",
"</details>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T6_IsQubitA\n",
"\n",
"operation IsQubitA (alpha : Double, q : Qubit) : Bool {\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Single Qubit Measurement Tutorial Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-6:-Distinguishing-orthogonal-states---2).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 7</span>: Measurement in the $\\ket A$, $\\ket B$ basis\n",
"\n",
"**Inputs:** \n",
"1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
"1. A qubit in some unknown state.\n",
"\n",
"\n",
"**Output:** Implement a measurement in the $\\{\\ket A$, $\\ket B\\}$ basis. Same as in the previous exercise, $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$. Return `Zero` if the measurement outcome is $A$, and `One` if the outcome is $B$. \n",
"The state of the qubit after the measurement should correspond to the measurement result.\n",
"\n",
"<br/>\n",
"<details>\n",
" <summary><strong>Need a hint? Click here</strong></summary>\n",
" An <a href=\"../SingleQubitGates/SingleQubitGates.ipynb#Rotation-Gates\">$R_x$ rotation</a> can be used to go from the computational basis $\\{ \\ket 0, \\ket 1 \\}$ to the $\\{ \\ket{A}, \\ket{B} \\}$ basis and vice versa. \n",
"</details>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T7_MeasureInABBasis\n",
"\n",
"operation MeasureInABBasis (alpha: Double, q : Qubit) : Result {\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Single Qubit Measurement Tutorial Workbook](./Workbook_SingleQubitSystemMeasurements.ipynb#Exercise-7:-Measurement-in-the-$\\ket-A$,-$\\ket-B$-basis).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"Congratulations! You have learned enough to try solving the first part of the [Measurements kata](../../Measurements/Measurements.ipynb). \n",
"When you are done with that, you can continue to the next tutorials in the series to learn about measurements for multi-qubit systems (coming soon...)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Q#",
"language": "qsharp",
"name": "iqsharp"
},
"language_info": {
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

Просмотреть файл

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30621.155
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SingleQubitSystemMeasurements", "SingleQubitSystemMeasurements.csproj", "{003B29F4-1347-4C48-BC9F-FE8DB21EC9D8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{003B29F4-1347-4C48-BC9F-FE8DB21EC9D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{003B29F4-1347-4C48-BC9F-FE8DB21EC9D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{003B29F4-1347-4C48-BC9F-FE8DB21EC9D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{003B29F4-1347-4C48-BC9F-FE8DB21EC9D8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A7683A15-2651-449A-84B2-50D03BA2686B}
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////
// This file is a back end for the tasks in the tutorial.
// We strongly recommend to use the Notebook version of the tutorial
// to enjoy the full experience.
//////////////////////////////////////////////////////////////////
namespace Quantum.Kata.SingleQubitSystemMeasurements {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
// Exercise 2. Distinguish |0❭ and |1❭
operation IsQubitZero (q : Qubit) : Bool {
// ...
return false;
}
// Exercise 3. Distinguish |+❭ and |-❭ using Measure operation
operation IsQubitMinus (q : Qubit) : Bool {
// ...
return false;
}
// Exercise 5. Distinguish specific orthogonal states
operation IsQubitPsiPlus (q : Qubit) : Bool {
// ...
return false;
}
// Exercise 6. Distinguish states |A❭ and |B❭
operation IsQubitA (alpha : Double, q : Qubit) : Bool {
// ...
return false;
}
// Exercise 7. Measure state in {|A❭, |B❭} basis
operation MeasureInABBasis (alpha : Double, q : Qubit) : Result {
// ...
return One;
}
}

Просмотреть файл

@ -0,0 +1,181 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains testing harness for all tasks.
// You should not modify anything in this file.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.SingleQubitSystemMeasurements {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Random;
//////////////////////////////////////////////////////////////////
// "Framework" operation for testing single-qubit tasks for distinguishing states of one qubit
// with Bool return
operation DistinguishTwoStates (statePrep : ((Qubit, Int) => Unit is Adj), testImpl : (Qubit => Bool), stateName : String[], checkFinalState : Bool) : Unit {
let nTotal = 100;
let nStates = 2;
mutable misclassifications = new Int[nStates];
using (q = Qubit()) {
for (i in 1 .. nTotal) {
// get a random bit to define whether qubit will be in a state corresponding to true return (1) or to false one (0)
// state = 0 false return
// state = 1 true return
let state = DrawRandomInt(0, 1);
// do state prep: convert |0⟩ to outcome with false return or to outcome with true return depending on state
statePrep(q, state);
// get the solution's answer and verify if NOT a match, then differentiate what kind of mismatch
let ans = testImpl(q);
if (ans != (state == 1)) {
set misclassifications w/= state <- misclassifications[state] + 1;
}
// If the final state is to be verified, check if it matches the measurement outcome
if (checkFinalState) {
Adjoint statePrep(q, state);
AssertQubit(Zero, q);
} else {
Reset(q);
}
}
}
mutable totalMisclassifications = 0;
for (i in 0 .. nStates - 1) {
if (misclassifications[i] != 0) {
set totalMisclassifications += misclassifications[i];
Message($"Misclassified {stateName[i]} as {stateName[1 - i]} in {misclassifications[i]} test runs.");
}
}
// This check will tell the total number of failed classifications
Fact(totalMisclassifications == 0, $"{totalMisclassifications} test runs out of {nTotal} returned incorrect state (see output for details).");
}
// ------------------------------------------------------
// Exercise 2. Distinguish |0❭ and |1❭
// ------------------------------------------------------
operation StatePrep_IsQubitZero (q : Qubit, state : Int) : Unit is Adj {
if (state == 0) {
// convert |0⟩ to |1⟩
X(q);
}
}
@Test("QuantumSimulator")
operation T2_IsQubitZero () : Unit {
DistinguishTwoStates(StatePrep_IsQubitZero, IsQubitZero, ["|1⟩", "|0⟩"], false);
}
// ------------------------------------------------------
// Exercise 3. Distinguish |+❭ and |-❭ using Measure operation
// ------------------------------------------------------
operation StatePrep_IsQubitMinus (q : Qubit, state : Int) : Unit is Adj {
if (state == 1) {
// convert |0⟩ to |-⟩
X(q);
H(q);
} else {
// convert |0⟩ to |+⟩
H(q);
}
}
@Test("QuantumSimulator")
operation T3_IsQubitMinus () : Unit {
DistinguishTwoStates(StatePrep_IsQubitMinus, IsQubitMinus, ["|+⟩", "|-⟩"], false);
}
// ------------------------------------------------------
// Exercise 5. Distinguish specific orthogonal states
// ------------------------------------------------------
// |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,
// |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.
operation StatePrep_IsQubitPsiPlus (q : Qubit, state : Int) : Unit is Adj {
if (state == 0) {
// convert |0⟩ to |ψ₋⟩
X(q);
Ry(2.0 * ArcTan2(0.8, 0.6), q);
} else {
// convert |0⟩ to |ψ₊⟩
Ry(2.0 * ArcTan2(0.8, 0.6), q);
}
}
@Test("QuantumSimulator")
operation T5_IsQubitPsiPlus () : Unit {
DistinguishTwoStates(StatePrep_IsQubitPsiPlus, IsQubitPsiPlus,
["|ψ₋⟩", "|ψ₊⟩"], false);
}
// ------------------------------------------------------
// Exercise 6. Distinguish states |A❭ and |B❭
// ------------------------------------------------------
// |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,
// |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.
operation StatePrep_IsQubitA (alpha : Double, q : Qubit, state : Int) : Unit is Adj {
if (state == 0) {
// convert |0⟩ to |B⟩
X(q);
Rx(2.0 * alpha, q);
} else {
// convert |0⟩ to |A⟩
Rx(2.0 * alpha, q);
}
}
@Test("QuantumSimulator")
operation T6_IsQubitA () : Unit {
for (i in 0 .. 10) {
let alpha = (PI() * IntAsDouble(i)) / 10.0;
DistinguishTwoStates(StatePrep_IsQubitA(alpha, _, _), IsQubitA(alpha, _),
[$"|B⟩ = -i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩", $"|A⟩ = cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩"], false);
}
}
// ------------------------------------------------------
// Exercise 7. Measure state in {|A❭, |B❭} basis
// ------------------------------------------------------
// |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,
// |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.
// Wrapper function to convert the Result output of MeasureInABBasis to a bool type
operation IsResultZero( MeasurementOperation : (Qubit => Result), givenQubit : Qubit) : Bool {
mutable isZero = false;
if (MeasurementOperation(givenQubit) == Zero) {
set isZero = true;
}
return isZero;
}
// We can use the StatePrep_IsQubitA operation for the testing
@Test("QuantumSimulator")
operation T7_MeasureInABBasis () : Unit {
for (i in 0 .. 10) {
let alpha = (PI() * IntAsDouble(i)) / 10.0;
DistinguishTwoStates(StatePrep_IsQubitA(alpha, _, _), IsResultZero(MeasureInABBasis(alpha, _),_),
[$"|B⟩=(-i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)", $"|A⟩=(cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩)"], true);
}
}
}

Просмотреть файл

@ -0,0 +1,379 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Single-Qubit System Measurements Tutorial Workbook\n",
"\n",
"**What is this workbook?**\n",
"A workbook is a collection of problems, accompanied by solutions to them. \n",
"The explanations focus on the logical steps required to solve a problem; they illustrate the concepts that need to be applied to come up with a solution to the problem, explaining the mathematical steps required. \n",
"\n",
"Note that a workbook should not be the primary source of knowledge on the subject matter; it assumes that you've already read a tutorial or a textbook and that you are now seeking to improve your problem-solving skills. You should attempt solving the tasks of the respective kata first, and turn to the workbook only if stuck. While a textbook emphasizes knowledge acquisition, a workbook emphasizes skill acquisition.\n",
"\n",
"This workbook describes the solutions to the problems offered in the [Single-Qubit System Measurements tutorial](./SingleQubitSystemMeasurements.ipynb). \n",
"Since the tasks are offered as programming problems, the explanations also cover some elements of Q# that might be non-obvious for a first-time user.\n",
"\n",
"**What you should know for this workbook**\n",
"\n",
"You should be familiar with the following concepts before tackling the Single-Qubit System Measurements tutorial (and this workbook):\n",
"1. Basic linear algebra\n",
"2. The concept of a qubit\n",
"3. Single-qubit gates\n",
"$\\newcommand{\\ket}[1]{\\left|#1\\right>}$\n",
"$\\newcommand{\\bra}[1]{\\left<#1\\right|}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 1</span>: The probability outcomes for a specific state\n",
"The qubit is in the following state:\n",
"$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1 \\equiv \\begin{bmatrix} 0.6 \\\\ 0.8\\end{bmatrix}.$$\n",
"\n",
"If this qubit is measured in the computational basis, what are the outcome probabilities?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"The given state $\\ket \\psi$ is normalized, since $0.6^2 + 0.8^2 = 1$. Hence, the probability of measuring $0$ is $|0.6|^2 = 0.36$, and the probability of measuring $1$ is $|0.8|^2 = 0.64$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 1 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-1:-The-probability-outcomes-for-a-specific-state)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 2</span>: Distinguish $|0\\rangle$ and $|1\\rangle$\n",
"\n",
"**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|1\\rangle$ state.\n",
"\n",
"**Output:** `true` if the qubit was in the $|0\\rangle$ state, or `false` if it was in the $|1\\rangle$ state. The state of the qubit at the end of the operation does not matter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"\n",
"The input qubit is guaranteed to be either in basis state $|0\\rangle$ or $|1\\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt. \n",
"\n",
"In Q# the operation [`M`](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.m) can be used to measure a single qubit in the computational basis. The measurement result is a value of type `Result`: the operation `M` will return `One` if the input qubit was in the $|1\\rangle$ state and `Zero` if the input qubit was in the $|0\\rangle$ state. Since we need to encode the first case as `false` and the second one as `true`, we can return the result of equality comparison between measurement result and `Zero`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T2_IsQubitZero\n",
"\n",
"operation IsQubitZero (q : Qubit) : Bool {\n",
" return M(q) == Zero;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 2 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-2:-Distinguish-$|0\\rangle$-and-$|1\\rangle$)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 3</span>: Distinguish $|+\\rangle$ and $|-\\rangle$ using the `Measure` operation\n",
"\n",
"**Input**: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.\n",
"\n",
"**Output**: `true` if the qubit is in the $\\ket +$ state, or `false` if it was in the $\\ket -$ state."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"\n",
"The input qubit is guaranteed to be either in basis state $|+\\rangle$ or $|-\\rangle$. This means that when measuring the qubit in the Pauli $X$ basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli $X$ matrix). \n",
"\n",
"In Q# the operation [`Measure`](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure) can be used to measure a qubit in Pauli basis of the user's choice. The operation returns a value of type `Result`, and is `Zero` if the measured state corresponds to the eigenvalue $+1$, and `One` if it corresponds to the eigenvalue $-1$ of the Pauli operator. \n",
"\n",
"Since the states $\\ket +$ and $\\ket -$ correspond to the eigenvalues $+1$ and $-1$ of the Pauli X operator, we can return the result of equality comparison between the measurement result and `One`. \n",
"Note that since `Measure` operation generally works with multiple qubits to perform multi-qubit measurements, it takes array parameters. To do a single-qubit measurement, you need to pass two arrays of one element, `[PauliX]` and `[q]`, rather than individual values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T3_IsQubitMinus\n",
"\n",
"operation IsQubitMinus (q : Qubit) : Bool {\n",
" return Measure([PauliX], [q]) == One;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 3 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-3:-Distinguish-$|+\\rangle$-and-$|-\\rangle$-using-the-Measure-operation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 4</span>: The outcome probabilities for a measurement in a specified basis\n",
"<ol>\n",
"<li>What are the outcome probabilities of measuring a qubit in the $\\ket{0}$ state in the Pauli X basis, i.e., the $\\{ \\ket +, \\ket -\\}$ basis?</li>\n",
"<li>What are the outcome probabilities of measuring a qubit in the $0.6\\ket{0} + 0.8 \\ket{1}$ state in the Pauli Y basis, i.e., the $\\{ \\ket i, \\ket{-i}\\}$ basis?</li>\n",
"</ol> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"\n",
"1. To find the probabilities of measuring $+$ and $-$, we first need to express the state $\\ket 0$ in terms of $\\ket +$ and $\\ket -$. Using the fact that $\\ket{\\pm} = \\frac{1}{\\sqrt{2}} (\\ket{0} \\pm \\ket{1})$, we can show that \n",
"$$\n",
"\\ket 0 = \\frac{1}{\\sqrt{2}} \\ket{+} + \\frac{1}{\\sqrt{2}} \\ket{-}.\n",
"$$\n",
" \n",
" Thus, the probability of measuring $+$ is $|\\frac1{\\sqrt2}|^2 = 0.5$, and similarly, the probability of measuring $-$ is $0.5$.\n",
"\n",
"2. Similar to the first part, we need to express the state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ in the $\\ket{\\pm i}$ basis. For this calculation, we use the projection matrix approach.\n",
"First, we recall that the states $\\ket{\\pm i}$ are given by\n",
"$$\n",
"\\ket{\\pm i} = \\frac1{\\sqrt2} (\\ket 0 \\pm i \\ket 1).\n",
"$$\n",
"We can now construct the two projectors $P_{\\pm i}$ onto states $\\ket {\\pm i}$ as follows:\n",
"\\begin{align}\n",
"P_{i} &= \\ket{i}\\bra{i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\end{bmatrix} \\begin{bmatrix} 1 & -i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\ i & 1\\end{bmatrix}; \\\\\n",
"P_{-i} &=\\ket{-i}\\bra{-i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\ -i \\end{bmatrix} \\begin{bmatrix} 1 & i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & i \\\\ -i & 1\\end{bmatrix}\n",
"\\end{align}\n",
"Recalling that the probabilities of measuring $\\pm i$ are equal to the norm of the vectors $P_{\\pm i}\\ket \\psi$, we now apply $P_{\\pm i}$ to $\\ket \\psi$:\n",
"\\begin{align}\n",
"P_{+i} \\ket \\psi &= \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\ i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 - 0.8i \\\\ 0.8 + 0.6i \\end{bmatrix} ;\\\\\n",
"P_{-i} \\ket \\psi &= \\frac{1}{2} \\begin{bmatrix}1 & i \\\\ -i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 + 0.8i \\\\ 0.8 - 0.6i \\end{bmatrix}.\n",
"\\end{align}\n",
"Hence, the probabilities of measuring $\\pm i$, which we denote by $p(\\pm i)$, are\n",
"\\begin{align}\n",
"p(+i)& = |P_{+i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 - 0.8i|^2 + |0.8 + 0.6i|^2) = \\frac{1}{2}; \\\\\n",
"p(-i)& = |P_{-i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 + 0.8i|^2 + |0.8 - 0.6i|^2) = \\frac{1}{2}.\n",
"\\end{align}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 4 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-4:-The-outcome-probabilities-for-a-measurement-in-a-specified-basis)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 5</span>: Distinguishing orthogonal states - 1\n",
"\n",
"**Inputs:** A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = 0.8\\ket 0 - 0.6 \\ket 1$.\n",
"\n",
"**Output:** `true` if the qubit was in the $\\ket {\\psi_+}$ state, or `false` if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"We can distinguish between the states $\\ket{\\psi_\\pm}$ if we implement a measurement in the $\\{ \\ket{\\psi_+}, \\ket{\\psi_-}\\}$ basis. This can be done if we construct a unitary transformation which maps the $\\ket{\\psi_+}$ state to the $\\ket{0}$ state, and the $\\ket{\\psi_{-}}$ state to the $\\ket{1}$ state. \n",
"\n",
"We can notice that the $R_y$ rotation gate with $\\theta = 2 \\arctan \\frac{0.8}{0.6}$ is an appropriate transformation:\n",
"\n",
"\\begin{align}\n",
"R_y(\\theta) \\ket 0 &= 0.6 \\ket 0 + 0.8 \\ket 1 = \\ket {\\psi_+} \\\\\n",
"R_y(\\theta) \\ket 1 &= -0.8 \\ket 0 + 0.6 \\ket 1 = \\ket{\\psi_-}\n",
"\\end{align}\n",
"\n",
"Thus, the inverse (adjoint) transformation $R_y(-\\theta)$ maps the $\\ket{\\psi_\\pm}$ basis to the computational basis, i.e.,\n",
"\\begin{align}\n",
"R_y(-\\theta) \\ket {\\psi_+} &= \\ket 0 \\\\\n",
"R_y(-\\theta) \\ket {\\psi_-} &= \\ket 1\n",
"\\end{align}\n",
"\n",
"Hence, if we apply $R_y(-\\theta)$ to the qubit, its state will be transformed to one of the computational basis states, at which point we can measure it using `M`. If `M` returns `Zero`, the rotated state is $\\ket{0}$, which means that the original state of the qubit was $\\ket{\\psi_+}$. Similarly, an output of `One` indicates that the qubit was originally in the state $\\ket{\\psi_-}$. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T5_IsQubitPsiPlus\n",
"\n",
"open Microsoft.Quantum.Math;\n",
"\n",
"operation IsQubitPsiPlus (q : Qubit) : Bool { \n",
" Ry(-2.0 * ArcTan2(0.8, 0.6), q);\n",
" return M(q) == Zero;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 5 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-5:-Distinguishing-orthogonal-states---1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 6</span>: Distinguishing orthogonal states - 2\n",
"\n",
"**Inputs:** \n",
"\n",
"1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
"2. A qubit which is guaranteed to be in either the $|A\\rangle$ or the $|B\\rangle$ state, where $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$.\n",
"\n",
"**Output:** `true` if the qubit was in the $|A\\rangle$ state, or `false` if it was in the $|B\\rangle$ state. The state of the qubit at the end of the operation does not matter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"We can distinguish between the states $\\ket{A}$ and $\\ket B$ if we implement a measurement in the $\\{ \\ket{A}, \\ket{B}\\}$ basis. \n",
"\n",
"We can notice that the $R_x$ rotation gate with $\\theta = 2 \\alpha$ is an appropriate transformation which maps the $\\ket 0 $ state to the $\\ket A$ state, and the $\\ket 1$ state to the $\\ket B$ state:\n",
"\n",
"\\begin{align}\n",
"R_x(\\theta) \\ket 0 &= \\cos \\alpha \\ket 0 -i \\sin \\alpha \\ket 1 = \\ket {A} \\\\\n",
"R_x(\\theta) \\ket 1 &= -i \\sin \\alpha \\ket 0 + \\cos \\alpha \\ket 1 = \\ket{B}\n",
"\\end{align}\n",
"\n",
"Thus, the inverse transformation $R_x(-\\theta)$ maps the $A/B$ basis to the $0/1$ basis.\n",
"\n",
"Therefore, if we apply $R_x(-\\theta)$ to the qubit and measure it using `M`, a measurement result of `Zero` will correspond to the qubit's original state being $\\ket{A}$, while a result of `One` will correspond to the qubit's original state being $\\ket B$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T6_IsQubitA\n",
"\n",
"operation IsQubitA (alpha: Double, q : Qubit) : Bool { \n",
" Rx(-2.0 * alpha, q);\n",
" return M(q) == Zero;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 6 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-6:-Distinguishing-orthogonal-states---2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <span style=\"color:blue\">Exercise 7</span>: Measurement in the $\\ket A$, $\\ket B$ basis\n",
"\n",
"**Inputs:** \n",
"1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
"1. A qubit in some unknown state.\n",
"\n",
"\n",
"**Output:** Implement a measurement in the $\\{\\ket A$, $\\ket B\\}$ basis. Same as in the previous exercise, $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$. Return `Zero` if the measurement outcome is $A$, and `One` if the outcome is $B$. \n",
"The state of the qubit after the measurement should correspond to the measurement result."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"For this problem, we follow the procedure given in the [arbitrary basis measurement section](./SingleQubitSystemMeasurements.ipynb#Arbitrary-basis-measurements-implementation) of the tutorial.\n",
"As noted in the solution of problem 6, the gate $R_x(-\\theta)$, with $\\theta = 2\\alpha$ transforms the $\\ket A/\\ket B$ states to the $\\ket 0/\\ket 1$ states:\n",
"\\begin{align}\n",
"R_x(-\\theta) \\ket A &= \\ket 0 \\\\\n",
"R_X(-\\theta) \\ket B &= \\ket 1\n",
"\\end{align}\n",
"Hence, we first apply $R_x(-\\theta)$ to the qubit. Next, we measure in the computational basis using the `M` operation. \n",
"If the `M` operation returned `Zero`, we get measurement outcome $A$, and if it returned `One`, we get measurement outcome $B$. \n",
"\n",
"After the measurement, we apply the inverse of the $R_x(-\\theta)$ gate, which is the $R_x(\\theta)$ gate.\n",
"The final rotation ensures that the state of the qubit is in the state corresponding to the measurement outcome."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%kata T7_MeasureInABBasis\n",
"\n",
"operation MeasureInABBasis (alpha: Double, q : Qubit) : Result {\n",
" Rx(-2.0 * alpha, q);\n",
" let measurementResult = M(q);\n",
" Rx(2.0 * alpha, q);\n",
" return measurementResult;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to exercise 7 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-7:-Measurement-in-the-$\\ket-A$,-$\\ket-B$-basis)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Q#",
"language": "qsharp",
"name": "iqsharp"
},
"language_info": {
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}