Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
[BasicGates] Add tasks 2.1, 2.2 to the workbook (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyholdroyd112 authored Feb 16, 2022
1 parent 4879c92 commit af06ca6
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 16 deletions.
14 changes: 14 additions & 0 deletions BasicGates/BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Basic Gates Workbook](./Workbook_BasicGates.ipynb#Task-2.1.-Two-qubit-gate---1).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -486,6 +493,13 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Basic Gates Workbook](./Workbook_BasicGates.ipynb#Task-2.2.-Two-qubit-gate---2).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
251 changes: 235 additions & 16 deletions BasicGates/Workbook_BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 1. Single-Qubit Gates\n"
"# Part 1. Single-Qubit Gates"
]
},
{
Expand Down Expand Up @@ -71,7 +71,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can recognise that the Pauli X gate will change the state $|0\\rangle$ to $|1\\rangle$ and vice versa, and $\\alpha |0\\rangle + \\beta |1\\rangle$ to $\\alpha |1\\rangle + \\beta |0\\rangle$.\n",
"We can recognize that the Pauli X gate will change the state $|0\\rangle$ to $|1\\rangle$ and vice versa, and $\\alpha |0\\rangle + \\beta |1\\rangle$ to $\\alpha |1\\rangle + \\beta |0\\rangle$.\n",
"\n",
"As a reminder, the Pauli X gate is defined by the following matrix:"
]
Expand Down Expand Up @@ -475,7 +475,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can recognise that we need to use one of the rotation gates Rx, Ry, and Rz (named because they \"rotate\" the qubit state in the three dimensional space visualized as the Bloch sphere about the x, y, and z axes, respectively), since they involve angle parameters. Of these three gates, only Ry rotates the basis states $|0\\rangle$ and $|1\\rangle$ to have real amplitudes (the other two gates introduce complex coefficients).\n",
"We can recognize that we need to use one of the rotation gates Rx, Ry, and Rz (named because they \"rotate\" the qubit state in the three dimensional space visualized as the Bloch sphere about the x, y, and z axes, respectively), since they involve angle parameters. Of these three gates, only Ry rotates the basis states $|0\\rangle$ and $|1\\rangle$ to have real amplitudes (the other two gates introduce complex coefficients).\n",
"\n",
"As a reminder, "
]
Expand Down Expand Up @@ -532,7 +532,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall that when applying a gate, you can tell what its matrix does to the basis states by looking at its columns: the first column of the matrix is the state into which it will transform the $|0\\rangle$ state, and the second column is the state into which it will transfrom the $|1\\rangle$ state."
"Recall that when applying a gate, you can tell what its matrix does to the basis states by looking at its columns: the first column of the matrix is the state into which it will transform the $|0\\rangle$ state, and the second column is the state into which it will transform the $|1\\rangle$ state."
]
},
{
Expand Down Expand Up @@ -631,7 +631,7 @@
"source": [
"### Solution\n",
"\n",
"We can recognise that the S gate performs this particular relative phase addition to the $|1\\rangle$ basis state. As a reminder,"
"We can recognize that the S gate performs this particular relative phase addition to the $|1\\rangle$ basis state. As a reminder,"
]
},
{
Expand Down Expand Up @@ -810,7 +810,7 @@
"metadata": {},
"source": [
"> Note that the results produced by the test harness can be unexpected.\n",
"If you run the kata several times and and examine the output, you'll notice that success is signalled even though the corresponding amplitudes of the desired and actual states look very different.\n",
"If you run the kata several times and examine the output, you'll notice that success is signaled even though the corresponding amplitudes of the desired and actual states look very different.\n",
">\n",
"> So what's going on? The full state simulator used in these tests performs the computations \"up to a global phase\", that is, sometimes the resulting state acquires a global phase that doesn't affect the computations or the measurement outcomes, but shows up in DumpMachine output. (You can read more about the global phase in the [Qubit tutorial](../tutorials/Qubit/Qubit.ipynb#Relative-and-Global-Phase).)\n",
">\n",
Expand Down Expand Up @@ -871,10 +871,10 @@
"source": [
"### Solution\n",
"\n",
"We recognise that a global phase change can be accomplished by using the R rotation gate with the PauliI (identity) gate.\n",
"As a reminder, the R gate is defined as $R_{\\mu}(\\theta) = \\exp(\\frac{\\theta}{2}i\\cdot\\sigma_{\\mu})$, wehere $\\sigma_{\\mu}$ is one of the Pauli gates I, X, Y or Z. \n",
"We recognize that a global phase change can be accomplished by using the R rotation gate with the PauliI (identity) gate.\n",
"As a reminder, the R gate is defined as $R_{\\mu}(\\theta) = \\exp(\\frac{\\theta}{2}i\\cdot\\sigma_{\\mu})$, where $\\sigma_{\\mu}$ is one of the Pauli gates I, X, Y or Z. \n",
"\n",
"> Note that a global phase is not detectable and has no physical meaning - it disappers when you take a measurement of the state. \n",
"> Note that a global phase is not detectable and has no physical meaning - it disappears when you take a measurement of the state. \n",
"> You can read more about this in the [Single-qubit measurements tutorial](../tutorials/SingleQubitSystemMeasurements/SingleQubitSystemMeasurements.ipynb#Measurements-in-arbitrary-orthogonal-bases)."
]
},
Expand Down Expand Up @@ -977,14 +977,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We recognise that the goal is another Bell state. In fact, it is one of the four Bell states.\n",
"We recognize that the goal is another Bell state. In fact, it is one of the four Bell states.\n",
"\n",
"We remember from Task 1.3 that the Pauli Z gate will change the state of the $|0\\rangle$ basis state of a single qubit, so this gate seems like a good candidate for what we want to achieve. This gate leaves the sign of the $|0\\rangle$ basis state of a superposition unchanged, but flips the sign of the $|1\\rangle$ basis state of the superposition. \n",
"\n",
"Don't forget that the Z gate acts on only a single qubit, and we have two here.\n",
"Lets also remember how the Bell state is made up from its individual qubits.\n",
"\n",
"If the two qubits are A and B, where A is `qs[0]` and and B is `qs[1]`, we can write that\n",
"If the two qubits are A and B, where A is `qs[0]` and B is `qs[1]`, we can write that\n",
"$|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|0_{A}0_{B}\\rangle + |1_{A}1_{B}\\rangle\\big)$, and also remember that this is a superposition."
]
},
Expand All @@ -997,7 +997,7 @@
"> If you run the solution below several times, you will notice that you get the state $\\frac{1}{\\sqrt{2}} \\big(-|00\\rangle + |11\\rangle\\big)$ at a guess as frequently as the goal state. Here is why. \n",
"> \n",
"> We can write this second state as $-\\frac{1}{\\sqrt{2}} \\big(|00\\rangle -|11\\rangle\\big)$ = $(-1)\\frac{1}{\\sqrt{2}} \\big(|00\\rangle - |11\\rangle\\big)$. \n",
"This state is the same as our goal state $|\\Phi^{-}\\rangle$ with an unmeasureable global phase of $-1$.\n",
"This state is the same as our goal state $|\\Phi^{-}\\rangle$ with an unmeasurable global phase of $-1$.\n",
"\n",
"The exact same calculations can be done if we apply Z to the qubit B, so that's another possible solution."
]
Expand Down Expand Up @@ -1026,7 +1026,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 1.9. Bell state change - 2\n",
"## Task 1.9. Bell state change - 2\n",
"\n",
"**Input:** Two entangled qubits in Bell state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$.\n",
"\n",
Expand All @@ -1042,7 +1042,7 @@
"\n",
"Let's compare the starting state $\\frac{1}{\\sqrt{2}} \\big(|0_A0_B\\rangle + |1_A1_B\\rangle\\big)$ with the goal state $\\frac{1}{\\sqrt{2}} \\big(1_A0_B\\rangle + |0_A1_B\\rangle\\big)$ term by term and see how we need to transform it to reach the goal.\n",
"\n",
"Using our nomencalure from Task 1.8, we can now see by comparing terms that $|0_{A}\\rangle$ has flipped to $|1_A\\rangle$ to get the first term, and $|1_{A}\\rangle$ has flipped to $|0\\rangle$ to get the second term. This allows us to say that the correct gate to use is Pauli X, applied to `qs[0]`."
"Using our nomenclature from Task 1.8, we can now see by comparing terms that $|0_{A}\\rangle$ has flipped to $|1_A\\rangle$ to get the first term, and $|1_{A}\\rangle$ has flipped to $|0\\rangle$ to get the second term. This allows us to say that the correct gate to use is Pauli X, applied to `qs[0]`."
]
},
{
Expand Down Expand Up @@ -1082,8 +1082,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution ##\n",
"We remember from Task 1.3 that the Pauli-Z gate leaves the sign of the $|0\\rangle$ component of the single qubit superposition unchanged but flips the sign of the $|1\\rangle$ component of the superposition. We have also just seen in Task 1.9 how to change our input state to the state $\\frac{1}{\\sqrt{2}} \\big(|01\\rangle + |10\\rangle\\big)$, which is almost our goal state (disregarding the phase change for the moment). So it would seem that a combination of these two gates wiill be what we need here. The remaining question is in what order to apply them, and to which qubit.\n",
"### Solution ##\n",
"We remember from Task 1.3 that the Pauli-Z gate leaves the sign of the $|0\\rangle$ component of the single qubit superposition unchanged but flips the sign of the $|1\\rangle$ component of the superposition. We have also just seen in Task 1.9 how to change our input state to the state $\\frac{1}{\\sqrt{2}} \\big(|01\\rangle + |10\\rangle\\big)$, which is almost our goal state (disregarding the phase change for the moment). So it would seem that a combination of these two gates will be what we need here. The remaining question is in what order to apply them, and to which qubit.\n",
"\n",
"First of all, which qubit? Looking back at Task 1.9, it seems clear that we need to use qubit `qs[0]`, like we did there.\n",
"\n",
Expand Down Expand Up @@ -1111,6 +1111,225 @@
"source": [
"[Return to Task 1.10 of the Basic Gates kata](./BasicGates.ipynb#Task-1.10.-Bell-state-change---3)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part II. Multi-Qubit Gates\n",
"\n",
"## Task 2.1. Two-qubit gate - 1\n",
"\n",
"**Input:** Two unentangled qubits (stored in an array of length 2).\n",
"The first qubit will be in state $|\\psi\\rangle = \\alpha |0\\rangle + \\beta |1\\rangle$, the second - in state $|0\\rangle$\n",
"(this can be written as two-qubit state $\\big(\\alpha |0\\rangle + \\beta |1\\rangle \\big) \\otimes |0\\rangle = \\alpha |00\\rangle + \\beta |10\\rangle$.\n",
"\n",
"**Goal:** Change the two-qubit state to $\\alpha |00\\rangle + \\beta |11\\rangle$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"Let's denote the first qubit in state $\\alpha |0\\rangle + \\beta |1\\rangle$ as A and the second qubit in state $|0\\rangle$ as B.\n",
"\n",
"Compare our input state $\\alpha |0_A0_B\\rangle + \\beta |1_A0_B\\rangle$ with the goal state $\\alpha |0_A0_B\\rangle + \\beta |1_A1_B\\rangle$. \n",
"We want to pass our input qubit through a gate or gates (to be decided) that do the following. If qubit A is in the $|0\\rangle$ state, then we want to leave qubit B alone (the first term of the superposition). \n",
"However, if A is in the $|1\\rangle$ state, we want to flip qubit B from $|0\\rangle$ into $|1\\rangle$ state. In other words, the state of B is to be made contingent upon the state of A. \n",
"This gate exists and is called a CNOT (controlled-not) gate. Depending upon the state of the **control** qubit (A in our case), the value of the controlled or **target** qubit (B in our case) is inverted or unchanged. Thus we get the goal state $\\alpha |00\\rangle + \\beta |11\\rangle$. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T201_TwoQubitGate1\n",
"\n",
"operation TwoQubitGate1 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" CNOT(qs[0], qs[1]);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to Task 2.1 of the Basic Gates kata](./BasicGates.ipynb#Task-2.1.-Two-qubit-gate---1)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 2.2. Two-qubit gate - 2\n",
"\n",
"**Input:** Two unentangled qubits (stored in an array of length 2) in state $|+\\rangle \\otimes |+\\rangle = \\frac{1}{2} \\big( |00\\rangle + |01\\rangle + |10\\rangle \\color{blue}+ |11\\rangle \\big)$.\n",
"\n",
"\n",
"**Goal:** Change the two-qubit state to $\\frac{1}{2} \\big( |00\\rangle + |01\\rangle + |10\\rangle \\color{red}- |11\\rangle \\big)$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"Firstly we notice that we are dealing with an unentangled pair of qubits. In vector form this is:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In vector form the transformation we need is \n",
"$$\\frac{1}{2}\n",
"\\begin{bmatrix}\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\\\\n",
"\\end{bmatrix} \\rightarrow \n",
"\\frac{1}{2}\n",
"\\begin{bmatrix}\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\\\\n",
" -1\\\\\n",
"\\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All that needs to happen to change the input into the goal is that the $|11\\rangle$ basis state needs to have its sign flipped. \n",
"\n",
"We remember that the Pauli Z gate flips signs in the single qubit case, so we need to investigate if there is a 2-qubit version of this gate that we can use here. We can also recall task 1.6 which dealt with phase shifts and, remembering that $e^{i\\cdot\\pi} = -1$, we can think of the transformation we're looking for as a phase shift.\n",
"It can be useful to investigate a general case and then use it to perform a specific state change, so let's look for a 2-qubit variant of the phase shift. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly to task 2.1, the phase shift only occurs on one of the basis states, so this suggests it might be a conditional shift. If we could have our phase shift applied to `qs[1]` conditional on `qs[0]` being in the state $|1\\rangle$, then we would have a description of our gate. If we now look though a list of gates in the [Single-qubit gates tutorial](../tutorials/SingleQubitGates/SingleQubitGates.ipynb), we'll find the R1 phase shift gate with angle parameter $\\theta$ (radians), defined as\n",
"\n",
"$$\n",
"R1(\\alpha)\n",
"= \n",
" \\begin{bmatrix}\n",
" 1 & 0\\\\\n",
" 0 & \\color{red}{e^{i\\alpha}}\n",
" \\end{bmatrix}\n",
" $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The controlled variant of this gate will look like this:\n",
"$$\n",
"CR1(\\alpha) = \n",
" \\begin{bmatrix}\n",
" 1 & 0 & 0 & 0\\\\\n",
" 0 & 1 & 0 & 0\\\\\n",
" 0 & 0 & 1 & 0\\\\\n",
" 0 & 0 & 0 &\\color{red}{e^{i\\alpha}}\n",
" \\end{bmatrix}\n",
" $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This gate is almost Pauli I, the identity gate, with the different in just the last column, showing what will happen to the $|11\\rangle$ basis state. Applying it to our input state for $\\alpha = \\pi$, we'll get:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\frac{1}{2}\n",
" \\begin{bmatrix}\n",
" 1 & 0 & 0 & 0\\\\\n",
" 0 & 1 & 0 & 0\\\\\n",
" 0 & 0 & 1 & 0\\\\\n",
" 0 & 0 & 0 &\\color{red}{e^{i\\alpha}}\n",
" \\end{bmatrix}\n",
"\\begin{bmatrix}\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\\\\n",
"\\end{bmatrix}\n",
"=\n",
"\\frac{1}{2}\n",
"\\begin{bmatrix}\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\\\\n",
" 1\\cdot\\color{red}{e^{i\\alpha}}\\\\\n",
"\\end{bmatrix}\n",
"=\n",
"\\frac{1}{2} \\big( |00\\rangle + |01\\rangle + |10\\rangle \\color{red}- |11\\rangle \\big)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The last thing we notice if we look through the [list of operations in the Microsoft.Quantum.Canon namespace](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon) is the CZ (Controlled Z) gate, a special case of CR1 that implements exactly this gate."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%kata T202_TwoQubitGate2\n",
"\n",
"operation TwoQubitGate2 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" CZ(qs[0], qs[1]);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, we can express this gate using the intrinsic gate Z and its controlled variant using the Controlled functor:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T202_TwoQubitGate2\n",
"\n",
"operation TwoQubitGate2 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" Controlled Z([qs[0]], qs[1]);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to Task 2.2 of the Basic Gates kata](./BasicGates.ipynb#Task-2.2.-Two-qubit-gate---2)."
]
}
],
"metadata": {
Expand Down

0 comments on commit af06ca6

Please sign in to comment.