classiq 0.53.0__py3-none-any.whl → 0.55.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.
- classiq/__init__.py +1 -3
- classiq/_analyzer_extras/_ipywidgets_async_extension.py +2 -1
- classiq/_internals/__init__.py +0 -20
- classiq/_internals/api_wrapper.py +8 -8
- classiq/_internals/async_utils.py +1 -3
- classiq/_internals/authentication/auth0.py +5 -5
- classiq/_internals/authentication/device.py +5 -4
- classiq/_internals/authentication/password_manager.py +3 -3
- classiq/_internals/authentication/token_manager.py +3 -2
- classiq/_internals/client.py +10 -12
- classiq/_internals/config.py +2 -2
- classiq/_internals/jobs.py +7 -6
- classiq/_internals/type_validation.py +9 -9
- classiq/analyzer/__init__.py +1 -3
- classiq/analyzer/analyzer.py +8 -7
- classiq/analyzer/analyzer_utilities.py +8 -8
- classiq/analyzer/rb.py +11 -11
- classiq/applications/__init__.py +1 -3
- classiq/applications/chemistry/__init__.py +1 -3
- classiq/applications/chemistry/ansatz_parameters.py +4 -4
- classiq/applications/chemistry/chemistry_model_constructor.py +10 -9
- classiq/applications/combinatorial_helpers/combinatorial_problem_utils.py +26 -9
- classiq/applications/combinatorial_helpers/encoding_mapping.py +10 -10
- classiq/applications/combinatorial_helpers/encoding_utils.py +4 -4
- classiq/applications/combinatorial_helpers/memory.py +5 -7
- classiq/applications/combinatorial_helpers/optimization_model.py +43 -24
- classiq/applications/combinatorial_helpers/pauli_helpers/pauli_utils.py +4 -6
- classiq/applications/combinatorial_helpers/pyomo_utils.py +95 -24
- classiq/applications/combinatorial_helpers/sympy_utils.py +2 -2
- classiq/applications/combinatorial_helpers/transformations/encoding.py +8 -8
- classiq/applications/combinatorial_helpers/transformations/fixed_variables.py +5 -5
- classiq/applications/combinatorial_helpers/transformations/ising_converter.py +7 -9
- classiq/applications/combinatorial_helpers/transformations/penalty.py +1 -2
- classiq/applications/combinatorial_helpers/transformations/sign_seperation.py +1 -2
- classiq/applications/combinatorial_helpers/transformations/slack_variables.py +1 -2
- classiq/applications/combinatorial_optimization/__init__.py +1 -3
- classiq/applications/combinatorial_optimization/combinatorial_optimization_config.py +2 -2
- classiq/applications/combinatorial_optimization/examples/__init__.py +1 -3
- classiq/applications/finance/__init__.py +1 -3
- classiq/applications/grover/__init__.py +1 -3
- classiq/applications/grover/grover_model_constructor.py +7 -9
- classiq/applications/hamiltonian/pauli_decomposition.py +6 -6
- classiq/applications/qnn/__init__.py +1 -3
- classiq/applications/qnn/circuit_utils.py +5 -5
- classiq/applications/qnn/datasets/__init__.py +1 -3
- classiq/applications/qnn/datasets/dataset_base_classes.py +5 -4
- classiq/applications/qnn/datasets/dataset_parity.py +2 -2
- classiq/applications/qnn/gradients/simple_quantum_gradient.py +2 -1
- classiq/applications/qnn/qlayer.py +3 -3
- classiq/applications/qnn/torch_utils.py +2 -2
- classiq/applications/qnn/types.py +5 -5
- classiq/applications/qsvm/qsvm.py +1 -3
- classiq/applications/qsvm/qsvm_data_generation.py +3 -3
- classiq/applications/qsvm/qsvm_model_constructor.py +5 -5
- classiq/execution/__init__.py +1 -3
- classiq/execution/all_hardware_devices.py +1 -3
- classiq/execution/execution_session.py +16 -16
- classiq/execution/jobs.py +4 -4
- classiq/execution/qaoa.py +3 -3
- classiq/execution/qnn.py +3 -3
- classiq/executor.py +3 -3
- classiq/interface/_version.py +1 -1
- classiq/interface/analyzer/analysis_params.py +9 -10
- classiq/interface/analyzer/cytoscape_graph.py +5 -5
- classiq/interface/analyzer/result.py +17 -17
- classiq/interface/applications/qsvm.py +6 -10
- classiq/interface/backend/backend_preferences.py +4 -3
- classiq/interface/backend/ionq/ionq_quantum_program.py +4 -5
- classiq/interface/backend/pydantic_backend.py +1 -2
- classiq/interface/chemistry/fermionic_operator.py +5 -5
- classiq/interface/chemistry/ground_state_problem.py +7 -8
- classiq/interface/chemistry/molecule.py +4 -4
- classiq/interface/chemistry/operator.py +11 -13
- classiq/interface/combinatorial_optimization/examples/ascending_sequence.py +1 -3
- classiq/interface/combinatorial_optimization/examples/integer_portfolio_optimization.py +2 -4
- classiq/interface/combinatorial_optimization/examples/knapsack.py +3 -3
- classiq/interface/combinatorial_optimization/examples/mht.py +2 -3
- classiq/interface/combinatorial_optimization/examples/portfolio_variations.py +2 -2
- classiq/interface/combinatorial_optimization/examples/set_cover.py +1 -2
- classiq/interface/combinatorial_optimization/mht_qaoa_input.py +5 -7
- classiq/interface/combinatorial_optimization/optimization_problem.py +2 -2
- classiq/interface/combinatorial_optimization/result.py +1 -3
- classiq/interface/debug_info/debug_info.py +8 -7
- classiq/interface/exceptions.py +8 -6
- classiq/interface/execution/jobs.py +2 -2
- classiq/interface/execution/primitives.py +3 -3
- classiq/interface/executor/aws_execution_cost.py +4 -4
- classiq/interface/executor/execution_request.py +2 -3
- classiq/interface/executor/execution_result.py +3 -3
- classiq/interface/executor/iqae_result.py +3 -5
- classiq/interface/executor/optimizer_preferences.py +2 -2
- classiq/interface/executor/quantum_code.py +6 -6
- classiq/interface/executor/register_initialization.py +2 -4
- classiq/interface/executor/result.py +23 -27
- classiq/interface/executor/vqe_result.py +8 -8
- classiq/interface/finance/function_input.py +2 -2
- classiq/interface/finance/gaussian_model_input.py +5 -5
- classiq/interface/finance/log_normal_model_input.py +2 -2
- classiq/interface/finance/model_input.py +1 -2
- classiq/interface/generator/adjacency.py +1 -3
- classiq/interface/generator/ansatz_library.py +4 -4
- classiq/interface/generator/application_apis/finance_declarations.py +1 -1
- classiq/interface/generator/arith/argument_utils.py +3 -3
- classiq/interface/generator/arith/arithmetic.py +7 -7
- classiq/interface/generator/arith/arithmetic_arg_type_validator.py +5 -5
- classiq/interface/generator/arith/arithmetic_expression_abc.py +11 -11
- classiq/interface/generator/arith/arithmetic_expression_parser.py +8 -7
- classiq/interface/generator/arith/arithmetic_expression_validator.py +8 -8
- classiq/interface/generator/arith/arithmetic_operations.py +4 -3
- classiq/interface/generator/arith/arithmetic_param_getters.py +6 -6
- classiq/interface/generator/arith/arithmetic_result_builder.py +9 -9
- classiq/interface/generator/arith/ast_node_rewrite.py +2 -1
- classiq/interface/generator/arith/binary_ops.py +10 -13
- classiq/interface/generator/arith/extremum_operations.py +3 -2
- classiq/interface/generator/arith/logical_ops.py +7 -6
- classiq/interface/generator/arith/number_utils.py +4 -4
- classiq/interface/generator/arith/register_user_input.py +4 -4
- classiq/interface/generator/arith/unary_ops.py +2 -1
- classiq/interface/generator/builtin_api_builder.py +2 -1
- classiq/interface/generator/circuit_code/circuit_code.py +4 -4
- classiq/interface/generator/circuit_code/types_and_constants.py +3 -5
- classiq/interface/generator/complex_type.py +1 -2
- classiq/interface/generator/control_state.py +2 -2
- classiq/interface/generator/custom_ansatz.py +1 -3
- classiq/interface/generator/distance.py +3 -5
- classiq/interface/generator/excitations.py +3 -2
- classiq/interface/generator/expressions/enums/finance_functions.py +1 -3
- classiq/interface/generator/expressions/evaluated_expression.py +4 -3
- classiq/interface/generator/expressions/expression.py +4 -5
- classiq/interface/generator/expressions/expression_constants.py +4 -4
- classiq/interface/generator/expressions/qmod_qarray_proxy.py +2 -1
- classiq/interface/generator/expressions/qmod_qscalar_proxy.py +2 -1
- classiq/interface/generator/expressions/qmod_qstruct_proxy.py +2 -1
- classiq/interface/generator/expressions/qmod_sized_proxy.py +2 -1
- classiq/interface/generator/expressions/qmod_struct_instance.py +2 -1
- classiq/interface/generator/expressions/sympy_supported_expressions.py +11 -13
- classiq/interface/generator/finance.py +2 -2
- classiq/interface/generator/function_param_library.py +6 -6
- classiq/interface/generator/function_params.py +13 -19
- classiq/interface/generator/functions/builtins/internal_operators.py +9 -1
- classiq/interface/generator/functions/classical_function_declaration.py +4 -3
- classiq/interface/generator/functions/classical_type.py +13 -13
- classiq/interface/generator/functions/concrete_types.py +1 -2
- classiq/interface/generator/functions/function_declaration.py +1 -1
- classiq/interface/generator/functions/qmod_python_interface.py +2 -1
- classiq/interface/generator/functions/type_name.py +3 -2
- classiq/interface/generator/generated_circuit_data.py +33 -22
- classiq/interface/generator/grover_diffuser.py +7 -7
- classiq/interface/generator/grover_operator.py +2 -2
- classiq/interface/generator/hardware/hardware_data.py +7 -6
- classiq/interface/generator/hardware_efficient_ansatz.py +8 -8
- classiq/interface/generator/identity.py +5 -6
- classiq/interface/generator/linear_pauli_rotations.py +6 -6
- classiq/interface/generator/mcu.py +2 -2
- classiq/interface/generator/mcx.py +6 -6
- classiq/interface/generator/model/__init__.py +1 -3
- classiq/interface/generator/model/constraints.py +2 -2
- classiq/interface/generator/model/model.py +5 -6
- classiq/interface/generator/model/preferences/preferences.py +11 -6
- classiq/interface/generator/model/quantum_register.py +6 -11
- classiq/interface/generator/oracles/arithmetic_oracle.py +1 -2
- classiq/interface/generator/oracles/custom_oracle.py +2 -2
- classiq/interface/generator/oracles/oracle_abc.py +6 -5
- classiq/interface/generator/partitioned_register.py +6 -5
- classiq/interface/generator/piecewise_linear_amplitude_loading.py +8 -7
- classiq/interface/generator/qpe.py +4 -4
- classiq/interface/generator/qsvm.py +3 -3
- classiq/interface/generator/quantum_function_call.py +24 -29
- classiq/interface/generator/quantum_program.py +9 -9
- classiq/interface/generator/register_role.py +2 -4
- classiq/interface/generator/slice_parsing_utils.py +4 -3
- classiq/interface/generator/standard_gates/standard_gates.py +3 -3
- classiq/interface/generator/state_preparation/bell_state_preparation.py +3 -3
- classiq/interface/generator/state_preparation/distributions.py +6 -5
- classiq/interface/generator/state_preparation/metrics.py +2 -4
- classiq/interface/generator/state_preparation/state_preparation.py +4 -4
- classiq/interface/generator/synthesis_metadata/synthesis_execution_data.py +3 -3
- classiq/interface/generator/transpiler_basis_gates.py +2 -2
- classiq/interface/generator/types/compilation_metadata.py +5 -0
- classiq/interface/generator/types/enum_declaration.py +2 -3
- classiq/interface/generator/types/qstruct_declaration.py +2 -1
- classiq/interface/generator/types/struct_declaration.py +3 -2
- classiq/interface/generator/ucc.py +2 -1
- classiq/interface/generator/unitary_gate.py +2 -2
- classiq/interface/generator/user_defined_function_params.py +1 -1
- classiq/interface/generator/validations/flow_graph.py +6 -5
- classiq/interface/generator/validations/validator_functions.py +3 -2
- classiq/interface/generator/visitor.py +9 -14
- classiq/interface/hardware.py +5 -6
- classiq/interface/helpers/custom_encoders.py +2 -2
- classiq/interface/helpers/custom_pydantic_types.py +8 -9
- classiq/interface/helpers/hashable_mixin.py +3 -2
- classiq/interface/helpers/hashable_pydantic_base_model.py +2 -1
- classiq/interface/helpers/pydantic_model_helpers.py +4 -3
- classiq/interface/helpers/validation_helpers.py +2 -2
- classiq/interface/ide/ide_data.py +11 -15
- classiq/interface/ide/visual_model.py +22 -22
- classiq/interface/jobs.py +2 -2
- classiq/interface/model/bind_operation.py +5 -4
- classiq/interface/model/classical_parameter_declaration.py +2 -2
- classiq/interface/model/control.py +22 -1
- classiq/interface/model/handle_binding.py +3 -2
- classiq/interface/model/inplace_binary_operation.py +2 -1
- classiq/interface/model/model.py +16 -11
- classiq/interface/model/native_function_definition.py +1 -1
- classiq/interface/model/port_declaration.py +2 -2
- classiq/interface/model/quantum_expressions/amplitude_loading_operation.py +3 -2
- classiq/interface/model/quantum_expressions/arithmetic_operation.py +4 -27
- classiq/interface/model/quantum_expressions/quantum_expression.py +8 -7
- classiq/interface/model/quantum_function_call.py +9 -14
- classiq/interface/model/quantum_function_declaration.py +10 -12
- classiq/interface/model/quantum_lambda_function.py +3 -16
- classiq/interface/model/quantum_statement.py +7 -3
- classiq/interface/model/quantum_type.py +5 -5
- classiq/interface/model/statement_block.py +2 -3
- classiq/interface/model/validation_handle.py +5 -4
- classiq/interface/server/global_versions.py +3 -3
- classiq/model_expansions/atomic_expression_functions_defs.py +3 -2
- classiq/model_expansions/capturing/captured_var_manager.py +4 -6
- classiq/model_expansions/capturing/propagated_var_stack.py +7 -7
- classiq/model_expansions/closure.py +83 -12
- classiq/model_expansions/evaluators/arg_type_match.py +3 -2
- classiq/model_expansions/evaluators/argument_types.py +3 -3
- classiq/model_expansions/evaluators/control.py +3 -3
- classiq/model_expansions/evaluators/parameter_types.py +7 -7
- classiq/model_expansions/evaluators/quantum_type_utils.py +2 -1
- classiq/model_expansions/evaluators/type_type_match.py +1 -1
- classiq/model_expansions/expression_evaluator.py +10 -9
- classiq/model_expansions/expression_renamer.py +6 -6
- classiq/model_expansions/function_builder.py +13 -12
- classiq/model_expansions/generative_functions.py +5 -4
- classiq/model_expansions/interpreter.py +20 -11
- classiq/model_expansions/model_tables.py +14 -14
- classiq/model_expansions/quantum_operations/bind.py +2 -4
- classiq/model_expansions/quantum_operations/classicalif.py +1 -1
- classiq/model_expansions/quantum_operations/control.py +81 -24
- classiq/model_expansions/quantum_operations/emitter.py +33 -20
- classiq/model_expansions/quantum_operations/expression_operation.py +47 -16
- classiq/model_expansions/quantum_operations/inplace_binary_operation.py +160 -35
- classiq/model_expansions/quantum_operations/phase.py +6 -6
- classiq/model_expansions/quantum_operations/quantum_assignment_operation.py +28 -31
- classiq/model_expansions/quantum_operations/quantum_function_call.py +9 -0
- classiq/model_expansions/quantum_operations/repeat.py +1 -3
- classiq/model_expansions/quantum_operations/within_apply.py +0 -16
- classiq/model_expansions/scope.py +11 -10
- classiq/model_expansions/scope_initialization.py +5 -5
- classiq/model_expansions/sympy_conversion/expression_to_sympy.py +6 -6
- classiq/model_expansions/sympy_conversion/sympy_to_python.py +2 -2
- classiq/model_expansions/visitors/variable_references.py +5 -4
- classiq/qmod/builtins/classical_execution_primitives.py +9 -9
- classiq/qmod/builtins/functions/__init__.py +72 -55
- classiq/qmod/builtins/functions/amplitude_estimation.py +4 -1
- classiq/qmod/builtins/functions/arithmetic.py +14 -1
- classiq/qmod/builtins/functions/discrete_sine_cosine_transform.py +86 -6
- classiq/qmod/builtins/functions/grover.py +41 -45
- classiq/qmod/builtins/functions/hea.py +60 -4
- classiq/qmod/builtins/functions/linear_pauli_rotation.py +26 -4
- classiq/qmod/builtins/functions/modular_exponentiation.py +90 -29
- classiq/qmod/builtins/functions/operators.py +1 -1
- classiq/qmod/builtins/functions/qaoa_penalty.py +14 -5
- classiq/qmod/builtins/functions/qft_functions.py +57 -0
- classiq/qmod/builtins/functions/qpe.py +20 -4
- classiq/qmod/builtins/functions/qsvt.py +49 -4
- classiq/qmod/builtins/functions/standard_gates.py +4 -4
- classiq/qmod/builtins/functions/state_preparation.py +92 -10
- classiq/qmod/builtins/functions/swap_test.py +7 -1
- classiq/qmod/builtins/functions/utility_functions.py +43 -0
- classiq/qmod/builtins/functions/variational.py +18 -2
- classiq/qmod/builtins/operations.py +117 -22
- classiq/qmod/cfunc.py +2 -2
- classiq/qmod/classical_function.py +3 -7
- classiq/qmod/create_model_function.py +16 -17
- classiq/qmod/declaration_inferrer.py +7 -10
- classiq/qmod/expression_query.py +3 -3
- classiq/qmod/generative.py +2 -1
- classiq/qmod/model_state_container.py +10 -8
- classiq/qmod/native/__init__.py +1 -3
- classiq/qmod/native/expression_to_qmod.py +9 -8
- classiq/qmod/native/pretty_printer.py +12 -6
- classiq/qmod/pretty_print/__init__.py +1 -3
- classiq/qmod/pretty_print/expression_to_python.py +13 -12
- classiq/qmod/pretty_print/pretty_printer.py +38 -23
- classiq/qmod/python_classical_type.py +8 -4
- classiq/qmod/qfunc.py +4 -4
- classiq/qmod/qmod_variable.py +11 -10
- classiq/qmod/quantum_expandable.py +12 -15
- classiq/qmod/quantum_function.py +35 -22
- classiq/qmod/semantics/annotation.py +1 -1
- classiq/qmod/semantics/error_manager.py +8 -7
- classiq/qmod/semantics/static_semantics_visitor.py +19 -24
- classiq/qmod/semantics/validation/constants_validation.py +1 -1
- classiq/qmod/semantics/validation/func_call_validation.py +2 -2
- classiq/qmod/semantics/validation/main_validation.py +33 -0
- classiq/qmod/semantics/validation/types_validation.py +2 -1
- classiq/qmod/symbolic.py +5 -8
- classiq/qmod/symbolic_type.py +2 -2
- classiq/qmod/synthesize_separately.py +1 -2
- {classiq-0.53.0.dist-info → classiq-0.55.0.dist-info}/METADATA +1 -1
- {classiq-0.53.0.dist-info → classiq-0.55.0.dist-info}/RECORD +300 -297
- classiq/qmod/builtins/functions/qft.py +0 -23
- {classiq-0.53.0.dist-info → classiq-0.55.0.dist-info}/WHEEL +0 -0
@@ -1,16 +1,12 @@
|
|
1
1
|
import functools
|
2
2
|
import operator
|
3
3
|
from collections import defaultdict
|
4
|
+
from collections.abc import Iterator, Mapping
|
4
5
|
from typing import (
|
5
6
|
TYPE_CHECKING,
|
6
7
|
Any,
|
7
8
|
DefaultDict,
|
8
|
-
Dict,
|
9
|
-
Iterator,
|
10
|
-
List,
|
11
|
-
Mapping,
|
12
9
|
Optional,
|
13
|
-
Tuple,
|
14
10
|
Union,
|
15
11
|
)
|
16
12
|
|
@@ -37,8 +33,8 @@ RegisterValue: TypeAlias = Union[float, int, list, dict]
|
|
37
33
|
MeasuredShots: TypeAlias = pydantic.NonNegativeInt
|
38
34
|
ParsedState: TypeAlias = Mapping[Name, RegisterValue]
|
39
35
|
ParsedStates: TypeAlias = Mapping[State, ParsedState]
|
40
|
-
Counts: TypeAlias =
|
41
|
-
StateVector: TypeAlias = Optional[
|
36
|
+
Counts: TypeAlias = dict[State, MeasuredShots]
|
37
|
+
StateVector: TypeAlias = Optional[dict[str, Complex]]
|
42
38
|
|
43
39
|
if TYPE_CHECKING:
|
44
40
|
DotAccessParsedState = Mapping[Name, Any]
|
@@ -59,7 +55,7 @@ class SampledState(BaseModel):
|
|
59
55
|
return {name: get_recursive_dotdict(value) for name, value in state.items()}
|
60
56
|
|
61
57
|
|
62
|
-
ParsedCounts: TypeAlias =
|
58
|
+
ParsedCounts: TypeAlias = list[SampledState]
|
63
59
|
|
64
60
|
|
65
61
|
class SimulatedState(BaseModel):
|
@@ -72,7 +68,7 @@ class SimulatedState(BaseModel):
|
|
72
68
|
|
73
69
|
|
74
70
|
SimulatedState.model_rebuild()
|
75
|
-
ParsedStateVector: TypeAlias =
|
71
|
+
ParsedStateVector: TypeAlias = list[SimulatedState]
|
76
72
|
|
77
73
|
|
78
74
|
class VaRResult(BaseModel):
|
@@ -81,10 +77,10 @@ class VaRResult(BaseModel):
|
|
81
77
|
|
82
78
|
|
83
79
|
class GroverSimulationResults(VersionedModel):
|
84
|
-
result:
|
80
|
+
result: dict[str, Any]
|
85
81
|
|
86
82
|
|
87
|
-
def _validate_qubit_indices(counts: Counts, indices:
|
83
|
+
def _validate_qubit_indices(counts: Counts, indices: tuple[int, ...]) -> None:
|
88
84
|
if not indices:
|
89
85
|
raise ClassiqError(_ILLEGAL_QUBIT_ERROR_MSG)
|
90
86
|
|
@@ -95,7 +91,7 @@ def _validate_qubit_indices(counts: Counts, indices: Tuple[int, ...]) -> None:
|
|
95
91
|
raise ClassiqError(_REPEATED_QUBIT_ERROR_MSG)
|
96
92
|
|
97
93
|
|
98
|
-
def _slice_str(s: str, indices:
|
94
|
+
def _slice_str(s: str, indices: tuple[int, ...]) -> str:
|
99
95
|
return "".join(s[i] for i in indices)
|
100
96
|
|
101
97
|
|
@@ -113,7 +109,7 @@ def get_sampled_state(
|
|
113
109
|
|
114
110
|
|
115
111
|
def reduce_parsed_states(
|
116
|
-
parsed_states: ParsedStates, outputs:
|
112
|
+
parsed_states: ParsedStates, outputs: tuple[Name, ...]
|
117
113
|
) -> ParsedStates:
|
118
114
|
return {
|
119
115
|
state: {
|
@@ -152,7 +148,7 @@ def prepare_parsed_state_vector(
|
|
152
148
|
|
153
149
|
|
154
150
|
class ExecutionDetails(BaseModel, QmodPyObject):
|
155
|
-
vendor_format_result:
|
151
|
+
vendor_format_result: dict[str, Any] = pydantic.Field(
|
156
152
|
..., description="Result in proprietary vendor format"
|
157
153
|
)
|
158
154
|
counts: Counts = pydantic.Field(
|
@@ -162,14 +158,14 @@ class ExecutionDetails(BaseModel, QmodPyObject):
|
|
162
158
|
True,
|
163
159
|
description="Is the qubit order of counts field such that the LSB is right?",
|
164
160
|
)
|
165
|
-
probabilities:
|
161
|
+
probabilities: dict[State, pydantic.NonNegativeFloat] = pydantic.Field(
|
166
162
|
default_factory=dict, description="Probabilities of each state"
|
167
163
|
)
|
168
164
|
parsed_states: ParsedStates = pydantic.Field(
|
169
165
|
default_factory=dict,
|
170
166
|
description="A mapping between the raw states of counts (bitstrings) to their parsed states (registers' values)",
|
171
167
|
)
|
172
|
-
histogram: Optional[
|
168
|
+
histogram: Optional[dict[State, pydantic.NonNegativeFloat]] = pydantic.Field(
|
173
169
|
None,
|
174
170
|
description="Histogram of probability per state (an alternative to counts)",
|
175
171
|
)
|
@@ -252,15 +248,15 @@ class ExecutionDetails(BaseModel, QmodPyObject):
|
|
252
248
|
return self.counts_of_qubits(*self.output_qubits_map[output_name])
|
253
249
|
|
254
250
|
def counts_of_multiple_outputs(
|
255
|
-
self, output_names:
|
256
|
-
) ->
|
251
|
+
self, output_names: tuple[Name, ...]
|
252
|
+
) -> dict[tuple[State, ...], pydantic.NonNegativeInt]:
|
257
253
|
if any(name not in self.output_qubits_map for name in output_names):
|
258
254
|
raise ClassiqError(_UNAVAILABLE_OUTPUT_ERROR_MSG)
|
259
255
|
|
260
|
-
output_regs:
|
256
|
+
output_regs: tuple[Qubits, ...] = tuple(
|
261
257
|
self.output_qubits_map[name] for name in output_names
|
262
258
|
)
|
263
|
-
reduced_counts: DefaultDict[
|
259
|
+
reduced_counts: DefaultDict[tuple[State, ...], int] = defaultdict(int)
|
264
260
|
for state_str, state_count in self.counts_by_qubit_order(
|
265
261
|
lsb_right=False
|
266
262
|
).items():
|
@@ -269,7 +265,7 @@ class ExecutionDetails(BaseModel, QmodPyObject):
|
|
269
265
|
return dict(reduced_counts)
|
270
266
|
|
271
267
|
def parsed_counts_of_outputs(
|
272
|
-
self, output_names: Union[Name,
|
268
|
+
self, output_names: Union[Name, tuple[Name, ...]]
|
273
269
|
) -> ParsedCounts:
|
274
270
|
if isinstance(output_names, Name):
|
275
271
|
output_names = (output_names,)
|
@@ -279,8 +275,8 @@ class ExecutionDetails(BaseModel, QmodPyObject):
|
|
279
275
|
reduced_parsed_states = reduce_parsed_states(self.parsed_states, output_names)
|
280
276
|
return get_parsed_counts(self.counts, reduced_parsed_states)
|
281
277
|
|
282
|
-
def register_output_from_qubits(self, qubits:
|
283
|
-
register_output:
|
278
|
+
def register_output_from_qubits(self, qubits: tuple[int, ...]) -> dict[float, int]:
|
279
|
+
register_output: dict[float, int] = {}
|
284
280
|
value_from_str_bin = functools.partial(
|
285
281
|
self._get_register_value_from_binary_string_results, register_qubits=qubits
|
286
282
|
)
|
@@ -296,7 +292,7 @@ class ExecutionDetails(BaseModel, QmodPyObject):
|
|
296
292
|
|
297
293
|
@staticmethod
|
298
294
|
def _get_register_value_from_binary_string_results(
|
299
|
-
binary_string: str, register_qubits:
|
295
|
+
binary_string: str, register_qubits: list[int]
|
300
296
|
) -> RegisterValue:
|
301
297
|
register_binary_string = "".join(
|
302
298
|
operator.itemgetter(*register_qubits)(binary_string)
|
@@ -305,7 +301,7 @@ class ExecutionDetails(BaseModel, QmodPyObject):
|
|
305
301
|
|
306
302
|
|
307
303
|
class MultipleExecutionDetails(VersionedModel):
|
308
|
-
details:
|
304
|
+
details: list[ExecutionDetails]
|
309
305
|
|
310
306
|
def __getitem__(self, index: int) -> ExecutionDetails:
|
311
307
|
return self.details[index]
|
@@ -314,7 +310,7 @@ class MultipleExecutionDetails(VersionedModel):
|
|
314
310
|
class EstimationMetadata(BaseModel, extra="allow"):
|
315
311
|
shots: Optional[pydantic.NonNegativeInt] = None
|
316
312
|
remapped_qubits: bool = False
|
317
|
-
input_qubit_map: Optional[
|
313
|
+
input_qubit_map: Optional[list[PydanticNonNegIntTuple]] = None
|
318
314
|
|
319
315
|
|
320
316
|
class EstimationResult(BaseModel, QmodPyObject):
|
@@ -328,7 +324,7 @@ class EstimationResult(BaseModel, QmodPyObject):
|
|
328
324
|
|
329
325
|
|
330
326
|
class EstimationResults(VersionedModel):
|
331
|
-
results:
|
327
|
+
results: list[EstimationResult]
|
332
328
|
|
333
329
|
def __len__(self) -> int:
|
334
330
|
return len(self.results)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import base64
|
2
2
|
import io
|
3
3
|
from datetime import datetime
|
4
|
-
from typing import
|
4
|
+
from typing import Optional
|
5
5
|
|
6
6
|
import pydantic
|
7
7
|
from PIL import Image
|
@@ -12,7 +12,7 @@ from classiq.interface.generator.complex_type import Complex
|
|
12
12
|
from classiq.interface.generator.functions.classical_type import QmodPyObject
|
13
13
|
from classiq.interface.helpers.custom_pydantic_types import PydanticProbabilityFloat
|
14
14
|
|
15
|
-
Solution =
|
15
|
+
Solution = tuple[int, ...]
|
16
16
|
|
17
17
|
|
18
18
|
class SolverResult(BaseModel):
|
@@ -34,13 +34,13 @@ class VQEIntermediateData(BaseModel):
|
|
34
34
|
iteration_number: pydantic.PositiveInt = pydantic.Field(
|
35
35
|
description="The iteration's number (evaluation count)"
|
36
36
|
)
|
37
|
-
parameters:
|
37
|
+
parameters: list[float] = pydantic.Field(
|
38
38
|
description="The optimizer parameters for the variational form"
|
39
39
|
)
|
40
40
|
mean_all_solutions: Optional[float] = pydantic.Field(
|
41
41
|
default=None, description="The mean score of all solutions in this iteration"
|
42
42
|
)
|
43
|
-
solutions:
|
43
|
+
solutions: list[SolutionData] = pydantic.Field(
|
44
44
|
description="Solutions found in this iteration, their score and"
|
45
45
|
"number of repetitions"
|
46
46
|
)
|
@@ -50,11 +50,11 @@ class VQEIntermediateData(BaseModel):
|
|
50
50
|
|
51
51
|
|
52
52
|
class VQESolverResult(SolverResult, QmodPyObject):
|
53
|
-
eigenstate:
|
54
|
-
reduced_probabilities: Optional[
|
53
|
+
eigenstate: dict[str, Complex]
|
54
|
+
reduced_probabilities: Optional[dict[str, float]] = None
|
55
55
|
optimized_circuit_sample_results: ExecutionDetails
|
56
|
-
intermediate_results:
|
57
|
-
optimal_parameters:
|
56
|
+
intermediate_results: list[VQEIntermediateData]
|
57
|
+
optimal_parameters: dict[str, float]
|
58
58
|
convergence_graph_str: str
|
59
59
|
num_solutions: Optional[int] = None
|
60
60
|
num_shots: int
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import Any,
|
1
|
+
from typing import Any, Optional, Union
|
2
2
|
|
3
3
|
import pydantic
|
4
4
|
from pydantic import ConfigDict
|
@@ -52,7 +52,7 @@ class FinanceFunctionInput(pydantic.BaseModel):
|
|
52
52
|
|
53
53
|
@pydantic.model_validator(mode="before")
|
54
54
|
@classmethod
|
55
|
-
def _convert_f_if_str(cls, values: Any, info: ValidationInfo) ->
|
55
|
+
def _convert_f_if_str(cls, values: Any, info: ValidationInfo) -> dict[str, Any]:
|
56
56
|
if isinstance(values, dict):
|
57
57
|
f = values.get("f")
|
58
58
|
elif isinstance(values, FinanceFunctionInput):
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import math
|
2
|
-
from typing import
|
2
|
+
from typing import Literal, Optional
|
3
3
|
|
4
4
|
import pydantic
|
5
5
|
|
@@ -18,15 +18,15 @@ class GaussianModelInput(FinanceModelInput):
|
|
18
18
|
normal_max_value: float = pydantic.Field(
|
19
19
|
description="Min/max value to truncate the " "latent normal random variable Z"
|
20
20
|
)
|
21
|
-
default_probabilities:
|
21
|
+
default_probabilities: list[PydanticProbabilityFloat] = pydantic.Field(
|
22
22
|
description="default probabilities for each asset"
|
23
23
|
)
|
24
24
|
|
25
|
-
rhos:
|
25
|
+
rhos: list[pydantic.PositiveFloat] = pydantic.Field(
|
26
26
|
description="Sensitivities of default probability of assets "
|
27
27
|
"with respect to Z (1/sigma(Z))"
|
28
28
|
)
|
29
|
-
loss:
|
29
|
+
loss: list[int] = pydantic.Field(
|
30
30
|
description="List of ints signifying loss per asset"
|
31
31
|
)
|
32
32
|
min_loss: Optional[int] = pydantic.Field(
|
@@ -38,7 +38,7 @@ class GaussianModelInput(FinanceModelInput):
|
|
38
38
|
return len(self.rhos)
|
39
39
|
|
40
40
|
@property
|
41
|
-
def distribution_range(self) ->
|
41
|
+
def distribution_range(self) -> tuple[float, float]:
|
42
42
|
return 0, sum(self.loss)
|
43
43
|
|
44
44
|
@property
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import Literal
|
1
|
+
from typing import Literal
|
2
2
|
|
3
3
|
import numpy as np
|
4
4
|
import pydantic
|
@@ -21,7 +21,7 @@ class LogNormalModelInput(FinanceModelInput):
|
|
21
21
|
)
|
22
22
|
|
23
23
|
@property
|
24
|
-
def distribution_range(self) ->
|
24
|
+
def distribution_range(self) -> tuple[float, float]:
|
25
25
|
mean = np.exp(self.mu + self.sigma**2 / 2)
|
26
26
|
variance = (np.exp(self.sigma**2) - 1) * np.exp(2 * self.mu + self.sigma**2)
|
27
27
|
stddev = np.sqrt(variance)
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import abc
|
2
|
-
from typing import Tuple
|
3
2
|
|
4
3
|
from pydantic import ConfigDict
|
5
4
|
|
@@ -19,5 +18,5 @@ class FinanceModelInput(HashablePydanticBaseModel):
|
|
19
18
|
|
20
19
|
@property
|
21
20
|
@abc.abstractmethod
|
22
|
-
def distribution_range(self) ->
|
21
|
+
def distribution_range(self) -> tuple[float, float]:
|
23
22
|
pass
|
@@ -1,9 +1,7 @@
|
|
1
|
-
from typing import List
|
2
|
-
|
3
1
|
import numpy as np
|
4
2
|
|
5
3
|
|
6
|
-
def get_rand_adjacency_matrix(num_vertices: int) ->
|
4
|
+
def get_rand_adjacency_matrix(num_vertices: int) -> list[list[bool]]:
|
7
5
|
adjacency_matrix = np.random.choice([0, 1], size=(num_vertices, num_vertices))
|
8
6
|
for i in range(num_vertices):
|
9
7
|
for j in range(num_vertices):
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import
|
1
|
+
from typing import Optional, Union
|
2
2
|
|
3
3
|
from pydantic import BaseModel
|
4
4
|
|
@@ -42,7 +42,7 @@ class EntanglingLayersArgs(CustomAnsatzArgs):
|
|
42
42
|
|
43
43
|
class RandomArgs(CustomAnsatzArgs):
|
44
44
|
gate_count: int = 100
|
45
|
-
gate_probabilities:
|
45
|
+
gate_probabilities: dict[str, float] = {"cx": 0.5, "u": 0.5}
|
46
46
|
random_seed: Optional[int] = None
|
47
47
|
|
48
48
|
|
@@ -52,11 +52,11 @@ class RandomTwoQubitGatesArgs(CustomAnsatzArgs):
|
|
52
52
|
|
53
53
|
|
54
54
|
class TwoLocalArgs(CustomAnsatzArgs):
|
55
|
-
rotation_blocks: Optional[Union[RotationBlocksType,
|
55
|
+
rotation_blocks: Optional[Union[RotationBlocksType, list[RotationBlocksType]]] = (
|
56
56
|
RotationBlocksType.ry
|
57
57
|
)
|
58
58
|
entanglement_blocks: Optional[
|
59
|
-
Union[EntanglementBlocksType,
|
59
|
+
Union[EntanglementBlocksType, list[EntanglementBlocksType]]
|
60
60
|
] = EntanglementBlocksType.cx
|
61
61
|
entanglement: EntanglementStructureType = EntanglementStructureType.full
|
62
62
|
reps: int = 3
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import
|
1
|
+
from typing import Union
|
2
2
|
|
3
3
|
from classiq.interface.generator.arith import number_utils
|
4
4
|
from classiq.interface.generator.arith.number_utils import (
|
@@ -45,7 +45,7 @@ def lower_bound(argument: RegisterOrConst) -> float:
|
|
45
45
|
return argument
|
46
46
|
|
47
47
|
|
48
|
-
def bounds(argument: RegisterOrConst) ->
|
48
|
+
def bounds(argument: RegisterOrConst) -> tuple[float, float]:
|
49
49
|
if isinstance(argument, RegisterArithmeticInfo):
|
50
50
|
return argument.bounds
|
51
51
|
return argument, argument
|
@@ -59,7 +59,7 @@ def limit_fraction_places(
|
|
59
59
|
return number_utils.limit_fraction_places(argument, machine_precision)
|
60
60
|
|
61
61
|
|
62
|
-
def arg_bounds_overlap(args:
|
62
|
+
def arg_bounds_overlap(args: tuple[RegisterOrConst, RegisterOrConst]) -> bool:
|
63
63
|
return (max(bounds(args[0])) - min(bounds(args[1]))) * (
|
64
64
|
min(bounds(args[0])) - max(bounds(args[1]))
|
65
65
|
) < 0
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import Any,
|
1
|
+
from typing import Any, Final, Optional
|
2
2
|
|
3
3
|
import networkx as nx
|
4
4
|
import pydantic
|
@@ -30,18 +30,18 @@ ARITHMETIC_EXPRESSION_GARBAGE_NAME: Final[str] = "expression_garbage"
|
|
30
30
|
|
31
31
|
class Arithmetic(ArithmeticExpressionABC):
|
32
32
|
target: Optional[RegisterArithmeticInfo] = None
|
33
|
-
inputs_to_save:
|
33
|
+
inputs_to_save: set[str] = pydantic.Field(default_factory=set)
|
34
34
|
|
35
35
|
@pydantic.field_validator("inputs_to_save")
|
36
36
|
@classmethod
|
37
37
|
def _validate_inputs_to_save(
|
38
|
-
cls, inputs_to_save:
|
39
|
-
) ->
|
38
|
+
cls, inputs_to_save: set[str], info: ValidationInfo
|
39
|
+
) -> set[str]:
|
40
40
|
assert all(reg in info.data.get("definitions", {}) for reg in inputs_to_save)
|
41
41
|
return inputs_to_save
|
42
42
|
|
43
43
|
@staticmethod
|
44
|
-
def _validate_expression_graph(graph: nx.DiGraph, values:
|
44
|
+
def _validate_expression_graph(graph: nx.DiGraph, values: dict[str, Any]) -> None:
|
45
45
|
target = values.get("target")
|
46
46
|
if target is None:
|
47
47
|
return
|
@@ -80,7 +80,7 @@ class Arithmetic(ArithmeticExpressionABC):
|
|
80
80
|
|
81
81
|
def get_arithmetic_params(
|
82
82
|
expr_str: str,
|
83
|
-
var_types:
|
83
|
+
var_types: dict[str, QuantumType],
|
84
84
|
machine_precision: int,
|
85
85
|
enable_target: bool = False,
|
86
86
|
) -> Arithmetic:
|
@@ -98,7 +98,7 @@ def get_arithmetic_params(
|
|
98
98
|
|
99
99
|
|
100
100
|
def compute_arithmetic_result_type(
|
101
|
-
expr_str: str, var_types:
|
101
|
+
expr_str: str, var_types: dict[str, QuantumType], machine_precision: int
|
102
102
|
) -> QuantumType:
|
103
103
|
arith_param = get_arithmetic_params(expr_str, var_types, machine_precision)
|
104
104
|
return register_info_to_quantum_type(
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import Callable
|
1
|
+
from typing import Callable
|
2
2
|
|
3
3
|
from typing_extensions import TypeAlias
|
4
4
|
|
@@ -6,15 +6,15 @@ from classiq.interface.exceptions import ClassiqArithmeticError
|
|
6
6
|
from classiq.interface.generator.arith import argument_utils
|
7
7
|
from classiq.interface.generator.arith.binary_ops import BOOLEAN_OP_WITH_FRACTIONS_ERROR
|
8
8
|
|
9
|
-
ArgTypeValidator: TypeAlias = Callable[[
|
9
|
+
ArgTypeValidator: TypeAlias = Callable[[list[argument_utils.RegisterOrConst]], None]
|
10
10
|
|
11
11
|
|
12
|
-
def _validate_bitwise_op_args(args:
|
12
|
+
def _validate_bitwise_op_args(args: list[argument_utils.RegisterOrConst]) -> None:
|
13
13
|
if any(argument_utils.fraction_places(arg) > 0 for arg in args):
|
14
14
|
raise ClassiqArithmeticError(BOOLEAN_OP_WITH_FRACTIONS_ERROR)
|
15
15
|
|
16
16
|
|
17
|
-
arg_type_validator_map:
|
17
|
+
arg_type_validator_map: dict[str, ArgTypeValidator] = dict(
|
18
18
|
BitXor=_validate_bitwise_op_args,
|
19
19
|
BitAnd=_validate_bitwise_op_args,
|
20
20
|
BitOr=_validate_bitwise_op_args,
|
@@ -23,7 +23,7 @@ arg_type_validator_map: Dict[str, ArgTypeValidator] = dict(
|
|
23
23
|
|
24
24
|
def validate_operation_arg_types(
|
25
25
|
operation: str,
|
26
|
-
arguments:
|
26
|
+
arguments: list[argument_utils.RegisterOrConst],
|
27
27
|
machine_precision: int,
|
28
28
|
) -> None:
|
29
29
|
if operation not in arg_type_validator_map:
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import abc
|
2
2
|
import ast
|
3
3
|
import re
|
4
|
-
from typing import Any,
|
4
|
+
from typing import Any, Optional, Union
|
5
5
|
|
6
6
|
import networkx as nx
|
7
7
|
import pydantic
|
@@ -41,16 +41,16 @@ class ArithmeticExpressionABC(abc.ABC, FunctionParams):
|
|
41
41
|
uncomputation_method: UncomputationMethods = UncomputationMethods.optimized
|
42
42
|
machine_precision: pydantic.NonNegativeInt = DEFAULT_MACHINE_PRECISION
|
43
43
|
expression: PydanticExpressionStr
|
44
|
-
definitions:
|
44
|
+
definitions: dict[str, ValidDefinitions]
|
45
45
|
qubit_count: Optional[pydantic.NonNegativeInt] = None
|
46
46
|
|
47
|
-
def _get_literal_set(self) ->
|
47
|
+
def _get_literal_set(self) -> set[str]:
|
48
48
|
return _extract_literals(self.expression)
|
49
49
|
|
50
50
|
@classmethod
|
51
51
|
def _validate_expression_literals_and_definitions(
|
52
|
-
cls, definitions:
|
53
|
-
) ->
|
52
|
+
cls, definitions: dict[str, ValidDefinitions], expression: PydanticExpressionStr
|
53
|
+
) -> dict[str, ValidDefinitions]:
|
54
54
|
literals = _extract_literals(expression)
|
55
55
|
|
56
56
|
forbidden = literals.intersection(FORBIDDEN_LITERALS)
|
@@ -69,13 +69,13 @@ class ArithmeticExpressionABC(abc.ABC, FunctionParams):
|
|
69
69
|
|
70
70
|
@pydantic.model_validator(mode="before")
|
71
71
|
@classmethod
|
72
|
-
def _validate_expression(cls, values: Any) ->
|
72
|
+
def _validate_expression(cls, values: Any) -> dict[str, Any]:
|
73
73
|
if not isinstance(values, dict):
|
74
74
|
return values
|
75
75
|
expression_adapter: TypeAdapter = TypeAdapter(Optional[PydanticExpressionStr])
|
76
76
|
expression = expression_adapter.validate_python(values.get("expression"))
|
77
77
|
definitions_adapter: TypeAdapter = TypeAdapter(
|
78
|
-
Optional[
|
78
|
+
Optional[dict[str, ValidDefinitions]]
|
79
79
|
)
|
80
80
|
definition_dict = values.get("definitions")
|
81
81
|
if (
|
@@ -148,16 +148,16 @@ class ArithmeticExpressionABC(abc.ABC, FunctionParams):
|
|
148
148
|
pass
|
149
149
|
|
150
150
|
@staticmethod
|
151
|
-
def _validate_expression_graph(graph: nx.DiGraph, values:
|
151
|
+
def _validate_expression_graph(graph: nx.DiGraph, values: dict[str, Any]) -> None:
|
152
152
|
pass
|
153
153
|
|
154
154
|
@classmethod
|
155
155
|
def _replace_const_definitions_in_expression(
|
156
156
|
cls,
|
157
157
|
expression: str,
|
158
|
-
definitions:
|
158
|
+
definitions: dict[str, ValidDefinitions],
|
159
159
|
machine_precision: int,
|
160
|
-
) ->
|
160
|
+
) -> tuple[str, dict[str, RegisterArithmeticInfo]]:
|
161
161
|
new_definitions = dict()
|
162
162
|
for var_name, value in definitions.items():
|
163
163
|
if isinstance(value, RegisterArithmeticInfo):
|
@@ -182,5 +182,5 @@ class ArithmeticExpressionABC(abc.ABC, FunctionParams):
|
|
182
182
|
return re.sub(r"\b" + var + r"\b", str(value), expression)
|
183
183
|
|
184
184
|
|
185
|
-
def _extract_literals(expression: str) ->
|
185
|
+
def _extract_literals(expression: str) -> set[str]:
|
186
186
|
return set(re.findall(SUPPORTED_VAR_NAMES_REG, expression)) - SUPPORTED_FUNC_NAMES
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import ast
|
2
2
|
from _ast import AST
|
3
|
-
from
|
3
|
+
from collections.abc import Collection
|
4
|
+
from typing import Optional, Union, cast
|
4
5
|
|
5
6
|
import networkx as nx
|
6
7
|
|
@@ -28,9 +29,9 @@ Node = Union[str, float, int]
|
|
28
29
|
class ExpressionVisitor(ExpressionValidator):
|
29
30
|
def __init__(
|
30
31
|
self,
|
31
|
-
supported_nodes:
|
32
|
+
supported_nodes: tuple[type[AST], ...],
|
32
33
|
expression_type: str = DEFAULT_EXPRESSION_TYPE,
|
33
|
-
supported_functions: Optional[
|
34
|
+
supported_functions: Optional[set[str]] = None,
|
34
35
|
) -> None:
|
35
36
|
super().__init__(supported_nodes, expression_type, supported_functions)
|
36
37
|
self.graph = nx.DiGraph()
|
@@ -90,8 +91,8 @@ class ExpressionVisitor(ExpressionValidator):
|
|
90
91
|
class InDegreeLimiter:
|
91
92
|
@staticmethod
|
92
93
|
def _sort_in_edges(
|
93
|
-
in_edges: Collection[
|
94
|
-
) ->
|
94
|
+
in_edges: Collection[tuple[Node, str]]
|
95
|
+
) -> list[tuple[Node, str]]:
|
95
96
|
return sorted(
|
96
97
|
in_edges,
|
97
98
|
key=lambda edge_tuple: isinstance(edge_tuple[0], str), # vars before consts
|
@@ -134,9 +135,9 @@ class InDegreeLimiter:
|
|
134
135
|
def parse_expression(
|
135
136
|
expression: str,
|
136
137
|
*,
|
137
|
-
supported_nodes:
|
138
|
+
supported_nodes: tuple[type[AST], ...] = DEFAULT_SUPPORTED_NODE_TYPES,
|
138
139
|
expression_type: str = DEFAULT_EXPRESSION_TYPE,
|
139
|
-
supported_functions: Optional[
|
140
|
+
supported_functions: Optional[set[str]] = None,
|
140
141
|
) -> nx.DiGraph:
|
141
142
|
supported_functions = supported_functions or DEFAULT_SUPPORTED_FUNC_NAMES
|
142
143
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import ast
|
2
2
|
import re
|
3
3
|
from _ast import AST
|
4
|
-
from typing import Any,
|
4
|
+
from typing import Any, Optional, Union
|
5
5
|
|
6
6
|
from typing_extensions import TypeAlias, get_args
|
7
7
|
|
@@ -11,7 +11,7 @@ from classiq.interface.generator.expressions.sympy_supported_expressions import
|
|
11
11
|
SYMPY_SUPPORTED_EXPRESSIONS,
|
12
12
|
)
|
13
13
|
|
14
|
-
DEFAULT_SUPPORTED_FUNC_NAMES:
|
14
|
+
DEFAULT_SUPPORTED_FUNC_NAMES: set[str] = {"CLShift", "CRShift", "min", "max"}
|
15
15
|
|
16
16
|
DEFAULT_EXPRESSION_TYPE = "arithmetic"
|
17
17
|
IDENITIFIER_REGEX = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*")
|
@@ -19,7 +19,7 @@ IDENITIFIER_REGEX = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*")
|
|
19
19
|
_REPEATED_VARIABLES_ERROR_MESSAGE: str = (
|
20
20
|
"Repeated variables in the beginning of an arithmetic expression are not allowed."
|
21
21
|
)
|
22
|
-
ValidKeyValuePairs: TypeAlias =
|
22
|
+
ValidKeyValuePairs: TypeAlias = dict[str, set[str]]
|
23
23
|
|
24
24
|
SupportedNodesTypes = Union[
|
25
25
|
ast.Expression,
|
@@ -68,9 +68,9 @@ def _is_constant(expr: str) -> bool:
|
|
68
68
|
class ExpressionValidator(ast.NodeVisitor):
|
69
69
|
def __init__(
|
70
70
|
self,
|
71
|
-
supported_nodes:
|
71
|
+
supported_nodes: tuple[type[AST], ...],
|
72
72
|
expression_type: str = DEFAULT_EXPRESSION_TYPE,
|
73
|
-
supported_functions: Optional[
|
73
|
+
supported_functions: Optional[set[str]] = None,
|
74
74
|
mode: str = "eval",
|
75
75
|
) -> None:
|
76
76
|
super().__init__()
|
@@ -108,7 +108,7 @@ class ExpressionValidator(ast.NodeVisitor):
|
|
108
108
|
return self._ast_obj
|
109
109
|
|
110
110
|
@staticmethod
|
111
|
-
def _check_repeated_variables(variables:
|
111
|
+
def _check_repeated_variables(variables: tuple[Any, Any]) -> None:
|
112
112
|
if (
|
113
113
|
all(isinstance(var, ast.Name) for var in variables)
|
114
114
|
and variables[0].id == variables[1].id
|
@@ -186,9 +186,9 @@ class ExpressionValidator(ast.NodeVisitor):
|
|
186
186
|
def validate_expression(
|
187
187
|
expression: str,
|
188
188
|
*,
|
189
|
-
supported_nodes:
|
189
|
+
supported_nodes: tuple[type[AST], ...] = DEFAULT_SUPPORTED_NODE_TYPES,
|
190
190
|
expression_type: str = DEFAULT_EXPRESSION_TYPE,
|
191
|
-
supported_functions: Optional[
|
191
|
+
supported_functions: Optional[set[str]] = None,
|
192
192
|
mode: str = "eval",
|
193
193
|
) -> ast.AST:
|
194
194
|
supported_functions = supported_functions or set(SYMPY_SUPPORTED_EXPRESSIONS).union(
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import abc
|
2
|
-
from
|
2
|
+
from collections.abc import Iterable
|
3
|
+
from typing import ClassVar, Final, Optional
|
3
4
|
|
4
5
|
import pydantic
|
5
6
|
|
@@ -43,8 +44,8 @@ class ArithmeticOperationParams(FunctionParams):
|
|
43
44
|
return self.output_size is None
|
44
45
|
|
45
46
|
def _legal_bounds(
|
46
|
-
self, suggested_bounds:
|
47
|
-
) -> Optional[
|
47
|
+
self, suggested_bounds: tuple[float, float], max_bounds: tuple[float, float]
|
48
|
+
) -> Optional[tuple[float, float]]:
|
48
49
|
if self.output_size is None or (
|
49
50
|
suggested_bounds[0] >= max_bounds[0]
|
50
51
|
and suggested_bounds[1] <= max_bounds[1]
|