classiq 0.86.1__py3-none-any.whl → 0.88.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of classiq might be problematic. Click here for more details.
- classiq/__init__.py +2 -0
- classiq/applications/__init__.py +1 -2
- classiq/applications/chemistry/hartree_fock.py +5 -1
- classiq/applications/chemistry/op_utils.py +2 -2
- classiq/applications/combinatorial_helpers/combinatorial_problem_utils.py +1 -1
- classiq/applications/combinatorial_helpers/encoding_mapping.py +11 -15
- classiq/applications/combinatorial_helpers/encoding_utils.py +6 -6
- classiq/applications/combinatorial_helpers/memory.py +4 -4
- classiq/applications/combinatorial_helpers/optimization_model.py +5 -5
- classiq/applications/combinatorial_helpers/pauli_helpers/pauli_utils.py +6 -10
- classiq/applications/combinatorial_helpers/pyomo_utils.py +27 -26
- classiq/applications/combinatorial_helpers/sympy_utils.py +2 -2
- classiq/applications/combinatorial_helpers/transformations/encoding.py +4 -6
- classiq/applications/combinatorial_helpers/transformations/fixed_variables.py +4 -4
- classiq/applications/combinatorial_helpers/transformations/ising_converter.py +2 -2
- classiq/applications/combinatorial_helpers/transformations/penalty_support.py +3 -3
- classiq/applications/combinatorial_optimization/combinatorial_problem.py +4 -0
- classiq/applications/hamiltonian/pauli_decomposition.py +34 -2
- classiq/evaluators/argument_types.py +15 -6
- classiq/evaluators/parameter_types.py +43 -39
- classiq/evaluators/qmod_annotated_expression.py +117 -17
- classiq/evaluators/qmod_expression_visitors/out_of_place_node_transformer.py +19 -0
- classiq/evaluators/qmod_expression_visitors/qmod_expression_bwc.py +0 -5
- classiq/evaluators/qmod_expression_visitors/qmod_expression_evaluator.py +66 -16
- classiq/evaluators/qmod_expression_visitors/qmod_expression_renamer.py +48 -26
- classiq/evaluators/qmod_expression_visitors/qmod_expression_simplifier.py +65 -72
- classiq/evaluators/qmod_node_evaluators/attribute_evaluation.py +13 -6
- classiq/evaluators/qmod_node_evaluators/binary_op_evaluation.py +175 -28
- classiq/evaluators/qmod_node_evaluators/classical_function_evaluation.py +36 -19
- classiq/evaluators/qmod_node_evaluators/compare_evaluation.py +17 -5
- classiq/evaluators/qmod_node_evaluators/constant_evaluation.py +24 -2
- classiq/evaluators/qmod_node_evaluators/min_max_evaluation.py +97 -0
- classiq/evaluators/qmod_node_evaluators/name_evaluation.py +11 -26
- classiq/evaluators/qmod_node_evaluators/numeric_attrs_utils.py +56 -0
- classiq/evaluators/qmod_node_evaluators/piecewise_evaluation.py +40 -0
- classiq/evaluators/qmod_node_evaluators/struct_instantiation_evaluation.py +3 -4
- classiq/evaluators/qmod_node_evaluators/subscript_evaluation.py +51 -24
- classiq/evaluators/qmod_node_evaluators/unary_op_evaluation.py +53 -9
- classiq/evaluators/qmod_node_evaluators/utils.py +28 -6
- classiq/evaluators/qmod_type_inference/classical_type_inference.py +188 -0
- classiq/evaluators/qmod_type_inference/quantum_type_inference.py +330 -0
- classiq/evaluators/quantum_type_utils.py +0 -131
- classiq/evaluators/type_type_match.py +1 -1
- classiq/execution/execution_session.py +18 -3
- classiq/execution/qnn.py +4 -1
- classiq/execution/user_budgets.py +1 -1
- classiq/interface/_version.py +1 -1
- classiq/interface/backend/backend_preferences.py +10 -30
- classiq/interface/backend/quantum_backend_providers.py +63 -52
- classiq/interface/execution/primitives.py +1 -0
- classiq/interface/generator/application_apis/__init__.py +0 -1
- classiq/interface/generator/arith/binary_ops.py +107 -115
- classiq/interface/generator/arith/extremum_operations.py +33 -45
- classiq/interface/generator/arith/number_utils.py +4 -1
- classiq/interface/generator/circuit_code/types_and_constants.py +0 -9
- classiq/interface/generator/compiler_keywords.py +2 -0
- classiq/interface/generator/expressions/atomic_expression_functions.py +0 -2
- classiq/interface/generator/function_param_list.py +129 -5
- classiq/interface/generator/functions/classical_type.py +67 -2
- classiq/interface/generator/functions/qmod_python_interface.py +15 -0
- classiq/interface/generator/functions/type_name.py +12 -0
- classiq/interface/generator/model/preferences/preferences.py +1 -17
- classiq/interface/generator/quantum_program.py +1 -13
- classiq/interface/generator/transpiler_basis_gates.py +5 -1
- classiq/interface/generator/types/builtin_enum_declarations.py +0 -8
- classiq/interface/helpers/model_normalizer.py +2 -2
- classiq/interface/helpers/text_utils.py +7 -2
- classiq/interface/interface_version.py +1 -1
- classiq/interface/model/classical_if.py +48 -0
- classiq/interface/model/classical_parameter_declaration.py +4 -0
- classiq/interface/model/handle_binding.py +28 -16
- classiq/interface/model/port_declaration.py +12 -0
- classiq/interface/model/quantum_function_declaration.py +12 -0
- classiq/interface/model/quantum_type.py +117 -2
- classiq/interface/pretty_print/expression_to_qmod.py +7 -8
- classiq/interface/pyomo_extension/__init__.py +0 -4
- classiq/interface/pyomo_extension/pyomo_sympy_bimap.py +2 -2
- classiq/model_expansions/arithmetic.py +43 -1
- classiq/model_expansions/arithmetic_compute_result_attrs.py +255 -0
- classiq/model_expansions/capturing/captured_vars.py +2 -5
- classiq/model_expansions/quantum_operations/allocate.py +23 -16
- classiq/model_expansions/quantum_operations/arithmetic/explicit_boolean_expressions.py +1 -3
- classiq/model_expansions/quantum_operations/assignment_result_processor.py +52 -71
- classiq/model_expansions/quantum_operations/bind.py +15 -7
- classiq/model_expansions/quantum_operations/call_emitter.py +2 -10
- classiq/model_expansions/quantum_operations/classical_var_emitter.py +6 -0
- classiq/model_expansions/quantum_operations/handle_evaluator.py +2 -8
- classiq/open_library/functions/__init__.py +4 -0
- classiq/open_library/functions/lcu.py +117 -0
- classiq/open_library/functions/state_preparation.py +47 -5
- classiq/qmod/builtins/__init__.py +0 -3
- classiq/qmod/builtins/classical_functions.py +0 -28
- classiq/qmod/builtins/enums.py +26 -20
- classiq/qmod/builtins/functions/__init__.py +0 -5
- classiq/qmod/builtins/operations.py +142 -0
- classiq/qmod/builtins/structs.py +33 -29
- classiq/qmod/native/pretty_printer.py +1 -1
- classiq/qmod/pretty_print/expression_to_python.py +1 -6
- classiq/qmod/pretty_print/pretty_printer.py +4 -1
- classiq/qmod/qmod_variable.py +94 -2
- classiq/qmod/semantics/annotation/call_annotation.py +4 -2
- classiq/qmod/semantics/annotation/qstruct_annotator.py +20 -5
- {classiq-0.86.1.dist-info → classiq-0.88.0.dist-info}/METADATA +5 -5
- {classiq-0.86.1.dist-info → classiq-0.88.0.dist-info}/RECORD +106 -124
- classiq/applications/finance/__init__.py +0 -15
- classiq/interface/finance/finance_modelling_params.py +0 -11
- classiq/interface/finance/function_input.py +0 -102
- classiq/interface/finance/gaussian_model_input.py +0 -50
- classiq/interface/finance/log_normal_model_input.py +0 -40
- classiq/interface/finance/model_input.py +0 -22
- classiq/interface/generator/amplitude_estimation.py +0 -34
- classiq/interface/generator/application_apis/finance_declarations.py +0 -108
- classiq/interface/generator/expressions/enums/__init__.py +0 -0
- classiq/interface/generator/expressions/enums/finance_functions.py +0 -12
- classiq/interface/generator/finance.py +0 -107
- classiq/interface/generator/function_param_list_without_self_reference.py +0 -160
- classiq/interface/generator/grover_diffuser.py +0 -93
- classiq/interface/generator/grover_operator.py +0 -106
- classiq/interface/generator/oracles/__init__.py +0 -3
- classiq/interface/generator/oracles/arithmetic_oracle.py +0 -82
- classiq/interface/generator/oracles/custom_oracle.py +0 -65
- classiq/interface/generator/oracles/oracle_abc.py +0 -76
- classiq/interface/generator/oracles/oracle_function_param_list.py +0 -6
- classiq/interface/generator/piecewise_linear_amplitude_loading.py +0 -165
- classiq/interface/generator/qpe.py +0 -169
- classiq/interface/grover/__init__.py +0 -0
- classiq/interface/grover/grover_modelling_params.py +0 -13
- classiq/model_expansions/transformers/var_splitter.py +0 -224
- classiq/qmod/builtins/functions/finance.py +0 -34
- /classiq/{interface/finance → evaluators/qmod_type_inference}/__init__.py +0 -0
- {classiq-0.86.1.dist-info → classiq-0.88.0.dist-info}/WHEEL +0 -0
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
from typing import Any, Optional, Union
|
|
2
|
-
|
|
3
|
-
import pydantic
|
|
4
|
-
from pydantic import ConfigDict
|
|
5
|
-
from pydantic_core.core_schema import ValidationInfo
|
|
6
|
-
|
|
7
|
-
from classiq.interface.exceptions import ClassiqValueError
|
|
8
|
-
from classiq.interface.generator.expressions.enums.finance_functions import (
|
|
9
|
-
get_finance_function_dict,
|
|
10
|
-
)
|
|
11
|
-
from classiq.interface.generator.types.builtin_enum_declarations import (
|
|
12
|
-
FinanceFunctionType,
|
|
13
|
-
)
|
|
14
|
-
from classiq.interface.helpers.custom_pydantic_types import (
|
|
15
|
-
PydanticNonZeroProbabilityFloat,
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class FunctionCondition(pydantic.BaseModel):
|
|
20
|
-
threshold: float
|
|
21
|
-
larger: bool = pydantic.Field(
|
|
22
|
-
default=False,
|
|
23
|
-
description="When true, function is set when input is larger to threshold and otherwise 0. Default is False.",
|
|
24
|
-
)
|
|
25
|
-
model_config = ConfigDict(frozen=True)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class FinanceFunctionInput(pydantic.BaseModel):
|
|
29
|
-
f: "FinanceFunctionType" = pydantic.Field(
|
|
30
|
-
description="An enumeration of the wanted financial function: VaR, expected "
|
|
31
|
-
"shortfall, European call options or x^2"
|
|
32
|
-
)
|
|
33
|
-
variable: str = pydantic.Field(
|
|
34
|
-
default="x", description="Variable/s of the function"
|
|
35
|
-
)
|
|
36
|
-
condition: FunctionCondition = pydantic.Field(
|
|
37
|
-
description="The condition for the function"
|
|
38
|
-
)
|
|
39
|
-
polynomial_degree: Optional[int] = pydantic.Field(
|
|
40
|
-
default=None,
|
|
41
|
-
description="The polynomial degree of approximation, uses linear approximation by default",
|
|
42
|
-
)
|
|
43
|
-
use_chebyshev_polynomial_approximation: bool = pydantic.Field(
|
|
44
|
-
default=False,
|
|
45
|
-
description="Flag if to use chebyshev polynomial approximation for target function",
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
tail_probability: Optional[PydanticNonZeroProbabilityFloat] = pydantic.Field(
|
|
49
|
-
default=None,
|
|
50
|
-
description="The required probability on the tail of the distribution (1 - percentile)",
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
@pydantic.model_validator(mode="before")
|
|
54
|
-
@classmethod
|
|
55
|
-
def _convert_f_if_str(cls, values: Any, info: ValidationInfo) -> dict[str, Any]:
|
|
56
|
-
if isinstance(values, dict):
|
|
57
|
-
f = values.get("f")
|
|
58
|
-
elif isinstance(values, FinanceFunctionInput):
|
|
59
|
-
f = values.f
|
|
60
|
-
values = values.model_dump()
|
|
61
|
-
else:
|
|
62
|
-
f = info.data.get("f")
|
|
63
|
-
if isinstance(f, str) and f in get_finance_function_dict():
|
|
64
|
-
values["f"] = get_finance_function_dict()[f]
|
|
65
|
-
return values
|
|
66
|
-
|
|
67
|
-
@pydantic.field_validator("use_chebyshev_polynomial_approximation", mode="before")
|
|
68
|
-
@classmethod
|
|
69
|
-
def _validate_polynomial_flag(
|
|
70
|
-
cls, use_chebyshev_flag: bool, info: ValidationInfo
|
|
71
|
-
) -> bool:
|
|
72
|
-
if use_chebyshev_flag ^ (info.data.get("polynomial_degree") is None):
|
|
73
|
-
return use_chebyshev_flag
|
|
74
|
-
raise ClassiqValueError(
|
|
75
|
-
"Degree must be positive and use_chebyshev_polynomial_approximation set to True"
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
@pydantic.field_validator("f", mode="before")
|
|
79
|
-
@classmethod
|
|
80
|
-
def _validate_finance_function(
|
|
81
|
-
cls, f: Union[int, str, "FinanceFunctionType"]
|
|
82
|
-
) -> FinanceFunctionType:
|
|
83
|
-
if isinstance(f, FinanceFunctionType):
|
|
84
|
-
return f
|
|
85
|
-
if isinstance(f, int):
|
|
86
|
-
return FinanceFunctionType(f)
|
|
87
|
-
return get_finance_function_dict()[f]
|
|
88
|
-
|
|
89
|
-
@pydantic.field_validator("tail_probability", mode="before")
|
|
90
|
-
@classmethod
|
|
91
|
-
def _validate_tail_probability_assignment_for_shortfall(
|
|
92
|
-
cls,
|
|
93
|
-
tail_probability: Optional[PydanticNonZeroProbabilityFloat],
|
|
94
|
-
info: ValidationInfo,
|
|
95
|
-
) -> Optional[PydanticNonZeroProbabilityFloat]:
|
|
96
|
-
if info.data.get("f") == FinanceFunctionType.SHORTFALL and not tail_probability:
|
|
97
|
-
raise ClassiqValueError(
|
|
98
|
-
"Tail probability must be set for expected shortfall"
|
|
99
|
-
)
|
|
100
|
-
return tail_probability
|
|
101
|
-
|
|
102
|
-
model_config = ConfigDict(frozen=True)
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import math
|
|
2
|
-
from typing import Literal, Optional
|
|
3
|
-
|
|
4
|
-
import pydantic
|
|
5
|
-
|
|
6
|
-
from classiq.interface.finance.model_input import FinanceModelInput
|
|
7
|
-
from classiq.interface.helpers.custom_pydantic_types import PydanticProbabilityFloat
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class GaussianModelInput(FinanceModelInput):
|
|
11
|
-
kind: Literal["gaussian"] = pydantic.Field(default="gaussian")
|
|
12
|
-
|
|
13
|
-
num_qubits: pydantic.PositiveInt = pydantic.Field(
|
|
14
|
-
description="The number of qubits represent"
|
|
15
|
-
"the latent normal random variable Z (Resolution of "
|
|
16
|
-
"the random variable Z)."
|
|
17
|
-
)
|
|
18
|
-
normal_max_value: float = pydantic.Field(
|
|
19
|
-
description="Min/max value to truncate the " "latent normal random variable Z"
|
|
20
|
-
)
|
|
21
|
-
default_probabilities: list[PydanticProbabilityFloat] = pydantic.Field(
|
|
22
|
-
description="default probabilities for each asset"
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
rhos: list[pydantic.PositiveFloat] = pydantic.Field(
|
|
26
|
-
description="Sensitivities of default probability of assets "
|
|
27
|
-
"with respect to Z (1/sigma(Z))"
|
|
28
|
-
)
|
|
29
|
-
loss: list[int] = pydantic.Field(
|
|
30
|
-
description="List of ints signifying loss per asset"
|
|
31
|
-
)
|
|
32
|
-
min_loss: Optional[int] = pydantic.Field(
|
|
33
|
-
description="Minimum possible loss for the model "
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
@property
|
|
37
|
-
def num_model_qubits(self) -> int:
|
|
38
|
-
return len(self.rhos)
|
|
39
|
-
|
|
40
|
-
@property
|
|
41
|
-
def distribution_range(self) -> tuple[float, float]:
|
|
42
|
-
return 0, sum(self.loss)
|
|
43
|
-
|
|
44
|
-
@property
|
|
45
|
-
def num_output_qubits(self) -> int:
|
|
46
|
-
return int(math.log2(sum(self.loss))) + 1
|
|
47
|
-
|
|
48
|
-
@property
|
|
49
|
-
def num_bernoulli_qubits(self) -> int:
|
|
50
|
-
return self.num_qubits + self.num_model_qubits
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
from typing import Literal
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
import pydantic
|
|
5
|
-
from pydantic import ConfigDict
|
|
6
|
-
|
|
7
|
-
from classiq.interface.finance.model_input import FinanceModelInput
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class LogNormalModelInput(FinanceModelInput):
|
|
11
|
-
kind: Literal["log_normal"] = pydantic.Field(default="log_normal")
|
|
12
|
-
|
|
13
|
-
num_qubits: pydantic.PositiveInt = pydantic.Field(
|
|
14
|
-
description="Number of qubits to represent the probability."
|
|
15
|
-
)
|
|
16
|
-
mu: pydantic.NonNegativeFloat = pydantic.Field(
|
|
17
|
-
description="Mean of the Normal distribution variable X s.t. ln(X) ~ log-normal."
|
|
18
|
-
)
|
|
19
|
-
sigma: pydantic.PositiveFloat = pydantic.Field(
|
|
20
|
-
description="Std of the Normal distribution variable X s.t. ln(X) ~ log-normal."
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
@property
|
|
24
|
-
def distribution_range(self) -> tuple[float, float]:
|
|
25
|
-
mean = np.exp(self.mu + self.sigma**2 / 2)
|
|
26
|
-
variance = (np.exp(self.sigma**2) - 1) * np.exp(2 * self.mu + self.sigma**2)
|
|
27
|
-
stddev = np.sqrt(variance)
|
|
28
|
-
low = np.maximum(0, mean - 3 * stddev)
|
|
29
|
-
high = mean + 3 * stddev
|
|
30
|
-
return low, high
|
|
31
|
-
|
|
32
|
-
@property
|
|
33
|
-
def num_model_qubits(self) -> int:
|
|
34
|
-
return self.num_qubits
|
|
35
|
-
|
|
36
|
-
@property
|
|
37
|
-
def num_output_qubits(self) -> int:
|
|
38
|
-
return self.num_qubits
|
|
39
|
-
|
|
40
|
-
model_config = ConfigDict(frozen=True)
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import abc
|
|
2
|
-
|
|
3
|
-
from pydantic import ConfigDict
|
|
4
|
-
|
|
5
|
-
from classiq.interface.helpers.hashable_pydantic_base_model import (
|
|
6
|
-
HashablePydanticBaseModel,
|
|
7
|
-
)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class FinanceModelInput(HashablePydanticBaseModel):
|
|
11
|
-
kind: str
|
|
12
|
-
|
|
13
|
-
@property
|
|
14
|
-
def num_output_qubits(self) -> int:
|
|
15
|
-
return 0
|
|
16
|
-
|
|
17
|
-
model_config = ConfigDict(frozen=True)
|
|
18
|
-
|
|
19
|
-
@property
|
|
20
|
-
@abc.abstractmethod
|
|
21
|
-
def distribution_range(self) -> tuple[float, float]:
|
|
22
|
-
pass
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import pydantic
|
|
2
|
-
|
|
3
|
-
from classiq.interface.generator.arith.register_user_input import RegisterArithmeticInfo
|
|
4
|
-
from classiq.interface.generator.function_params import FunctionParams
|
|
5
|
-
from classiq.interface.generator.grover_operator import GroverOperator
|
|
6
|
-
|
|
7
|
-
ESTIMATED_AMPLITUDE_OUTPUT_NAME: str = "ESTIMATED_AMPLITUDE_OUTPUT"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class AmplitudeEstimation(FunctionParams):
|
|
11
|
-
"""
|
|
12
|
-
Creates a quantum circuit for amplitude estimation
|
|
13
|
-
Provide the state preparation and oracle within the GroverOperator parameter
|
|
14
|
-
Choose estimation accuracy with the estimation_register_size parameter
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
grover_operator: GroverOperator = pydantic.Field(
|
|
18
|
-
description="The Grover Operator used in the algorithm. "
|
|
19
|
-
"Composed of the oracle and the state preparation operator."
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
estimation_register_size: pydantic.PositiveInt = pydantic.Field(
|
|
23
|
-
description="The number of qubits used to estimate the amplitude. "
|
|
24
|
-
"Bigger register provides a better estimate of the good states' amplitude."
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
def _create_ios(self) -> None:
|
|
28
|
-
self._inputs = dict()
|
|
29
|
-
self._outputs = {
|
|
30
|
-
ESTIMATED_AMPLITUDE_OUTPUT_NAME: RegisterArithmeticInfo(
|
|
31
|
-
size=self.estimation_register_size
|
|
32
|
-
),
|
|
33
|
-
**self.grover_operator.outputs,
|
|
34
|
-
}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
from collections.abc import Mapping
|
|
2
|
-
from enum import Enum
|
|
3
|
-
|
|
4
|
-
from classiq.interface.generator.expressions.expression import Expression
|
|
5
|
-
from classiq.interface.generator.functions.classical_function_declaration import (
|
|
6
|
-
ClassicalFunctionDeclaration,
|
|
7
|
-
)
|
|
8
|
-
from classiq.interface.generator.functions.classical_type import Real
|
|
9
|
-
from classiq.interface.generator.functions.port_declaration import (
|
|
10
|
-
PortDeclarationDirection,
|
|
11
|
-
)
|
|
12
|
-
from classiq.interface.generator.functions.type_modifier import TypeModifier
|
|
13
|
-
from classiq.interface.generator.functions.type_name import Struct
|
|
14
|
-
from classiq.interface.model.classical_parameter_declaration import (
|
|
15
|
-
ClassicalParameterDeclaration,
|
|
16
|
-
)
|
|
17
|
-
from classiq.interface.model.port_declaration import PortDeclaration
|
|
18
|
-
from classiq.interface.model.quantum_function_declaration import (
|
|
19
|
-
NamedParamsQuantumFunctionDeclaration,
|
|
20
|
-
)
|
|
21
|
-
from classiq.interface.model.quantum_type import QuantumBit, QuantumBitvector
|
|
22
|
-
|
|
23
|
-
FUNCTION_PORT_NAME = "func_port"
|
|
24
|
-
OBJECTIVE_PORT_NAME = "obj_port"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class FinanceModelType(Enum):
|
|
28
|
-
LogNormal = "log_normal"
|
|
29
|
-
Gaussian = "gaussian"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
FINANCE_FUNCTION_PORT_SIZE_MAPPING: Mapping[FinanceModelType, str] = {
|
|
33
|
-
FinanceModelType.Gaussian: "get_field(finance_model, 'num_qubits') + get_field(get_field(finance_model, 'rhos'), 'len') + floor(log(sum(get_field(finance_model, 'loss')), 2)) + 1",
|
|
34
|
-
FinanceModelType.LogNormal: "get_field(finance_model, 'num_qubits')",
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def _generate_finance_function(
|
|
39
|
-
finance_model: FinanceModelType,
|
|
40
|
-
) -> NamedParamsQuantumFunctionDeclaration:
|
|
41
|
-
return NamedParamsQuantumFunctionDeclaration(
|
|
42
|
-
name=f"{finance_model.value}_finance",
|
|
43
|
-
positional_arg_declarations=[
|
|
44
|
-
ClassicalParameterDeclaration(
|
|
45
|
-
name="finance_model",
|
|
46
|
-
classical_type=Struct(name=f"{finance_model.name}Model"),
|
|
47
|
-
),
|
|
48
|
-
ClassicalParameterDeclaration(
|
|
49
|
-
name="finance_function", classical_type=Struct(name="FinanceFunction")
|
|
50
|
-
),
|
|
51
|
-
PortDeclaration(
|
|
52
|
-
name=FUNCTION_PORT_NAME,
|
|
53
|
-
quantum_type=QuantumBitvector(
|
|
54
|
-
length=Expression(
|
|
55
|
-
expr=FINANCE_FUNCTION_PORT_SIZE_MAPPING[finance_model]
|
|
56
|
-
)
|
|
57
|
-
),
|
|
58
|
-
direction=PortDeclarationDirection.Inout,
|
|
59
|
-
type_modifier=TypeModifier.Mutable,
|
|
60
|
-
),
|
|
61
|
-
PortDeclaration(
|
|
62
|
-
name=OBJECTIVE_PORT_NAME,
|
|
63
|
-
quantum_type=QuantumBit(),
|
|
64
|
-
direction=PortDeclarationDirection.Inout,
|
|
65
|
-
type_modifier=TypeModifier.Mutable,
|
|
66
|
-
),
|
|
67
|
-
],
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
LOG_NORMAL_FINANCE_FUNCTION = _generate_finance_function(FinanceModelType.LogNormal)
|
|
72
|
-
|
|
73
|
-
GAUSSIAN_FINANCE_FUNCTION = _generate_finance_function(FinanceModelType.Gaussian)
|
|
74
|
-
|
|
75
|
-
LOG_NORMAL_FINANCE_POST_PROCESS = ClassicalFunctionDeclaration(
|
|
76
|
-
name="log_normal_finance_post_process",
|
|
77
|
-
positional_parameters=[
|
|
78
|
-
ClassicalParameterDeclaration(
|
|
79
|
-
name="finance_model", classical_type=Struct(name="LogNormalModel")
|
|
80
|
-
),
|
|
81
|
-
ClassicalParameterDeclaration(
|
|
82
|
-
name="estimation_method", classical_type=Struct(name="FinanceFunction")
|
|
83
|
-
),
|
|
84
|
-
ClassicalParameterDeclaration(name="probability", classical_type=Real()),
|
|
85
|
-
],
|
|
86
|
-
return_type=Real(),
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
GAUSSIAN_FINANCE_POST_PROCESS = ClassicalFunctionDeclaration(
|
|
90
|
-
name="gaussian_finance_post_process",
|
|
91
|
-
positional_parameters=[
|
|
92
|
-
ClassicalParameterDeclaration(
|
|
93
|
-
name="finance_model", classical_type=Struct(name="GaussianModel")
|
|
94
|
-
),
|
|
95
|
-
ClassicalParameterDeclaration(
|
|
96
|
-
name="estimation_method", classical_type=Struct(name="FinanceFunction")
|
|
97
|
-
),
|
|
98
|
-
ClassicalParameterDeclaration(name="probability", classical_type=Real()),
|
|
99
|
-
],
|
|
100
|
-
return_type=Real(),
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
__all__ = [
|
|
104
|
-
"GAUSSIAN_FINANCE_FUNCTION",
|
|
105
|
-
"GAUSSIAN_FINANCE_POST_PROCESS",
|
|
106
|
-
"LOG_NORMAL_FINANCE_FUNCTION",
|
|
107
|
-
"LOG_NORMAL_FINANCE_POST_PROCESS",
|
|
108
|
-
]
|
|
File without changes
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
from classiq.interface.generator.types.builtin_enum_declarations import (
|
|
2
|
-
FinanceFunctionType,
|
|
3
|
-
)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def get_finance_function_dict() -> dict[str, "FinanceFunctionType"]:
|
|
7
|
-
return {
|
|
8
|
-
"var": FinanceFunctionType.VAR,
|
|
9
|
-
"expected shortfall": FinanceFunctionType.SHORTFALL,
|
|
10
|
-
"x**2": FinanceFunctionType.X_SQUARE,
|
|
11
|
-
"european call option": FinanceFunctionType.EUROPEAN_CALL_OPTION,
|
|
12
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from typing import Union
|
|
4
|
-
|
|
5
|
-
import pydantic
|
|
6
|
-
|
|
7
|
-
from classiq.interface.finance.function_input import FinanceFunctionInput
|
|
8
|
-
from classiq.interface.finance.gaussian_model_input import GaussianModelInput
|
|
9
|
-
from classiq.interface.finance.log_normal_model_input import LogNormalModelInput
|
|
10
|
-
from classiq.interface.generator import function_params
|
|
11
|
-
from classiq.interface.generator.arith.register_user_input import RegisterUserInput
|
|
12
|
-
from classiq.interface.generator.function_params import DEFAULT_ZERO_NAME
|
|
13
|
-
|
|
14
|
-
FUNCTION_INPUT_NAME = "function_in"
|
|
15
|
-
FUNCTION_OUTPUT_NAME = "function_out"
|
|
16
|
-
OBJECTIVE_INPUT_NAME = "objective_in"
|
|
17
|
-
OBJECTIVE_OUTPUT_NAME = "objective_out"
|
|
18
|
-
DEFAULT_POST_INPUT_NAME = "post_function_input"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class Finance(function_params.FunctionParams):
|
|
22
|
-
model: Union[GaussianModelInput, LogNormalModelInput] = pydantic.Field(
|
|
23
|
-
description="Load a financial model", discriminator="kind"
|
|
24
|
-
)
|
|
25
|
-
finance_function: FinanceFunctionInput = pydantic.Field(
|
|
26
|
-
description="The finance function to solve the model"
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
def _create_ios(self) -> None:
|
|
30
|
-
finance_model = FinanceModels(model=self.model)
|
|
31
|
-
# 1 for the objective qubit
|
|
32
|
-
function_size = sum(
|
|
33
|
-
reg.size for reg in finance_model._outputs.values() if reg is not None
|
|
34
|
-
)
|
|
35
|
-
self._inputs = {
|
|
36
|
-
FUNCTION_INPUT_NAME: RegisterUserInput(
|
|
37
|
-
name=FUNCTION_INPUT_NAME, size=function_size
|
|
38
|
-
),
|
|
39
|
-
OBJECTIVE_INPUT_NAME: RegisterUserInput(name=OBJECTIVE_INPUT_NAME, size=1),
|
|
40
|
-
}
|
|
41
|
-
self._outputs = {
|
|
42
|
-
FUNCTION_OUTPUT_NAME: RegisterUserInput(
|
|
43
|
-
name=FUNCTION_OUTPUT_NAME, size=function_size
|
|
44
|
-
),
|
|
45
|
-
OBJECTIVE_OUTPUT_NAME: RegisterUserInput(
|
|
46
|
-
name=OBJECTIVE_OUTPUT_NAME, size=1
|
|
47
|
-
),
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
DEFAULT_INPUT_NAME = "in"
|
|
52
|
-
DEFAULT_OUTPUT_NAME = "out"
|
|
53
|
-
DEFAULT_BERNOULLI_OUTPUT_NAME = "bernoulli_random_variables"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
class FinanceModels(function_params.FunctionParams):
|
|
57
|
-
model: Union[GaussianModelInput, LogNormalModelInput] = pydantic.Field(
|
|
58
|
-
description="Load a financial model"
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
def _create_ios(self) -> None:
|
|
62
|
-
self._outputs = {
|
|
63
|
-
DEFAULT_OUTPUT_NAME: RegisterUserInput(
|
|
64
|
-
name=DEFAULT_OUTPUT_NAME, size=self.model.num_output_qubits
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
if isinstance(self.model, GaussianModelInput):
|
|
68
|
-
self._inputs = {
|
|
69
|
-
DEFAULT_INPUT_NAME: RegisterUserInput(
|
|
70
|
-
name=DEFAULT_INPUT_NAME, size=self.model.num_bernoulli_qubits
|
|
71
|
-
)
|
|
72
|
-
}
|
|
73
|
-
self._create_zero_input_registers(
|
|
74
|
-
{DEFAULT_ZERO_NAME: self.model.num_output_qubits}
|
|
75
|
-
)
|
|
76
|
-
self._outputs[DEFAULT_BERNOULLI_OUTPUT_NAME] = RegisterUserInput(
|
|
77
|
-
name=DEFAULT_BERNOULLI_OUTPUT_NAME,
|
|
78
|
-
size=self.model.num_bernoulli_qubits,
|
|
79
|
-
)
|
|
80
|
-
elif isinstance(self.model, LogNormalModelInput):
|
|
81
|
-
self._inputs = {
|
|
82
|
-
DEFAULT_INPUT_NAME: RegisterUserInput(
|
|
83
|
-
name=DEFAULT_INPUT_NAME, size=self.model.num_model_qubits
|
|
84
|
-
)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
class FinancePayoff(function_params.FunctionParams):
|
|
89
|
-
finance_function: FinanceFunctionInput = pydantic.Field(
|
|
90
|
-
description="The finance function to solve the model"
|
|
91
|
-
)
|
|
92
|
-
num_qubits: pydantic.PositiveInt
|
|
93
|
-
distribution_range: tuple[float, float]
|
|
94
|
-
|
|
95
|
-
def _create_ios(self) -> None:
|
|
96
|
-
self._inputs = {
|
|
97
|
-
DEFAULT_INPUT_NAME: RegisterUserInput(
|
|
98
|
-
name=DEFAULT_INPUT_NAME, size=self.num_qubits
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
self._create_zero_input_registers({DEFAULT_ZERO_NAME: 1})
|
|
102
|
-
self._outputs = {
|
|
103
|
-
DEFAULT_OUTPUT_NAME: RegisterUserInput(name=DEFAULT_OUTPUT_NAME, size=1),
|
|
104
|
-
DEFAULT_POST_INPUT_NAME: RegisterUserInput(
|
|
105
|
-
name=DEFAULT_INPUT_NAME, size=self.num_qubits
|
|
106
|
-
),
|
|
107
|
-
}
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import itertools
|
|
2
|
-
|
|
3
|
-
from classiq.interface.generator.amplitude_estimation import AmplitudeEstimation
|
|
4
|
-
from classiq.interface.generator.amplitude_loading import AmplitudeLoading
|
|
5
|
-
from classiq.interface.generator.arith.arithmetic import Arithmetic
|
|
6
|
-
from classiq.interface.generator.arith.binary_ops import (
|
|
7
|
-
Adder,
|
|
8
|
-
BitwiseAnd,
|
|
9
|
-
BitwiseOr,
|
|
10
|
-
BitwiseXor,
|
|
11
|
-
CyclicShift,
|
|
12
|
-
Equal,
|
|
13
|
-
GreaterEqual,
|
|
14
|
-
GreaterThan,
|
|
15
|
-
LessEqual,
|
|
16
|
-
LessThan,
|
|
17
|
-
LShift,
|
|
18
|
-
Modulo,
|
|
19
|
-
Multiplier,
|
|
20
|
-
NotEqual,
|
|
21
|
-
Power,
|
|
22
|
-
RShift,
|
|
23
|
-
Subtractor,
|
|
24
|
-
)
|
|
25
|
-
from classiq.interface.generator.arith.extremum_operations import Max, Min
|
|
26
|
-
from classiq.interface.generator.arith.logical_ops import LogicalAnd, LogicalOr
|
|
27
|
-
from classiq.interface.generator.arith.unary_ops import BitwiseInvert, Negation, Sign
|
|
28
|
-
from classiq.interface.generator.commuting_pauli_exponentiation import (
|
|
29
|
-
CommutingPauliExponentiation,
|
|
30
|
-
)
|
|
31
|
-
from classiq.interface.generator.copy import Copy
|
|
32
|
-
from classiq.interface.generator.entangler_params import (
|
|
33
|
-
GridEntangler,
|
|
34
|
-
HypercubeEntangler,
|
|
35
|
-
TwoDimensionalEntangler,
|
|
36
|
-
)
|
|
37
|
-
from classiq.interface.generator.finance import Finance, FinanceModels, FinancePayoff
|
|
38
|
-
from classiq.interface.generator.function_param_library import FunctionParamLibrary
|
|
39
|
-
from classiq.interface.generator.grover_diffuser import GroverDiffuser
|
|
40
|
-
from classiq.interface.generator.grover_operator import GroverOperator
|
|
41
|
-
from classiq.interface.generator.hadamard_transform import HadamardTransform
|
|
42
|
-
from classiq.interface.generator.hamiltonian_evolution.exponentiation import (
|
|
43
|
-
Exponentiation,
|
|
44
|
-
)
|
|
45
|
-
from classiq.interface.generator.hamiltonian_evolution.qdrift import QDrift
|
|
46
|
-
from classiq.interface.generator.hamiltonian_evolution.suzuki_trotter import (
|
|
47
|
-
SuzukiTrotter,
|
|
48
|
-
)
|
|
49
|
-
from classiq.interface.generator.hardware_efficient_ansatz import (
|
|
50
|
-
HardwareEfficientAnsatz,
|
|
51
|
-
)
|
|
52
|
-
from classiq.interface.generator.hartree_fock import HartreeFock
|
|
53
|
-
from classiq.interface.generator.hva import HVA
|
|
54
|
-
from classiq.interface.generator.identity import Identity
|
|
55
|
-
from classiq.interface.generator.linear_pauli_rotations import LinearPauliRotations
|
|
56
|
-
from classiq.interface.generator.mcu import Mcu
|
|
57
|
-
from classiq.interface.generator.mcx import Mcx
|
|
58
|
-
from classiq.interface.generator.oracles.oracle_function_param_list import (
|
|
59
|
-
oracle_function_param_library,
|
|
60
|
-
)
|
|
61
|
-
from classiq.interface.generator.piecewise_linear_amplitude_loading import (
|
|
62
|
-
PiecewiseLinearAmplitudeLoading,
|
|
63
|
-
PiecewiseLinearRotationAmplitudeLoading,
|
|
64
|
-
)
|
|
65
|
-
from classiq.interface.generator.qft import QFT
|
|
66
|
-
from classiq.interface.generator.qsvm import QSVMFeatureMap
|
|
67
|
-
from classiq.interface.generator.randomized_benchmarking import RandomizedBenchmarking
|
|
68
|
-
from classiq.interface.generator.reset import Reset
|
|
69
|
-
from classiq.interface.generator.standard_gates.standard_gates_param_list import (
|
|
70
|
-
standard_gate_function_param_library,
|
|
71
|
-
)
|
|
72
|
-
from classiq.interface.generator.standard_gates.u_gate import UGate
|
|
73
|
-
from classiq.interface.generator.state_preparation import (
|
|
74
|
-
BellStatePreparation,
|
|
75
|
-
ComputationalBasisStatePreparation,
|
|
76
|
-
ExponentialStatePreparation,
|
|
77
|
-
GHZStatePreparation,
|
|
78
|
-
StatePreparation,
|
|
79
|
-
UniformDistributionStatePreparation,
|
|
80
|
-
WStatePreparation,
|
|
81
|
-
)
|
|
82
|
-
from classiq.interface.generator.ucc import UCC
|
|
83
|
-
from classiq.interface.generator.unitary_gate import UnitaryGate
|
|
84
|
-
from classiq.interface.generator.user_defined_function_params import CustomFunction
|
|
85
|
-
|
|
86
|
-
function_param_library_without_self_reference: FunctionParamLibrary = (
|
|
87
|
-
FunctionParamLibrary(
|
|
88
|
-
param_list=itertools.chain(
|
|
89
|
-
{
|
|
90
|
-
StatePreparation,
|
|
91
|
-
ComputationalBasisStatePreparation,
|
|
92
|
-
UniformDistributionStatePreparation,
|
|
93
|
-
BellStatePreparation,
|
|
94
|
-
GHZStatePreparation,
|
|
95
|
-
WStatePreparation,
|
|
96
|
-
ExponentialStatePreparation,
|
|
97
|
-
QFT,
|
|
98
|
-
BitwiseAnd,
|
|
99
|
-
BitwiseOr,
|
|
100
|
-
BitwiseXor,
|
|
101
|
-
BitwiseInvert,
|
|
102
|
-
Adder,
|
|
103
|
-
Arithmetic,
|
|
104
|
-
Sign,
|
|
105
|
-
Equal,
|
|
106
|
-
NotEqual,
|
|
107
|
-
GreaterThan,
|
|
108
|
-
GreaterEqual,
|
|
109
|
-
LessThan,
|
|
110
|
-
LessEqual,
|
|
111
|
-
Negation,
|
|
112
|
-
LogicalAnd,
|
|
113
|
-
LogicalOr,
|
|
114
|
-
Subtractor,
|
|
115
|
-
RShift,
|
|
116
|
-
LShift,
|
|
117
|
-
CyclicShift,
|
|
118
|
-
Modulo,
|
|
119
|
-
TwoDimensionalEntangler,
|
|
120
|
-
Finance,
|
|
121
|
-
FinanceModels,
|
|
122
|
-
FinancePayoff,
|
|
123
|
-
HypercubeEntangler,
|
|
124
|
-
AmplitudeEstimation,
|
|
125
|
-
GridEntangler,
|
|
126
|
-
GroverDiffuser,
|
|
127
|
-
GroverOperator,
|
|
128
|
-
Mcx,
|
|
129
|
-
Mcu,
|
|
130
|
-
CustomFunction,
|
|
131
|
-
HardwareEfficientAnsatz,
|
|
132
|
-
UnitaryGate,
|
|
133
|
-
LinearPauliRotations,
|
|
134
|
-
Multiplier,
|
|
135
|
-
Power,
|
|
136
|
-
HartreeFock,
|
|
137
|
-
UCC,
|
|
138
|
-
Min,
|
|
139
|
-
Max,
|
|
140
|
-
Exponentiation,
|
|
141
|
-
CommutingPauliExponentiation,
|
|
142
|
-
SuzukiTrotter,
|
|
143
|
-
QDrift,
|
|
144
|
-
Identity,
|
|
145
|
-
RandomizedBenchmarking,
|
|
146
|
-
HVA,
|
|
147
|
-
UGate,
|
|
148
|
-
AmplitudeLoading,
|
|
149
|
-
QSVMFeatureMap,
|
|
150
|
-
PiecewiseLinearAmplitudeLoading,
|
|
151
|
-
PiecewiseLinearRotationAmplitudeLoading,
|
|
152
|
-
HadamardTransform,
|
|
153
|
-
Copy,
|
|
154
|
-
Reset,
|
|
155
|
-
},
|
|
156
|
-
standard_gate_function_param_library.param_list,
|
|
157
|
-
oracle_function_param_library.param_list,
|
|
158
|
-
)
|
|
159
|
-
)
|
|
160
|
-
)
|