EntanglementDistillation_LOCCNET_EN.ipynb 17.2 KB
Newer Older
Q
Quleaf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Entanglement Distillation -- Protocol Design with LOCCNet\n",
    "\n",
    "\n",
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Overview\n",
    "\n",
    "Quantum entanglement plays a vital role in quantum communication, quantum computing, and many other quantum technologies. Therefore, detecting, transmitting, and distributing quantum entanglement reliably are essential tasks if we want to build real applications in those fields. However, errors are inevitable in the real world. They could come from imperfect equipment when we create entanglement (preparation errors), or the quantum channel used to transmit entanglement is noisy, and we gradually lose the degree of entanglement as the transmission distance increases. The aim of entanglement distillation is to compensate for those losses and restore a **maximally entangled state** at the cost of many noisy entangled states. In this sense, one could also refer entanglement distillation as a purification/error-correction protocol. This process often involves two remote parties Alice and Bob such that only Local Operations and Classical Communication (LOCC) are allowed [1]. Many distillation protocols have been proposed since 1996, including the famous BBPSSW [2] and the DEJMPS protocol [3]. \n",
    "\n",
    "However, the BBPSSW and DEJMPS distillation protocols are designed for specific types of noisy states (i.e., isotropic states and Bell-diagonal states, respectively). It is nearly impossible to find a single distillation protocol that could purify all kinds of noises. Due to the complicated mathematical structure of LOCC, designing a new distillation protocol is time-consuming with paper and pencil only. LOCCNet, a machine learning framework for LOCC protocols, is designed to reduce the effort as much as possible. With LOCCNet, it will be easier to design new distillation protocols as long as we can characterize the mathematical form of noise introduced to the entanglement resources. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preliminary\n",
    "\n",
    "In the context of entanglement distillation, we usually use the **state fidelity** $F$ between the distilled state $\\rho_{out}$ and the maximally entangled Bell state $|\\Phi^+\\rangle$ to quantify the performance of a distillation protocol, where\n",
    "\n",
    "$$\n",
    "F(\\rho_{out}, \\Phi^+) \\equiv \\langle \\Phi^+|\\rho_{out}|\\Phi^+\\rangle.\n",
    "\\tag{1}\n",
    "$$\n",
    "\n",
    "**Note:** In general, LOCC distillation protocols are probabilistic, and hence we are also interested in the success rate $p_{succ}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Protocol design logic\n",
    "\n",
    "In this tutorial, we will go through an example that distills four identical **isotropic states** (or Werner state) $\\rho_{in}= \\rho_{\\text{iso}}$ into a single final state $\\rho_{out}$ with a higher state fidelity $F$ (closer to the Bell state $|\\Phi^+\\rangle$). We call this type of protocol as the $4\\rightarrow 1$ LOCC distillation class, while the original BBPSSW and DEJMPS protocols belong to the $2\\rightarrow 1$ LOCC distillation class. The isotropic state is a parametrized family of mixed states consist of $|\\Phi^+\\rangle$ and the completely mixed state (white noise) $I/4$,\n",
    "\n",
    "$$\n",
    "\\rho_{\\text{iso}}(p) = p\\lvert\\Phi^+\\rangle \\langle\\Phi^+\\rvert + (1-p)\\frac{I}{4}, \\quad p \\in [0,1]\n",
    "\\tag{2}\n",
    "$$\n",
    "\n",
    "In our example, we set $p=0.7$ and the input state becomes:\n",
    "\n",
    "$$\n",
    "\\rho_{in} = \\rho_{\\text{iso}}(0.7)= 0.7\\lvert\\Phi^+\\rangle \\langle\\Phi^+\\rvert + 0.075 I.\n",
    "\\tag{3}\n",
    "$$\n",
    "\n",
    "\n",
    "To fulfill the task of distillation through LOCC, we introduce two remote parties, $A$ (Alice) and $B$ (Bob). At the very beginning, they share four copies of entangled qubit pairs $\\rho_{A_0B_0}, \\rho_{A_1B_1}, \\rho_{A_2B_2}$ and $\\rho_{A_3B_3}$. Each copy is initialized as $\\rho_{in} = \\rho_{\\text{iso}}(p =0.7)$.  Alice holds four qubits $A_0, A_1, A_2, A_3$ in one place and Bob holds $B_0, B_1, B_2, B_3$ in another place. With these initial setups, Alice and Bob could choose the communication rounds $r$, which indicates how many times they would measure their subsystems and transmit classical data with each other. For simplicity, it is natural to start with $r=1$. Then, Alice and Bob can use LOCCNet to find out the correct local operations (encoded as quantum neural networks, QNNs) before communication by following the steps below:\n",
    "\n",
    "1. Design a general QNN architecture $U(\\boldsymbol\\theta)$ as shown in Figure 1, where each $R(\\theta)$ represents a general rotation operation on the Bloch sphere. They are referred as the `u3(theta, phi, lam, which_qubit)` gate in Paddle Quantum.\n",
    "2. After implementing the QNN, Alice and Bob measure all of the qubits except $A_0$ and $B_0$ in the computational basis. Then the measurement results $M = \\{m_{A_1}m_{B_1}, m_{A_2}m_{B_2}, m_{A_3}m_{B_3}\\}$ will be exchanged through a classical channel.\n",
    "3. If the measurement results of all qubit pairs are the same (each pair $m_{A_1}m_{B_1}, m_{A_2}m_{B_2}, m_{A_3}m_{B_3}$ is either 00 or 11), the distillation is successful, and the pair $A_0B_0$ is reserved as the memory qubit pair to store the purified entanglement; If the measurement results are different, the protocol fails. Discard the quantum pair $A_0B_0$. At this point, we obtain the purified quantum pair $A_0B_0$ probabilistically, and its state is denoted as $\\rho_{AB}'$.\n",
    "4. Define the accumulated loss function $L = \\sum_{m_{A_j}m_{B_j}\\in \\{00,11\\}} \\big(1- \\text{Tr}(\\rho_{tar}\\rho_{AB}')\\big)$ over the successful distillations, where $\\text{Tr}(\\rho_{tar}\\rho_{AB}')$ is the state overlap between the current state $\\rho_{AB}'$ and the target state $\\rho_{tar}=\\lvert\\Phi^+\\rangle \\langle \\Phi^+\\rvert$.\n",
    "5. Use gradient-based optimization methods to update parameters in QNN and minimize the loss function.\n",
    "6. Repeat steps 1-5 until the loss function converges.\n",
    "7. Output the purified state $\\rho_{out} = \\rho'_{A_0B_0}$.\n",
    "\n",
    "**Note:** The QNN structure used is merely an illustration. Readers are welcomed to try their own designs.\n",
    "\n",
    "\n",
    "<center><img src=\"figures/distillation-fig-LOCCNet4.png\" height=\"200\" width=\"400\"></center>\n",
    "<div style=\"text-align:center\">Figure 1: Schematic diagram of a distillation protocol designed with LOCCNet. </div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simulation with Paddle Quantum\n",
    "\n",
    "First, we import all the dependencies:"
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
90
   "execution_count": 2,
