{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Measurements in systems with multiple qubits\n", "\n", "In the previous tutorial, we discussed the concept of measurements done on single-qubit systems. \n", "Building upon those ideas, this tutorial will introduce you to measurements done on multi-qubit systems, and how to implement such measurements in Q#. \n", "This will include measuring a single qubit in a multi-qubit system, as well as measuring multiple qubits simultaneously. \n", "\n", "We recommend to go through the [tutorial that introduces single-qubit system measurements](../SingleQubitSystemMeasurements/SingleQubitSystemMeasurements.ipynb) before starting this one.\n", "$\\renewcommand{\\ket}[1]{\\left\\lvert#1\\right\\rangle}$\n", "$\\renewcommand{\\bra}[1]{\\left\\langle#1\\right\\rvert}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Types of measurements on multi-qubit systems\n", "There are several types of measurements you can perform on an $n$-qubit system ($n>1$):\n", "- Measuring all the qubits simultaneously in an orthogonal basis ($2^n$ possible outcomes). As we shall see below, this is a direct generalization of orthogonal basis measurements done in single-qubit systems introduced in the previous tutorial.\n", "- Partial measurement: measuring $m$ qubits out of $n$, for $m\n", " \n", " \n", " \n", " \n", " Measurement outcome\n", " Probability of outcome\n", " State after measurement\n", " \n", " \n", " $b_i$\n", " $|c_i|^2$\n", " $\\ket{b_i}$\n", " \n", "\n", " \n", " \n", "> Similar to measurements in single-qubit systems, the assumption of normalization of the original wave function is required in order to ensure that the sum of all the outcome probabilities is 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1: Multi-qubit measurement outcome probabilities I \n", "Suppose that a two-qubit system is known to be in the following state:\n", "$$\\ket \\psi = \\frac{1}{3}\\ket {00} + \\frac{2}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$\n", "\n", "If all the qubits are measured simultaneously in the computational basis, what are the outcome probabilities?\n", "\n", "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-1:-Multi-qubit-measurement-outcome-probabilities-I).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2: Multi-qubit measurement outcome probabilities II \n", "Suppose that a two-qubit system is known to be in the following state:\n", "$$\\ket \\psi = \\frac{2}{3}\\ket {00} + \\frac{1}{3} \\ket {01} + \\frac{2}{3}\\ket {11}$$\n", "\n", "If all the qubits are measured simultaneously in the Pauli X basis, i.e., in the $\\{ \\ket{++}, \\ket{+-}, \\ket{-+}, \\ket{--}\\}$ basis, what are the outcome probabilities?\n", "\n", "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-2:-Multi-qubit-measurement-outcome-probabilities-II).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Measuring each qubit in a system one after another\n", "As described in the previous sections, in theory it is possible to measure all the qubits in an $n$-qubit system simultaneously in an orthogonal basis. The post-measurement state of the qubits is then exactly one of the $2^n$ possible basis states.\n", "\n", "In practice, this is implemented by measuring all the qubits one after another. For example, if one wants to measure a two-qubit system in the computational basis, one can implement this by first measuring the first qubit in the computational basis to obtain $0$ or $1$, and then measuring the second qubit in the computational basis. This can result in one of the four possible outcomes: $00, 01, 10, 11$.\n", "\n", "This can be generalized to measurements in other bases, such as the 2-qubit Pauli X basis $\\ket{++}, \\ket{+-}, \\ket{-+}, \\ket{--}$, and the bases for larger numbers of qubits.\n", "\n", "> Note that measuring all qubits one after another can only be used to measure in orthogonal bases $\\{ \\ket{b_i}\\}$ such that each $\\ket{b_i}$ is a 'tensor product state'. That is, each $\\ket{b_i}$ must be of the form $\\ket{v_0} \\otimes \\ket{v_1} \\dotsc \\otimes \\ket{v_{n-1}}$, with each $\\ket{v_j}$ being a single-qubit basis state. \n", "For example, for the 2-qubit Pauli X basis $\\ket{++}, \\ket{+-}, \\ket{-+}, \\ket{--}$ each basis state is a tensor product of states $\\ket{+}$ and $\\ket{-}$, which form a single-qubit basis state.\n", "> \n", "> Measuring in orthogonal bases which contain states which are not tensor product states, such as the Bell basis, are trickier to implement, and require appropriate unitary rotations in addition to measuring all qubits one after another. \n", "> We will not discuss such measurements in this tutorial.\n", "> You can find examples of such measurements and their implementations in the [Measurements kata](../../Measurements/Measurements.ipynb).\n", ">\n", "> If we restrict ourselves to measurements in tensor product states, the distinction between measuring all the qubits simultaneously versus one after another is not important for an ideal quantum computer: in terms of the outcomes and measurement probabilities, both are identical. Furthermore, as long as all the qubits are measured, the sequence in which they are measured is also inconsequential. These factors can be important in the case of real quantum computers with imperfect qubits, but we restrict the discussion to ideal systems in this tutorial.\n", "\n", "\n", "### Demo: Measurement statistics for qubit-by-qubit full measurement\n", "This demo illustrates the equivalence of the measurement probabilities for simultaneous measurement on all qubits, and measurements on each of the qubits executed one after another. Using the wave function from exercise 1 above as an example, we show that the measurement probabilities obtained using the `M` operation in Q# are the same as those expected theoretically for exercise 1. " ] }, { "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.Preparation;\n", "open Microsoft.Quantum.Arithmetic;\n", "open Microsoft.Quantum.Convert;\n", "\n", "operation DemoBasisMeasurement(numRuns : Int) : Unit {\n", " \n", " // Define coefficients and obtain measurement probabilities\n", " // for the state corresponding to Exercise 1\n", " // |𝜓❭ = 0.33 |00❭ + 0.67 |01❭ + 0.67 |11❭ \n", " // Use little endian format to encode basis states as integer indices.\n", " let coefficients = [0.333, 0.0, 0.667, 0.667]; \n", " let expected_probabilities = [0.111, 0.0, 0.445, 0.445];\n", " \n", " // Set up counter array for measurements.\n", " mutable countArray = [0, 0, 0, 0]; \n", " \n", " use qs = Qubit[2];\n", " for i in 1 .. numRuns {\n", " let register = LittleEndian(qs);\n", " // Prepare the state using PrepareArbitraryStateD library operation:\n", " PrepareArbitraryStateD(coefficients, register);\n", " if i == 1 {\n", " Message(\"The state |𝜓❭ of the system before measurement is:\");\n", " DumpMachine();\n", " } \n", "\n", " // Measure the first qubit, followed by the second qubit, and convert the result to little endian integer\n", " let result = MeasureInteger(register);\n", "\n", " // Update countArray\n", " set countArray w/= result <- countArray[result] + 1; \n", " }\n", " \n", " // Obtain simulated probability of measurement for each outcome\n", " mutable simulated_probabilities = [];\n", " for i in 0 .. 3 {\n", " set simulated_probabilities += [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)];\n", " }\n", " \n", " Message($\"Theoretical measurement probabilities are {expected_probabilities}\");\n", " Message($\"Simulated measurement probabilities are {simulated_probabilities}\");\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%simulate DemoBasisMeasurement numRuns=100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The simulated probabilities will be different for each run of `DemoBasisMeasurement`. The simulated and theoretical probabilities are not expected to be identical, since measurements are probabilistic. However, we expect the values to be similar, and the simulated probabilities to approach the theoretical probabilities as the parameter `numRuns` is increased." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using full measurements to identify the state of the system\n", "\n", "Full measurements can also be used to identify the state of the system, if it is guaranteed to be in one of several possible orthogonal states.\n", "\n", "### Exercise 3: Distinguish four basis states\n", "\n", "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in one of the four basis states ($|00\\rangle$, $|01\\rangle$, $|10\\rangle$, or $|11\\rangle$).\n", "\n", "**Output:**\n", "\n", "* 0 if the qubits were in the $|00\\rangle$ state,\n", "* 1 if they were in the $|01\\rangle$ state, \n", "* 2 if they were in the $|10\\rangle$ state, \n", "* 3 if they were in the $|11\\rangle$ state.\n", "\n", "In this task and the subsequent ones the order of qubit states in task description matches the order of qubits in the array (i.e., $|10\\rangle$ state corresponds to `qs[0]` in state $|1\\rangle$ and `qs[1]` in state $|0\\rangle$).\n", "\n", "The state of the qubits at the end of the operation does not matter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T1_BasisStateMeasurement\n", "\n", "operation BasisStateMeasurement(qs : Qubit[]) : Int {\n", " // Type your answer below\n", " // Then run the cell using Ctrl+Enter (⌘+Enter on macOS).\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb##Exercise-3:-Distinguish-four-basis-states).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Partial Measurements\n", "\n", "For a system with $n>1$ qubits, it is possible to measure $m The symbol $\\otimes$ represents the tensor product or the Kronecker product of two matrices. It is different from the usual matrix multiplication (see the [Linear Algebra tutorial](../LinearAlgebra/LinearAlgebra.ipynb#Tensor-Product) for a refresher). In the current context, $|b_i\\rangle \\langle b_i| \\otimes \\mathbb{1}_{n-m}$ simply means that the operator $|b_i\\rangle \\langle b_i|$ acts only on the $m$ qubits being measured, while the effect of $P_i$ on the remaining qubits is $\\mathbb{1}_{n-m}$, i.e., the identity operator. \n", "\n", "Analogous to the case for measurements for single-qubit systems, the rules for partial measurement probabilities and outcomes can be summarized as follows:\n", "- When a measurement is done, one of these projectors is chosen randomly. The probability of choosing projector $P_i$ is $\\big|P_i|\\psi\\rangle\\big|^2$.\n", "- If the projector $P_i$ is chosen, the measurement outcome is $b_i$, and the state of the system after the measurement is given by\n", "$$\n", "\\frac{P_i |\\psi\\rangle}{\\big|P_i |\\psi\\rangle\\big|}.\n", "$$\n", "\n", "For example, consider a two-qubit system in the state $\\ket \\psi = \\frac{1}{\\sqrt{2}}\\ket{01} - \\frac{1}{\\sqrt 2}\\ket{10}$. Consider a measurement of the first qubit in the computational basis, i.e., in the $\\{\\ket 0 , \\ket 1 \\}$ basis. Then, we have two projectors that represent this measurement:\n", "\\begin{align*}\n", "P_0 &= \\ket 0\\bra 0 \\otimes \\mathbb{1},\\\\\n", "P_1 &= \\ket 1 \\bra 1 \\otimes \\mathbb{1}.\n", "\\end{align*}\n", "\n", "The action of $P_0$ on $\\ket \\psi$ is \n", "\\begin{align*}\n", "P_0 \\ket \\psi &= \\left(\\ket 0\\bra 0 \\otimes \\mathbb{1}\\right) \\frac{1}{\\sqrt 2}\\big(\\ket{01} - \\ket{10}\\big) = \\\\\n", " &= \\frac{1}{\\sqrt 2} \\big( \\ket 0\\bra 0 0\\rangle \\otimes \\mathbb{1} \\ket{1} - \\ket 0 \\bra 0 1\\rangle \\otimes \\mathbb{1} \\ket 0 \\big) = \\\\\n", " &= \\frac{1}{\\sqrt 2} \\ket{01}.\n", "\\end{align*}\n", "\n", "Similarly, we obtain \n", "$$\n", "P_1 \\ket\\psi = -\\frac{1}{\\sqrt 2} \\ket{10}.\n", "$$\n", "\n", "Clearly, we have $\\big|P_0 \\ket \\psi\\big| = \\big|P_1 \\ket \\psi\\big| = \\frac{1}{2}$ in this case. Thus, the probabilities of measuring $0$ and $1$ are both $0.5$, with the post-measurement states of system being $\\ket{01}$ and $\\ket{10}$, respectively.\n", "\n", "> Similar to the case of single-qubit system measurements, the applicability of the formalism above requires the state of the multi-qubit system, $\\ket \\psi$, to be normalized. This is required to ensure that all the probabilities of individual outcomes add up to 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4: Partial measurement probabilities for the Hardy state\n", "\n", "Consider a 2-qubit system in the state $\\ket \\psi = \\frac{1}{\\sqrt{12}} \\big(3|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$.\n", "\n", "If only the first qubit is measured in the computational basis, what are the probabilities of the outcomes, and the post-measurement states of the system?\n", "\n", "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-4:-Partial-measurement-probabilities-for-the-Hardy-state).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demo: Measurement statistics for partial measurement\n", "Using the `M` operation in Q#, we demonstrate that the simulated outcome probabilities and post-measurement outcomes match the theoretical values obtained using the projection operators as described above. We use the Hardy state from Exercise 4 with a computational basis measurement on the first qubit for this purpose." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "open Microsoft.Quantum.Diagnostics;\n", "open Microsoft.Quantum.Preparation;\n", "open Microsoft.Quantum.Arithmetic;\n", "open Microsoft.Quantum.Convert;\n", "open Microsoft.Quantum.Math;\n", "\n", "operation DemoPartialMeasurement(numRuns : Int) : Unit {\n", " let divider = \"--------------------------------------------------------------------------------------------------\";\n", " // \n", " // We can use coefficients without normalization in PrepareArbitraryStateD, \n", " // the operation will normalize them automatically.\n", " let coefficients = [3., 1., 1., 1.]; \n", " let expected_probabilities = [0.833, 0.167];\n", " \n", " // Set up the counter array for measurements.\n", " mutable countArray = [0, 0];\n", " \n", " use qs = Qubit[2];\n", " for i in 1 .. numRuns {\n", " // Prepare the state from Exercise 4:\n", " // |𝜓❭ = (1/√12)(3|00⟩+|01⟩+|10⟩+|11⟩) \n", " PrepareArbitraryStateD(coefficients, LittleEndian(qs));\n", " \n", " // Display the state of the qubits.\n", " if i == 1 {\n", " Message(\"The state |𝜓❭ of the system before measurement is:\");\n", " DumpMachine();\n", " Message(divider);\n", " }\n", "\n", " // Measure the first qubit.\n", " let outcome = M(qs[0]) == Zero ? 0 | 1;\n", " set countArray w/= outcome <- countArray[outcome] + 1;\n", " \n", " if countArray[outcome] == 1 { \n", " // The first time the outcome is 0/1, print the system state afterwards.\n", " Message(\"For outcome {outcome}, the post-measurement state of the system is:\");\n", " DumpMachine();\n", " }\n", " ResetAll(qs);\n", " }\n", " \n", " // Obtain simulated probability of measurement for each outcome\n", " mutable simulated_probabilities = [];\n", " for i in 0 .. 1 {\n", " set simulated_probabilities += [IntAsDouble(countArray[i]) / IntAsDouble(numRuns)];\n", " }\n", " \n", " Message($\"Theoretical measurement probabilities are {expected_probabilities}\");\n", " Message($\"Simulated measurement probabilities are {simulated_probabilities}\");\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%simulate DemoPartialMeasurement numRuns=100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The simulated and theoretical measurement probabilities are not expected to match exactly, but should be close to each other, since measurement is probabilistic. However, the post-measurement states from the simulation should match the expected states for Exercise 4 precisely, since partial state collapse is not a probabilistic process." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using partial measurements to identify the state of the system\n", "\n", "In certain situations, it is possible to distinguish between orthogonal states of multi-qubit systems using partial measurements, as illustrated in the next exercise.\n", "\n", "### Exercise 5: Distinguish orthogonal states using partial measurements\n", "\n", "**Input**: A 3-qubit system which is guaranteed to be in either the $\\ket{++-}$ state, or the $\\ket{---}$ state.\n", "\n", "**Output**: \n", "- 0 if the qubit is in the $\\ket{++-}$ state,\n", "- 1 if the qubit is in the $\\ket{---}$ state.\n", "\n", "The state of the qubits at the end of the operation should be the same as the initial state." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T2_IsPlusPlusMinus\n", "\n", "operation IsPlusPlusMinus (qs : Qubit[]) : Int {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-5:-Distinguish-orthogonal-states-using-partial-measurements).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Measurements and entanglement\n", "\n", "Qubits entanglement has an effect on the measurement statistics of the system. If two qubits are entangled, then their measurement outcomes will be correlated, while separable states (which are by definition not entangled) have uncorrelated measurement outcomes.\n", "\n", "> It is useful to revisit the concepts of entanglement and separable states, which were introduced in the [tutorial on multi-qubit systems](../MultiQubitSystems/MultiQubitSystems.ipynb#Entanglement). Consider a system of $n>1$ number of qubits, which we divide into two parts: A, consisting of $m$ qubits, and B, consisting of the remaining $n-m$ qubits. We say that the state $\\ket \\psi$ of the entire system is separable if it can be expressed as a tensor product of the states of parts A and B: \n", "$$\n", "\\ket \\psi = \\ket {\\phi_A} \\otimes \\ket{\\phi_B}\n", "$$\n", "where $\\ket{\\phi_A}$ and $\\ket{\\phi_B}$ are wave functions that describe parts $A$ and $B$, respectively. If it is not possible to express $\\ket \\psi$ in such a form, then we say that system A is entangled with system B.\n", "\n", "Consider a measurement on the subsystem $A$ of a separable state. Let the measurement be done in a basis $\\{ \\ket{b_0},\\dotsc,\\ket{b_{2^m-1}}\\}$. According to the projection formalism, a projection operator $P_i = \\ket{b_i}\\bra{b_i} \\otimes \\mathbb{1}$ is chosen randomly. The corresponding post-measurement state of the system is then given by\n", "\\begin{align*}\n", "\\ket{\\psi}_{i} &\\equiv \\frac{P_i \\ket{\\psi}}{\\big|P_i \\ket{\\psi}\\big|} = \\\\\n", " &= \\frac{\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\bra{b_i}\\phi_A\\rangle \\otimes \\ket {\\phi_B}\\big|} = \\\\\n", " &= \\frac{\\bra{b_i}\\phi_A\\rangle \\cdot \\ket{b_i} \\otimes \\ket {\\phi_B}}{\\big|\\ket{b_i}\\big| \\cdot \\bra{b_i}\\phi_A\\rangle \\cdot \\big| \\ket {\\phi_B}\\big|} = \\\\\n", " &= \\ket{b_i} \\otimes \\ket{\\phi_B}.\n", "\\end{align*}\n", "\n", "Thus, the state of subsystem $B$ after the measurement is $\\ket{\\phi_B}$ independently of the outcome $i$ of the measurement on the first qubit. The results of a subsequent measurement on subsystem $B$, including outcome probabilities, will be independent of the result of the first measurement. In other words, the outcomes of the two measurements will be uncorrelated.\n", "\n", "On the other hand, if the system is entangled, then the measurement outcomes will be correlated, in a manner dictated by the bases chosen for the measurements on the two subsystems. The following exercise illustrates this phenomenon." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 6: Sequential measurements on an entangled state and a separable state\n", "Consider two two-qubit states:\n", "- The Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$.\n", "- A state $\\ket \\Theta = \\frac{1}{2} \\big( \\ket{00} + \\ket{01} + \\ket{10} + \\ket{11} \\big)$.\n", "\n", "For both states, consider a measurement on the first qubit, followed by a measurement on the second qubit, both done in the computational basis. For which state can we expect the measurement outcomes to be correlated? Verify by calculating the sequential measurement probabilities explicitly for both states. \n", "\n", "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-6:-Sequential-measurements-on-an-entangled-state-and-a-separable-state).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Congratulations! You have learned enough about the measurements to start solving problems from the [Measurements kata](../../Measurements/Measurements.ipynb)!\n", "You can give them a try and return to the rest of this tutorial later, or keep reading." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## State modification using partial measurements\n", "\n", "For certain multi-qubit systems prepared in a superposition state, it is possible to use partial measurements to collapse a part of the system to some desired state. \n", "\n", "### Exercise 7: State selection using partial measurements \n", "\n", "**Input**: \n", "1. A 2-qubit system in the state\n", "$$\n", "|\\psi\\rangle = \\frac{1}{\\sqrt{2}} |0\\rangle \\otimes ( a |0\\rangle + b|1\\rangle) + \\frac{1}{\\sqrt{2}} |1\\rangle \\otimes (b|0\\rangle + a |1\\rangle),\n", "$$\n", "where the constants $a$ and $b$ satisfying $|a|^2 + |b|^2 = 1$ are unknown.\n", "2. An integer $ind$ which is either $0$ or $1$.\n", "\n", "**Goal**: \n", "- If $ind$ equals 0, the state of the second qubit should be $a|0\\rangle + b|1\\rangle$\n", "- If $ind$ equals 1, the state of the second qubit should be $b|0\\rangle + a |1\\rangle$. \n", "\n", "The state of the first qubit at the end does not matter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T3_StateSelction\n", "\n", "operation StateSelction (qs : Qubit[], ind : Int) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-7:-State-selection-using-partial-measurements).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any multi-qubit state can be prepared from the $|0...0\\rangle$ state using an appropriate combination of quantum gates. \n", "However, sometimes it is easier and more efficient to prepare a state using partial measurements. \n", "You could prepare a simpler state involving additional qubits, which, when measured, result in a collapse of the remaining qubits to the desired state with a high probability. This is called **post-selection**, and is particularly useful if it is easier to prepare the pre-measurement state with the extra qubits than to prepare the desired state directly using unitary gates alone. This is demonstrated by the following exercise.\n", "\n", "### Exercise 8: State preparation using partial measurements\n", "\n", "**Input:** Two qubits (in an array) which are in the state $\\ket{00}$.\n", "\n", "**Goal:** Modify the qubits to the state $\\frac{1}{\\sqrt{3}} \\big(|00\\rangle + |01\\rangle + |10\\rangle\\big)$ using post-selection.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " Consider a 3-qubit state $\\frac{1}{2}(|00\\rangle + |01\\rangle + |11\\rangle) \\otimes |0\\rangle + \\frac{1}{2} |11\\rangle \\otimes |1\\rangle$.\n", " What happens when one measures the third qubit?\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T4_PostSelection\n", "\n", "operation PostSelection (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-8:-State-preparation-using-partial-measurements).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Joint measurements\n", "\n", "Joint measurements, also known as Pauli measurements, are a generalization of 2-outcome measurements to multiple qubits and other bases. In Q#, joint measurements in Pauli bases are implemented using the [Measure](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure) operation. Let's review single-qubit measurements in a different light before discussing joint measurements. \n", "\n", "### Single-qubit Pauli measurement\n", "For single-qubit systems, any measurement corresponding to an orthogonal basis can be associated with a Hermitian matrix with eigenvalues $\\pm 1$. The possible measurement outcomes (represented as `Result` in Q#) are the eigenvalues of the Hermitian matrix, and the corresponding projection matrices for the measurement are the projection operators onto the *eigenspaces* corresponding to the eigenvalues. \n", "\n", "For example, consider the computational basis measurement, which can result in outcomes `Zero` or `One` corresponding to states $\\ket 0$ and $\\ket 1$. This measurement is associated with the Pauli Z operator, which is given by \n", "$$\n", "Z = \\begin{pmatrix} 1 & 0 \\\\ 0 & -1\\end{pmatrix} = \\ket{0}\\bra{0} - \\ket{1}\\bra{1}.\n", "$$\n", "The $Z$ operator has two eigenvalues, $1$ and $-1$, with corresponding eigenvectors $\\ket{0}$ and $\\ket{1}$. A $Z$-measurement is then a measurement in the $\\{\\ket{0},\\ket{1}\\}$ basis, with the measurement outcomes being $1$ and $-1$ respectively. In Q#, by convention, an eigenvalue of $1$ corresponds to a `Result` of `Zero`, while an eigenvalue of $-1$ corresponds to a `Result` of `One`.\n", "\n", "Similarly, one can implement measurements corresponding to the Pauli X and Y operators. We summarize the various properties below:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Pauli OperatorMatrixEigenvalueEigenvector/post-measurement stateMeasurement Result in Q#
$X$$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$+1$\\ket{+}$Zero
-1$\\ket{-}$One
$Y$$\\begin{bmatrix} 0 & -i \\\\ i & 0 \\end{bmatrix}$+1$\\ket{i}$Zero
-1$\\ket{-i}$One
$Z$$\\begin{bmatrix} 1 & 0 \\\\ 0 & -1 \\end{bmatrix}$+1$\\ket{0}$Zero
-1$\\ket{1}$One
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In general, any measurement on a single qubit which results in two outcomes corresponds to the Hermitian operator $U Z U^\\dagger$, for some $2\\times 2$ unitary matrix $U$.\n", "\n", "Joint measurements are a generalization of this principle for multi-qubit matrices.\n", "\n", "\n", "### Parity measurements\n", "The simplest joint measurement is a parity measurement. A parity measurement treats computational basis vectors differently depending on whether the number of 1's in the basis vector is even or odd. \n", "\n", "For example, the operator $Z\\otimes Z$, or $ZZ$ in short, is the parity measurement operator for a two-qubit system. The eigenvalues $1$ and $-1$ correspond to the subspaces spanned by basis vectors $\\{ |00\\rangle, |11\\rangle \\}$ and $\\{ |01\\rangle, |10\\rangle \\}$, respectively. That is, when a $ZZ$ measurement results in a `Zero` (i.e. the eigenvalue $+1$), the post-measurement state is a superposition of only those computational basis vectors which have an even number of $1$'s. On the other hand, a result of `One` corresponds to a post-measurement state with only odd parity computational basis vectors.\n", "\n", "> Let's see what happens to various two-qubit states after the parity measurement. The $Z \\otimes Z$ matrix for two qubits is: \n", ">\n", ">$$Z \\otimes Z = \\begin{bmatrix}\n", " 1 & 0 & 0 & 0 \\\\\n", " 0 & -1 & 0 & 0 \\\\\n", " 0 & 0 & -1 & 0 \\\\\n", " 0 & 0 & 0 & 1 \\\\\n", "\\end{bmatrix}$$\n", ">\n", ">When this transformation is applied to a basis state $|00\\rangle$, we get\n", ">\n", "> $$\\begin{bmatrix}\n", " 1 & 0 & 0 & 0 \\\\\n", " 0 & -1 & 0 & 0 \\\\\n", " 0 & 0 & -1 & 0 \\\\\n", " 0 & 0 & 0 & 1 \\\\\n", "\\end{bmatrix} \n", "\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\\\ \\end{bmatrix} = \n", "\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\\\ \\end{bmatrix}$$\n", ">\n", "> Comparing this to the characteristic equation for eigenvectors of $Z \\otimes Z$ given by\n", "$ Z \\otimes Z |\\psi\\rangle = \\lambda |\\psi\\rangle$,\n", "it is easy to see that $|00\\rangle$ belongs to the $+1$ eigenspace, hence the $Z \\otimes Z$ measurement will return `Zero` and leave the state unchanged.\n", ">\n", "> Similarly, it can easily be verified that $|11\\rangle$ also belongs to $+1$ eigenspace, while $|01\\rangle$ and $|10\\rangle$ belong to the $-1$ eigenspace.\n", "> \n", "> Now, what happens if we apply a $Z \\otimes Z$ measurement to a superposition state $\\alpha |00\\rangle + \\beta |11\\rangle$? We can see that \n", ">\n", "> $$\\begin{bmatrix}\n", " 1 & 0 & 0 & 0 \\\\\n", " 0 & -1 & 0 & 0 \\\\\n", " 0 & 0 & -1 & 0 \\\\\n", " 0 & 0 & 0 & 1 \\\\\n", "\\end{bmatrix} \n", "\\begin{bmatrix} \\alpha \\\\ 0 \\\\ 0 \\\\ \\beta \\\\ \\end{bmatrix} = \n", "\\begin{bmatrix} \\alpha \\\\ 0 \\\\ 0 \\\\ \\beta \\\\ \\end{bmatrix}$$\n", ">\n", ">So this state also belongs to the $+1$ eigenspace, and measuring it will return `Zero` and leave the state unchanged. Similarly, we can verify that an $\\alpha |01\\rangle + \\beta |10\\rangle$ state belongs to the $-1$ eigenspace, and measuring it will return `One` without changing the state.\n", "\n", "Similarly, a parity measurement on a higher number of qubits can be implemented using a $Z \\otimes \\dotsc \\otimes Z$ measurement." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 9: Two-qubit parity measurement\n", "\n", "**Inputs**: Two qubits stored in an array which are guaranteed to be either in a superposition of the states $|00\\rangle$ and $|11\\rangle$ or in a superposition of states $|01\\rangle$ and $|10\\rangle$.\n", "\n", "**Output**: 0 if qubits were in the first superposition, 1 if they were in the second superposition. \n", "*The state of the qubits at the end of the operation should be the same as the starting state.*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T5_ParityMeasurement\n", "\n", "operation ParityMeasurement (qs : Qubit[]) : Int {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-9:-Two-qubit-parity-measurement).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multi-qubit Pauli measurements\n", "Joint measurement is a generalization of the measurement in the computational basis. \n", "Pauli measurements can also be generalized to a larger number of qubits. A multi-qubit Pauli measurement corresponds to an operator $M_1 \\otimes \\dotsc \\otimes M_n$, with each $M_i$ being from the set of gates $\\{X,Y,Z,I\\}$. If at least one of the operators is not the identity matrix, then the measurement can result in two outcomes: a `Result` of `Zero` corresponding to eigenvalue $+1$ and a `Result` of `One` corresponding to the eigenvalue $-1$. The corresponding projection operators are the projections onto the corresponding eigenspaces.\n", "\n", "For example, a Pauli/joint measurement corresponding to the $X\\otimes Z$ operator can be characterized as follows:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EigenvalueMeasurement Result in Q#EigenbasisMeasurement Projector
$+1$Zero$\\{ \\ket{0,+}, \\ket{1,-} \\}$$P_{+1} = \\ket{0,+}\\bra{0,+} + \\ket{1,-} \\bra{1,-}$
$-1$One$\\{ \\ket{0,-}, \\ket{1,+} \\}$$P_{-1} = \\ket{0,-}\\bra{0,-} + \\ket{1,+} \\bra{1,+}$
\n", " \n", " The rules for measurements are then the same as those outlined in the [partial measurements section](#Partial-Measurements), with the projection operators in the table." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 10: Parity measurement in different basis\n", "\n", "Consider a system which is in a state $\\alpha |00\\rangle + \\beta |01\\rangle + \\beta |10\\rangle + \\alpha |11\\rangle$.\n", "\n", "What are the possible outcomes and their associated probabilities, if a measurement in an $XX$ Pauli measurement is done?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Multi-Qubit System Measurement Workbook](./Workbook_MultiQubitSystemMeasurements.ipynb#Exercise-10:-Parity-measurement-in-different-basis).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion\n", "\n", "Congratulations! You have learned enough to solve problems from the [Measurements kata](../../Measurements/Measurements.ipynb) and the [Joint Measurements kata](../../JointMeasurements/JointMeasurements.ipynb)." ] } ], "metadata": { "kernelspec": { "display_name": "Q#", "language": "qsharp", "name": "iqsharp" }, "language_info": { "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", "version": "0.24" } }, "nbformat": 4, "nbformat_minor": 4 }