{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Distinguish Unitaries Kata\n", "\n", "The **Distinguish Unitaries** quantum kata offers tasks in which you are given a unitary and have to figure out which of the list it is by designing and performing experiments on it.\n", "\n", "Each task is wrapped in one operation preceded by the description of the task.\n", "Your goal is to fill in the blank (marked with `// ...` comments)\n", "with some Q# code that solves the task. To verify your answer, run the cell using Ctrl+Enter (⌘+Enter on macOS).\n", "\n", "The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part I. Single-Qubit Gates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.1. Identity or Pauli X?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the identity (**I** gate)\n", "or the Pauli X gate (**X** gate). \n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **I** gate, 1 if the given operation is the **X** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T101_DistinguishIfromX \n", "\n", "operation DistinguishIfromX (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.1.-Identity-or-Pauli-X?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.2. Identity or Pauli Z?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the identity (**I** gate)\n", "or the Pauli Z gate (**Z** gate). \n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **I** gate, 1 if the given operation is the **Z** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T102_DistinguishIfromZ \n", "\n", "operation DistinguishIfromZ (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.2.-Identity-or-Pauli-Z?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.3. Z or S?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the **Z** gate\n", "or the **S** gate. \n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **Z** gate, 1 if the given operation is the **S** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants at most **twice**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T103_DistinguishZfromS \n", "\n", "operation DistinguishZfromS (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.3.-Z-or-S?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.4. Hadamard or X?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the Hadamard (**H**) gate\n", "or the **X** gate. \n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **H** gate, 1 if the given operation is the **X** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants at most **twice**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%kata T104_DistinguishHfromX \n", "\n", "operation DistinguishHfromX (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.4.-Hadamard-or-X?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.5. Z or $-$Z?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the **Z** gate\n", "or the minus **Z** gate (i.e., the gate $- |0\\rangle\\langle0| + |1\\rangle\\langle1|$). \n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **Z** gate, 1 if the given operation is the $-$**Z** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T105_DistinguishZfromMinusZ \n", "\n", "operation DistinguishZfromMinusZ (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.5.-Z-or-$-$Z?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.6. Rz or R1 (arbitrary angle)?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the [**Rz** gate](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.rz)\n", "or the [**R1** gate](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.r1). \n", "\n", "This operation will take two parameters: the first parameter is the rotation angle, in radians, and the second parameter is the qubit to which the gate should be applied (matching normal **Rz** and **R1** gates in Q#).\n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **Rz** gate, 1 if the given operation is the **R1** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T106_DistinguishRzFromR1 \n", "\n", "operation DistinguishRzFromR1 (unitary : ((Double, Qubit) => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.6.-Rz-or-R1-(arbitrary-angle)?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.7. Y or XZ?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the **Y** gate\n", "or the sequence of Pauli **Z** and Pauli **X** gates (equivalent to applying the **Z** gate followed by the **X** gate).\n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **Y** gate, 1 if the given operation is the **XZ** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants at most **twice**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T107_DistinguishYfromXZ \n", "\n", "operation DistinguishYfromXZ (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.7.-Y-or-XZ?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.8. Y, XZ, $-$Y or $-$XZ?\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the **Y** gate (possibly with an extra global phase of $-1$) or the sequence of Pauli **Z** and Pauli **X** gates (possibly with an extra global phase of $-1$).\n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** \n", "* 0 if the given operation is the **Y** gate,\n", "* 1 if the given operation is the $-$**XZ** gate,\n", "* 2 if the given operation is the $-$**Y** gate,\n", "* 3 if the given operation is the **XZ** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants at most **three times**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T108_DistinguishYfromXZWithPhases \n", "\n", "operation DistinguishYfromXZWithPhases (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.8.-Y,-XZ,-$-$Y-or-$-$XZ?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.9. Rz or Ry (fixed angle)?\n", "\n", "**Inputs:** \n", "\n", "1. An angle $\\theta \\in [0.01 \\pi; 0.99 \\pi]$.\n", "2. An operation that implements a single-qubit unitary transformation:\n", "either the [$R_z(\\theta)$ gate](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.rz)\n", "or the [$R_y(\\theta)$ gate](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.ry). \n", "\n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **Rz** gate, 1 if the given operation is the **Ry** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants **any number of times**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%kata T109_DistinguishRzFromRy \n", "\n", "operation DistinguishRzFromRy (theta : Double, unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.9.-Rz-or-Ry-(fixed-angle)?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.10*. Rz or R1 (fixed angle)?\n", "\n", "**Inputs:** \n", "\n", "1. An angle $\\theta \\in [0.01 \\pi; 0.99 \\pi]$.\n", "2. An operation that implements a single-qubit unitary transformation:\n", "either the [$R_z(\\theta)$ gate](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.rz)\n", "or the [$R_1(\\theta)$ gate](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.r1). \n", "\n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is the **Rz** gate, 1 if the given operation is the **R1** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants **any number of times**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%kata T110_DistinguishRzFromR1WithAngle \n", "\n", "operation DistinguishRzFromR1WithAngle (theta : Double, unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.11. Distinguish 4 Pauli unitaries\n", "\n", "**Input:** An operation that implements a single-qubit unitary transformation:\n", "either the identity (**I** gate) or one of the Pauli gates (**X**, **Y** or **Z** gate).\n", "The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** \n", "* 0 if the given operation is the **I** gate,\n", "* 1 if the given operation is the **X** gate,\n", "* 2 if the given operation is the **Y** gate,\n", "* 3 if the given operation is the **Z** gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%kata T111_DistinguishPaulis \n", "\n", "operation DistinguishPaulis (unitary : (Qubit => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-1.11.-Distinguish-4-Pauli-unitaries).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part II. Multi-Qubit Gates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.1. $I \\otimes X$ or $\\text{CNOT}$?\n", "\n", "**Input:** An operation that implements a two-qubit unitary transformation:\n", "either the $I \\otimes X$ (the X gate applied to the second qubit)\n", "or the $\\text{CNOT}$ gate with the first qubit as control and the second qubit as target.\n", "* The operation will accept an array of qubits as input, but it will fail if the array is empty or has one or more than two qubits.\n", "* The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is $I \\otimes X$, 1 if the given operation is the $\\text{CNOT}$ gate.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T201_DistinguishIXfromCNOT\n", "\n", "operation DistinguishIXfromCNOT (unitary : (Qubit[] => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-2.1.-$I-\\otimes-X$-or-$\\text{CNOT}$?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.2. Figure out the direction of $\\text{CNOT}$\n", "\n", "**Input:** An operation that implements a two-qubit unitary transformation:\n", "either the $\\text{CNOT}$ gate with the first qubit as control and the second qubit as target ($\\text{CNOT}_{12}$)\n", "or the $\\text{CNOT}$ gate with the second qubit as control and the first qubit as target ($\\text{CNOT}_{21}$).\n", "* The operation will accept an array of qubits as input, but it will fail if the array is empty or has one or more than two qubits.\n", "* The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is $\\text{CNOT}_{12}$, 1 if the given operation is $\\text{CNOT}_{21}$.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%kata T202_CNOTDirection\n", "\n", "operation CNOTDirection (unitary : (Qubit[] => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-2.2.-Figure-out-the-direction-of-$\\text{CNOT}$).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.3. $\\text{CNOT}_{12}$ or $\\text{SWAP}$?\n", "\n", "**Input:** An operation that implements a two-qubit unitary transformation:\n", "either the $\\text{CNOT}$ gate with the first qubit as control and the second qubit as target ($\\text{CNOT}_{12}$)\n", "or the $SWAP$ gate.\n", "* The operation will accept an array of qubits as input, but it will fail if the array is empty or has one or more than two qubits.\n", "* The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** 0 if the given operation is $\\text{CNOT}_{12}$, 1 if the given operation is $\\text{SWAP}$.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants exactly **once**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "%kata T203_DistinguishCNOTfromSWAP\n", "\n", "operation DistinguishCNOTfromSWAP (unitary : (Qubit[] => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-2.3.-$\\text{CNOT}_{12}$-or-$\\text{SWAP}$?).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.4. Identity, $\\text{CNOT}$s or $\\text{SWAP}$?\n", "\n", "**Input:** An operation that implements a two-qubit unitary transformation:\n", "either the identity ($I \\otimes I$), the $\\text{CNOT}$ gate with one of the qubits as control and the other qubit as a target, \n", "or the $SWAP$ gate.\n", "* The operation will accept an array of qubits as input, but it will fail if the array is empty or has one or more than two qubits.\n", "* The operation will have Adjoint and Controlled variants defined.\n", "\n", "**Output:** \n", "\n", "* 0 if the given operation is $I \\otimes I$, \n", "* 1 if the given operation is $\\text{CNOT}_{12}$,\n", "* 2 if the given operation is $\\text{CNOT}_{21}$,\n", "* 3 if the given operation is $\\text{SWAP}$.\n", "\n", "You are allowed to apply the given operation and its adjoint/controlled variants at most **twice**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "%kata T204_DistinguishTwoQubitUnitaries\n", "\n", "operation DistinguishTwoQubitUnitaries (unitary : (Qubit[] => Unit is Adj+Ctl)) : Int {\n", " // ...\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Distinguish Unitaries Workbook](./Workbook_DistinguishUnitaries.ipynb#Task-2.4.-Identity,-$\\text{CNOT}$s-or-$\\text{SWAP}$?).*" ] } ], "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": 2 }