View on GitHub
Open this notebook in GitHub to run it yourself
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 (). 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 , compared to in the classical case, where 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: 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: , where is the standard normal distribution with mean equal to and standard deviation equal to . 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: Where, in this case, . 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 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 rotations.
- Evaluate the expected payoff using iterative amplitude estimation.
Data Definitions
The problem inputs are:NUM_QUBITS: the number of qubits representing an underlying assetNUM_ASSETS: the number of underlying assetsK: the strike priceS0: the arrays of underlying assets pricesdt: the number of days to the maturity dateCOV: the covariance matrix that correlate the underlying assetsMU_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 taking values in describing the assets’ prices at the maturity date. The number of discretized values, denoted as , depends on the precision of the state preparation module and is consequently connected to the number of qubits by .SANITY CHECK
Maximum Computation
Precision utils for accurate arithmetic operations
Affine and maximum arithmetic definitions
Considering the time delta between the starting date () and the maturity date (), we can express the return value for the -th asset as . Where: , being the expected daily log-return value. It can be estimated by considering the historical time series of log returns for the -th asset. is obtained through the dot product between the matrix and the standard multivariate Gaussian sample: is the Gaussian discretization step, is the lower Gaussian truncation value and is the sample taken from the -th standard Gaussian. is the entry of the matrix , defined as , where 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).
- const: A parameter of a quantum operation is constant if it is immutable up to a phase.
Zis a permutation and its parameter is const as it merely flips the phase of the state.Xis a permutation but its parameter is not const as it flips between and .His not a permutation and its parameter is not const, as it introduces superposition.SWAPis a permutation but its two parameters are not const, as it swaps between and .
Instructions
For the following function, you need to compute the maximum of two affine expressions and assign the result tores.
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 theassign_amplitude_table and lookup_table functions to load the normalized payoff function into the indicator qubit:
Using the amplitude loading of the payoff function, we can estimate the expected value of the payoff function by measuring the indicator qubit and calculating the probability of measuring using the iterative quantum amplitude estimation (IQAE) algorithm [4].
First, we will build the amplitude loading of the payoff function .
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 , we want to load the value of into the amplitude of the indicator qubit . Therefore, we use theassign_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 structureQStruct 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 thebrute_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:
where is a unitary operator while and are some normalized states. Thus, is the probability of measuring in the last qubit.
The value is:
Which is estimated later on by the IQAE algorithm.
Building the quantum model
In Classiq, we build the quantum model in themain function.
Synthesizing the quantum model
Output:
IQAE algorithm
The IQAE algorithm estimates by using the operation (that uses therainbow_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 .
The IQAE class allows to easily use the IQAE algorithm.
You are welcome to see how it is built inside.
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 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: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.