Q
Quleaf 已提交
91 92
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
93 94
     "end_time": "2021-03-09T06:19:13.646334Z",
     "start_time": "2021-03-09T06:19:10.780432Z"
Q
Quleaf 已提交
95 96 97 98 99
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
Q
Quleaf 已提交
100
    "import paddle\n",
Q
Quleaf 已提交
101 102 103
    "from paddle import matmul, trace\n",
    "import paddle_quantum\n",
    "from paddle_quantum.locc import LoccNet\n",
Q
Quleaf 已提交
104
    "from paddle_quantum.state import isotropic_state, bell_state\n",
Q
Quleaf 已提交
105 106 107
    "from paddle_quantum.qinfo import logarithmic_negativity\n",
    "# Change to density matrix mode\n",
    "paddle_quantum.set_backend('density_matrix')"
Q
Quleaf 已提交
108 109 110 111 112 113
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Q
Quleaf 已提交
114
    "Secondly, we define the QNN and the loss function:"
Q
Quleaf 已提交
115 116 117 118
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
119
   "execution_count": 3,
Q
Quleaf 已提交
120 121
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
122 123
     "end_time": "2021-03-09T06:19:13.700268Z",
     "start_time": "2021-03-09T06:19:13.650127Z"
Q
Quleaf 已提交
124 125 126 127 128 129 130
    }
   },
   "outputs": [],
   "source": [
    "class LOCC(LoccNet):\n",
    "    def __init__(self):\n",
    "        super(LOCC, self).__init__()\n",
Q
Quleaf 已提交
131
    "\n",
Q
Quleaf 已提交
132 133
    "        # Add the first party Alice \n",
    "        # The first parameter 4 stands for how many qubits A holds\n",
Q
Quleaf 已提交
134
    "        # The second parameter records the name of this party\n",
Q
Quleaf 已提交
135 136 137
    "        self.add_new_party(4, party_name=\"Alice\")\n",
    "        # Add the second party Bob\n",
    "        # The first parameter 4 stands for how many qubits B holds\n",
Q
Quleaf 已提交
138
    "        # The second parameter records the name of this party\n",
Q
Quleaf 已提交
139
    "        self.add_new_party(4, party_name=\"Bob\")\n",
Q
Quleaf 已提交
140
    "\n",
Q
Quleaf 已提交
141
    "        # Generate input isotropic states\n",
Q
Quleaf 已提交
142
    "        _state = isotropic_state(2, 0.7)\n",
Q
Quleaf 已提交
143
    "        # Distribute the pre-shared entangled states\n",
Q
Quleaf 已提交
144 145 146 147
    "        self.set_init_state(_state, [[\"Alice\", 0], [\"Bob\", 0]])\n",
    "        self.set_init_state(_state, [[\"Alice\", 1], [\"Bob\", 1]])\n",
    "        self.set_init_state(_state, [[\"Alice\", 2], [\"Bob\", 2]])\n",
    "        self.set_init_state(_state, [[\"Alice\", 3], [\"Bob\", 3]])\n",
Q
Quleaf 已提交
148
    "\n",
Q
Quleaf 已提交
149 150 151 152 153 154 155 156
    "        # Create Alice's circuit\n",
    "        self.cir1 = self.create_ansatz(\"Alice\")\n",
    "        self.QNN(self.cir1)\n",
    "        # Create Bob's circuit\n",
    "        self.cir2 = self.create_ansatz(\"Bob\")\n",
    "        self.QNN(self.cir2)\n",
    "\n",
    "    def QNN(self, cir):\n",
Q
Quleaf 已提交
157 158 159
    "        '''\n",
    "        Define the QNN illustrated in Figure 1\n",
    "        '''\n",
Q
Quleaf 已提交
160 161 162
    "        cir.u3('full')\n",
    "        cir.cnot('cycle')\n",
    "        cir.u3('full')\n",
Q
Quleaf 已提交
163 164 165 166
    "\n",
    "    def New_Protocol(self):\n",
    "        status = self.init_status\n",
    "        # Execute Alice's circuit\n",
Q
Quleaf 已提交
167
    "        status = self.cir1(status)\n",
Q
Quleaf 已提交
168
    "        # Execute Bob's circuit\n",
Q
Quleaf 已提交
169 170
    "        status = self.cir2(status)\n",
    "\n",
Q
Quleaf 已提交
171 172 173
    "        # Measure qubits,[\"000000\", \"000011\",\"001100\",\"110000\",\"001111\",\"111100\",\"110011\",\"111111\"] represent successful cases\n",
    "        status1 = self.measure(status, [[\"Alice\", 1], [\"Bob\", 1],[\"Alice\", 2], [\"Bob\", 2], [\"Alice\", 3], [\"Bob\", 3]], \n",
    "                               [\"000000\", \"000011\", \"001100\", \"110000\", \"001111\", \"111100\", \"110011\", \"111111\"])\n",
Q
Quleaf 已提交
174
    "\n",
Q
Quleaf 已提交
175 176
    "        # Trace out all the qubits but A_0 and B_0\n",
    "        status_fin = self.partial_state(status1, [[\"Alice\", 0], [\"Bob\", 0]])\n",
Q
Quleaf 已提交
177 178
    "        target_state = bell_state(2)\n",
    "\n",
Q
Quleaf 已提交
179 180 181
    "        # Calculate loss function\n",
    "        loss = 0\n",
    "        for idx in range(0, len(status_fin)):\n",
Q
Quleaf 已提交
182 183 184
    "            loss += 1 - paddle.real(trace(matmul(target_state.data, status_fin[idx].data)))\n",
    "\n",
    "        return loss, status_fin"
Q
Quleaf 已提交
185 186 187 188 189 190 191 192 193 194 195
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lastly, minimize the loss function with gradient-based optimization methods."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
196
   "execution_count": 5,
Q
Quleaf 已提交
197 198
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
199 200
     "end_time": "2021-03-09T06:22:27.071785Z",
     "start_time": "2021-03-09T06:19:13.706482Z"
Q
Quleaf 已提交
201 202 203 204 205 206 207
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Q
Quleaf 已提交
208 209 210 211 212 213 214 215 216 217
      "itr 0: 5.7033257\n",
      "itr 10: 1.552211\n",
      "itr 20: 0.8980296\n",
      "itr 30: 0.7061223\n",
      "itr 40: 0.57139534\n",
      "itr 50: 0.5278059\n",
      "itr 60: 0.5121539\n",
      "itr 70: 0.50759834\n",
      "itr 80: 0.5052042\n",
      "itr 90: 0.50493234\n",
Q
Quleaf 已提交
218
      "The fidelity of the input quantum state is:0.77500\n",
Q
Quleaf 已提交
219 220
      "The fidelity of the purified quantum state is: 0.93690\n",
      "The probability of successful purification is:0.38654\n",
Q
Quleaf 已提交
221 222
      "========================================================\n",
      "The output state is:\n",
Q
Quleaf 已提交
223 224 225 226 227 228 229 230 231 232
      " [[ 4.790e-01-0.e+00j -1.000e-04-0.e+00j  1.000e-04-0.e+00j\n",
      "   4.579e-01+5.e-04j]\n",
      " [-1.000e-04+0.e+00j  2.100e-02+0.e+00j  0.000e+00+0.e+00j\n",
      "  -1.000e-04+0.e+00j]\n",
      " [ 1.000e-04+0.e+00j  0.000e+00-0.e+00j  2.100e-02+0.e+00j\n",
      "   1.000e-04+1.e-04j]\n",
      " [ 4.579e-01-5.e-04j -1.000e-04-0.e+00j  1.000e-04-1.e-04j\n",
      "   4.790e-01-0.e+00j]]\n",
      "The initial logarithmic negativity is: 0.6322681307792664\n",
      "The final logarithmic negativity is: 0.9059638381004333\n"
Q
Quleaf 已提交
233 234 235 236 237 238
     ]
    }
   ],
   "source": [
    "ITR = 100    # Number of iterations\n",
    "LR = 0.2     # Learning rate\n",
Q
Quleaf 已提交
239 240 241 242
    "paddle.seed(999)\n",
    "\n",
    "net = LOCC()\n",
    "# Choose Adam optimizer\n",
Q
Quleaf 已提交
243
    "opt = paddle.optimizer.Adam(learning_rate=LR, parameters=net.cir1.parameters() + net.cir2.parameters())\n",
Q
Quleaf 已提交
244 245 246 247 248 249 250 251
    "\n",
    "# Optimization loop\n",
    "for itr in range(ITR):\n",
    "    loss, status_fin = net.New_Protocol()\n",
    "    # Backpropagation\n",
    "    loss.backward()          \n",
    "    opt.minimize(loss)\n",
    "    # Clean gradients\n",
Q
Quleaf 已提交
252
    "    opt.clear_grad()\n",
Q
Quleaf 已提交
253 254 255
    "    # Print training result\n",
    "    if itr % 10 == 0:\n",
    "        print(\"itr \" + str(itr) + \":\", loss.numpy()[0])\n",
Q
Quleaf 已提交
256
    "\n",
Q
Quleaf 已提交
257 258 259 260 261 262 263 264 265 266
    "# Calculate input state fidelity\n",
    "fidelity_in = (3 * 0.7 + 1) / 4\n",
    "# Calculate output state fidelity\n",
    "fidelity = (len(status_fin) - loss) / len(status_fin)\n",
    "# Calculate successful rate\n",
    "suc_rate = sum([s.prob for s in status_fin])\n",
    "\n",
    "print(\"The fidelity of the input quantum state is:%.5f\" % fidelity_in)\n",
    "print(\"The fidelity of the purified quantum state is: %.5f\" % fidelity.numpy()[0])\n",
    "print(\"The probability of successful purification is:%.5f\" % suc_rate.numpy()[0])\n",
Q
Quleaf 已提交
267
    "rho_out = status_fin[0]\n",
Q
Quleaf 已提交
268
    "print(\"========================================================\")\n",
Q
Quleaf 已提交
269 270 271
    "print(f\"The output state is:\\n {np.around(rho_out.data.numpy(), 4)}\")\n",
    "print(f\"The initial logarithmic negativity is: {logarithmic_negativity(isotropic_state(2, 0.7)).numpy()[0]}\")\n",
    "print(f\"The final logarithmic negativity is: {logarithmic_negativity(rho_out).numpy()[0]}\")"
Q
Quleaf 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "\n",
    "As we can see, this new distillation protocol can purify four copies of isotropic states, each with a fidelity of 0.775, into a single two-qubit state with a fidelity of 0.937, which outperforms the extended DEJMPS protocol [3] with a distillation fidelity of 0.924 under the same setting. At the same time, our framework also exhibits advantages in terms of flexibility and scalability. With the help of LOCCNet, one can try various combinations and see what's the effect of increasing the communication rounds $r$, adding non-identical noisy entanglement before distillation, and importing different noise types.\n",
    "\n",
    "LOCCNet has a wide range of applications, and distillation is merely one of them. We want to point out that the protocols trained by LOCCNet are hardware efficient since every operation is applicable in near term quantum devices, and we offer the possibility of customizing QNN architectures. Now, it's time to design and train your own LOCC protocol! "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## References\n",
    "\n",
    "[1] Chitambar, Eric, et al. \"Everything you always wanted to know about LOCC (but were afraid to ask).\" [Communications in Mathematical Physics 328.1 (2014): 303-326.](https://link.springer.com/article/10.1007/s00220-014-1953-9)\n",
    "\n",
    "[2] Bennett, Charles H., et al. \"Purification of noisy entanglement and faithful teleportation via noisy channels.\" [Physical Review Letters 76.5 (1996): 722.](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.76.722)\n",
    "\n",
    "[3] Deutsch, David, et al. \"Quantum privacy amplification and the security of quantum cryptography over noisy channels.\" [Physical Review Letters 77.13 (1996): 2818.](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.77.2818)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
Q
Quleaf 已提交
304
   "display_name": "Python 3 (ipykernel)",
Q
Quleaf 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
Q
Quleaf 已提交
318
   "version": "3.8.13"
Q
Quleaf 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
Q
Quleaf 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
Q
Quleaf 已提交
361 362 363 364 365
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}