{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Superposition Kata\n", "\n", "**Superposition** quantum kata is a series of exercises designed\n", "to get you familiar with the concept of superposition and with programming in Q#.\n", "It covers the following topics:\n", "* basic single-qubit and multi-qubit gates,\n", "* superposition,\n", "* flow control and recursion in Q#.\n", "\n", "It is recommended to complete the [BasicGates kata](./../BasicGates/BasicGates.ipynb) before this one to get familiar with the basic gates used in quantum computing. The list of basic gates available in Q# can be found at [Microsoft.Quantum.Intrinsic](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic).\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. Simple gates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.1. Plus state.\n", "\n", "**Input:** A qubit in the $|0\\rangle$ state.\n", "\n", "**Goal:** Change the state of the qubit to $|+\\rangle = \\frac{1}{\\sqrt{2}} \\big(|0\\rangle + |1\\rangle\\big)$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T101_PlusState \n", "\n", "operation PlusState (q : Qubit) : Unit {\n", " // Hadamard gate H will convert |0⟩ state to |+⟩ state.\n", " // Type the following: H(q);\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 [Superposition Workbook](./Workbook_Superposition.ipynb#plus-state).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.2. Minus state.\n", "\n", "**Input**: A qubit in the $|0\\rangle$ state.\n", "\n", "**Goal**: Change the state of the qubit to $|-\\rangle = \\frac{1}{\\sqrt{2}} \\big(|0\\rangle - |1\\rangle\\big)$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T102_MinusState \n", "\n", "operation MinusState (q : Qubit) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition.ipynb#minus-state).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.3. Superposition of all basis vectors on two qubits.\n", "\n", "**Input:** Two qubits in the $|00\\rangle$ state (stored in an array of length 2).\n", "\n", "**Goal:** Change the state of the qubits to $|+\\rangle \\otimes |+\\rangle = \\frac{1}{2} \\big(|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T103_AllBasisVectors_TwoQubits\n", "\n", "operation AllBasisVectors_TwoQubits (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition.ipynb#superposition-of-all-basis-vectors-on-two-qubits).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.4. Superposition of basis vectors with phase flip.\n", "\n", "**Input:** Two qubits in the $|00\\rangle$ sate (stored in an array of length 2).\n", "\n", "**Goal:** Change the state of the qubits to $\\frac{1}{2}\\big(|00\\rangle+|01\\rangle+|10\\rangle-|11\\rangle \\big)$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T104_AllBasisVectorWithPhaseFlip_TwoQubits\n", "\n", "operation AllBasisVectorWithPhaseFlip_TwoQubits (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition.ipynb#superposition-of-basis-vectors-with-phase-flip).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.5. Superposition of basis vectors with phases.\n", "\n", "**Input:** Two qubits in the $|00\\rangle$ state (stored in an array of length 2).\n", "\n", "**Goal:** Change the state of the qubits to $\\frac{1}{2} \\big(|00\\rangle + i|01\\rangle - |10\\rangle - i|11\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " Is this state separable?\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T105_AllBasisVectorsWithPhases_TwoQubits\n", "\n", "operation AllBasisVectorsWithPhases_TwoQubits (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition.ipynb#superposition-of-basis-vectors-with-phases).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.6. Bell state $|\\Phi^{+}\\rangle$.\n", "\n", "**Input:** Two qubits in the $|00\\rangle$ state (stored in an array of length 2).\n", "\n", "**Goal:** Change the state of the qubits to $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$.\n", "\n", "> You can find detailed coverage of Bell states and their creation [in this blog post](https://blogs.msdn.microsoft.com/uk_faculty_connection/2018/02/06/a-beginners-guide-to-quantum-computing-and-q/)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T106_BellState\n", "\n", "operation BellState (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition.ipynb#bell-state).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.7. All Bell states.\n", "\n", "**Inputs:** \n", "\n", "1. Two qubits in the $|00\\rangle$ state (stored in an array of length 2).\n", "2. An integer index.\n", "\n", "**Goal:** Change the state of the qubits to one of the Bell states, based on the value of index:\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", "
IndexState
0$|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |11\\rangle\\big)$
1$|\\Phi^{-}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|00\\rangle - |11\\rangle\\big)$
2$|\\Psi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|01\\rangle + |10\\rangle\\big)$
3$|\\Psi^{-}\\rangle = \\frac{1}{\\sqrt{2}} \\big (|01\\rangle - |10\\rangle\\big)$
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T107_AllBellStates\n", "\n", "operation AllBellStates (qs : Qubit[], index : Int) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition.ipynb#all-bell-states).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.8. Greenberger–Horne–Zeilinger state.\n", "\n", "**Input:** $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state (stored in an array of length $N$).\n", "\n", "**Goal:** Change the state of the qubits to the GHZ state $\\frac{1}{\\sqrt{2}} \\big (|0\\dots0\\rangle + |1\\dots1\\rangle\\big)$.\n", "\n", "> For the syntax of flow control statements in Q#, see [Q# iterations](https://docs.microsoft.com/azure/quantum/user-guide/language/statements/iterations) and [Q# conditional branching](https://docs.microsoft.com/azure/quantum/user-guide/language/statements/conditionalbranching) documentation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T108_GHZ_State\n", "\n", "operation GHZ_State (qs : Qubit[]) : Unit {\n", " // You can find N as Length(qs).\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#greenberger-horne-zeilinger).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.9. Superposition of all basis vectors.\n", "\n", "**Input:** $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state.\n", "\n", "**Goal:** Change the state of the qubits to an equal superposition of all basis vectors $\\frac{1}{\\sqrt{2^N}} \\big (|0 \\dots 0\\rangle + \\dots + |1 \\dots 1\\rangle\\big)$.\n", "\n", "> For example, for $N = 2$ the final state should be $\\frac{1}{2} \\big (|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " Is this state separable?\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T109_AllBasisVectorsSuperposition\n", "\n", "operation AllBasisVectorsSuperposition (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#superposition-of-all-basis-vectors).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.10. Superposition of all even or all odd numbers.\n", "\n", "**Inputs:** \n", "\n", "1. $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state (stored in an array of length $N$).\n", "2. A boolean `isEven`.\n", "\n", "**Goal:** Prepare a superposition of all *even* numbers if `isEven` is `true`, or of all *odd* numbers if `isEven` is `false`. \n", "A basis state encodes an integer number using [big-endian](https://en.wikipedia.org/wiki/Endianness) binary notation: state $|01\\rangle$ corresponds to the integer $1$, and state $|10 \\rangle$ - to the integer $2$. \n", "\n", "> For example, for $N = 2$ and `isEven = false` you need to prepare superposition $\\frac{1}{\\sqrt{2}} \\big (|01\\rangle + |11\\rangle\\big )$, \n", "and for $N = 2$ and `isEven = true` - superposition $\\frac{1}{\\sqrt{2}} \\big (|00\\rangle + |10\\rangle\\big )$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T110_EvenOddNumbersSuperposition\n", "\n", "operation EvenOddNumbersSuperposition (qs : Qubit[], isEven : Bool) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#superposition-of-all-even-or-all-odd-numbers).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.11. Superposition of $|0 \\dots 0\\rangle$ and the given bit string.\n", "\n", "**Inputs:** \n", "\n", "1. $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state.\n", "2. A bit string of length $N$ represented as `Bool[]`. Bit values `false` and `true` correspond to $|0\\rangle$ and $|1\\rangle$ states. You are guaranteed that the first bit of the bit string is `true`.\n", "\n", "**Goal:** Change the state of the qubits to an equal superposition of $|0 \\dots 0\\rangle$ and the basis state given by the bit string.\n", "\n", "> For example, for the bit string `[true, false]` the state required is $\\frac{1}{\\sqrt{2}}\\big(|00\\rangle + |10\\rangle\\big)$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T111_ZeroAndBitstringSuperposition\n", "\n", "operation ZeroAndBitstringSuperposition (qs : Qubit[], bits : Bool[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#superposition-of-zero-and-given-bit-string).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.12. Superposition of two bit strings.\n", "\n", "**Inputs:** \n", "\n", "1. $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state.\n", "2. Two bit strings of length $N$ represented as `Bool[]`s. Bit values `false` and `true` correspond to $|0\\rangle$ and $|1\\rangle$ states. You are guaranteed that the two bit strings differ in at least one bit.\n", "\n", "**Goal:** Change the state of the qubits to an equal superposition of the basis states given by the bit strings.\n", "\n", "> For example, for bit strings `[false, true, false]` and `[false, false, true]` the state required is $\\frac{1}{\\sqrt{2}}\\big(|010\\rangle + |001\\rangle\\big)$.\n", "\n", "> If you need to define any helper functions, you'll need to create an extra code cell for it and execute it before returning to this cell.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " \n", " Use [`ControlledOnInt`](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.controlledonint) and [`ControlledOnBitString`](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.controlledonbitstring) library functions to apply a controlled gate on a target qubit(s). \n", " \n", " `ControlledOnInt` generates a gate which is applied when the control register is in a state corresponding to a non-negative integer (e.g., for integer 0, the control register must be in state $|0\\rangle$). Read more in the [documentation](https://docs.microsoft.com/en-gb/qsharp/api/qsharp/microsoft.quantum.canon.controlledonint). \n", " \n", " `ControlledOnBitString ` is similar, but the state is specified with a bit string (e.g., for bit string `[false, true]`, the control register must be in state $|01\\rangle$). Read more in the [documentation](https://docs.microsoft.com/en-gb/qsharp/api/qsharp/microsoft.quantum.canon.controlledonbitstring).\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T112_TwoBitstringSuperposition\n", "\n", "operation TwoBitstringSuperposition (qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#superposition-of-two-bit-strings).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.13*. Superposition of four bit strings.\n", "\n", "**Inputs:** \n", "\n", "1. $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state.\n", "2. Four bit strings of length $N$, represented as `Bool[][]` `bits`. `bits` is an $4 \\times N$ array which describes the bit strings as follows: `bits[i]` describes the `i`-th bit string and has $N$ elements. You are guaranteed that all four bit strings will be distinct.\n", "\n", "**Goal:** Change the state of the qubits to an equal superposition of the four basis states given by the bit strings.\n", "\n", "> For example, for $N = 3$ and `bits = [[false, true, false], [true, false, false], [false, false, true], [true, true, false]]` the state required is $\\frac{1}{2}\\big(|010\\rangle + |100\\rangle + |001\\rangle + |110\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " Remember that you can allocate extra qubits. If you do, you'll need to return them to the $|0\\rangle$ state before releasing them.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T113_FourBitstringSuperposition\n", "\n", "operation FourBitstringSuperposition (qs : Qubit[], bits : Bool[][]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#superposition-of-four-bit-strings).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 1.14. Superposition of all bit strings of the given parity.\n", "\n", "**Inputs:** \n", "\n", "1. $N$ ($N \\ge 1$) qubits in the $|0 \\dots 0\\rangle$ state.\n", "2. An `Int` `parity`.\n", "\n", "**Goal:** change the state to an equal superposition of all basis states that have\n", "* an even number of 1s in them if `parity = 0`, or\n", "* an odd number of 1s in them if `parity = 1`.\n", "\n", "> For example, for $N = 2$ the required state is $\\frac{1}{\\sqrt{2}}\\big(|00\\rangle + |11\\rangle\\big)$ if `parity = 0`, or $\\frac{1}{\\sqrt{2}}\\big(|01\\rangle + |10\\rangle\\big)$ if `parity = 1`.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " Remember that you can call the solution recursively. You are allowed to modify the signature of the method to include adjoint and/or controlled variants.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%kata T114_AllStatesWithParitySuperposition\n", "\n", "operation AllStatesWithParitySuperposition (qs : Qubit[], parity : Int) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#superposition-of-all-strings-with-parity).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part II. Arbitrary rotations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.1. Unequal superposition.\n", "\n", "**Inputs:**\n", "\n", "1. A qubit in the $|0\\rangle$ state.\n", "2. Angle $\\alpha$, in radians, represented as `Double`.\n", "\n", "**Goal** : Change the state of the qubit to $\\cos{α} |0\\rangle + \\sin{α} |1\\rangle$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " Experiment with rotation gates from Microsoft.Quantum.Intrinsic namespace.\n", " Note that all rotation operators rotate the state by half of its angle argument.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T201_UnequalSuperposition \n", "\n", "operation UnequalSuperposition (q : Qubit, alpha : Double) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#unequal-superposition).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.2. $\\frac{1}{\\sqrt{2}}|00\\rangle+\\frac{1}{2}|10\\rangle+\\frac{1}{2}|11\\rangle$ state. \n", "\n", "**Input:** Two qubits in the $|00\\rangle$ state.\n", "\n", "**Goal**: Change the state of the qubits to $\\frac{1}{\\sqrt{2}}|00\\rangle+\\frac{1}{2}|10\\rangle+\\frac{1}{2}|11\\rangle$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T202_ControlledRotation \n", "\n", "operation ControlledRotation (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#controlled-split).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.3*. $\\frac{1}{\\sqrt{3}} \\big(|00\\rangle + |01\\rangle + |10\\rangle\\big)$ state.\n", "\n", "**Input:** Two qubits in the $|00\\rangle$ state.\n", "\n", "**Goal:** Change the state of the qubits to $\\frac{1}{\\sqrt{3}} \\big(|00\\rangle + |01\\rangle + |10\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " If you need trigonometric functions, you can find them in Microsoft.Quantum.Math namespace; you'll need to add
open Microsoft.Quantum.Math;
to the code before the operation definition.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T203_ThreeStates_TwoQubits\n", "\n", "operation ThreeStates_TwoQubits (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#threestates-twoqubits).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.4*. $\\frac{1}{\\sqrt{3}} \\big( |00\\rangle + \\omega |01\\rangle + \\omega^2 |10\\rangle \\big)$ state. \n", "\n", "**Input:** Two qubits in $|0\\rangle$ state (stored in an array of length 2).\n", "\n", "**Output:** Change the state of the qubits to $\\frac{1}{\\sqrt{3}} \\big( |00\\rangle + \\omega |01\\rangle + \\omega^2 |10\\rangle \\big)$. \n", "Here $\\omega = e^{2\\pi i/3}$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " If you need trigonometric functions, you can find them in Microsoft.Quantum.Math namespace; you'll need to add
open Microsoft.Quantum.Math;
to the code before the operation definition.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T204_ThreeStates_TwoQubits_Phases\n", "\n", "operation ThreeStates_TwoQubits_Phases (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#three-states-two-qubits-phases).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.5*. Hardy state.\n", "\n", "**Input:** Two qubits in the $|00\\rangle$ state.\n", "\n", "**Goal:** Change the state of the qubits to $\\frac{1}{\\sqrt{12}} \\big(3|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " If you need trigonometric functions, you can find them in Microsoft.Quantum.Math namespace; you'll need to add
open Microsoft.Quantum.Math;
to the code before the operation definition.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T205_Hardy_State\n", "\n", "operation Hardy_State (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#hardy-state).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.6*. W state on $2^k$ qubits.\n", "\n", "**Input:** $N = 2^k$ qubits in the $|0 \\dots 0\\rangle$ state.\n", "\n", "**Goal:** Change the state of the qubits to the [W state](https://en.wikipedia.org/wiki/W_state) - an equal superposition of $N$ basis states on $N$ qubits which have Hamming weight of 1.\n", "\n", "> For example, for $N = 4$ the required state is $\\frac{1}{2}\\big(|1000\\rangle + |0100\\rangle + |0010\\rangle + |0001\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " You can use Controlled modifier to perform arbitrary controlled gates.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T206_WState_PowerOfTwo\n", "\n", "operation WState_PowerOfTwo (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#wstate-on-2k-qubits).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 2.7**. W state on an arbitrary number of qubits.\n", "\n", "**Input:** $N$ qubits in the $|0 \\dots 0\\rangle$ state ($N$ is not necessarily a power of 2).\n", "\n", "**Goal:** Change the state of the qubits to the [W state](https://en.wikipedia.org/wiki/W_state) - an equal superposition of $N$ basis states on $N$ qubits which have Hamming weight of 1.\n", "\n", "> For example, for $N = 3$ the required state is $\\frac{1}{\\sqrt{3}}\\big(|100\\rangle + |010\\rangle + |001\\rangle\\big)$.\n", "\n", "
\n", "
\n", " Need a hint? Click here\n", " You can modify the signature of the given operation to specify its controlled specialization.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%kata T207_WState_Arbitrary\n", "\n", "operation WState_Arbitrary (qs : Qubit[]) : Unit {\n", " // ...\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Can't come up with a solution? See the explained solution in the [Superposition Workbook](./Workbook_Superposition_Part2.ipynb#wstate-on-arbitray-number-of-qubits).*" ] } ], "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": 2 }