# Financial Models¶

The financial problems model is divided into two parts:

1. The financial model
2. 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  .

$VaR = argmin_{x_0}{(P(x > x_0) <= \alpha)}$

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 program that can solve this kind of problem:

1. Create a distribution function that represents the assets portfolio.
2. Construct the VaR operator.
3. Extract the VaR using amplitude estimation.

Use the Classiq financial package to do this in two phases:

1. Financial model which contains the distribution function.
2. Financial function $$f_x(i)$$ that is implemented by the model.

### Financial Model¶

In our example, the model is a Gaussian model (called Conditioned Gaussian) of a two-asset portfolio.

The parameters that define this model:

• num qubits (int): Number of qubits that describe the accuracy of the Gaussian
• normal max value (float): The discrete number where the Gaussian is truncated
• default probabilities (List[float]): The loss probability of each asset
• rhos (List[float]): The width of the Gaussian
• loss (List[int]): The loss of each asset
• min loss (int): The minimum loss of the entire distribution

### Financial Function¶

On the same financial model, you can generate several quantum programs with different financial functions. The current example generates the VaR function.

The parameters for the finance function:

• f: The finance function type
• condition:
• threshold: threshold to use the function. otherwise it's 0.
• larger: whether the threshold is from beneath or above.

### Full Example¶

In the next example, we will demonstrate how to extract the CDF value of a given value x for a given probability distribution. That can be used to extract the Var of the distribution, using a bisection search.

qmod
{
"functions": [
{
"name": "qmci",
"param_decls": {
"num_phase_qubits": {
"kind": "int"
},
"num_unitary_qubits": {
"kind": "int"
}
},
"port_declarations": {
"phase_port": {
"name": "phase_port",
"direction": "output",
"size": {
"expr": "num_phase_qubits"
}
},
"unitary_port": {
"name": "unitary_port",
"direction": "output",
"size": {
"expr": "num_unitary_qubits"
}
}
},
"operand_declarations": {
"sp_op": {
"name": "sp_op",
"param_decls": {
"num_unitary_qubits": {
"kind": "int"
}
},
"port_declarations": {
"reg": {
"name": "reg",
"direction": "inout",
"size": {
"expr": "num_unitary_qubits-1"
}
},
"ind": {
"name": "ind",
"direction": "inout",
"size": {
"expr": "1"
}
}
}
}
},
"body": [
{
"function": "amplitude_estimation",
"params": {
"num_unitary_qubits": {
"expr": "num_unitary_qubits"
},
"num_phase_qubits": {
"expr": "num_phase_qubits"
}
},
"function_params": {},
"outputs": {
"phase_port": {
"name": "phase_port"
},
"unitary_port": {
"name": "unitary_port"
}
},
"operands": {
"oracle_op": {
"body": [
{
"function": "Z",
"function_params": {},
"inouts": {
"target": {
"name": "oq",
"start": {
"expr": "num_unitary_qubits-1"
},
"end": {
"expr": "num_unitary_qubits"
}
}
}
}
]
},
"sp_op": {
"body": [
{
"function": "sp_op",
"params": {
"num_unitary_qubits": {
"expr": "num_unitary_qubits"
}
},
"function_params": {},
"inouts": {
"reg": {
"name": "spq",
"start": {
"expr": "0"
},
"end": {
"expr": "num_unitary_qubits-1"
}
},
"ind": {
"name": "spq",
"start": {
"expr": "num_unitary_qubits-1"
},
"end": {
"expr": "num_unitary_qubits"
}
}
}
}
]
}
}
}
]
},
{
"name": "main",
"port_declarations": {
"phase_port": {
"name": "phase_port",
"direction": "output",
"size": {
"expr": "2"
}
}
},
"body": [
{
"function": "qmci",
"params": {
"num_unitary_qubits": {
"expr": "6+1"
},
"num_phase_qubits": {
"expr": "2"
}
},
"function_params": {},
"outputs": {
"phase_port": {
"name": "phase_port"
}
},
"operands": {
"sp_op": {
"body": [
{
"function": "gaussian_finance",
"params": {
"finance_model": {
"expr": "struct_literal(GaussianModel, num_qubits=2, normal_max_value=2.0, default_probabilities=[0.15, 0.25], rhos=[0.1, 0.05], loss=[1, 2], min_loss=0)"
},
"finance_function": {
"expr": "struct_literal(FinanceFunction, f=FinanceFunctionType.VAR, threshold=2.0, larger=True, polynomial_degree=0, use_chebyshev_polynomial_approximation=False, tail_probability=0.0)"
}
},
"function_params": {},
"inputs": {
"func_port": {
"name": "reg"
},
"obj_port": {
"name": "ind"
}
},
"outputs": {
"func_port": {
"name": "reg"
},
"obj_port": {
"name": "ind"
}
}
}
]
}
}
}
]
}
],
"classical_execution_code": "\ndef execute_amplitude_estimation(phase_port_size):\n    result = sample()\n    estimation = qae_with_qpe_result_post_processing(\n        estimation_register_size=phase_port_size,\n        estimation_method=1,\n        result=result\n    )\n    save({\"result\": result, \"estimation\": estimation})\n    return estimation\n\nestimation = execute_amplitude_estimation(2)\nresult = gaussian_finance_post_process(struct_literal(GaussianModel, num_qubits=2, normal_max_value=2.0, default_probabilities=[0.15, 0.25], rhos=[0.1, 0.05], loss=[1, 2], min_loss=0), struct_literal(FinanceFunction, f=FinanceFunctionType.VAR, threshold=2.0, larger=True, polynomial_degree=0, use_chebyshev_polynomial_approximation=False, tail_probability=0.0), estimation)\nsave({'result': result})\n",
"constraints": {
"max_width": 10
}
}

