Skip to content

Commit

Permalink
Completed notebook docu of BTC_03_QPE
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed Jan 16, 2024
1 parent 5671deb commit d5c759f
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 130 deletions.
6 changes: 2 additions & 4 deletions tnbs/BTC_01_PL/PL/notebooks/01_ProbabilityLoadingKernel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,7 @@
{
"cell_type": "markdown",
"id": "38bef876-ea3a-41e9-a1bc-ec0c527c5388",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"metadata": {},
"source": [
"## 05. Generating the JSON file.\n",
"\n",
Expand All @@ -588,7 +586,7 @@
"source": [
"## 06. Complete Workflow.\n",
"\n",
"The bash script **benchmark_exe.sh** allows to automatize the execution of the benchamrk and the JSON file generation (once the *my_benchmark_execution.py* and the *neasqc_benchmark.py* are properly configured).\n",
"The bash script **benchmark_exe.sh** allows to automatize the execution of the benchmark and the JSON file generation (once the *my_benchmark_execution.py* and the *neasqc_benchmark.py* are properly configured).\n",
"\n",
" bash benchmark_exe.sh"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"source": [
"# rz_lib\n",
"\n",
"In order to develop the **Benchmark Test Case** for **QPE** we have developed the **rz_library** python package that have all mandatory functions for computing eigenvalues of a $Rz^{\\otimes n} (\\vec{\\theta})$ operator theoretically and usign Atos myqlm simulation library. In this notbook we explain the **QPE** for the $Rz^{\\otimes n} (\\vec{\\theta})$ and shows how to use the **rz_library** package"
"In order to develop the **Benchmark Test Case** for **QPE** we have developed the **rz_library** python package that has all mandatory functions for computing eigenvalues of a $Rz^{\\otimes n} (\\vec{\\theta})$ operator theoretically and using *myqlm* simulation library. In this notebook we explain the **QPE** for the $Rz^{\\otimes n} (\\vec{\\theta})$ and shows how to use the **rz_library** package"
]
},
{
Expand All @@ -61,13 +61,13 @@
"\n",
"$$\\lambda_j = \\frac{y_j}{2^m}$$\n",
"\n",
"It can be shown that if the eigenvalue $\\lambda_j$ can be mapped to $\\frac{y_j}{2^m}$ then the probability of getting the correct eigenvalue is 100%. If this is not posible then it can be shown that the measured $\\lambda^{Meas}_j$ have an error with respect to the true value $\\lambda^{True}_j$ of:\n",
"It can be shown that if the eigenvalue $\\lambda_j$ can be mapped to $\\frac{y_j}{2^m}$ then the probability of getting the correct eigenvalue is 100%. If this is not possible then it can be shown that the measured $\\lambda^{Meas}_j$ have an error concerning the true value $\\lambda^{True}_j$ of:\n",
"\n",
"$$|\\lambda^{Meas}_j - \\lambda^{True}_j| \\leq \\frac{1}{2^m}$$\n",
"\n",
"with a probability higher than $80\\%$. \n",
"\n",
"If all eigenstates of the operator $\\mathcal{Q}$ are know we can compute all the correspondent eigenvalues using the aforementioned circuit for each eigenstate."
"If all eigenstates of the operator $\\mathcal{Q}$ are known we can compute all the correspondent eigenvalues using the aforementioned circuit for each eigenstate."
]
},
{
Expand Down Expand Up @@ -145,10 +145,9 @@
"### n qubit \n",
"\n",
"\n",
"\n",
"This can be easily generalizable for a $n$ qbits kronecker product: $R_z^{\\otimes n} (\\vec{\\theta})$.\n",
"\n",
"Additionally the eigenvalues for each eigenvector have an easy computation recipe: for a system with $n$ qubits the eigenvalue for an eigenstate $\\ket{i_0i_1i_2 \\cdots i_n}$, with $i_j=\\{0,1\\}$, of a $Rz^{\\otimes n} (\\vec{\\theta})$ operator have following eigenvalue:\n",
"Additionally, the eigenvalues for each eigenvector have an easy computation recipe: for a system with $n$ qubits the eigenvalue for an eigenstate $\\ket{i_0i_1i_2 \\cdots i_n}$, with $i_j=\\{0,1\\}$, of a $Rz^{\\otimes n} (\\vec{\\theta})$ operator have following eigenvalue:\n",
"\n",
"$$2\\pi \\lambda_{\\ket{i_0, i_1, \\cdots , i_n}} = - \\frac{\\sum_{j=0}^{n} (-1)^{i_j} \\theta_j}{2}$$\n",
"\n",
Expand All @@ -175,7 +174,7 @@
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../../QPE')\n",
"sys.path.append('../')\n",
"import rz_lib"
]
},
Expand All @@ -186,7 +185,7 @@
"source": [
"### 3.1. Theoretical Eigenvalues\n",
"\n",
"The **rz_eigv** function from *rz_lib* allows to compute the complete eigenvalues of a generic $R_z^{\\otimes n} (\\vec{\\theta})$ operator for an input list of angles. In order to understand the package we are going to use two types of list of angles:\n",
"The **rz_eigv** function from *rz_lib* allows to compute the complete eigenvalues of a generic $R_z^{\\otimes n} (\\vec{\\theta})$ operator for an input list of angles. In order to understand the package we are going to use two types of lists of angles:\n",
"\n",
"1. A list with the same angle for all the qubits, set to $\\frac{\\pi}{2}$.\n",
"2. A list of different random angles"
Expand Down Expand Up @@ -261,12 +260,12 @@
"id": "9a792b69",
"metadata": {},
"source": [
"Once the theoretical eigenvalues are found we need to create the correspondent histogram of the eigenvalues between $[0, 1]$ and with fixed discretization that will be given by the number of auxiliary qubits that will be used for computing the **QPE** (the $m$ qubit register in the **QPE** showed at the begining of the notebook). This should be done for a direct comparing between the theoretical results and the results of the **QPE**.\n",
"Once the theoretical eigenvalues are found we need to create the correspondent histogram of the eigenvalues between $[0, 1]$ and with fixed discretization that will be given by the number of auxiliary qubits that will be used for computing the **QPE** (the $m$ qubit register in the **QPE** showed at the beginning of the notebook). This should be done for a direct comparison between the theoretical results and the results of the **QPE**.\n",
"\n",
"\n",
"In the **QPE** we can measure the different eigenvalues of the input unitary operator but the precision of the meaturement will be given by the $m$ auxiliar qbit register. So the output of the **QPE** will be a histogram between $[0,1]$ with $2^m$ bins. In each bin the histogram will give us the frecuency of eigenvalues that will be inside it. \n",
"In the **QPE** we can measure the different eigenvalues of the input unitary operator but the precision of the measurement will be given by the $m$ auxiliary qbit register. So the output of the **QPE** will be a histogram between $[0,1]$ with $2^m$ bins. In each bin, the histogram will give us the frequency of eigenvalues that will be inside it. \n",
"\n",
"For computing the correspondet histogram the **make_histogram** from *rz_library* can be used. The inputs will be the therotical computed eigenvalues and a discretization parameter (that must be equal to the $m$ that we are going to use for the **QPE**)\n"
"For computing the corresponding histogram the **make_histogram** from *rz_library* can be used. The inputs will be the theoretical computed eigenvalues and a discretization parameter (that must be equal to the $m$ that we are going to use for the **QPE**)\n"
]
},
{
Expand Down Expand Up @@ -373,18 +372,18 @@
"source": [
"#### Computing the number of shots.\n",
"\n",
"The main problem is computing the number of shots. In general, we want to have for each measured eigenvalue a number of shots enough for having a good statistic. But if the eigenvalues are degenerated it could happend that the most degeneracy eigenvalues will be obtained more frequently than others. Main problem is for eigenvalues with lower degeneracy the number of shots can be not enough for having a good statistic. \n",
"The main problem is computing the number of shots. In general, we want to have for each measured eigenvalue a number of shots enough for having a good statistic. But if the eigenvalues are degenerated it could happen that the most degeneracy eigenvalues will be obtained more frequently than others. The main problem is for eigenvalues with lower degeneracy the number of shots can be not enough to have a good statistic. \n",
"\n",
"So we are going to execute following strategy:\n",
"So we are going to execute the following strategy:\n",
"\n",
"1. We are going to compute the probability of getting the lowest degeneracy eigenvalue from all the posible theoretical eigenvalues: $p_{lde}$.\n",
"1. We are going to compute the probability of getting the lowest degeneracy eigenvalue from all the possible theoretical eigenvalues: $p_{lde}$.\n",
"2. The number of shots will be equal:\n",
"\n",
"$$\\text{shots} = \\lceil\\frac{1000}{0.81 * p_{lde}}\\rceil$$\n",
"\n",
"This is because we want to have for each obtained eigenvalue near of 1000 measures such that the error with respect to the theoretical value will be lower than the discretization ($\\frac{1}{2^{m}}$).\n",
"This is because we want to have for each obtained eigenvalue near 1000 measures such that the error concerning the theoretical value will be lower than the discretization ($\\frac{1}{2^{m}}$).\n",
"\n",
"This computation can be done with *computing_shots* function"
"This computation can be done with the *computing_shots* function"
]
},
{
Expand Down Expand Up @@ -415,22 +414,36 @@
"source": [
"The *qpe_rz_qlm* function from **rz_qlm** script creates the atos qlm implementation of the **QPE** circuit for a $R_z^{\\otimes n}$ operator and solves the circuit providing the different eigenvalues ($\\lambda_j$). The mandatory inputs are:\n",
"\n",
"* angles : list. List with the angles that are applied to each qubit of the circuit\n",
"* auxiliar_qbits_number : int. Number of auxiliar qubits for doing QPE.\n",
"* angles: list. List with the angles that are applied to each qubit of the circuit\n",
"* auxiliar_qbits_number: int. The number of auxiliary qubits for doing QPE.\n",
"\n",
"The function returns a pandas dataframe with the distribution of eigenvalues obtained.\n",
"The function returns a pandas DataFrame with the distribution of eigenvalues obtained.\n",
"\n",
"\n",
"* lambda : lamda eigenvalue of the correspondent state\n",
"* Probability : Probability of getting the correspondent state\n",
"* Probability: Probability of getting the correspondent state\n",
"\n",
"The format of the pandas is compatible with the pandas dataframe returned by *make_histogram* for doing direct comparison!\n",
"The format of the pandas is compatible with the pandas DataFrame returned by *make_histogram* for doing direct comparison!\n",
"\n",
"**BE AWARE**\n",
"\n",
"The ordering of the **States** columns is the least significant bit at the right (it corresponds to the column labelled as **Int_lsb**)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24aab959-d5e1-40cc-afdc-427be1e618a7",
"metadata": {},
"outputs": [],
"source": [
"# For getting the QPU\n",
"sys.path.append(\"../../\")\n",
"from get_qpu import get_qpu\n",
"\n",
"qpu = get_qpu(\"c\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -446,7 +459,8 @@
"pdf_qlm_rz_same, qpe_object_ = rz_lib.qpe_rz_qlm(\n",
" theta_same, \n",
" auxiliar_qbits_number=auxiliar_qbuits, \n",
" shots=same_shots\n",
" shots=same_shots,\n",
" qpu=qpu\n",
")"
]
},
Expand All @@ -459,7 +473,7 @@
},
"outputs": [],
"source": [
"# QPE circuito\n",
"# QPE circuit\n",
"c = qpe_object_.circuit\n",
"%qatdisplay c --svg"
]
Expand Down Expand Up @@ -505,7 +519,9 @@
"pdf_qlm_rz_diff, qpe_object = rz_lib.qpe_rz_qlm(\n",
" theta_diff, \n",
" auxiliar_qbits_number=auxiliar_qbuits, \n",
" shots=diff_shots)"
" shots=diff_shots, \n",
" qpu=qpu\n",
")"
]
},
{
Expand Down Expand Up @@ -536,9 +552,7 @@
"cell_type": "code",
"execution_count": null,
"id": "20697520",
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [],
"source": [
"plt.plot(pdf_qlm_rz_diff['lambda'], pdf_qlm_rz_diff['Probability'] * diff_shots , 'o-')\n",
Expand Down Expand Up @@ -599,7 +613,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit d5c759f

Please sign in to comment.