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

Commit 3242fe0

Browse files
[BasicGates] Add task 2.5 to the workbook (#758)
1 parent 12661af commit 3242fe0

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

BasicGates/BasicGates.ipynb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@
590590
" // ...\n",
591591
"}"
592592
]
593+
},
594+
{
595+
"cell_type": "markdown",
596+
"metadata": {},
597+
"source": [
598+
"*Can't come up with a solution? See the explained solution in the [Basic Gates Workbook](./Workbook_BasicGates.ipynb#Task-2.5.-Fredkin-gate).*"
599+
]
593600
}
594601
],
595602
"metadata": {

BasicGates/Workbook_BasicGates.ipynb

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,142 @@
16521652
"source": [
16531653
"[Return to Task 2.4 of the Basic Gates kata](./BasicGates.ipynb#Task-2.4.-Toffoli-gate)."
16541654
]
1655+
},
1656+
{
1657+
"cell_type": "markdown",
1658+
"metadata": {},
1659+
"source": [
1660+
"## Task 2.5. Fredkin gate\n",
1661+
"\n",
1662+
"**Input:** Three qubits (stored in an array of length 3) in an arbitrary three-qubit state \n",
1663+
"$\\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",
1664+
"\n",
1665+
"**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",
1666+
"\n",
1667+
"\n",
1668+
"### Solution\n",
1669+
"\n",
1670+
"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",
1671+
"The Fredkin gate is also known as the controlled swap gate (Controlled SWAP):"
1672+
]
1673+
},
1674+
{
1675+
"cell_type": "markdown",
1676+
"metadata": {},
1677+
"source": [
1678+
"$$\n",
1679+
" \\begin{bmatrix}\n",
1680+
" 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
1681+
" 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
1682+
" 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n",
1683+
" 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\\n",
1684+
" 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\\\\n",
1685+
" 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\\\\n",
1686+
" 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\\n",
1687+
" 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\\n",
1688+
" \\end{bmatrix}\n",
1689+
"$$"
1690+
]
1691+
},
1692+
{
1693+
"cell_type": "markdown",
1694+
"metadata": {},
1695+
"source": [
1696+
"and our initial state is:"
1697+
]
1698+
},
1699+
{
1700+
"cell_type": "markdown",
1701+
"metadata": {},
1702+
"source": [
1703+
"$$\n",
1704+
"\\begin{bmatrix}\n",
1705+
" \\alpha\\\\\n",
1706+
" \\beta\\\\\n",
1707+
" \\gamma\\\\\n",
1708+
" \\delta\\\\\n",
1709+
" \\epsilon\\\\\n",
1710+
" \\zeta\\\\\n",
1711+
" \\eta\\\\\n",
1712+
" \\theta\\\\ \n",
1713+
"\\end{bmatrix}\n",
1714+
"$$"
1715+
]
1716+
},
1717+
{
1718+
"cell_type": "markdown",
1719+
"metadata": {},
1720+
"source": [
1721+
"So we have:"
1722+
]
1723+
},
1724+
{
1725+
"cell_type": "markdown",
1726+
"metadata": {},
1727+
"source": [
1728+
"$$\n",
1729+
" \\begin{bmatrix}\n",
1730+
" 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
1731+
" 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
1732+
" 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n",
1733+
" 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\\n",
1734+
" 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\\\\n",
1735+
" 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\\\\n",
1736+
" 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\\n",
1737+
" 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\\n",
1738+
" \\end{bmatrix}\n",
1739+
" \\begin{bmatrix}\n",
1740+
" \\alpha\\\\\n",
1741+
" \\beta\\\\\n",
1742+
" \\gamma\\\\\n",
1743+
" \\delta\\\\\n",
1744+
" \\epsilon\\\\\n",
1745+
" \\color{blue} \\zeta\\\\\n",
1746+
" \\color{blue} \\eta\\\\\n",
1747+
" \\theta\\\\ \n",
1748+
"\\end{bmatrix}\n",
1749+
"=\n",
1750+
" \\begin{bmatrix}\n",
1751+
" \\alpha\\\\\n",
1752+
" \\beta\\\\\n",
1753+
" \\gamma\\\\\n",
1754+
" \\delta\\\\\n",
1755+
" \\epsilon\\\\\n",
1756+
" \\color{red} \\eta\\\\\n",
1757+
" \\color{red} \\zeta\\\\\n",
1758+
" \\theta\\\\ \n",
1759+
"\\end{bmatrix}\n",
1760+
"=\n",
1761+
"\\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",
1762+
"$$"
1763+
]
1764+
},
1765+
{
1766+
"cell_type": "markdown",
1767+
"metadata": {},
1768+
"source": [
1769+
"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)."
1770+
]
1771+
},
1772+
{
1773+
"cell_type": "code",
1774+
"execution_count": null,
1775+
"metadata": {},
1776+
"outputs": [],
1777+
"source": [
1778+
"%kata T205_FredkinGate\n",
1779+
"\n",
1780+
"operation FredkinGate (qs : Qubit[]) : Unit is Adj+Ctl {\n",
1781+
" Controlled SWAP([qs[0]], (qs[1], qs[2]));\n",
1782+
"}"
1783+
]
1784+
},
1785+
{
1786+
"cell_type": "markdown",
1787+
"metadata": {},
1788+
"source": [
1789+
"[Return to Task 2.5 of the Basic Gates kata](./BasicGates.ipynb#Task-2.5.-Fredkin-gate)."
1790+
]
16551791
}
16561792
],
16571793
"metadata": {

0 commit comments

Comments
 (0)