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

Basic Gates Workbook Task 2.5 #758

Merged
merged 2 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions BasicGates/BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,13 @@
" // ...\n",
"}"
]
},
{
"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.5.-Fredkin-gate).*"
]
}
],
"metadata": {
Expand Down
136 changes: 136 additions & 0 deletions BasicGates/Workbook_BasicGates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,142 @@
"source": [
"[Return to Task 2.4 of the Basic Gates kata](./BasicGates.ipynb#Task-2.4.-Toffoli-gate)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 2.5. Fredkin 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 + \\color{blue}\\zeta|101\\rangle + \\color{blue}\\eta|110\\rangle + \\theta|111\\rangle$.\n",
"\n",
"**Goal:** Swap the states of second and third qubit if and only if the state of the first qubit is $|1\\rangle$, i.e., change the three-qubit state to $\\alpha |000\\rangle + \\beta |001\\rangle + \\gamma |010\\rangle + \\delta |011\\rangle + \\epsilon |100\\rangle + \\color{red}\\eta|101\\rangle + \\color{red}\\zeta|110\\rangle + \\theta|111\\rangle$.\n",
"\n",
"\n",
"### Solution\n",
"\n",
"Again this is essentially bookwork, because there is only one gate that performs this state change (and the task title already gave it away!) \n",
"The Fredkin gate is also known as the controlled swap gate (Controlled SWAP):"
]
},
{
"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 & 0 & 1 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\\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 & 0 & 1 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\\n",
" \\end{bmatrix}\n",
" \\begin{bmatrix}\n",
" \\alpha\\\\\n",
" \\beta\\\\\n",
" \\gamma\\\\\n",
" \\delta\\\\\n",
" \\epsilon\\\\\n",
" \\color{blue} \\zeta\\\\\n",
" \\color{blue} \\eta\\\\\n",
" \\theta\\\\ \n",
"\\end{bmatrix}\n",
"=\n",
" \\begin{bmatrix}\n",
" \\alpha\\\\\n",
" \\beta\\\\\n",
" \\gamma\\\\\n",
" \\delta\\\\\n",
" \\epsilon\\\\\n",
" \\color{red} \\eta\\\\\n",
" \\color{red} \\zeta\\\\\n",
" \\theta\\\\ \n",
"\\end{bmatrix}\n",
"=\n",
"\\alpha |000\\rangle + \\beta |001\\rangle + \\gamma |010\\rangle + \\delta |011\\rangle + \\epsilon |100\\rangle + \\color{red}\\eta|101\\rangle + \\color{red}\\zeta|110\\rangle + \\theta|111\\rangle\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice carefully how the qubits are passed to the gate: `[qs[0]], (qs[1], [qs[2])`. The `Controlled` functor produces an operation that takes two parameters: the first one is an array of control qubits (in this case a single-element array consisting of the first qubit), and the second parameter is a tuple of all parameters you'd pass to the original gate (in this gate two single-qubit parameters that would be arguments to a SWAP gate)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T205_FredkinGate\n",
"\n",
"operation FredkinGate (qs : Qubit[]) : Unit is Adj+Ctl {\n",
" Controlled SWAP([qs[0]], (qs[1], qs[2]));\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to Task 2.5 of the Basic Gates kata](./BasicGates.ipynb#Task-2.5.-Fredkin-gate)."
]
}
],
"metadata": {
Expand Down