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

Commit

Permalink
[BasicGates] Add task 2.4 to the workbook (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyholdroyd112 authored Feb 26, 2022
1 parent b31c625 commit 12661af
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
7 changes: 7 additions & 0 deletions BasicGates/BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,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.4.-Toffoli-gate).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
127 changes: 127 additions & 0 deletions BasicGates/Workbook_BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,133 @@
"source": [
"[Return to Task 2.3 of the Basic Gates kata](./BasicGates.ipynb#Task-2.3.-Two-qubit-gate---3)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 2.4. Toffoli gate\n",
"\n",
"**Input:** Three qubits (stored in an array of length 3) in an arbitrary three-qubit state \n",
"$\\alpha |000\\rangle + \\beta |001\\rangle + \\gamma |010\\rangle + \\delta |011\\rangle + \\epsilon |100\\rangle + \\zeta|101\\rangle + \\color{blue}\\eta|110\\rangle + \\color{blue}\\theta|111\\rangle$.\n",
"\n",
"**Goal:** Flip the state of the third qubit if the state of the first two is $|11\\rangle$, i.e., change the three-qubit state to $\\alpha |000\\rangle + \\beta |001\\rangle + \\gamma |010\\rangle + \\delta |011\\rangle + \\epsilon |100\\rangle + \\zeta|101\\rangle + \\color{red}\\theta|110\\rangle + \\color{red}\\eta|111\\rangle$.\n",
"\n",
"### Solution\n",
"\n",
"This is essentially bookwork, because there is only one gate that performs this state change (and the task title already gave it away!) The Toffoli gate is:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
" \\begin{bmatrix}\n",
" 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\\\\n",
" \\end{bmatrix}\n",
" $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and our initial state is:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\begin{bmatrix}\n",
" \\alpha\\\\\n",
" \\beta\\\\\n",
" \\gamma\\\\\n",
" \\delta\\\\\n",
" \\epsilon\\\\\n",
" \\zeta\\\\\n",
" \\eta\\\\\n",
" \\theta\\\\ \n",
"\\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we have:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
" \\begin{bmatrix}\n",
" 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\\\\n",
" \\end{bmatrix}\n",
" \\begin{bmatrix}\n",
" \\alpha\\\\\n",
" \\beta\\\\\n",
" \\gamma\\\\\n",
" \\delta\\\\\n",
" \\epsilon\\\\\n",
" \\zeta\\\\\n",
" \\color{blue}\\eta\\\\\n",
" \\color{blue}\\theta\\\\ \n",
"\\end{bmatrix}\n",
"=\n",
" \\begin{bmatrix}\n",
" \\alpha\\\\\n",
" \\beta\\\\\n",
" \\gamma\\\\\n",
" \\delta\\\\\n",
" \\epsilon\\\\\n",
" \\zeta\\\\\n",
" \\color{red}\\theta\\\\\n",
" \\color{red}\\eta\\\\ \n",
"\\end{bmatrix}\n",
"=\n",
"\\alpha |000\\rangle + \\beta |001\\rangle + \\gamma |010\\rangle + \\delta |011\\rangle + \\epsilon |100\\rangle + \\zeta|101\\rangle + \\color{red}\\theta|110\\rangle + \\color{red}\\eta|111\\rangle\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T204_ToffoliGate\n",
"\n",
"operation ToffoliGate (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" CCNOT(qs[0], qs[1], qs[2]);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to Task 2.4 of the Basic Gates kata](./BasicGates.ipynb#Task-2.4.-Toffoli-gate)."
]
}
],
"metadata": {
Expand Down

0 comments on commit 12661af

Please sign in to comment.