We run and customize the IBM Qiskit tutorial for portfolio optimization (8 stocks) - and give you the source code
Jeffrey Cohen, President, US Advanced Computing Infrastructure, Inc.
Jan 19, 2021, 4pm CDT
Python source code attached to the bottom of this blog (file and github link) so you can run it too.
We decide to bring the author up to speed on gate-based computing. The simplest way to do that is to spend ~ 1.5 hours today and run the portfolio optimization code in IBM Qiskit. Of course, we customize it, add some extra steps, and run it on a real quantum computer. This BLOG will explain what we do what we learn (high-level, no math or quantum needed).
Our team has different levels of understanding of gate-based quantum computing:
- One of us teaches at a university about using quantum computing (gate-based and annealing).
- One is writing a book about quantum computing theory and algorithms (math expert)
- One uses quantum computing to make money in the stock market, and only uses annealing.
Today, our teacher (Alex Khan) shows our user (Jeffrey Cohen), how to run a portfolio optimization job on IBM Qiskit, first on simulators and then on IBM's 16 qubit system in Melbourne, Australia. In fact, it is still running (probably go a full 24-hours, sorry IBM).
We start with the registration, confirm our email address, and install qiskit into our Anaconda python system ('pip install qiskit' in the root terminal), which is smooth. We are now part of the IBM Experience. We then run the setup code at the beginning of our job. It imports the correct modules into our Jupyter notebook:
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from iqx import *
#IBMQ.save_account('our token was here')
# Loading your IBM Q account(s)
provider = IBMQ.load_account()
We have the full set of code in the github and in an attached file below.
This goes more easily if you code in python already. Simple steps stay that way, errors are corrected in moments, and it just goes well. If you want to work in quantum computing, you should learn python. I learned the only way I know how...by solving a problem that was important to me. For me, the problem was to build efficient stock portfolios. In fact, I just made ~7% on an investment held for two trading days that was picked with our quantum algorithm.
We then run the code for inputting the stock data (primarily expected return and covariance), then some parameters like the number of stocks and risk tolerance. We then run the portfolio optimization in a numpy minimum eigensolver, IBM's version of the classical minimum eigensolver. This is a 'true' classical solver that gives us the best answer. We assume this is the best answer, and it used randomized data, and selected 4 stocks out of 8. We did not check.
We then run on the variable quantum eigensolver (VQE) solver, also classically. First with 4 stocks, then 8 stocks. In general, this did not find the best portfolio. We ran it 1,000 times, and finally adjusted the risk score to be 4x larger than the default to have VQE find the correct solution. My intuition for this is that VQE is not a native optimization solver, and we had to give it 'a boost' by increasing the separation, or distance, between the best and 2nd best portfolio. This also tells me that stock picking may not work best on VQE.
We then run the problem on the quantum approximate optimization algorithm (QAOA) solver, also classically. With four and eight assets at a time, we were able to find the best portfolio of stocks, but only with a small probability. For eight stocks, it gave us the 'best' portfolio found classically with a 1.4% probability. QAOA's results are better than VQE, which I believe is the key learning of the portfolio optimization exercise.
We then run the QAOA algorithm with 8 assets on the IBM quantum simulator. This is still a classical model, but it simulates and give us the 'look and feel' of what a problem will look like on the IBM Q hardware. We select the 32 qubit simulator and submit our 8 asset job. We run it for 1,000 shots and the best answer found was not a very good answer (e.g., it had a positive score whereas the optimal answer had a negative score). Our intuition from this is that an error-free classical solver executes QAOA better than a quantum computer (at least one being simulated).
Finally, we run on the IBM Q 16 qubit quantum machine named 'Melbourne.' We submit it with 1,000 shots, 100 'seeds' and let it fly. The first shot was ready about 29 minutes later. We have been running it for about 5 hours, and completed 6 shots. This seems to be a very, very busy quantum computer!
We review the first six 'shots' completed on the quantum computer. In general, the quantum computer prefers you keep your money (it picks zero stocks) or it prefers a one stock answer. In a few cases, it picks two stocks. This is compared to a four-stock optimal classical answer. As we think about this, it seems the answer is more randomized than expected, and maybe a bias to drop to a 'zero' value (zero means no stock in the portfolio) when there is an error. We may be able to work around this bias if we can confirm it.
(technical details omitted, and will be in the next blog post)
We next plan to run the 8-stock portfolio on a 5-qubit system called 'athens.' We need to let our code run on the IBM 'melbourne' system until it completes (995 shots left to run). We finally interrupted our python kernel to write this blog. We may try again tomorrow.
We quickly register a new account and join the IBM Experience. We install and set up the IBM Qiskit python libraries and modules, and run and customize a portfolio optimization experiment in under 20 minutes. This was a very fun and interesting experience.
We test the IBM Qiskit classical solver (the one that always works) against VQE, QAOA, QAOA on a quantum simulator, and QAOA on the 16-qubit 'melbourne' IBM quantum computer. QAOA does better than VQE, and the classical simulator does much better than the simulator, which does better than the actual quantum computer.
We notic the 16-qubit IBM quantum system we chose is very busy. Each shot took about 45 minutes. This is ok for basic research, but we probably need to either figure out how to run on smaller quantum computers (e.g., the 5-qubit models) if we want to keep doing this for free.
There are a bunch of technical things we learn. We have screen shots of these things. Going to explain a few of them in the next BLOG post (later today).
Thank you IBM for giving us access to your quantum simulators and quantum system.
The code can be found here in our github. Here is the file.
Strategic IT Management Consultant with a strong interest in Quantum Computing. Consulting for 29 years and this looks as interesting as cloud computing was in 2010.