Let me try to answer the first question about how to use quantum algorithms to optimize the portfolio.
Step 1. Formulate the problem in a quadratic form
Step 2. Find the Hamiltonian corresponds to the quadratic problem
Step 3. Choose a method to search for the ground state of the Hamiltonian, which would be the optimal solution
Step 1
Let x be the vector that record the asset allocation. The optimal portfolio maximize the return and minimize the risk, which is equivalent to minimizing
$$
q x^T \Sigma x - \mu^T x,
$$
which is subject to
$$
\mathbb{1}^T x = B
$$
This is a typical quadratic optimization problem. In Qiskit, you can code it simple by calling the PortfolioOptimization
package
from qiskit_finance.applications.optimization import PortfolioOptimization
# Define the portfolio
portfolio = PortfolioOptimization(expected_returns = mu, covariances = sigma, risk_factor = q, budget = B, bounds = None)
# Translate the portfolio into a quadratic program
qp = portfolio.to_quadratic_program()
Step 2 and 3
In quantum computation, a quadratic optimization problem can be solved as a ground state search of a Hamiltonian. Especially, in simple cases where elements of $x$ can only take 1 or 0 (in other words, you only decide whether to allocate investment in a certain asset), the corresponding quantum model is called Ising model, where every element in $x$ is mapped to a particle with spin up or spin down.
Qiskit has very well written package to help you do this mapping from quadratic problem to Hamiltonian. You only need to choose a method to search for the ground state, such as VQE
.
from qiskit import Aer
from qiskit.algorithms import VQE
from qiskit.algorithms.optimizers import *
optimizer = SLSQP(maxiter=1000)
algorithm_globals.random_seed = 1234
backend = Aer.get_backend('statevector_simulator')
vqe = VQE(optimizer=optimizer, quantum_instance=backend)
vqe_meo = MinimumEigenOptimizer(vqe)
result = vqe_meo.solve(qp)