Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
In this workshop Notebook we will go through the implementation using Qmod for the rainbow option pricing [1].

Guidance for the Workshop:

The # Your code is there for you to do yourself. **The # Solution start and # Solution end are only for helping you. Please delete the Solution and try doing it yourself…** For completing the code, please refer to the Classiq documentation, Classiq documentation, or Classiq Library. Search for the required quantum function to find its corresponding documentation page.

Introduction and Background

In finance, a crucial aspect of asset pricing pertains to derivatives. Derivatives are contracts whose value is contingent upon another source, known as the underlying. The pricing of options, a specific derivative instrument, involves determining the fair market value (discounted payoff) of contracts affording their holders the right, though not the obligation, to buy (call) or sell (put) one or more underlying assets at a predefined strike price by a specified future expiration date (maturity date). This process relies on mathematical models, considering variables like current asset prices, time to expiration, volatility, and interest rates. In many financial models we are often interested in calculating the average of a function of a given probability distribution (E[f(x)]E[f(x)]). The most popular method to estimate the average is Monte Carlo [2] due to its flexibility and ability to generically handle stochastic parameters. Classical Monte Carlo methods, however, generally require extensive computational resources to provide an accurate estimation. By leveraging the laws of quantum mechanics, a quantum computer may provide novel ways to solve computationally intensive financial problems, such as risk management, portfolio optimization, and option pricing. The core quantum advantage of several of these applications is the Amplitude Estimation algorithm [3] which can estimate a parameter with a convergence rate of Ω(1/M2)\Omega(1/M^{2}), compared to Ω(1/M)\Omega(1/M) in the classical case, where MM is the number of Grover iterations in the quantum case and the number of the Monte Carlo samples in the classical case. This represents a theoretical quadratic speed-up of the quantum method over classical Monte Carlo methods!

Rainbow Option Pricing

