{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "bc54d7bc-b2d8-4b30-9b0b-05689e07a463",
      "metadata": {},
      "source": [
        "---\n",
        "title: Construct circuits\n",
        "description: How to construct and visualize quantum circuits in Qiskit.\n",
        "---\n",
        "\n",
        "# Construct circuits\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e11dd8ad-7edc-4cf8-bc13-388fe04e84fa",
      "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=\"Package versions\">\n",
        "    The code on this page was developed using the following requirements.\n",
        "    We recommend using these versions or newer.\n",
        "\n",
        "    ```\n",
        "    qiskit[all]~=2.3.0\n",
        "    ```\n",
        "  </AccordionItem>\n",
        "</Accordion>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c50d8e43-ae82-4e41-8d17-a37332d1bf6d",
      "metadata": {},
      "source": [
        "This page takes a closer look at the [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit) class in the Qiskit SDK, including some more advanced methods you can use to create quantum circuits.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2664d407-aa95-43a3-9101-d3ad58c2df58",
      "metadata": {},
      "source": [
        "## What is a quantum circuit?\n",
        "\n",
        "A simple quantum circuit is a collection of qubits and a list of instructions that act on those qubits. To demonstrate, the following cell creates a new circuit with two new qubits, then displays the circuit's [`qubits`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#qubits) attribute, which is a list of [`Qubits`](/docs/api/qiskit/circuit#qiskit.circuit.Qubit) in order from the least significant bit $q_0$ to the most significant bit $q_n$.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "b410d397-b67d-4f31-90cf-9c1c34c157c5",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[<Qubit register=(2, \"q\"), index=0>, <Qubit register=(2, \"q\"), index=1>]"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit import QuantumCircuit\n",
        "\n",
        "qc = QuantumCircuit(2)\n",
        "qc.qubits"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "392e3d6c",
      "metadata": {},
      "source": [
        "Multiple `QuantumRegister` and `ClassicalRegister` objects can be combined to create a circuit. Every [`QuantumRegister`](/docs/api/qiskit/circuit#qiskit.circuit.QuantumRegister) and [`ClassicalRegister`](/docs/api/qiskit/circuit#qiskit.circuit.ClassicalRegister) can also be named.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "6160e3f7",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[<Qubit register=(2, \"qreg1\"), index=0>,\n",
              " <Qubit register=(2, \"qreg1\"), index=1>,\n",
              " <Qubit register=(1, \"qreg2\"), index=0>]"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit import QuantumRegister, ClassicalRegister\n",
        "\n",
        "qr1 = QuantumRegister(2, \"qreg1\")  # Create a QuantumRegister with 2 qubits\n",
        "qr2 = QuantumRegister(1, \"qreg2\")  # Create a QuantumRegister with 1 qubit\n",
        "cr1 = ClassicalRegister(3, \"creg1\")  # Create a ClassicalRegister with 3 cbits\n",
        "\n",
        "combined_circ = QuantumCircuit(\n",
        "    qr1, qr2, cr1\n",
        ")  # Create a quantum circuit with 2 QuantumRegisters and 1 ClassicalRegister\n",
        "combined_circ.qubits"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6e7b212f",
      "metadata": {},
      "source": [
        "You can find a qubit's index and register by using the circuit's [`find_bit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#qiskit.circuit.QuantumCircuit.find_bit) method and its attributes.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "faeb3fbc",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Index: 2\n",
            "Register: [(QuantumRegister(1, 'qreg2'), 0)]\n"
          ]
        }
      ],
      "source": [
        "desired_qubit = qr2[0]  # Qubit 0 of register 'qreg2'\n",
        "\n",
        "print(\"Index:\", combined_circ.find_bit(desired_qubit).index)\n",
        "print(\"Register:\", combined_circ.find_bit(desired_qubit).registers)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f5c95cb2-a94f-48f3-b2a6-8ec6c25da5cd",
      "metadata": {},
      "source": [
        "Adding an instruction to the circuit appends the instruction to the circuit's [`data`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#data) attribute. The following cell output shows `data` is a list of [`CircuitInstruction`](/docs/api/qiskit/qiskit.circuit.CircuitInstruction) objects, each of which has an `operation` attribute, and a `qubits` attribute.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "f7b5573c-b2b2-4cbf-ba55-c53c9221ce71",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[CircuitInstruction(operation=Instruction(name='x', num_qubits=1, num_clbits=0, params=[]), qubits=(<Qubit register=(2, \"q\"), index=0>,), clbits=())]"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc.x(0)  # Add X-gate to qubit 0\n",
        "qc.data"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "17a82d8a-b717-44b8-b3f8-ce89e2588261",
      "metadata": {},
      "source": [
        "The easiest way to view this information is through the [`draw`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#draw) method, which returns a visualization of a circuit. See [Visualize circuits](./visualize-circuits) for different ways of displaying quantum circuits.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "43a57258-3e33-4071-8a48-2bf127c8a5be",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/43a57258-3e33-4071-8a48-2bf127c8a5be-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ab5f4bc9-7d7c-4ee7-b1bc-70a313b4fe29",
      "metadata": {},
      "source": [
        "Circuit instruction objects can contain \"definition\" circuits that describe the instruction in terms of more fundamental instructions. For example, the [X-gate](/docs/api/qiskit/qiskit.circuit.library.XGate) is defined as a specific case of the [U3-gate](/docs/api/qiskit/qiskit.circuit.library.U3Gate), a more general single-qubit gate.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "653e2427-e301-4d2f-84de-1959185ace8e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/653e2427-e301-4d2f-84de-1959185ace8e-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Draw definition circuit of 0th instruction in `qc`\n",
        "qc.data[0].operation.definition.draw(\"mpl\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "ee893af1-db43-449f-bfc4-8f636bd98546",
      "metadata": {},
      "source": [
        "Instructions and circuits are similar in that they both describe operations on bits and qubits, but they have different purposes:\n",
        "\n",
        "* Instructions are treated as fixed, and their methods will usually return new instructions (without mutating the original object).\n",
        "* Circuits are designed to be built over many lines of code, and [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit) methods often mutate the existing object.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2e451022",
      "metadata": {},
      "source": [
        "### What is circuit depth?\n",
        "\n",
        "The [depth()](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#qiskit.circuit.QuantumCircuit.depth) of a quantum circuit is a measure of the number of “layers” of quantum gates, executed in parallel, it takes to complete the computation defined by the circuit. Because quantum gates take time to implement, the depth of a circuit roughly corresponds to the amount of time it takes the quantum computer to execute the circuit. Thus, the depth of a circuit is one important quantity used to measure if a quantum circuit can be run on a device.\n",
        "\n",
        "The rest of this page illustrates how to manipulate quantum circuits.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ff4f08f3-48eb-454c-9647-2af505bf79bc",
      "metadata": {},
      "source": [
        "## Build circuits\n",
        "\n",
        "Methods such as [`QuantumCircuit.h`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#h) and [`QuantumCircuit.cx`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#cx) add specific instructions to circuits. To add instructions to a circuit more generally, use the [`append`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#append) method. This takes an instruction and a list of qubits to apply the instruction to. See the [Circuit Library API documentation](/docs/api/qiskit/circuit_library) for a list of supported instructions.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "66813cae-9841-47ea-96b7-8fd7b82e9759",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/66813cae-9841-47ea-96b7-8fd7b82e9759-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import HGate\n",
        "\n",
        "qc = QuantumCircuit(1)\n",
        "qc.append(\n",
        "    HGate(),  # New HGate instruction\n",
        "    [0],  # Apply to qubit 0\n",
        ")\n",
        "qc.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "bbdbef08-07c0-4fb0-ab3e-ed6efb7a3dc3",
      "metadata": {},
      "source": [
        "To combine two circuits, use the [`compose`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#compose) method. This accepts another [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit) and an optional list of qubit mappings.\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  The [`compose`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#compose) method returns a new circuit and does **not** mutate either circuit it acts on. To mutate the circuit on which you're calling the [`compose`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#compose) method, use the argument `inplace=True`.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "29152dfa-2275-4bc4-aadb-82185b9e0e86",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/29152dfa-2275-4bc4-aadb-82185b9e0e86-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc_a = QuantumCircuit(4)\n",
        "qc_a.x(0)\n",
        "\n",
        "qc_b = QuantumCircuit(2, name=\"qc_b\")\n",
        "qc_b.y(0)\n",
        "qc_b.z(1)\n",
        "\n",
        "# compose qubits (0, 1) of qc_a to qubits (1, 3) of qc_b respectively\n",
        "combined = qc_a.compose(qc_b, qubits=[1, 3])\n",
        "combined.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d4529506-8397-4208-9cfd-31e1a1978741",
      "metadata": {},
      "source": [
        "You might also want to compile circuits into instructions to keep your circuits organized. You can convert a circuit to an instruction by using the [`to_instruction`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#to_instruction) method, then append this to another circuit as you would any other instruction. The circuit drawn in the following cell is functionally equivalent to the circuit drawn in the previous cell.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "81b682dd-45cb-4492-809e-d9e8ebbf5600",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/81b682dd-45cb-4492-809e-d9e8ebbf5600-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "inst = qc_b.to_instruction()\n",
        "qc_a.append(inst, [1, 3])\n",
        "qc_a.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6e81c46b-a2d2-430e-9d3d-73e5030e3548",
      "metadata": {},
      "source": [
        "If your circuit is unitary, you can convert it to a [`Gate`](/docs/api/qiskit/qiskit.circuit.Gate)  by using the [`to_gate`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#to_gate) method. [`Gate`](/docs/api/qiskit/qiskit.circuit.Gate) objects are specific types of instructions that have some extra features, such as the [`control`](/docs/api/qiskit/qiskit.circuit.Gate#control) method, which adds a quantum control.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "ed362e64-d6a4-4dfd-a5cf-5e6bdc7a81b5",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/ed362e64-d6a4-4dfd-a5cf-5e6bdc7a81b5-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "gate = qc_b.to_gate().control()\n",
        "qc_a.append(gate, [0, 1, 3])\n",
        "qc_a.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ae850362-84eb-4998-a2cf-a93a1be4ac2a",
      "metadata": {},
      "source": [
        "To see what's going on, you can use the [`decompose`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#decompose) method to expand each instruction into its definition.\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  The [`decompose`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#decompose) method returns a new circuit and does **not** mutate the circuit it acts on.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "3c0633db-929b-4428-a888-7a3d493bd6dd",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/3c0633db-929b-4428-a888-7a3d493bd6dd-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc_a.decompose().draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "87f1e053",
      "metadata": {},
      "source": [
        "<span id=\"measure-qubits\" />\n",
        "\n",
        "## Measure qubits\n",
        "\n",
        "Measurements are used to sample the states of individual qubits and transfer the results to a classical register. Note that if you are submitting circuits to a [Sampler](./primitives#sampler) primitive, measurements are required. However, circuits submitted to an [Estimator](./primitives#estimator) primitive must not contain measurements.\n",
        "\n",
        "Qubits can be measured using three methods: [`measure`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#qiskit.circuit.QuantumCircuit.measure), [`measure_all`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#measure_all) and [`measure_active`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#measure_active). To learn how to visualize measured results, see the [Visualize results](./visualize-results) page.\n",
        "\n",
        "1. `QuantumCircuit.measure` : measures each qubit in the first argument onto the classical bit given as the second argument. This method allows full control over where the measurement result is stored.\n",
        "\n",
        "2. `QuantumCircuit.measure_all` : takes no argument and can be used for quantum circuits without pre-defined classical bits. It creates classical wires and stores measurement results in order. For example, measurement of  qubit $q_i$ is stored in cbit $meas_i$). It also adds a barrier before the measurement.\n",
        "\n",
        "3. `QuantumCircuit.measure_active` : similar to `measure_all`, but measures only qubits that have operations.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "0cdb2273",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/0cdb2273-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc1 = QuantumCircuit(2, 2)\n",
        "qc1.measure(0, 1)\n",
        "qc1.draw(\"mpl\", cregbundle=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "6f33698c",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/6f33698c-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc2 = QuantumCircuit(2)\n",
        "qc2.measure_all()\n",
        "qc2.draw(\"mpl\", cregbundle=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "ca3f225f",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/ca3f225f-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc3 = QuantumCircuit(2)\n",
        "qc3.x(1)\n",
        "qc3.measure_active()\n",
        "qc3.draw(\"mpl\", cregbundle=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "649fc3fd-caf1-45f1-ad8e-3b5d26ca859b",
      "metadata": {},
      "source": [
        "## Parameterized circuits\n",
        "\n",
        "Many near-term quantum algorithms involve executing many variations of a quantum circuit. Since constructing and optimizing large circuits can be computationally expensive, Qiskit supports **parameterized** circuits. These circuits have undefined parameters, and their values do not need to be defined until just before executing the circuit. This lets you move circuit construction and optimization out of the main program loop.  The following cell creates and displays a parameterized circuit.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "a580552c-d585-4047-99f0-32aafd06e4f3",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/a580552c-d585-4047-99f0-32aafd06e4f3-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.transpiler import generate_preset_pass_manager\n",
        "from qiskit.circuit import Parameter\n",
        "\n",
        "angle = Parameter(\"angle\")  # undefined number\n",
        "\n",
        "# Create and optimize circuit once\n",
        "qc = QuantumCircuit(1)\n",
        "qc.rx(angle, 0)\n",
        "qc = generate_preset_pass_manager(\n",
        "    optimization_level=3, basis_gates=[\"u\", \"cx\"]\n",
        ").run(qc)\n",
        "\n",
        "qc.draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a174600b-8f4a-47ca-aa4c-cd16dcbecb7c",
      "metadata": {},
      "source": [
        "The following cell creates many variations of this circuit and displays one of the variations.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "85af6231-921a-4130-99d3-f6998f761df8",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/docs/images/guides/construct-circuits/extracted-outputs/85af6231-921a-4130-99d3-f6998f761df8-0.svg\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "circuits = []\n",
        "for value in range(100):\n",
        "    circuits.append(qc.assign_parameters({angle: value}))\n",
        "\n",
        "circuits[0].draw(\"mpl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ede2ff7c-5e68-4458-87b3-887b5b027ee1",
      "metadata": {},
      "source": [
        "You can find a list of a circuit's undefined parameters in its `parameters` attribute.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "0990b840-f8c4-4416-b2b5-9861173a9471",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "ParameterView([Parameter(angle)])"
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc.parameters"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8e415c37",
      "metadata": {},
      "source": [
        "### Change a parameter's name\n",
        "\n",
        "By default, parameter names for a parameterized circuit are prefixed by `x`- for example, `x[0]`. You can change the names after they are defined, as shown in the following example.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "id": "1d3e6424",
      "metadata": {},
      "outputs": [],
      "source": [
        "from qiskit.circuit.library import z_feature_map\n",
        "from qiskit.circuit import ParameterVector\n",
        "\n",
        "# Define a parameterized circuit with default names\n",
        "# For example, x[0]\n",
        "circuit = z_feature_map(2)\n",
        "\n",
        "# Set new parameter names\n",
        "# They will now be prefixed by `hi` instead\n",
        "# For example, hi[0]\n",
        "training_params = ParameterVector(\"hi\", 2)\n",
        "\n",
        "# Assign parameter names to the quantum circuit\n",
        "circuit = circuit.assign_parameters(parameters=training_params)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ae19e48d",
      "metadata": {},
      "source": [
        "<CodeAssistantAdmonition\n",
        "  tagLine=\"Forgotten the method name? Try asking Qiskit Code Assistant.\"\n",
        "  prompts={[\n",
        "  \"# Assign all parameters in qc to 0\"\n",
        "]}\n",
        "/>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5af44bc3",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "<Admonition type=\"tip\" title=\"Recommendations\">\n",
        "  * To learn about near-term quantum algorithms, take the [Variational algorithm design](/learning/courses/variational-algorithm-design) course.\n",
        "  * See an example of circuits being used in the [Grover's Algorithm](/docs/tutorials/grovers-algorithm) tutorial.\n",
        "  * Work with simple circuits using [IBM Quantum Composer](/docs/guides/composer).\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": 5
}