from classiq import (
construct_finance_model,
synthesize,
execute,
set_constraints,
Constraints,
)
from classiq.applications.finance import function_input, gaussian_model_input

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 = construct_finance_model(
finance_model_input=gaussian_model,
finance_function_input=finance_function,
phase_port_size=2,
)

model = set_constraints(model, Constraints(max_width=10))

quantum_program = synthesize(model)
results = execute(quantum_program).result()
print(results.name, ":", results.value)


This is the output quantum program, with some (but not all) of its blocks expanded: This program can be executed on a quantum computer.

### IDE¶

#### Synthesizing a Financial Model¶

This screenshot shows the IDE when the Option Pricing option is selected from the Built-in Algorithms pane. By modifying the Synthesis Configuration options and then clicking the Generate Model button, the model is populated in the Synthesis Model pane. Pressing the Synthesize button generates the quantum program and shows its graphical representation.

#### Executing the Quantum Program¶

The synthesized quantum program can be executed from the execution section of the IDE or from the SDK (see execution).

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. ## 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:

$\Bigg\{\begin{array}{lr} 0, & \text{when } K\geq S\\ S - K, & \text{when } K < S\end{array}$

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:

1. Load the wanted distribution, that is, discretize the distribution using $$2^n$$ points (where n is the number of qubits) and truncate it.
2. 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:

qmod

json { "functions": [ { "name": "qmci", "param_decls": { "num_phase_qubits": { "kind": "int" }, "num_unitary_qubits": { "kind": "int" } }, "port_declarations": { "phase_port": { "name": "phase_port", "direction": "output", "size": { "expr": "num_phase_qubits" } }, "unitary_port": { "name": "unitary_port", "direction": "output", "size": { "expr": "num_unitary_qubits" } } }, "operand_declarations": { "sp_op": { "name": "sp_op", "param_decls": { "num_unitary_qubits": { "kind": "int" } }, "port_declarations": { "reg": { "name": "reg", "direction": "inout", "size": { "expr": "num_unitary_qubits-1" } }, "ind": { "name": "ind", "direction": "inout", "size": { "expr": "1" } } } } }, "body": [ { "function": "amplitude_estimation", "params": { "num_unitary_qubits": { "expr": "num_unitary_qubits" }, "num_phase_qubits": { "expr": "num_phase_qubits" } }, "function_params": {}, "outputs": { "phase_port": { "name": "phase_port" }, "unitary_port": { "name": "unitary_port" } }, "operands": { "oracle_op": { "body": [ { "function": "Z", "function_params": {}, "inouts": { "target": { "name": "oq", "start": { "expr": "num_unitary_qubits-1" }, "end": { "expr": "num_unitary_qubits" } } } } ] }, "sp_op": { "body": [ { "function": "sp_op", "params": { "num_unitary_qubits": { "expr": "num_unitary_qubits" } }, "function_params": {}, "inouts": { "reg": { "name": "spq", "start": { "expr": "0" }, "end": { "expr": "num_unitary_qubits-1" } }, "ind": { "name": "spq", "start": { "expr": "num_unitary_qubits-1" }, "end": { "expr": "num_unitary_qubits" } } } } ] } } } ] }, { "name": "main", "port_declarations": { "phase_port": { "name": "phase_port", "direction": "output", "size": { "expr": "2" } } }, "body": [ { "function": "qmci", "params": { "num_unitary_qubits": { "expr": "3+1" }, "num_phase_qubits": { "expr": "2" } }, "function_params": {}, "outputs": { "phase_port": { "name": "phase_port" } }, "operands": { "sp_op": { "body": [ { "function": "log_normal_finance", "params": { "finance_model": { "expr": "struct_literal(LogNormalModel, num_qubits=3, mu=0.68986, sigma=0.132417)" }, "finance_function": { "expr": "struct_literal(FinanceFunction, f=FinanceFunctionType.EUROPEAN_CALL_OPTION, threshold=1.896, larger=True, polynomial_degree=0, use_chebyshev_polynomial_approximation=False, tail_probability=0.0)" } }, "function_params": {}, "inputs": { "func_port": { "name": "reg" }, "obj_port": { "name": "ind" } }, "outputs": { "func_port": { "name": "reg" }, "obj_port": { "name": "ind" } } } ] } } } ] } ], "classical_execution_code": "\ndef execute_amplitude_estimation(phase_port_size):\n result = sample()\n estimation = qae_with_qpe_result_post_processing(\n estimation_register_size=phase_port_size,\n estimation_method=1,\n result=result\n )\n save({\"result\": result, \"estimation\": estimation})\n return estimation\n\nestimation = execute_amplitude_estimation(2)\nresult = log_normal_finance_post_process(struct_literal(LogNormalModel, num_qubits=3, mu=0.68986, sigma=0.132417), struct_literal(FinanceFunction, f=FinanceFunctionType.EUROPEAN_CALL_OPTION, threshold=1.896, larger=True, polynomial_degree=0, use_chebyshev_polynomial_approximation=False, tail_probability=0.0), estimation)\nsave({'result': result})\n", "constraints": { "max_width": 10 } }



In this case, the threshold argument refers to the strike price $$K$$, and we look for the case where the maturity price ($$S$$) is larger than the strike price (putting it to True). The phase_port_size determines the number of qubits used by the phase estimation algorithm (implemented inside the amplitude estimation algorithm) that sets the accuracy of the calculation. In other words, more qubits lead to a more accurate solution with a price of bigger quantum programs.

The result shows the expected value of the payoff function according to the given probability distribution.

### 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

 Woerner, S., Egger, D.J. Quantum risk analysis. npj Quantum Inf 5, 15 (2019).