Financial Models¶
The financial problems model is divided into two parts:
- The financial model
- The function that acts on this model
See the examples below. Note that you can add user-defined functions to create your own type of model example.
Example 1 - Quantum Risk Analysis¶
Risk analysis aims to evaluate the potential for loss in an investment. Specifically, an investor needs to quantify how large a loss on investment could be, given a certain level of confidence, over a period of time. This quantity is known as the value at risk (VaR). The VaR is based on the statistical characteristics of the investment and the shape of its distribution curve. It is evaluated using intensive computations of the aggregated value for M different realizations of portfolio assets model. The width of the confidence interval scales like \(O(M^{-1/2})\). To calculate the VaR of an assets portfolio on a quantum computer, define the VaR such that [1] .
where \(x \in {0,...N-1}\) and \(\alpha\) is the confidence level, i.e., the percentage that a loss is larger than the VaR. The VaR is the smallest value of x with a confidence level of \(\alpha\).
To create a quantum circuit that can solve this kind of problem:
- Create a distribution function that represents the assets portfolio.
- Construct the VaR operator.
- Extract the VaR using amplitude estimation.
Use the Classiq financial package to do this in two phases:
- Financial model which contains the distribution function.
- Financial function \(f_x(i)\) that is implemented by the model.
Financial Model¶
{
"function_library": {
"functions": [
{
"name": "main",
"logic_flow": [
{
"function": "Finance",
"function_params": {
"model": {
"num_qubits":2,
"normal_max_value":2,
"default_probabilities":[0.15, 0.25],
"rhos":[0.1, 0.05],
"loss":[1, 2],
"min_loss":0
},
"finance_function": {
"f": "var",
"condition": {
"threshold":2,
"larger": true}
}
}
}
]
}
]
}
}
from classiq import Model
from classiq.builtin_functions import Finance
from classiq.applications.finance import (
model_input,
function_input,
gaussian_model_input
)
model = Model()
gaussian_model = gaussian_model_input.GaussianModelInput(
num_qubits=2,
normal_max_value=2,
default_probabilities=[0.15, 0.25],
rhos=[0.1, 0.05],
loss=[1, 2],
min_loss=0
)
condition = function_input.FunctionCondition(threshold=2, larger=True)
finance_function = function_input.FinanceFunctionInput(
f="var",
condition=condition,
)
model.Finance(Finance(model=gaussian_model, finance_function=finance_function))
circuit = model.synthesize()
In the example above, the model is a Gaussian model (called Conditioned Gaussian) of a two-asset portfolio.
These parameters define this model:
num qubits (int)
: Number of qubits that describe the accuracy of the Gaussiannormal max value (float)
: The discrete number where the Gaussian is truncateddefault probabilities (List[float])
: The loss probability of each assetrhos (List[float])
: The width of the Gaussianloss (List[int])
: The loss of each assetmin loss (int)
: The minimum loss of the entire distribution
Financial Function¶
On the same financial model, you can generate several quantum circuits with different financial functions. The current example generates the VaR function.
{
"function_library": {
"functions": [
{
"name": "main",
"logic_flow": [
{
"function": "Finance",
"function_params": {
"model": {
"num_qubits": 2,
"normal_max_value": 2,
"default_probabilities": [
0.15,
0.25
],
"rhos": [
0.1,
0.05
],
"loss": [
1,
2
],
"min_loss": 0
},
"finance_function": {
"f": "var",
"condition": {
"threshold": 2,
"larger": true
}
}
}
}
]
}
]
}
}
from classiq import Model
from classiq.builtin_functions import Finance
from classiq.applications.finance import (
model_input,
function_input,
gaussian_model_input
)
model = Model()
gaussian_model = gaussian_model_input.GaussianModelInput(
num_qubits=2,
normal_max_value=2,
default_probabilities=[0.15, 0.25],
rhos=[0.1, 0.05],
loss=[1, 2],
min_loss=0
)
condition = function_input.FunctionCondition(threshold=2, larger=True)
finance_function = function_input.FinanceFunctionInput(
f="var",
condition=condition,
)
model.Finance(Finance(model=gaussian_model, finance_function=finance_function))
circuit = model.synthesize()
This is the output circuit, with some (but not all) of its blocks expanded:
This circuit can be executed on a quantum computer.
Synthesizing a Financial Model in the IDE¶
This screenshot shows the IDE when the Finance option is selected from the application suite.
An additional section below Preferences has editable model parameters, corresponding to the model input parameters described on this page. This side panel simplifies manual tweaking of the model parameters. Pressing the Synthesize button generates the model and shows its graphical representation.
Executing the Quantum Circuit¶
The generated circuit can be executed from the execution section of the IDE (see execution) using Amplitude Estimation or from the SDK.
To execute from within the IDE, select Execution from the side panel.
Select the execution resource using the drop-down filters for vendor and device type.
Type your preferences for performing amplitude estimation into the appropriate group of fields. The image below shows an example of such parameters.
An example of executing using the SDK is shown in the snippet below.
from classiq import Model, Executor
from classiq.builtin_functions import Finance
from classiq.execution import AmplitudeEstimation
from classiq.applications.finance import (
model_input,
function_input,
gaussian_model_input,
)
model = Model()
gaussian_model = gaussian_model_input.GaussianModelInput(
num_qubits=2,
normal_max_value=2,
default_probabilities=[0.15, 0.25],
rhos=[0.1, 0.05],
loss=[1, 2],
min_loss=0,
)
condition = function_input.FunctionCondition(threshold=2, larger=True)
finance_function = function_input.FinanceFunctionInput(
f="var",
condition=condition,
)
model.Finance(Finance(model=gaussian_model, finance_function=finance_function))
circuit = model.synthesize()
res = Executor(
amplitude_estimation=AmplitudeEstimation(
alpha=0.5, epsilon=0.5, binary_search_threshold=0.05
)
).execute(circuit)
This example runs amplitude estimation, which is the quantum analog to Monte Carlo simulation generally used for this kind of problem. To find the value at risk, use a binary search. The value at risk is the loss value that corresponds to the percentile you inserted at the binary search threshold.
Example 2 - 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). The option has a maturity price (S). The payoff function to describe, for example, a European call option, is this:
The maturity price is unknown, therefore it is expressed by a price distribution function, which may be any type of distribution function. For example, a log-normal distribution: \(X=e^{\mu+\sigma Z}\), where \(\mu,\sigma\) are the mean and STD respectively and Z is the standard normal distribution.
To estimate the option price using a quantum computer:
- Load the wanted distribution, that is, discretize the distribution using \(2^n\) points (where n is the number of qubits) and truncate it.
- Implement the payoff function that is equal to zero if \(S\leq{K}\) and increases linearly. The linear part is approximated to load it properly using \(R_y\) rotations [3] .
- Evaluate the expected payoff using amplitude estimation.
It can be implemented as follows.
{
"function_library": {
"functions": [
{
"name": "main",
"logic_flow": [
{
"function": "Finance",
"function_params": {
"model": {
"num_qubits": 3,
"mu": 0.68986,
"sigma": 0.132417
},
"finance_function": {
"f": "european call option",
"condition": {
"threshold": 1.896,
"larger": true
}
}
}
}
]
}
]
}
}
from classiq import Model
from classiq.builtin_functions import Finance
from classiq.applications.finance import (
model_input,
function_input,
log_normal_model_input,
)
model = Model()
log_normal_model = log_normal_model_input.LogNormalModelInput(
num_qubits=3, mu=0.68986, sigma=0.132417
)
condition = function_input.FunctionCondition(threshold=1.896, larger=True)
finance_function = function_input.FinanceFunctionInput(
f="european call option",
condition=condition,
)
model.Finance(Finance(model=log_normal_model, finance_function=finance_function))
circuit = model.synthesize()
Execute this circuit using the IDE (see execution) or the SDK as demonstrated below.
from classiq import Model, Executor
from classiq.builtin_functions import Finance
from classiq.applications.finance import (
model_input,
function_input,
log_normal_model_input,
)
from classiq.execution import AmplitudeEstimation
model = Model()
log_normal_model = log_normal_model_input.LogNormalModelInput(
num_qubits=3, mu=0.68986, sigma=0.132417
)
condition = function_input.FunctionCondition(threshold=1.896, larger=True)
finance_function = function_input.FinanceFunctionInput(
f="european call option",
condition=condition,
)
model.Finance(Finance(model=log_normal_model, finance_function=finance_function))
circuit = model.synthesize()
res = Executor(
amplitude_estimation=AmplitudeEstimation(
alpha=0.5, epsilon=0.5, binary_search_threshold=None
)
).execute(circuit)
Models¶
The available models on the Classiq platform:
gaussian
- abbreviation of 'Gaussian conditional independence model'log normal
Functions¶
The available functions:
var
expected shortfall
european call option
x\*\*2
Example 3 - Expected Shortfall¶
The Classiq finance package allows generating any function using Chebyshev polynomial approximations. In the following example, the polynomial degree is the approximation degree.
{
"logic_flow": [
{
"function": "Finance",
"function_params": {
"model": {
"num_qubits": 2,
"normal_max_value": 2,
"default_probabilities": [
0.15,
0.25
],
"rhos": [
0.1,
0.05
],
"loss": [
1,
2
],
"min_loss": 0
},
"finance_function": {
"f": "european call option",
"condition": {
"threshold": 1.896,
"larger": true
},
"polynomial_degree": 1,
"use_chebyshev_polynomial_approximation": true
}
}
}
]
}
from classiq.applications.finance import (
function_input,
log_normal_model_input,
)
condition = function_input.FunctionCondition(threshold=1.896, larger=True)
finance_function = function_input.FinanceFunctionInput(
f="european call option",
condition=condition,
polynomial_degree=1,
use_chebyshev_polynomial_approximation=True,
)
References¶
[1] Woerner, S., Egger, D.J. Quantum risk analysis. npj Quantum Inf 5, 15 (2019).