StateDiscrimination_EN.ipynb 15.0 KB
Newer Older
Q
Quleaf 已提交
1 2 3 4
{
 "cells": [
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
5
   "id": "2dfbb4cb",
Q
Quleaf 已提交
6 7 8 9 10 11 12 13 14 15
   "metadata": {},
   "source": [
    "# Quantum State Discrimination\n",
    "\n",
    "\n",
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>"
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
16
   "id": "0aa43109",
Q
Quleaf 已提交
17 18 19 20
   "metadata": {},
   "source": [
    "## Overview\n",
    "\n",
Q
Quleaf 已提交
21
    "Quantum state discrimination (QSD) [1-2] is a fundamental question in quantum communication, quantum computation, and quantum cryptography. In this tutorial, we will explain how to discriminate two orthogonal bipartite pure states $\\lvert\\psi\\rangle$ and $\\lvert\\phi\\rangle$, which satisfies $\\langle\\psi\\lvert\\phi\\rangle=0$, under the constraint of Local Operations and Classical Communication (LOCC). We refer all the theoretical details to the original paper [3]."
Q
Quleaf 已提交
22 23 24 25
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
26
   "id": "f04bce42",
Q
Quleaf 已提交
27 28 29 30
   "metadata": {},
   "source": [
    "## QSD Protocol\n",
    "\n",
Q
Quleaf 已提交
31
    "Firstly, we want to make the problem definition clear. Consider two spatially separated parties $A$ (Alice) and $B$ (Bob) sharing a given two-qubit system. The system state is $\\lvert\\varphi\\rangle$ previously distributed by another party $C$ (Charlie). Alice and Bob were only notified that $\\lvert\\varphi\\rangle$ is either $\\lvert\\psi\\rangle$ or $\\lvert\\phi\\rangle$ (both are pure states), satisfying $\\langle\\psi\\lvert\\phi\\rangle=0$. Then, Charlie provides many copies of $\\lvert\\psi\\rangle$ and $\\lvert\\phi\\rangle$ to them, and he asks Alice and Bob to cooperate with each other to figure out which state they are actually sharing.\n",
Q
Quleaf 已提交
32
    "\n",
Q
Quleaf 已提交
33
    "Solving this problem under our LOCCNet framework is trivial. As always, let's start with the simplest one-round LOCC protocol with a QNN architecture shown in Figure 1. Then, the difficulty lies in the design of an appropriate loss function $L$. Since we choose to let both parties measure their subsystem, there will be four possible measurement results $m_Am_B\\in\\{00, 01, 10, 11\\}$. To distinguish $\\lvert\\psi\\rangle$ and $\\lvert\\phi\\rangle$, we will label the former state with measurement results $m_Am_B\\in\\{00, 10\\}$ and the latter with $m_Am_B\\in\\{01, 11\\}$. This step can be understood as adding labels to the data in supervised learning. With these labels, we can define the loss function as the probability of guessing wrong label,\n",
Q
Quleaf 已提交
34 35
    "\n",
    "$$\n",
Q
Quleaf 已提交
36
    "L = p_{\\lvert\\psi\\rangle\\_01}+p_{\\lvert\\psi\\rangle\\_11}+p_{\\lvert\\phi\\rangle\\_10}+p_{\\lvert\\phi\\rangle\\_00},\n",
Q
Quleaf 已提交
37 38 39 40 41 42
    "\\tag{1}\n",
    "$$\n",
    "\n",
    "where $p_{\\lvert\\psi\\rangle\\_01}$ stands for the probability of measuring 01 when the input state is $\\lvert\\psi\\rangle$. Then we can begin the training stage to minimize the loss function.\n",
    "\n",
    "\n",
Q
Quleaf 已提交
43
    "![qsd](figures/discrimination-fig-circuit.png \"Figure 1: Schematic diagram of state discrimination with LOCCNet.\")\n",
Q
Quleaf 已提交
44 45 46 47 48 49 50 51 52 53 54 55
    "<div style=\"text-align:center\">Figure 1: Schematic diagram of state discrimination with LOCCNet. </div>\n",
    "\n",
    "\n",
    "We summarize the workflow below:\n",
    "\n",
    "\n",
    "1. Alice and Bob share a two-qubit system, which state is either $\\lvert\\psi\\rangle$ or $\\lvert\\phi\\rangle$. \n",
    "2. Alice operates a general rotation gate $U_A$ on her qubit.\n",
    "3. Alice measures her qubit on the computational basis, and the result $m_A\\in \\{0, 1\\}$. Then, she communicates with Bob about the measurement result through a classical channel.\n",
    "4.  Bob operates different gates on his qubit depending on Alice's measurement result. If, $m_A=0$ Bob acts $U_{B0}$ on his qubit; If $m_A = 1$, then Bob acts $U_{B1}$. Then, Bob measures his qubit and obtain $m_B \\in \\{0,1\\}$. **Note**: Both $U_{B0}$ and $U_{B1}$ are universal single-qubit gate `u3()`.\n",
    "5. Calculate the loss function $L = p_{\\lvert\\psi\\rangle\\_01}+p_{\\lvert\\psi\\rangle\\_11}+ p_{\\lvert\\phi\\rangle\\_10}+ p_{\\lvert\\phi\\rangle\\_00}$, and use gradient-based optimization methods to minimize it.\n",
    "6. Repeat 1-5 until the loss function converges.\n",
Q
Quleaf 已提交
56
    "7. Input the pre-shared state $\\lvert\\varphi\\rangle$ to make a decision and compare with Charlie's answer."
Q
Quleaf 已提交
57 58 59 60
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
61
   "id": "0a114f6a",
Q
Quleaf 已提交
62 63 64 65 66 67 68 69 70
   "metadata": {},
   "source": [
    "## Simulation with Paddle Quantum \n",
    "\n",
    "First, import relevant packages."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
71 72
   "execution_count": 2,
   "id": "3c5adbc4",
Q
Quleaf 已提交
73 74
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
75 76
     "end_time": "2021-03-09T04:11:04.474304Z",
     "start_time": "2021-03-09T04:11:01.071347Z"
Q
Quleaf 已提交
77 78 79 80 81 82
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import unitary_group\n",
Q
Quleaf 已提交
83
    "import paddle\n",
Q
Quleaf 已提交
84 85 86 87
    "import paddle_quantum\n",
    "from paddle_quantum.locc import LoccNet\n",
    "# Change to density matrix mode\n",
    "paddle_quantum.set_backend('density_matrix')"
Q
Quleaf 已提交
88 89 90 91
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
92
   "id": "96931b91",
Q
Quleaf 已提交
93 94 95 96 97 98 99
   "metadata": {},
   "source": [
    "Randomly generate two orthogonal pure states $\\lvert\\psi\\rangle$ and $\\lvert\\phi\\rangle$ by Charlie."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
100 101
   "execution_count": 3,
   "id": "32fab324",
Q
Quleaf 已提交
102 103
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
104 105
     "end_time": "2021-03-09T04:11:04.498238Z",
     "start_time": "2021-03-09T04:11:04.478356Z"
Q
Quleaf 已提交
106 107 108 109 110 111 112 113
    }
   },
   "outputs": [],
   "source": [
    "def states_orthogonal_random(n, num=2):\n",
    "    # Randomly generate two orthogonal states\n",
    "    assert num <= 2 ** n, \"return too many orthognal states\"\n",
    "    U = unitary_group.rvs(2 ** n)\n",
Q
Quleaf 已提交
114 115
    "    return_list = [np.array(U[i], dtype=np.complex64) for i in range(num)]\n",
    "\n",
Q
Quleaf 已提交
116 117 118 119 120
    "    return return_list"
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
121
   "id": "fe517847",
Q
Quleaf 已提交
122 123 124 125 126 127 128
   "metadata": {},
   "source": [
    "Below is the main part of our LOCC protocol:"
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
129 130
   "execution_count": 4,
   "id": "5b281baf",
Q
Quleaf 已提交
131 132
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
133 134
     "end_time": "2021-03-09T04:11:04.551149Z",
     "start_time": "2021-03-09T04:11:04.506818Z"
Q
Quleaf 已提交
135 136 137 138
    }
   },
   "outputs": [],
   "source": [
Q
Quleaf 已提交
139
    "class Net(LoccNet):\n",
Q
Quleaf 已提交
140 141 142 143
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        # Add the first party Alice \n",
    "        # The first parameter 1 stands for how many qubits A holds\n",
Q
Quleaf 已提交
144
    "        # The second parameter records the name of this party\n",
Q
Quleaf 已提交
145 146 147
    "        self.add_new_party(1, party_name='Alice')\n",
    "        # Add the first party Bob \n",
    "        # The first parameter 1 stands for how many qubits B holds\n",
Q
Quleaf 已提交
148
    "        # The second parameter records the name of this party\n",
Q
Quleaf 已提交
149
    "        self.add_new_party(1, party_name='Bob')\n",
Q
Quleaf 已提交
150 151
    "\n",
    "        # Rewrite the input states into density matrices\n",
Q
Quleaf 已提交
152
    "        _states = states_orthogonal_random(2)\n",
Q
Quleaf 已提交
153
    "        _states = [paddle_quantum.State(np.outer(init_state, init_state.conjugate())) for init_state in _states]\n",
Q
Quleaf 已提交
154
    "        # Initialize the system by distributing states\n",
Q
Quleaf 已提交
155
    "        self.set_init_state(_states[0], [('Alice', 0), ('Bob', 0)])\n",
Q
Quleaf 已提交
156 157 158 159
    "        self.psi = self.init_status\n",
    "        self.phi = self.reset_state(self.init_status, _states[1], [('Alice', 0), ('Bob', 0)])\n",
    "\n",
    "        # Alice's local operations\n",
Q
Quleaf 已提交
160
    "        self.cirA = self.create_ansatz('Alice')\n",
Q
Quleaf 已提交
161
    "        # Add single-qubit universal gate\n",
Q
Quleaf 已提交
162 163 164
    "        self.cirA.u3(0)\n",
    "        # Bob has to prepare two circuits according Alice's measurement result\n",
    "        self.cirB = [self.create_ansatz('Bob'), self.create_ansatz('Bob')]\n",
Q
Quleaf 已提交
165
    "        # Add single-qubit universal gate\n",
Q
Quleaf 已提交
166 167 168 169
    "        self.cirB[0].u3(0)\n",
    "        self.cirB[1].u3(0)\n",
    "\n",
    "    def run_circuit(self, party, cir, state, res):\n",
Q
Quleaf 已提交
170
    "        # Run circuit\n",
Q
Quleaf 已提交
171
    "        after_state = cir(state)\n",
Q
Quleaf 已提交
172
    "        # Measure the circuit and record the measurement results \n",
Q
Quleaf 已提交
173 174
    "        after_state = self.measure(status=after_state, which_qubits=(party, 0), results_desired=res)\n",
    "\n",
Q
Quleaf 已提交
175
    "        return after_state\n",
Q
Quleaf 已提交
176
    "\n",
Q
Quleaf 已提交
177 178 179
    "    def forward(self):\n",
    "        # Training steps\n",
    "        # Quantum state after Alice's operation\n",
Q
Quleaf 已提交
180 181
    "        psi = self.run_circuit('Alice', self.cirA, self.psi, ['0', '1'])\n",
    "        phi = self.run_circuit('Alice', self.cirA, self.phi, ['0', '1'])\n",
Q
Quleaf 已提交
182 183 184 185 186
    "\n",
    "        # Calculate the loss function\n",
    "        loss = 0\n",
    "        for each_psi in psi:\n",
    "            if each_psi.measured_result == '0':\n",
Q
Quleaf 已提交
187 188
    "                psi_01 = self.run_circuit('Bob', self.cirB[0], each_psi, '1')\n",
    "                loss += psi_01.prob\n",
Q
Quleaf 已提交
189
    "            elif each_psi.measured_result == '1':\n",
Q
Quleaf 已提交
190 191 192 193 194 195 196 197 198
    "                psi_11 = self.run_circuit('Bob', self.cirB[1], each_psi, '1')\n",
    "                loss += psi_11.prob\n",
    "        for each_phi in phi:\n",
    "            if each_phi.measured_result == '0':\n",
    "                phi_00 = self.run_circuit('Bob', self.cirB[0], each_phi, '0')\n",
    "                loss += phi_00.prob\n",
    "            elif each_phi.measured_result == '1':\n",
    "                phi_10 = self.run_circuit('Bob', self.cirB[1], each_phi, '0')\n",
    "                loss += phi_10.prob\n",
Q
Quleaf 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
    "\n",
    "        return loss\n",
    "\n",
    "    def evaluate(self):\n",
    "        # Test step\n",
    "        choice = np.random.choice(['phi', 'psi'])\n",
    "        if choice == 'phi':\n",
    "            self.status = self.phi\n",
    "        else:\n",
    "            self.status = self.psi\n",
    "        print('Charlie chooses the state', choice)\n",
    "\n",
    "        # Alice's operations\n",
Q
Quleaf 已提交
212
    "        status = self.run_circuit('Alice', self.cirA, self.status, ['0', '1'])\n",
Q
Quleaf 已提交
213 214 215 216 217
    "        # Bob's operations \n",
    "        result_0 = list()\n",
    "        result_1 = list()\n",
    "        for each_status in status:\n",
    "            if each_status.measured_result == '0':\n",
Q
Quleaf 已提交
218 219 220
    "                status = self.run_circuit('Bob', self.cirB[0], each_status, ['0', '1'])\n",
    "                result_0.append(status[0].prob.numpy()[0])\n",
    "                result_0.append(status[1].prob.numpy()[0])\n",
Q
Quleaf 已提交
221
    "            elif each_status.measured_result == '1':\n",
Q
Quleaf 已提交
222 223 224 225 226 227
    "                status = self.run_circuit('Bob', self.cirB[1], each_status, ['0', '1'])\n",
    "                result_1.append(status[0].prob.numpy()[0])\n",
    "                result_1.append(status[1].prob.numpy()[0])\n",
    "\n",
    "        print(\"The probability that Alice and Bob recognize it as psi:\", result_0[0] + result_1[0])\n",
    "        print(\"The probability that Alice and Bob recognize it as phi:\", result_0[1] + result_1[1])"
Q
Quleaf 已提交
228 229 230 231
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
232
   "id": "f3c844c1",
Q
Quleaf 已提交
233 234 235 236 237 238 239
   "metadata": {},
   "source": [
    "Train the QNN parameters, and Charlie randomly select one of the two orthogonal states $\\lvert\\psi\\rangle$ and $\\lvert\\phi\\rangle$ and see whether Alice and Bob can distinguish it correctly."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
240 241
   "execution_count": 6,
   "id": "24722ab6",
Q
Quleaf 已提交
242 243
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
244 245
     "end_time": "2021-03-09T04:11:19.312821Z",
     "start_time": "2021-03-09T04:11:14.759835Z"
Q
Quleaf 已提交
246 247 248 249 250 251 252
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Q
Quleaf 已提交
253 254 255 256 257 258 259 260 261 262 263
      "itr 0: 1.1238832\n",
      "itr 10: 0.32665575\n",
      "itr 20: 0.085007355\n",
      "itr 30: 0.085270524\n",
      "itr 40: 0.026622297\n",
      "itr 50: 0.015240545\n",
      "itr 60: 0.007836903\n",
      "itr 70: 0.004827206\n",
      "itr 80: 0.0035075857\n",
      "itr 90: 0.002215183\n",
      "Minimum loss: 0.0016813411\n",
Q
Quleaf 已提交
264 265
      "======================== test stage ===============================\n",
      "Charlie chooses the state psi\n",
Q
Quleaf 已提交
266 267
      "The probability that Alice and Bob recognize it as psi: 0.9990063\n",
      "The probability that Alice and Bob recognize it as phi: 0.0009937042\n",
Q
Quleaf 已提交
268
      "Charlie chooses the state phi\n",
Q
Quleaf 已提交
269 270
      "The probability that Alice and Bob recognize it as psi: 0.0006236615\n",
      "The probability that Alice and Bob recognize it as phi: 0.9993763\n"
Q
Quleaf 已提交
271 272 273 274 275 276 277
     ]
    }
   ],
   "source": [
    "ITR = 100  # Set the number of training iterations\n",
    "LR = 0.1   # Set learning rate\n",
    "SEED = 999 # Fix randome seed for parameters in PQC\n",
Q
Quleaf 已提交
278 279
    "np.random.seed(SEED)\n",
    "paddle.seed(SEED)\n",
Q
Quleaf 已提交
280
    "\n",
Q
Quleaf 已提交
281
    "net = Net()\n",
Q
Quleaf 已提交
282 283
    "params = net.cirA.parameters() + net.cirB[0].parameters() + net.cirB[1].parameters()\n",
    "opt = paddle.optimizer.Adam(learning_rate=LR, parameters=params)\n",
Q
Quleaf 已提交
284 285 286 287 288 289 290 291
    "# Train the LOCC net for ITR iterations by gradient descent\n",
    "for itr in range(ITR):\n",
    "    loss = net()\n",
    "    loss.backward()\n",
    "    opt.minimize(loss)\n",
    "    opt.clear_grad()\n",
    "    if itr % 10 == 0:\n",
    "        print(\"itr \" + str(itr) + \":\", loss.numpy()[0])\n",
Q
Quleaf 已提交
292
    "print(\"Minimum loss:\", loss.numpy()[0])\n",
Q
Quleaf 已提交
293
    "\n",
Q
Quleaf 已提交
294 295 296 297 298
    "print(\"======================== test stage ===============================\")\n",
    "np.random.seed(10)\n",
    "net.evaluate()\n",
    "np.random.seed(6)\n",
    "net.evaluate()"
Q
Quleaf 已提交
299 300 301 302
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
303
   "id": "46b30cd0",
Q
Quleaf 已提交
304 305 306 307 308 309 310 311 312
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "It can be seen from the simulation results that the trained quantum circuit can distinguish two orthogonal quantum states almost perfectly with an accuracy $>99.9\\%$. There is an interesting question that can we generalize this discrimination scheme by adding more states to the category."
   ]
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
313
   "id": "e1aea01e",
Q
Quleaf 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
   "metadata": {},
   "source": [
    "---\n",
    "## References\n",
    "\n",
    "[1] Barnett, Stephen M., and Sarah Croke. \"Quantum state discrimination.\" [Advances in Optics and Photonics 1.2 (2009): 238-278.](https://www.osapublishing.org/abstract.cfm?id=176580)\n",
    "\n",
    "[2] Chefles, Anthony. \"Quantum state discrimination.\" [Contemporary Physics 41.6 (2000): 401-424.](https://arxiv.org/abs/quant-ph/0010114)\n",
    "\n",
    "[3] Walgate, Jonathan, et al. \"Local distinguishability of multipartite orthogonal quantum states.\" [Physical Review Letters 85.23 (2000): 4972.](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.85.4972)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
Q
Quleaf 已提交
329
   "display_name": "Python 3 (ipykernel)",
Q
Quleaf 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342
   "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 已提交
343
   "version": "3.8.13"
Q
Quleaf 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  },
  "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
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}