{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 噪声模拟器\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.2/resource/_static/logo_notebook.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.2/mindquantum/zh_cn/middle_level/mindspore_noise_simulator.ipynb) \n",
    "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.2/resource/_static/logo_download_code.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.2/mindquantum/zh_cn/middle_level/mindspore_noise_simulator.py) \n",
    "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.2/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/r2.2/docs/mindquantum/docs/source_zh_cn/middle_level/noise_simulator.ipynb)\n",
    "[![在ModelArts平台运行](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.2/resource/_static/logo_modelarts.svg)](https://authoring-modelarts-cnnorth4.huaweicloud.com/console/lab?share-url-b64=aHR0cHM6Ly9taW5kc3BvcmUtd2Vic2l0ZS5vYnMuY24tbm9ydGgtNC5teWh1YXdlaWNsb3VkLmNvbS9ub3RlYm9vay9yMi4yL21pbmRxdWFudHVtL3poX2NuL21pZGRsZV9sZXZlbC9taW5kc3BvcmVfbm9pc2Vfc2ltdWxhdG9yLmlweW5i&imageid=73522c7f-6d98-43d8-b306-8ff374c7d3a7)\n",
    "\n",
    "MindQuantum 中包含各种噪声信道,利用噪声信道我们可以对真实的量子芯片进行模拟。在 MindQuantum 中,我们定义了各种 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html),可以有选择性的在量子线路的不同位置添加噪声信道,依次完成含噪声的量子模拟。下面介绍如何利用 MindQuantum 完成此任务。\n",
    "\n",
    "## [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html)\n",
    "\n",
    "[ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html) 是一个能够在量子线路特定位置添加特定信道的处理器,例如在测量门后添加比特翻转信道。`ChannelAdder` 类主要由三个函数构成,`_accepter()`、`_excluder()` 和 `_handler(BasicGate)`,其功能对应如下:\n",
    "\n",
    "- `_accepter()`:返回一个由函数构成的列表,称为接受规则集,其中每个接受规则函数的输入都是一个量子门,当函数返回值为 `True` 时表示我们可以在该量子门后添加信道。\n",
    "\n",
    "- `_excluder()`:返回一个由函数构成的列表,称为拒绝规则集,其中每个拒绝规则函数的输入都是一个量子门,当函数返回值为 `True` 时表示我们拒绝在该量子门后添加信道。\n",
    "\n",
    "- `_handler(BasicGate)`:输入一个量子门,返回一段量子线路,表示在输入量子门后添加一段自定义的信道。\n",
    "\n",
    "我们重定义类 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html) 的 `__call__` 函数,直接调用 `ChannelAdder` 即可生成处理后的量子线路。\n",
    "下面介绍几种 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html)。\n",
    "\n",
    "### [BitFlipAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.BitFlipAdder.html)\n",
    "\n",
    "[BitFlipAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.BitFlipAdder.html) 的接口定义为:\n",
    "\n",
    "```python\n",
    "BitFlipAdder(flip_rate: float, with_ctrl=True, focus_on: int = None, add_after: bool = True)\n",
    "```\n",
    "\n",
    "该 `Adder` 会在量子门后添加一个比特翻转信道,接口的参数含义为:\n",
    "\n",
    "- **flip_rate** (float):比特翻转信道的翻转概率。\n",
    "- **with_ctrl** (bool):是否在控制位上添加比特。默认值: ``True``。\n",
    "- **focus_on** (bool):只将该噪声信道作用在 ``focus_on`` 比特上。如果为 ``None``,则作用在量子门的所有比特上。默认值: ``None``。\n",
    "- **add_after** (bool):是否在量子门后面添加信道。如果为 ``False``,信道将会加在量子门前面。默认值: ``True``。\n",
    "\n",
    "例如,我们可以通过如下接口,在给定量子线路的每个量子门后都添加一个翻转概率为 ``0.3`` 的比特翻转信道:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe819f3caf0>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit.channel_adder import BitFlipAdder\n",
    "from mindquantum.core import gates as G\n",
    "from mindquantum.core.circuit import Circuit\n",
    "\n",
    "circ = Circuit()+G.H(0)+G.RX('a').on(1)+G.Z(1, 0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"416.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"416.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"396.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"396.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=3/10 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><rect x=\"132.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"172.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=3/10 </text><circle cx=\"252.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"252.8\" x2=\"252.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"232.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"252.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"292.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=3/10 </text></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe8280fe610>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bit_flip_adder = BitFlipAdder(0.3, with_ctrl=False)\n",
    "new_circ = bit_flip_adder(circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [MeasureAccepter](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MeasureAccepter.html)\n",
    "\n",
    "```python\n",
    "MeasureAccepter()\n",
    "```\n",
    "\n",
    "该 `Adder` 会选择对应的测量门,它目前只是一个 `Accepter`,不会改变量子线路中的任何门,需要利用 [MixerAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MixerAdder.html),跟其他的 `Adder` 搭配使用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [MixerAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MixerAdder.html)\n",
    "\n",
    "```python\n",
    "MixerAdder(adders: typing.List[ChannelAdderBase])\n",
    "```\n",
    "\n",
    "[MixerAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MixerAdder.html) 可以将多个 `Adder` 混合起来,保证量子门在每一个 `Adder` 中的接受函数集和拒绝函数集同时满足时,顺序添加 `_handler` 产生的量子线路。\n",
    "\n",
    "举例来说,我们可以将上文提到的 [BitFlipAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.BitFlipAdder.html) 和 [MeasureAccepter](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MeasureAccepter.html) 混合起来,达到只在测量门前添加比特翻转信道的功能:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MixerAdder<\n",
      "  BitFlipAdder<flip_rate=0.01, with_ctrl=True>\n",
      "  MeasureAccepter<>\n",
      ">\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"376.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"376.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"356.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"356.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"192.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"232.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"232.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"292.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"312.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 300.0 50.4 A 12.8 12.8 0 0 1 325.6 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 316.9018483174859 33.93539030917347 L 325.21569219381655 29.135390309173467 L 325.21569219381655 38.73539030917347 L 321.8901546432843 36.815390309173466 L 314.0470765814496 50.4 L 312.3843078061835 49.44 L 320.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe818473070>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import display_svg\n",
    "from mindquantum.core.circuit.channel_adder import MixerAdder, MeasureAccepter\n",
    "\n",
    "mixer = MixerAdder([\n",
    "    BitFlipAdder(flip_rate=0.01),\n",
    "    MeasureAccepter(),\n",
    "], add_after=False)\n",
    "print(mixer)\n",
    "\n",
    "circ = Circuit() + G.H(0) + G.RX('a').on(1) + G.Z(1, 0) + G.Measure().on(0)\n",
    "display_svg(circ.svg())\n",
    "new_circ = mixer(circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [SequentialAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.SequentialAdder.html)\n",
    "\n",
    "```python\n",
    "SequentialAdder(adders: typing.List[ChannelAdderBase])\n",
    "```\n",
    "\n",
    "[SequentialAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.SequentialAdder.html) 是由多个 `Adder` 顺序构成类,量子线路会经过 [SequentialAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.SequentialAdder.html) 中的 `Adder` 依次处理,生成最终的量子线路。例如,我们想构建一个先在测量门前添加一个 $p=0.01$ 的比特翻转信道,然后在 `q1` 比特上的非测量门和非噪声信道后添加 $p=0.05$ 的去极化信道。\n",
    "\n",
    "### 自定义 `Adder`\n",
    "\n",
    "我们首先自定义在某个比特添加比特翻转信道的 `Adder`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SequentialAdder<\n",
      "  MixerAdder<\n",
      "    MeasureAccepter<>\n",
      "    BitFlipAdder<flip_rate=0.01, with_ctrl=True>\n",
      "  >\n",
      "  CustomDepolarizingAdder<q=1, flip_rate=0.05>\n",
      ">\n"
     ]
    }
   ],
   "source": [
    "from mindquantum.core.circuit.channel_adder import ChannelAdderBase, SequentialAdder\n",
    "\n",
    "class CustomDepolarizingAdder(ChannelAdderBase):\n",
    "    def __init__(self, q, p):\n",
    "        self.q = q\n",
    "        self.p = p\n",
    "        super().__init__()\n",
    "\n",
    "    def _accepter(self):\n",
    "        return [lambda x: self.q in x.obj_qubits or self.q in x.ctrl_qubits]\n",
    "\n",
    "    def _excluder(self):\n",
    "        return  [lambda x: isinstance(x, (G.Measure, G.NoiseGate))]\n",
    "\n",
    "    def _handler(self, g):\n",
    "        return Circuit([G.DepolarizingChannel(self.p).on(self.q)])\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"CustomDepolarizingAdder<q={self.q}, flip_rate={self.p}>\"\n",
    "\n",
    "seq_adder = SequentialAdder([\n",
    "    MixerAdder([\n",
    "        MeasureAccepter(),\n",
    "        BitFlipAdder(flip_rate=0.01),\n",
    "    ], add_after=False),\n",
    "    CustomDepolarizingAdder(q=1, p=0.05),\n",
    "])\n",
    "print(seq_adder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"476.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"476.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"456.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"456.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><rect x=\"132.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"172.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/20 </text><circle cx=\"252.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"252.8\" x2=\"252.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"232.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"252.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"292.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/20 </text><rect x=\"292.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"332.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"392.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"412.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 400.0 50.4 A 12.8 12.8 0 0 1 425.6 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 416.9018483174859 33.93539030917347 L 425.21569219381655 29.135390309173467 L 425.21569219381655 38.73539030917347 L 421.8901546432843 36.815390309173466 L 414.04707658144963 50.4 L 412.3843078061835 49.44 L 420.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe82b17dac0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = Circuit() + G.H(0) + G.RX('a').on(1) + G.Z(1, 0) + G.Measure().on(0)\n",
    "display_svg(circ.svg())\n",
    "new_circ = seq_adder(circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上述自定义量子信道也可以通过 MindQuantum 中的预定义信道搭建而成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SequentialAdder<\n",
      "  MixerAdder<\n",
      "    MeasureAccepter<>\n",
      "    BitFlipAdder<flip_rate=0.01, with_ctrl=True>\n",
      "  >\n",
      "  MixerAdder<\n",
      "    ReverseAdder<\n",
      "      MeasureAccepter<>\n",
      "    >\n",
      "    NoiseExcluder<>\n",
      "    NoiseChannelAdder<channel=DC(p=1/20), with_ctrl=True>\n",
      "  >\n",
      ">\n"
     ]
    }
   ],
   "source": [
    "from mindquantum.core.circuit import ReverseAdder, NoiseExcluder, NoiseChannelAdder\n",
    "seq_adder = SequentialAdder([\n",
    "    MixerAdder([\n",
    "        MeasureAccepter(),\n",
    "        BitFlipAdder(flip_rate=0.01),\n",
    "    ], add_after=False),\n",
    "    MixerAdder([\n",
    "        ReverseAdder(MeasureAccepter()),\n",
    "        NoiseExcluder(),\n",
    "        NoiseChannelAdder(G.DepolarizingChannel(0.05), focus_on=1),\n",
    "    ])\n",
    "])\n",
    "print(seq_adder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"476.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"476.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"456.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"456.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text><rect x=\"132.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"172.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/20 </text><circle cx=\"252.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"252.8\" x2=\"252.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"232.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"252.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"292.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/20 </text><rect x=\"292.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"332.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"392.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"412.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 400.0 50.4 A 12.8 12.8 0 0 1 425.6 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 416.9018483174859 33.93539030917347 L 425.21569219381655 29.135390309173467 L 425.21569219381655 38.73539030917347 L 421.8901546432843 36.815390309173466 L 414.04707658144963 50.4 L 412.3843078061835 49.44 L 420.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe789aca6d0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq_adder(circ).svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 更复杂的例子\n",
    "\n",
    "下面我们来搭建一个更复杂的 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html) 例子,在该例子中,芯片的不同比特上的单比特门操作的噪声可以忽略不记,而双比特门在不同比特上具有不同的去极化信道,且线路的测量具有一个翻转概率为0.01的比特翻转错误。\n",
    "\n",
    "我们假设不同比特上的去极化信道为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "dc0 = G.DepolarizingChannel(0.01)\n",
    "dc1 = G.DepolarizingChannel(0.02)\n",
    "dc2 = G.DepolarizingChannel(0.03)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后,我们定义出满足要求的 `Adder`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SequentialAdder<\n",
       "  MixerAdder<\n",
       "    NoiseExcluder<>\n",
       "    ReverseAdder<\n",
       "      MeasureAccepter<>\n",
       "    >\n",
       "    QubitNumberConstrain<n_qubits=2, with_ctrl=True>\n",
       "    NoiseChannelAdder<channel=DC(p=1/100), with_ctrl=True>\n",
       "  >\n",
       "  MixerAdder<\n",
       "    NoiseExcluder<>\n",
       "    ReverseAdder<\n",
       "      MeasureAccepter<>\n",
       "    >\n",
       "    QubitNumberConstrain<n_qubits=2, with_ctrl=True>\n",
       "    NoiseChannelAdder<channel=DC(p=1/50), with_ctrl=True>\n",
       "  >\n",
       "  MixerAdder<\n",
       "    NoiseExcluder<>\n",
       "    ReverseAdder<\n",
       "      MeasureAccepter<>\n",
       "    >\n",
       "    QubitNumberConstrain<n_qubits=2, with_ctrl=True>\n",
       "    NoiseChannelAdder<channel=DC(p=0.03), with_ctrl=True>\n",
       "  >\n",
       "  MixerAdder<\n",
       "    NoiseExcluder<>\n",
       "    MeasureAccepter<>\n",
       "    BitFlipAdder<flip_rate=0.01, with_ctrl=True>\n",
       "  >\n",
       ">"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import QubitNumberConstrain\n",
    "noise_adder = SequentialAdder([\n",
    "    MixerAdder([\n",
    "        NoiseExcluder(),\n",
    "        ReverseAdder(MeasureAccepter()),\n",
    "        QubitNumberConstrain(2),\n",
    "        NoiseChannelAdder(dc0, focus_on=0),\n",
    "    ]),\n",
    "    MixerAdder([\n",
    "        NoiseExcluder(),\n",
    "        ReverseAdder(MeasureAccepter()),\n",
    "        QubitNumberConstrain(2),\n",
    "        NoiseChannelAdder(dc1, focus_on=1),\n",
    "    ]),\n",
    "    MixerAdder([\n",
    "        NoiseExcluder(),\n",
    "        ReverseAdder(MeasureAccepter()),\n",
    "        QubitNumberConstrain(2),\n",
    "        NoiseChannelAdder(dc2, focus_on=2),\n",
    "    ]),\n",
    "    MixerAdder([\n",
    "        NoiseExcluder(),\n",
    "        MeasureAccepter(),\n",
    "        BitFlipAdder(0.01)\n",
    "    ], add_after=False),\n",
    "])\n",
    "noise_adder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假设我们想要处理的量子线路是哈密顿量 $H=a_{01} Z_0Z_1 + a_{12} Z_1Z_2 + b_0 X_0 + b_1 X_1 + b_2 X_2$ 含时演化的一阶Trotter近似线路:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " b_0 [X0] +\n",
       " b_1 [X1] +\n",
       " b_2 [X2] +\n",
       "a_01 [Z0 Z1] +\n",
       "a_12 [Z1 Z2]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.operators import TimeEvolution, QubitOperator\n",
    "\n",
    "ham = sum([\n",
    "    QubitOperator('X0', 'b_0'),\n",
    "    QubitOperator('X1', 'b_1'),\n",
    "    QubitOperator('X2', 'b_2'),\n",
    "    QubitOperator('Z0 Z1', 'a_01'),\n",
    "    QubitOperator('Z1 Z2', 'a_12')\n",
    "])\n",
    "ham"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"736.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"736.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"716.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"716.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"716.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*b_0 </text><rect x=\"72.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*b_1 </text><rect x=\"72.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*b_2 </text><rect x=\"172.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"192.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"192.8\" x2=\"192.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"172.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"178.8\" x2=\"206.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"192.8\" x2=\"192.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"232.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"232.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"272.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*a_01 </text><rect x=\"332.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"352.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"352.8\" x2=\"352.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"332.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"338.8\" x2=\"366.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"352.8\" x2=\"352.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"392.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"392.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"412.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"412.8\" x2=\"412.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"392.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"398.8\" x2=\"426.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"412.8\" x2=\"412.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"452.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"452.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"492.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"492.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*a_12 </text><rect x=\"552.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"572.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"572.8\" x2=\"572.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"552.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"558.8\" x2=\"586.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"572.8\" x2=\"572.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"612.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"612.8\" y=\"20.0\" width=\"20\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"652.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"672.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 660.0 50.4 A 12.8 12.8 0 0 1 685.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 676.9018483174859 33.93539030917347 L 685.2156921938165 29.135390309173467 L 685.2156921938165 38.73539030917347 L 681.8901546432843 36.815390309173466 L 674.0470765814496 50.4 L 672.3843078061834 49.44 L 680.2273858680181 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"652.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"672.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 660.0 110.4 A 12.8 12.8 0 0 1 685.5999999999999 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 676.9018483174859 93.93539030917347 L 685.2156921938165 89.13539030917347 L 685.2156921938165 98.73539030917347 L 681.8901546432843 96.81539030917347 L 674.0470765814496 110.4 L 672.3843078061834 109.44 L 680.2273858680181 95.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"652.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"672.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 660.0 170.4 A 12.8 12.8 0 0 1 685.5999999999999 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 676.9018483174859 153.93539030917347 L 685.2156921938165 149.13539030917346 L 685.2156921938165 158.73539030917345 L 681.8901546432843 156.81539030917347 L 674.0470765814496 170.4 L 672.3843078061834 169.44 L 680.2273858680181 155.85539030917346 Z\" fill=\"#ffffff\" /></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe789b34cd0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = TimeEvolution(ham).circuit\n",
    "circ.barrier()\n",
    "circ.measure_all()\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此线路经过上述定义的 `noise_adder` 处理后的量子线路为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1236.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"1236.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"1216.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"1216.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"1216.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*b_0 </text><rect x=\"72.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*b_1 </text><rect x=\"72.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*b_2 </text><rect x=\"172.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"192.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"192.8\" x2=\"192.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"172.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"178.8\" x2=\"206.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"192.8\" x2=\"192.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"232.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"272.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/50 </text><rect x=\"232.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"272.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"332.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"332.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"372.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"372.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*a_01 </text><rect x=\"432.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"452.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"452.8\" x2=\"452.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"438.8\" x2=\"466.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"452.8\" x2=\"452.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"492.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"532.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"532.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/50 </text><rect x=\"492.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"532.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"532.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"592.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"592.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"612.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"612.8\" x2=\"612.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"592.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"598.8\" x2=\"626.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"612.8\" x2=\"612.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"652.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"692.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"692.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=0.03 </text><rect x=\"652.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"692.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"692.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/50 </text><rect x=\"752.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"752.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"792.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"792.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2*a_12 </text><rect x=\"852.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><circle cx=\"872.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"872.8\" x2=\"872.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"852.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"858.8\" x2=\"886.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"872.8\" x2=\"872.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"912.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"952.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"952.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=0.03 </text><rect x=\"912.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"952.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"952.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/50 </text><rect x=\"1012.8\" y=\"20.0\" width=\"0\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"1012.8\" y=\"20.0\" width=\"20\" height=\"160\" fill=\"gray\" fill-opacity=\"0.8\" /><rect x=\"1052.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"1092.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"1092.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"1152.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"1172.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 1160.0 50.4 A 12.8 12.8 0 0 1 1185.6 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 1176.901848317486 33.93539030917347 L 1185.2156921938165 29.135390309173467 L 1185.2156921938165 38.73539030917347 L 1181.8901546432842 36.815390309173466 L 1174.0470765814496 50.4 L 1172.3843078061834 49.44 L 1180.227385868018 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"1052.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"1092.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"1092.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"1152.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"1172.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 1160.0 110.4 A 12.8 12.8 0 0 1 1185.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 1176.901848317486 93.93539030917347 L 1185.2156921938165 89.13539030917347 L 1185.2156921938165 98.73539030917347 L 1181.8901546432842 96.81539030917347 L 1174.0470765814496 110.4 L 1172.3843078061834 109.44 L 1180.227385868018 95.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"1052.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"1092.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"1092.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"1152.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"1172.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 1160.0 170.4 A 12.8 12.8 0 0 1 1185.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 1176.901848317486 153.93539030917347 L 1185.2156921938165 149.13539030917346 L 1185.2156921938165 158.73539030917345 L 1181.8901546432842 156.81539030917347 L 1174.0470765814496 170.4 L 1172.3843078061834 169.44 L 1180.227385868018 155.85539030917346 Z\" fill=\"#ffffff\" /></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe789aca610>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "noise_adder(circ).svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html) 列表\n",
    "\n",
    "下面列举出 MindQuantum 中现有的一些 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html),并给出具体含义:\n",
    "\n",
    "|[ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html)| 功能|\n",
    "|--|--|\n",
    "|[ChannelAdderBase](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html)|在量子门前面或者后面添加信道|\n",
    "|[NoiseChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.NoiseChannelAdder.html)|添加一个单比特量子信道|\n",
    "|[MeasureAccepter](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MeasureAccepter.html)|选取测量门|\n",
    "|[ReverseAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ReverseAdder.html)|翻转给定信道添加器的接受和拒绝规则|\n",
    "|[NoiseExcluder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.NoiseExcluder.html)|排除噪声门|\n",
    "|[BitFlipAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.BitFlipAdder.html)|在量子门前面或者后面添加一个比特翻转信道|\n",
    "|[MixerAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.MixerAdder.html)|在子添加器的接受集被满足、拒绝集被拒绝时依次执行所有的添加器|\n",
    "|[SequentialAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.SequentialAdder.html)|依次执行每一个添加器|\n",
    "|[QubitNumberConstrain](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.QubitNumberConstrain.html)|只将噪声信道作用在比特数为 ``n_qubits`` 的量子门上|\n",
    "|[QubitIDConstrain](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.QubitIDConstrain.html)|只将噪声信道作用在给定比特序号的量子门上|\n",
    "|[GateSelector](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.GateSelector.html)|选择想要的量子门添加信道|\n",
    "|[DepolarizingChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.DepolarizingChannelAdder.html)|添加去极化信道|\n",
    "\n",
    "MindQuantum 中 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html) 的API接口文档请参考:[channel_adder](https://mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/mindquantum.core.circuit.html#channel-adder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于 [ChannelAdder](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.ChannelAdderBase.html) 的噪声模拟器\n",
    "\n",
    "我们可以将如上定义的各种 `Adder` 与现有的模拟器组合,构成一个含噪声模拟器。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1 </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"212.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 200.0 110.4 A 12.8 12.8 0 0 1 225.60000000000002 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 216.90184831748593 93.93539030917347 L 225.21569219381655 89.13539030917347 L 225.21569219381655 98.73539030917347 L 221.8901546432843 96.81539030917347 L 214.04707658144957 110.4 L 212.38430780618347 109.44 L 220.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" /><text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Shots:\n",
       " 10000 </text><text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Keys: q1 </text><line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.0 </text><line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.154 </text><line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.307 </text><line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.461 </text><line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.614 </text><line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.768 </text><line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >0 </text><line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"31.2\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1704179475563011745\" fill=\"#5e7ce0\" /><text x=\"341.2\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1704179475563056820\" fill-opacity=\"0\" >7677 </text><text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >1 </text><line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"31.2\" y=\"103.0\" width=\"90.77764751856193\" height=\"24\" id=\"bar_1_1704179475563086978\" fill=\"#16acff\" /><text x=\"131.97764751856192\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1704179475563105899\" fill-opacity=\"0\" >2323 </text><animate xlink:href=\"#bar_0_1704179475563011745\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_1_1704179475563086978\" attributeName=\"width\" from=\"0\" to=\"90.77764751856193\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 90.77764751856193\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_0_1704179475563011745\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_0_1704179475563056820\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_1_1704179475563105899\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >probability </text></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7fe789b16a60>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mindquantum.simulator import Simulator\n",
    "from mindquantum.simulator.noise import NoiseBackend\n",
    "\n",
    "noiseless_sim = Simulator('mqvector', 2)\n",
    "noiseless_circ = Circuit().h(0).rx(1.0, 1).z(1, 0).measure(1)\n",
    "display_svg(noiseless_circ.svg())\n",
    "res1 = noiseless_sim.sampling(noiseless_circ, shots=10000)\n",
    "display(res1.svg())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" /><text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Shots:\n",
       " 10000 </text><text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Keys: q1 </text><line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.0 </text><line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.149 </text><line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.297 </text><line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.446 </text><line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.594 </text><line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.743 </text><line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >0 </text><line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"31.2\" y=\"73.0\" width=\"299.99999999999994\" height=\"24\" id=\"bar_0_1704179475616572010\" fill=\"#5e7ce0\" /><text x=\"341.19999999999993\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1704179475616594075\" fill-opacity=\"0\" >7426 </text><text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >1 </text><line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"31.2\" y=\"103.0\" width=\"103.98599515216804\" height=\"24\" id=\"bar_1_1704179475616608854\" fill=\"#16acff\" /><text x=\"145.18599515216803\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1704179475616618184\" fill-opacity=\"0\" >2574 </text><animate xlink:href=\"#bar_0_1704179475616572010\" attributeName=\"width\" from=\"0\" to=\"299.99999999999994\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 299.99999999999994\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_1_1704179475616608854\" attributeName=\"width\" from=\"0\" to=\"103.98599515216804\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 103.98599515216804\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_0_1704179475616572010\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_0_1704179475616594075\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_1_1704179475616618184\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >probability </text></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7fe8280fe7f0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"92.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1 </text><rect x=\"132.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"172.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/20 </text><circle cx=\"252.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"252.8\" x2=\"252.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"232.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"252.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><rect x=\"292.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >DC </text><text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/20 </text><rect x=\"392.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#c77532\" fill-opacity=\"1\" /><text x=\"432.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >BFC </text><text x=\"432.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >p=1/100 </text><rect x=\"492.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 110.4 A 12.8 12.8 0 0 1 525.5999999999999 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 93.93539030917347 L 525.2156921938165 89.13539030917347 L 525.2156921938165 98.73539030917347 L 521.8901546432843 96.81539030917347 L 514.0470765814496 110.4 L 512.3843078061834 109.44 L 520.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe819f3cdc0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "noise_sim = Simulator(NoiseBackend('mqvector', 2, seq_adder))\n",
    "res2 = noise_sim.sampling(noiseless_circ, shots=10000)\n",
    "display(res2.svg())\n",
    "display(noise_sim.backend.transform_circ(noiseless_circ).svg())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<table border=\"1\">\n",
       "  <tr>\n",
       "    <th>Software</th>\n",
       "    <th>Version</th>\n",
       "  </tr>\n",
       "<tr><td>mindquantum</td><td>0.9.11</td></tr>\n",
       "<tr><td>scipy</td><td>1.10.1</td></tr>\n",
       "<tr><td>numpy</td><td>1.24.4</td></tr>\n",
       "<tr>\n",
       "    <th>System</th>\n",
       "    <th>Info</th>\n",
       "</tr>\n",
       "<tr><td>Python</td><td>3.8.17</td></tr><tr><td>OS</td><td>Linux x86_64</td></tr><tr><td>Memory</td><td>16.62 GB</td></tr><tr><td>CPU Max Thread</td><td>16</td></tr><tr><td>Date</td><td>Tue Jan  2 15:11:15 2024</td></tr>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<mindquantum.utils.show_info.InfoTable at 0x7fe789ad0820>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.utils.show_info import InfoTable\n",
    "\n",
    "InfoTable('mindquantum', 'scipy', 'numpy')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MindSpore",
   "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",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}