{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Power Flow Study\n", "*Copyright (c) 2023 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*\n", "## Background\n", "\n", "Power flow is an important numerical analysis in a power system, and it aims to calculate the various parameters of each node in a power system according to the given conditions and constraints. Power flow study is the foundation for almost all the power system analysis and evaluatons, and it also underpins the further planning, expansion, and operation determining for a power system. A simple example is that we can check whether the proposed power system planning scheme can meet the requirements through power flow calculation. Therefore, power flow calculation has a very important practical significance.\n", "\n", "In a power flow problem, each part of a power system is represented by nodes and lines connecting nodes. For AC power, there are generally four parameters to be considered for each node, namely voltage magnitute, phase angle, active power and reactive power. Active power refers to the power of the system that actually converts the electric energy into other forms of energy, while reactive power is the power continuously flows back and forth between the source and load. According to the given initial conditions, we can obtain the unknown parameters on each node by solving the power balance equations. From the perspective of mathematical model, power flow problem can be reduced to solving a series of nonlinear equations. Speficially, for the $i$ th node in a power system with $n$ ndoes, we have:\n", "\n", "$$\\begin{cases}\n", "P_i-U_i\\sum^n_{j=1}U_j(G_{ij}\\cos\\delta_{ij}+B_{ij}\\sin\\delta_{ij}) &= 0 \\\\\n", "Q_i-U_i\\sum^n_{j=1}U_j(G_{ij}\\sin\\delta_{ij}-B_{ij}\\cos\\delta_{ij}) &= 0 \n", "\\end{cases}$$\n", "where $P_i$ is the injective active power of $i$ th node, $Q_i$ is the injective reactive power of $i$ th node, and $U_i$ is the voltage magnitude of $i$ th node. Meanwhile, $G$ represents the real part of admittance matrix, $B$ represents the imaginary part of the admittance matrix, $\\delta_ {ij}$ represents the phase difference between the $i$ th and $j$ th nodes.\n", "\n", "Solving nonlinear equations is a difficult task, so we usually use some numerical methods to obtain approximate solutions. One of the most commonly used methods is Newton-Raphson method, which can gradually approach the precise solution of the equations by increasing the number of iterations. In short, Newton-Raphson method is to find the appropriate optimization direction from an initial point by calculating the derivative, and then continuously update our approximate solution to get a result that is close to the precise solution." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Quantum solutions\n", "We have known that Newton-Raphson method is commonly used in power flow problem. Its complexity mainly comes from the process of calculating the inversion of Jacobian matrix, and the complexity is $\\mathcal{O}(poly(n))$. In Newton-Raphson method, calculating the inversion of Jacobian matrix can also be seen as the process of solving linear equations. In the field of quantum computing, many quantum algorithms have been proposed to solve linear equations e.g. Harrow-Hassidim-Lloyd (HHL) algorithm and Variational Quantum Linear Solver (VQLS). Compared with classical algorithms, they have been proved to have the exponential acceleration in solving linear equations under certain conditions. Here, we would use the variational quantum linear solver (VQLS) to replace the Jacobian matrix inversion process in Newton-Raphson method, so as to achieve the quantum acceleration of power flow problems [1]. We also provide a model and a tutorial of VQLS in our Quantum Application Model Library, you can find more details of this algorithm in its tutorial. Next, we will take a 5-node power system as an example to demonstrate the quantum solution for power flow problem based on Newton-Raphson method and VQLS." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## User's guide\n", "\n", "We provide a configuration file with previously chosen parameters. The user just needs to change the parameters in the `config.toml` file, and run `python power_flow.py --config config.toml` in the terminal, to solve the power flow problem.\n", "### Output\n", "The results of power flow problem will be output to the `pf_result.txt` file. And the process of optimization will be documented in the `power_flow.log` file. Users can check the evolution of loss and error value as the number of looping iterations increases." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Online demonstration\n", "Here, we demonstrate a demo that can be tested online. We firstly define the contents of the configuration file:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "test_toml = r\"\"\"\n", "# The path of the input data. It should be a .txt file and in the IEEE Common Data Format.\n", "data_dir = './ieee5cdf.txt'\n", "\n", "# Threshold for loss value to end optmization for power flow, default is 1e-3\n", "threshold = 1e-3\n", "\n", "# Minimum number of iterations of power flow optimization.\n", "minIter = 3\n", "\n", "# Maximum number of iterations of power flow optimization.\n", "maxIter = 100\n", "\n", "# The depth of the quantum ansatz circuit.\n", "depth = 4\n", "\n", "# Number optimization cycles of quantum circuit. \n", "iterations = 100\n", "\n", "# The learning rate of the optimizer.\n", "LR = 0.1\n", "\n", "# Threshold for loss value to end optimization for quantum circuit early, default is 0.\n", "gamma = 0\n", "\n", "\"\"\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Then we present the code for the model:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 100/100 [00:59<00:00, 1.68it/s]\n", "100%|██████████| 100/100 [00:59<00:00, 1.69it/s]\n", "100%|██████████| 100/100 [00:59<00:00, 1.69it/s]\n", "100%|██████████| 100/100 [00:59<00:00, 1.69it/s]\n", "100%|██████████| 100/100 [00:58<00:00, 1.70it/s]\n", "100%|██████████| 100/100 [00:58<00:00, 1.70it/s]\n", "100%|██████████| 100/100 [00:58<00:00, 1.70it/s]\n", "100%|██████████| 100/100 [00:58<00:00, 1.70it/s]\n", "100%|██████████| 100/100 [01:00<00:00, 1.65it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Here is the power flow results:\n", "Power Flow:\n", "\n", "| Bus | Bus | V | Angle | Injection | Generation | Load |\n", "| No | Name | pu | Degree | MW | MVar | MW | Mvar | MW | MVar |\n", "| 1 |Bus 1 HV| 1.060 | 0.000 | 129.816 | 24.447 | 129.816 | 24.447 | 0.000 | 0.000 |\n", "| 2 |Bus 2 HV| 1.036 | -0.046 | 20.000 | 20.000 | 40.000 | 30.000 | 20.000 | 10.000 |\n", "| 3 |Bus 3 HV| 1.009 | -0.084 | -45.000 | -15.000 | 0.000 | 0.000 | 45.000 | 15.000 |\n", "| 4 |Bus 4 HV| 1.007 | -0.090 | -40.000 | -5.000 | -0.000 | -0.000 | 40.000 | 5.000 |\n", "| 5 |Bus 5 HV| 1.002 | -0.104 | -60.000 | -10.000 | 0.000 | 0.000 | 60.000 | 10.000 |\n", "----------------------------------------------------------------------------------------------------------\n", "\n", "Network and losses:\n", "\n", "| From | To | P | Q | From | To | P | Q | Branch Loss |\n", "| Bus | Bus | MW | MVar | Bus | Bus | MW | MVar | MW | MVar |\n", "| 1 | 2 | 95.69 | 13.87 | 2 | 1 | -81.06 | -9.54 | 14.63 | 4.33 |\n", "| 1 | 3 | 46.48 | 10.58 | 3 | 1 | -34.51 | -6.77 | 11.97 | 3.81 |\n", "| 2 | 3 | 28.99 | 8.15 | 3 | 2 | -20.24 | -7.01 | 8.74 | 1.13 |\n", "| 2 | 4 | 32.23 | 8.06 | 4 | 2 | -23.40 | -6.65 | 8.83 | 1.42 |\n", "| 2 | 5 | 58.11 | 13.33 | 5 | 2 | -50.69 | -9.77 | 7.42 | 3.56 |\n", "| 3 | 4 | 20.94 | -1.21 | 4 | 3 | -16.84 | 1.32 | 4.10 | 0.11 |\n", "| 4 | 5 | 11.40 | 0.33 | 5 | 4 | -1.28 | -0.23 | 10.12 | 0.10 |\n", "----------------------------------------------------------------------------------------------------------\n", "\n", "Total active power losses: 65.82, Total reactive power losses: 14.45\n", "Error: 1.203040833536173e-06\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import os\n", "import warnings\n", "\n", "warnings.filterwarnings('ignore')\n", "os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'\n", "\n", "import toml\n", "from paddle_quantum.data_analysis.power_flow import data_to_Grid\n", "\n", "config = toml.loads(test_toml)\n", "file_name = config.pop('data_dir')\n", "grid = data_to_Grid(file_name)\n", "grid.powerflow(**config)\n", "print(\"Here is the power flow results:\")\n", "grid.printResults()\n", "Error = grid.tolerances[-1] \n", "print(f\"Error: {Error}\")\n", "grid.saveResults()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The error here represents the maximum absolute error of nonlinear equations." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Note\n", "\n", "The input data should be in the IEEE common format [2]." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Citation\n", "\n", "```\n", "@article{liu2022quantum,\n", " title={Quantum Power Flows: From Theory to Practice},\n", " author={Liu, Junyu and Zheng, Han and Hanada, Masanori and Setia, Kanav and Wu, Dan},\n", " journal={arXiv preprint arXiv:2211.05728},\n", " year={2022}\n", "}\n", "\n", "@article{pierce1973common,\n", " title={Common format for exchange of solved load flow data},\n", " author={Pierce, HE and others},\n", " journal={IEEE Transactions on Power Apparatus and Systems},\n", " volume={92},\n", " number={6},\n", " pages={1916--1925},\n", " year={1973}\n", "}\n", "```\n", "\n", "## References\n", "[1] Liu, Junyu, et al. \"Quantum Power Flows: From Theory to Practice.\" arXiv preprint arXiv:2211.05728 (2022).\n", "\n", "[2] Pierce, H. E. \"Common format for exchange of solved load flow data.\" IEEE Transactions on Power Apparatus and Systems 92.6 (1973): 1916-1925." ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "power_flow", "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.7.16" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "09b405f4ce7ff94b18a2e1d1d7346b8a2e6101e2bd963fee0349fb6bd6dc2572" } } }, "nbformat": 4, "nbformat_minor": 2 }