An option is the possibility to buy (call) or sell (put) an item (or share) at a known price - the strike price (K), where the option has a maturity price (S). The payoff function to describe for example a call option will be: f(S)={0,if KSSK,if K<Sf(S) = \begin{cases} 0, & \text{if } K \geq S \\ S - K, & \text{if } K < S \end{cases} The maturity price is unknown. Therefore, it is expressed by a price distribution function, which may be any type of a distribution function. For example a log-normal distribution: ln(S) N(μ,σ)\mathcal{ln}(S)\sim~\mathcal{N}(\mu,\sigma), where N(μ,σ)\mathcal{N}(\mu,\sigma) is the standard normal distribution with mean equal to μ\mu and standard deviation equal to σ\sigma. In the case of a rainbow options, the payoff function is defined by the maximum of the maturity prices of multiple assets. “Best of”, The best-performing asset is chosen as a reference for payoff calculation. For call options, the payoff function is defined as follows: f(S)=max(SK,0)f(S) = max(S-K, 0) Where, in this case, S=max(Sˉt)S=max(\bar{S}_t). There is another type of asset called “worst of”, where the worst-performing asset is chosen as a reference for payoff calculation. We will not treat this type of option in this notebook.

To Estimate the Average Option Price Using a Quantum Computer, We Need To:

  • Load the distribution, that is, discretize the distribution using 2n2^n points (n is the number of qubits) and truncate it.
  • Implement the affine transformation to bring the assets to the maturity date.
  • Implement the payoff function for rainbow options and make amplitude loading using control RyR_y rotations.
  • Evaluate the expected payoff using iterative amplitude estimation.
The algorithmic framework is called Quantum Monte-Carlo Integration. For a basic example, see QMCI. In the link, we use a simular framework to estimate European call option, where the underlying asset distribution at the maturity data is modeled as log-normal distribution.

Data Definitions

The problem inputs are:
  • NUM_QUBITS: the number of qubits representing an underlying asset
  • NUM_ASSETS: the number of underlying assets
  • K: the strike price
  • S0: the arrays of underlying assets prices
  • dt: the number of days to the maturity date
  • COV: the covariance matrix that correlate the underlying assets
  • MU_LOG_RET: the array containing the mean of the log return of each underlying asset

Gaussian State Preparation

Encode the probability distribution of a discrete multivariate random variable WW taking values in {w0,..,wN1}\{w_0, .., w_{N-1}\} describing the assets’ prices at the maturity date. The number of discretized values, denoted as NN, depends on the precision of the state preparation module and is consequently connected to the number of qubits nn by N=2nN=2^n. i=0N1p(wi)wi\sum_{i=0}^{N-1} \sqrt{p(w_i)}\left|w_i\right\rangle

Sanity Check

Maximum Computation

Precision Utils for Accurate Arithmetic Operations

Affine and Maximum Arithmetic Definitions

Considering the time delta between the starting date (t0t_0) and the maturity date (tt), we can express the return value RiR_i for the ii-th asset as Ri=μi+yiR_i = \mu_i + y_i. Where: μi=(tt0)μ~i\mu_i= (t-t_0)\tilde{\mu}_i, being μ~i\tilde{\mu}_i the expected daily log-return value. It can be estimated by considering the historical time series of log returns for the ii-th asset. yiy_i is obtained through the dot product between the matrix L\mathbf{L} and the standard multivariate Gaussian sample: yi=Δxklikdk+xminkliky_i = \Delta x \cdot \sum_kl_{ik}d_k + x_{min} \cdot \sum_k l_{ik} Δx\Delta x is the Gaussian discretization step, xminx_{min} is the lower Gaussian truncation value and dk[0,2m1]d_k \in [0,2^m-1] is the sample taken from the kk-th standard Gaussian. likl_{ik} is the i,ki,k entry of the matrix L\mathbf{L}, defined as L=C(tt0)\mathbf{L}=\mathbf{C}\sqrt{(t-t_0)}, where C\mathbf{C} is the lower triangular matrix obtained by applying the Cholesky decomposition to the historical daily log-returns correlation matrix.

Extra Information:

  • permutation: A quantum operation is a permutation if it maps computational-basis states to computational-basis states (with possible phase shifts).
Such an operation neither introduces nor destroys quantum superpositions, and its computation can be described classically.
  • const: A parameter of a quantum operation is constant if it is immutable up to a phase.
That is, the magnitudes of its computational-basis state components remain unchanged, while their phases may shift. For example:
  • Z is a permutation and its parameter is const as it merely flips the phase of the 1|1\rangle state.
  • X is a permutation but its parameter is not const as it flips between 0|0\rangle and 1|1\rangle.
  • H is not a permutation and its parameter is not const, as it introduces superposition.
  • SWAP is a permutation but its two parameters are not const, as it swaps between 01|01\rangle and 10|10\rangle.
For more information, see the Uncomputation documentation.

Instructions

For the following function, you need to compute the maximum of two affine expressions and assign the result to res. Use the qmax function from the classiq.qmod.symbolic module to compute the maximum of two expressions, and the Out-of-place assignment operator |= to assign the result to res. The qmax need to choose the maximum between two expressions:
  • The affine formula of the first asset: get_affine_formula([x1, x2], 0)
  • The affine formula of the second asset plus the constant c: get_affine_formula([x1, x2], 1) + c.

Brute-Force Amplitude Loading Method

This type of amplitude loading has an exponential scale, is used for validating result from the direct method and integration method that are part of the paper [1]. We use here the Classiq amplitude loading functionality using the assign_amplitude_table and lookup_table functions to load the normalized payoff function f(x)f(x) into the indicator qubit: x01f2(x)x0+f(x)x1|x\rangle |0\rangle \rightarrow \sqrt{1-f^{2}(x)}|x\rangle |0\rangle + f(x)|x\rangle |1\rangle Using the amplitude loading of the payoff function, we can estimate the expected value of the payoff function E[f(x)]E[f(x)] by measuring the indicator qubit and calculating the probability of measuring 1|1\rangle using the iterative quantum amplitude estimation (IQAE) algorithm [4]. First, we will build the amplitude loading of the payoff function f(x)f(x). This is the brute-forced method, in the paper, there two more efficient methods, the direct method and the integration method, which are implemented in the Classiq library. Then, we will put that in the iterative quantum amplitude estimation (IQAE) algorithm to estimate the expected value of the payoff function.

The Payoff Function Expression

The Amplitude Loading of the Payoff Function

For each value of x|x\rangle, we want to load the value of f(x)f(x) into the amplitude of the indicator qubit ind|ind\rangle. Therefore, we use the assign_amplitude_table function with the lookup_table function inside it. The payoff function (get_payoff_expression_normalized) is the heart of the computation, and thus it used inside the lookup table function. See example in the Classiq documentation for more details.

Allocation of Quantum Variables

It is useful to allocate quantum structure QStruct to hold the quantum variables used in the algorithm.

Brute Force Method for Rainbow Options

**We first prepare distribution of the assets and them apply the affine transformation and bring the assets to the maturity date. Then we apply the amplitude loading using the brute_force_payoff function.** Then, we uncompute the registers to stay with the correct state using local variable max_out inside the function. In the paper [1], it makes the following operation: A0=1a2ψ00+aψ11A|0\rangle = \sqrt{1-a^{2}}|\psi_{0}\rangle|0\rangle + a|\psi_{1}\rangle|1\rangle where AA is a unitary operator while ψ1|\psi_{1}\rangle and ψ1|\psi_{1}\rangle are some normalized states. Thus, aa is the probability of measuring 1|1\rangle in the last qubit. The value aa is: a=i=0N1f~(wi)p(wi)=E[f~]a=\sum_{i=0}^{N-1} \tilde{f}(w_i)p(w_i) = E[\tilde{f}] Which is estimated later on by the IQAE algorithm.

Building the Quantum Model

In Classiq, we build the quantum model in the main function.

Synthesizing the Quantum Model

Output:
Output:

IQAE Algorithm

The IQAE algorithm estimates aa by using the AA operation (that uses the rainbow_brute_force function) in the grover algorithm. It repeats the Grover operation iteratively until the desired precision is reached according to a certain criteria [4]. In each iteration, it repeats the Grover operation with a different number of iterations kk. The IQAE class allows to easily use the IQAE algorithm. You are welcome to see how it is built inside.
Output:
Output:

Executing the IQAE Algorithm

Executes the IQAE algorithm iteratively, according to the IQAE algorithm. Basically, it is running the quantum program multiple times with different number of Grover iterations kk until the desired precision is reached. In each iteration, it adds a different number of Grover repetitions to the quantum circuit based on the results of the previous iterations according to the protocol in the article. The complexity is similar to the regular QAE but uses less qubits and thus is more appealing for simulations.

Post Process

We need to add to the post-processing function a term: E[max(ebz,Keb)]ebK=E[max(eax^,Kebaxmax)]eb+axmaxK\begin{split} &\mathbb{E} \left[\max\left(e^{b \cdot z}, Ke^{-b'}\right) \right] e^{b'} - K \\ = &\mathbb{E} \left[\max\left(e^{-a\hat{x}}, Ke^{-b'-ax_{max}}\right) \right]e^{b'+ ax_{max}} - K \end{split}

Run Method

See the IQAE results.
Output:
Output:

References

[1]: Francesca Cibrario et al., Quantum Amplitude Loading for Rainbow Options Pricing. Preprint [2]: Paul Glasserman, Monte Carlo Methods in Financial Engineering. Springer-Verlag New York, 2003, p. 596.
[3]: Gilles Brassard, Peter Hoyer, Michele Mosca, and Alain Tapp, Quantum Amplitude Amplification and Estimation. Contemporary Mathematics 305 (2002) [4]: Grinko, Dmitry, et al. “Iterative quantum amplitude estimation.” npj Quantum Information 7.1 (2021): 52.