{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "0af88701-d9bc-47f2-8ceb-2ae51f567934",
      "metadata": {},
      "source": [
        "---\n",
        "title: \"회로를 시각화하다\"\n",
        "description: \"Qiskit 시각화 모듈을 사용하여 회로 시각화를 생성하고 작업 데이터를 플롯합니다\"\n",
        "---\n",
        "\n",
        "{/* cspell:ignore qcircuit mactex, backgroundcolor, lightgreen */}\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8d75dc24-b8d2-45f8-830b-48e45f794a31",
      "metadata": {},
      "source": [
        "<span id=\"visualize-circuits\" />\n",
        "\n",
        "# 회로를 시각화하다\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7ea76afb-aecf-4ea2-b173-5d5e7e7d68ec",
      "metadata": {
        "tags": [
          "version-info"
        ]
      },
      "source": [
        "{/*\n",
        "  DO NOT EDIT THIS CELL!!!\n",
        "  This cell's content is generated automatically by a script. Anything you add\n",
        "  here will be removed next time the notebook is run. To add new content, create\n",
        "  a new cell before or after this one.\n",
        "  */}\n",
        "\n",
        "<Accordion>\n",
        "  <AccordionItem title=\"패키지 버전\">\n",
        "    이 페이지의 코드는 다음 요구 사항을 사용하여 개발되었습니다.\n",
        "    다음 버전 이상을 사용하는 것이 좋습니다.\n",
        "\n",
        "    ```\n",
        "    qiskit[all]~=2.5.2\n",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "bd5865b3-adfa-4c87-be55-51f693335184",
      "metadata": {},
      "source": [
        "생성 중인 회로를 보는 것은 종종 유용합니다. 다음 옵션을 사용하여 키스킷 회로를 표시할 수 있습니다.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "488c8d7d-5615-40ec-bf84-f1fa94832fce",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit import QuantumCircuit"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8bbddcca-300c-4f20-98dd-a0723cdef026",
      "metadata": {},
      "source": [
        "<span id=\"draw-a-quantum-circuit\" />\n",
        "\n",
        "## 양자 회로를 그리세요\n",
        "\n",
        "`QuantumCircuit` 클래스는 `draw()` 메서드를 통해 회로를 그리거나 회로 객체를 인쇄하여 회로를 그릴 수 있도록 지원합니다. 기본적으로 둘 다 회로도의 ASCII 아트 버전을 렌더링합니다.\n",
        "\n",
        "`print` 은 `None` 을 반환하지만 다이어그램을 인쇄하는 부작용이 있는 반면 `QuantumCircuit.draw` 은 부작용 없이 다이어그램을 반환한다는 점에 유의하세요. 주피터 노트북은 각 셀의 마지막 줄의 출력을 표시하기 때문에 동일한 효과가 있는 것처럼 보입니다.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "547f07e8-7891-433c-ac53-3186196b3aa7",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Build a quantum circuit\n",
        "circuit = QuantumCircuit(3, 3)\n",
        "circuit.x(1)\n",
        "circuit.h(range(3))\n",
        "circuit.cx(0, 1)\n",
        "circuit.measure(range(3), range(3));"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "d69ae2df-c31c-414f-bba2-4c4a1012de41",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "     ┌───┐          ┌─┐   \n",
            "q_0: ┤ H ├───────■──┤M├───\n",
            "     ├───┤┌───┐┌─┴─┐└╥┘┌─┐\n",
            "q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├\n",
            "     ├───┤└┬─┬┘└───┘ ║ └╥┘\n",
            "q_2: ┤ H ├─┤M├───────╫──╫─\n",
            "     └───┘ └╥┘       ║  ║ \n",
            "c: 3/═══════╩════════╩══╩═\n",
            "            2        0  1 \n"
          ]
        }
      ],
      "source": [
        "print(circuit)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "04c91fd9-50a1-4ff3-84e4-0b51a6b1541a",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "     ┌───┐          ┌─┐   \n",
              "q_0: ┤ H ├───────■──┤M├───\n",
              "     ├───┤┌───┐┌─┴─┐└╥┘┌─┐\n",
              "q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├\n",
              "     ├───┤└┬─┬┘└───┘ ║ └╥┘\n",
              "q_2: ┤ H ├─┤M├───────╫──╫─\n",
              "     └───┘ └╥┘       ║  ║ \n",
              "c: 3/═══════╩════════╩══╩═\n",
              "            2        0  1 "
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "circuit.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2159af06-93b8-441d-b8aa-c29324d26c6e",
      "metadata": {},
      "source": [
        "<span id=\"alternative-renderers\" />\n",
        "\n",
        "### 대체 렌더러\n",
        "\n",
        "텍스트 출력은 회로를 개발하는 동안 출력을 빠르게 확인하는 데 유용하지만 유연성이 떨어지는 단점이 있습니다. 양자 회로에는 두 가지 대체 출력 렌더러가 있습니다. 하나는 용도 [Matplotlib](https://matplotlib.org/) 를 사용하고 다른 하나는 [LaTeX](https://www.latex-project.org/). LaTeX 렌더러에는 [qcircuit 패키지가](https://github.com/CQuIC/qcircuit) 필요합니다. \"output\" 인수를 문자열 `mpl` 및 `latex` 으로 설정하여 이러한 렌더러를 선택합니다.\n",
        "\n",
        "<Admonition type=\"tip\">\n",
        "  OSX 사용자는 [mactex 패키](https://www.tug.org/mactex/) 지를 통해 필요한 LaTeX 패키지를 받을 수 있습니다.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "3f9c61c9-58f9-4315-a639-455fa2e58450",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/3f9c61c9-58f9-4315-a639-455fa2e58450-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Matplotlib drawing\n",
        "circuit.draw(output=\"mpl\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "94948dab-57de-45f0-8dd7-5901ae69b70a",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/94948dab-57de-45f0-8dd7-5901ae69b70a-0.avif\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Latex drawing\n",
        "circuit.draw(output=\"latex\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "51fc6f51-6345-435d-abed-857f29a3f451",
      "metadata": {},
      "source": [
        "<span id=\"save-output\" />\n",
        "\n",
        "### 출력 내용 저장\n",
        "\n",
        "주피터 노트북에서 대규모 회로를 인라인으로 그리면 속도가 느리거나 판독이 어려울 수 있습니다.\n",
        "다이어그램을 파일에 직접 저장한 다음 이미지 뷰어에서 열어 필요에 따라 확대할 수 있습니다.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "17889caf-d953-4661-9188-00505c17064e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/17889caf-d953-4661-9188-00505c17064e-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Save as an image using the Matplotlib drawer\n",
        "circuit.draw(output=\"mpl\", filename=\"circuit-mpl.jpeg\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "a36d1aa9-fa0d-4e27-ac83-5deee43a20dd",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/a36d1aa9-fa0d-4e27-ac83-5deee43a20dd-0.avif\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Or save a LaTeX rendering\n",
        "circuit.draw(output=\"latex\", filename=\"circuit-latex.pdf\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "78688e6c-3ed2-4ef2-b3db-80fdae35e585",
      "metadata": {
        "jp-MarkdownHeadingCollapsed": true
      },
      "source": [
        "<span id=\"control-circuit-drawings\" />\n",
        "\n",
        "### 제어 회로도\n",
        "\n",
        "기본적으로 `draw()` 메서드는 렌더링된 이미지를 객체로 반환하고 아무것도 출력하지 않습니다. 반환되는 정확한 클래스는 지정된 출력에 따라 다릅니다: `'text'` (기본값)은 `TextDrawer` 객체를 반환하고, `'mpl'` 은 `matplotlib.Figure` 객체를 반환하며, `latex` 은 `PIL.Image` 객체를 반환합니다. Jupyter 노트북은 이러한 반환 유형을 이해하고 올바르게 렌더링하지만, Jupyter 외부에서 실행할 때는 이미지가 자동으로 표시되지 않습니다.\n",
        "\n",
        "`draw()` 메서드에는 출력을 표시하거나 저장하기 위한 선택적 인수가 있습니다. 지정하면 `filename` kwarg는 렌더링된 출력을 저장하는 경로를 사용합니다. 또는 `mpl` 또는 `latex` 출력을 사용하는 경우, `interactive` kwarg를 사용해 새 창에서 이미지를 열 수 있습니다(노트북 내에서 항상 작동하는 것은 아님).\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fc41270b-3518-40af-8921-dfa9666fc8e0",
      "metadata": {},
      "source": [
        "<span id=\"customize-the-output\" />\n",
        "\n",
        "### 출력 맞춤 설정\n",
        "\n",
        "출력에 따라 회로도를 사용자 지정할 수 있는 옵션도 있습니다.\n",
        "\n",
        "<span id=\"disable-plot-barriers-and-reverse-bit-order\" />\n",
        "\n",
        "#### 플롯 장벽 비활성화 및 비트 순서 반전\n",
        "\n",
        "처음 두 옵션은 세 백엔드 모두에서 공유됩니다. 이를 통해 비트 주문과 장벽을 그릴지 여부를 모두 구성할 수 있습니다. 이는 각각 `reverse_bits` kwarg 및 `plot_barriers` kwarg로 설정할 수 있습니다. 다음 예제는 모든 출력 렌더러에서 작동하며, 여기서는 간결성을 위해 `mpl` 을 사용했습니다.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "db61dc71-4cdc-4bb7-8139-8820e7785e55",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit import QuantumRegister, ClassicalRegister\n",
        "\n",
        "# Draw a new circuit with barriers and more registers\n",
        "q_a = QuantumRegister(3, name=\"a\")\n",
        "q_b = QuantumRegister(5, name=\"b\")\n",
        "c_a = ClassicalRegister(3)\n",
        "c_b = ClassicalRegister(5)\n",
        "\n",
        "circuit = QuantumCircuit(q_a, q_b, c_a, c_b)\n",
        "circuit.x(q_a[1])\n",
        "circuit.x(q_b[1])\n",
        "circuit.x(q_b[2])\n",
        "circuit.x(q_b[4])\n",
        "circuit.barrier()\n",
        "circuit.h(q_a)\n",
        "circuit.barrier(q_a)\n",
        "circuit.h(q_b)\n",
        "circuit.cswap(q_b[0], q_b[1], q_b[2])\n",
        "circuit.cswap(q_b[2], q_b[3], q_b[4])\n",
        "circuit.cswap(q_b[3], q_b[4], q_b[0])\n",
        "circuit.barrier(q_b)\n",
        "circuit.measure(q_a, c_a)\n",
        "circuit.measure(q_b, c_b);"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "8e57cd43-8a48-469d-8f69-8e7c936d4a1e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/8e57cd43-8a48-469d-8f69-8e7c936d4a1e-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Draw the circuit\n",
        "circuit.draw(output=\"mpl\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "8e7a251a-0a4f-43e0-8cf5-48493df7bad9",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/8e7a251a-0a4f-43e0-8cf5-48493df7bad9-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Draw the circuit with reversed bit order\n",
        "circuit.draw(output=\"mpl\", reverse_bits=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "b4a601ad-1c04-4b16-afbd-ac5a0ad42653",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/b4a601ad-1c04-4b16-afbd-ac5a0ad42653-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Draw the circuit without barriers\n",
        "circuit.draw(output=\"mpl\", plot_barriers=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "18b0bfbe-b586-4182-9a0d-b858655a9240",
      "metadata": {},
      "source": [
        "<span id=\"renderer-specific-customizations\" />\n",
        "\n",
        "### 렌더러별 맞춤 설정\n",
        "\n",
        "사용 가능한 일부 사용자 지정 옵션은 렌더러에 따라 다릅니다.\n",
        "\n",
        "`fold` 인수는 출력의 최대 너비를 설정합니다. `text` 렌더러에서는 다음 줄로 래핑되기 전 다이어그램의 줄 길이를 설정합니다.  'mpl' 렌더러를 사용하는 경우 다음 줄로 접히기 전의 (시각적) 레이어 수입니다.\n",
        "\n",
        "`mpl` 렌더러에는 색과 윤곽선을 변경하는 `style` kwarg가 있습니다. 자세한 내용은 [API 설명서를](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#draw) 참조하세요.\n",
        "\n",
        "`scale` 옵션은 `mpl` 및 `latex` 렌더러의 출력 크기를 조정합니다.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "1e08bf74-378f-45e6-b195-a9539061013d",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "   ┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐»\n",
              "q: ┤ H ├┤ H ├┤ H ├┤ H ├┤ H ├┤ H ├┤ H ├»\n",
              "   └───┘└───┘└───┘└───┘└───┘└───┘└───┘»\n",
              "«   ┌───┐┌───┐┌───┐\n",
              "«q: ┤ H ├┤ H ├┤ H ├\n",
              "«   └───┘└───┘└───┘"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "circuit = QuantumCircuit(1)\n",
        "for _ in range(10):\n",
        "    circuit.h(0)\n",
        "# limit line length to 40 characters\n",
        "circuit.draw(output=\"text\", fold=40)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "decadf88-4866-45a0-9e2f-836c51491f9e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/decadf88-4866-45a0-9e2f-836c51491f9e-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Change the background color in mpl\n",
        "\n",
        "style = {\"backgroundcolor\": \"lightgreen\"}\n",
        "circuit.draw(output=\"mpl\", style=style)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "ade9a653-3243-4ac9-bb0e-c8fb82f7a034",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/ade9a653-3243-4ac9-bb0e-c8fb82f7a034-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Scale the mpl output to 1/2 the normal size\n",
        "circuit.draw(output=\"mpl\", scale=0.5)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "495b560a-d835-448b-aa01-5025a7a0689e",
      "metadata": {},
      "source": [
        "<span id=\"standalone-circuit-drawing-function\" />\n",
        "\n",
        "### 독립형 회로도 그리기 기능\n",
        "\n",
        "회로 객체의 메서드가 아닌 독립된 함수로 회로를 그리는 것을 선호하는 애플리케이션이 있는 경우 `qiskit.visualization` 에서 공개 안정 인터페이스의 일부인 `circuit_drawer()` 함수를 직접 사용할 수 있습니다. 이 함수는 회로 객체를 필수 인수로 받는다는 점을 제외하면 `circuit.draw()` 메서드와 동일하게 작동합니다.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "256dd092-b2eb-47af-a025-0ecdf85c2d5a",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/visualize-circuits/extracted-outputs/256dd092-b2eb-47af-a025-0ecdf85c2d5a-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.visualization import circuit_drawer\n",
        "\n",
        "circuit_drawer(circuit, output=\"mpl\", plot_barriers=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0761df2f-183f-48e3-9070-766a9920c2b9",
      "metadata": {},
      "source": [
        "<span id=\"next-steps\" />\n",
        "\n",
        "## 다음 단계\n",
        "\n",
        "<Admonition type=\"tip\" title=\"권장사항\">\n",
        "  * [그로버 알고리즘](/docs/tutorials/grovers-algorithm) 튜토리얼에서 회로 시각화 예시를 확인하세요.\n",
        "  * [IBM Quantum Composer](/docs/guides/composer) 를 사용하여 간단한 회로를 시각화하세요.\n",
        "  * [회로 타이밍을 시각화하십시오](/docs/guides/visualize-circuit-timing).\n",
        "  * [Qiskit 시각화 API](/docs/api/qiskit/visualization) 문서를 검토하십시오.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "id": "a1b8767d",
      "source": "© IBM Corp., 2017-2026"
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}