{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "07f4a5ad-7429-4478-91c8-d6143e3ed407",
      "metadata": {
        "gloss": {
          "reference": {
            "text": "The initial, fixed start for our problem. Typically this refers to $|0\\rangle$, but we can configure it to a variety of states that suitably encode our problem.",
            "title": "Reference state"
          }
        }
      },
      "source": [
        "---\n",
        "title: Reference states\n",
        "description: This lesson describes reference states, the initialization of a quantum state prior to any variational parameters.\n",
        "---\n",
        "\n",
        "# Reference states\n",
        "\n",
        "In this lesson, we will explore how we can initialize our system with a <DefinitionTooltip definition=\"The initial, fixed start for our problem. Typically this refers to |0>, but we can configure it to a variety of states that suitably encode our problem.\">reference state</DefinitionTooltip> to help our variational algorithm converge faster. First, we will learn how to construct a reference state manually, and then explore several standard options that can be used in a variational algorithm.\n",
        "\n",
        "![Diagram of options for references states including default, application-specific, and quantum.](https://eu-de.quantum.cloud.ibm.com/learning/images/courses/variational-algorithm-design/reference-states/reference-workflow.svg)\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "61d1dbca-5992-4228-b04d-a731b329e59d",
      "metadata": {},
      "source": [
        "## Default state\n",
        "\n",
        "A *reference state* refers to the initial fixed start for our problem. To prepare a reference state, we need to apply the appropriate, non-parametrized unitary $U_R$ at the start of our quantum circuit, such that $|\\rho\\rangle = U_R |0\\rangle$. If you have an educated guess or datapoint from an existing optimal solution, the variational algorithm will likely converge faster if you use that as a starting point.\n",
        "\n",
        "The simplest possible reference state is the default state, where we use the starting state of an $n$-qubit quantum circuit: $|0\\rangle^{\\otimes n}$. For the default state, our unitary operator $U_R \\equiv I$. Due to its simplicity, the default state is a valid reference state used in many scenarios.\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "a03c8fff-2cd5-403f-bf3b-d06682eee3ce",
      "metadata": {},
      "source": [
        "## Classical reference state\n",
        "\n",
        "Suppose you have a three-qubit system and you want to start in the state $|001\\rangle$ instead of the default state $|000\\rangle$. This is an example of a purely classical reference state, and to construct it, you simply need to apply an [X gate](/docs/api/qiskit/qiskit.circuit.library.XGate) to qubit $0$ (following Qiskit's qubit ordering), as $|001\\rangle = X_0 |000\\rangle$.\n",
        "\n",
        "In this case, our unitary operator is $U_R \\equiv X_0$, which leads to the reference state $|\\rho\\rangle \\equiv |001\\rangle$.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "cfd71225-1f9f-4a34-b825-ae19813956a8",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/learning/images/courses/variational-algorithm-design/reference-states/extracted-outputs/cfd71225-1f9f-4a34-b825-ae19813956a8-0.avif\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit import QuantumCircuit\n",
        "\n",
        "qc = QuantumCircuit(3)\n",
        "qc.x(0)\n",
        "\n",
        "qc.draw(\"mpl\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "36223992-268a-4244-a261-a2b98d44770c",
      "metadata": {},
      "source": [
        "## Quantum reference state\n",
        "\n",
        "Suppose you aim to start with a more complex state that involves superposition and/or entanglement, such as $\\frac{1}{\\sqrt{2}}(|100\\rangle+|111\\rangle)$.\n",
        "\n",
        "To obtain this state from $|000\\rangle$, one approach is to use a [Hadamard gate](/docs/api/qiskit/qiskit.circuit.library.HGate) on qubit $0$ ($H_0$), a [CNOT (CX)](/docs/api/qiskit/qiskit.circuit.library.CXGate) gate with qubit $0$ as the control qubit and qubit $1$ as the target qubit ($CNOT_{01}$), and finally an $X$ gate applied to qubit $2$ ($X_2$).\n",
        "\n",
        "In this scenario, our unitary operator is $U_{R} \\equiv X_2CNOT_{01}H_0|000\\rangle$, and our reference state is $|\\rho\\rangle \\equiv \\frac{1}{\\sqrt{2}}(|100\\rangle+|111\\rangle)$.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "77297158-60d7-469a-8a82-27d38a52d795",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/learning/images/courses/variational-algorithm-design/reference-states/extracted-outputs/77297158-60d7-469a-8a82-27d38a52d795-0.avif\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "qc = QuantumCircuit(3)\n",
        "qc.h(0)\n",
        "qc.cx(0, 1)\n",
        "qc.x(2)\n",
        "\n",
        "qc.draw(\"mpl\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "54a7c9c6-760a-4521-a178-002424200607",
      "metadata": {},
      "source": [
        "## Constructing Reference States using template circuits\n",
        "\n",
        "We can also use various template circuits, such as [`TwoLocal`](/docs/api/qiskit/qiskit.circuit.library.TwoLocal) which allows for expressing multiple tunable parameters and entanglements with ease. We will cover these template circuits in more detail in the next lesson, but we can use them for our reference states *if* we bind the parameters:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "8f3948ad-4caf-4ee6-8a72-bb91ed1bfeae",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/learning/images/courses/variational-algorithm-design/reference-states/extracted-outputs/8f3948ad-4caf-4ee6-8a72-bb91ed1bfeae-0.avif\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import TwoLocal\n",
        "from math import pi\n",
        "\n",
        "reference_circuit = TwoLocal(2, \"rx\", \"cz\", entanglement=\"linear\", reps=1)\n",
        "theta_list = [pi / 2, pi / 3, pi / 3, pi / 2]\n",
        "\n",
        "reference_circuit = reference_circuit.assign_parameters(theta_list)\n",
        "\n",
        "reference_circuit.decompose().draw(\"mpl\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "7b5fd0c2-6b94-40dc-9d2b-ff7d56553a29",
      "metadata": {},
      "source": [
        "## Application-specific reference states\n",
        "\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "3229a734-9c4a-4747-8118-faddfc7dac47",
      "metadata": {},
      "source": [
        "### Quantum machine learning\n",
        "\n",
        "In the context of a variational quantum classifier (VQC), the training data is encoded into a quantum state with a parameterized circuit known as a *feature map*, where each parameter value represents a data point from the training dataset. The `zz_feature_map` is a type of parameterized circuit that can be utilized to pass our data points ($x$) to this feature map.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4bfd5a76-0db3-499d-bda1-0cb95c3f23c2",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<Image src=\"/learning/images/courses/variational-algorithm-design/reference-states/extracted-outputs/4bfd5a76-0db3-499d-bda1-0cb95c3f23c2-0.avif\" alt=\"Output of the previous code cell\" />"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit.circuit.library import zz_feature_map\n",
        "\n",
        "data = [0.1, 0.2]\n",
        "\n",
        "zz_feature_map_reference = zz_feature_map(feature_dimension=2, reps=2)\n",
        "zz_feature_map_reference = zz_feature_map_reference.assign_parameters(data)\n",
        "zz_feature_map_reference.decompose().draw(\"mpl\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "d32468d1-0f72-43d5-bfa8-715a885c9ac2",
      "metadata": {},
      "source": [
        "## Summary\n",
        "\n",
        "With this lesson, you learned how to initialize your system using:\n",
        "\n",
        "* Default reference state\n",
        "* Classical reference states\n",
        "* Quantum reference states\n",
        "* Application-specific reference states\n",
        "\n",
        "Our high-level variational workload looks as follows:\n",
        "\n",
        "![A circuit diagram of a unitary operator preparing a reference state.](https://eu-de.quantum.cloud.ibm.com/learning/images/courses/variational-algorithm-design/reference-states/reference-circuit.svg)\n",
        "\n",
        "While reference states are fixed, initial starting points, we can use a *variational form* to define an *ansatz* to represent a collection of parametrized states for our variational algorithm to explore.\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": 2
}