classiq 0.38.0__py3-none-any.whl → 0.39.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 +21 -22
- classiq/_internals/api_wrapper.py +13 -1
- classiq/_internals/client.py +12 -2
- classiq/analyzer/analyzer.py +3 -1
- classiq/applications/__init__.py +1 -8
- classiq/applications/chemistry/__init__.py +6 -0
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/arithmetic/arithmetic_expression.py +1 -1
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/combinatorial_problem_utils.py +26 -6
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/encoding_mapping.py +1 -1
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/encoding_utils.py +1 -1
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/memory.py +2 -4
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/optimization_model.py +6 -10
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/pauli_helpers/pauli_utils.py +10 -0
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/pyomo_utils.py +4 -2
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/encoding.py +3 -10
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/fixed_variables.py +4 -6
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/ising_converter.py +3 -5
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/penalty_support.py +3 -7
- classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/slack_variables.py +4 -6
- classiq/applications/combinatorial_optimization/__init__.py +9 -3
- classiq/{applications_model_constructors → applications/combinatorial_optimization}/combinatorial_optimization_model_constructor.py +7 -8
- classiq/applications/finance/__init__.py +3 -2
- classiq/{applications_model_constructors → applications/finance}/finance_model_constructor.py +24 -14
- classiq/applications/grover/__init__.py +11 -0
- classiq/applications/libraries/qmci_library.py +35 -0
- classiq/applications/qsvm/__init__.py +5 -1
- classiq/execution/all_hardware_devices.py +13 -0
- classiq/executor.py +2 -1
- classiq/interface/_version.py +1 -1
- classiq/interface/analyzer/result.py +1 -5
- classiq/interface/applications/qsvm.py +4 -2
- classiq/interface/ast_node.py +23 -0
- classiq/interface/combinatorial_optimization/examples/mht.py +8 -3
- classiq/interface/executor/execution_request.py +2 -37
- classiq/interface/executor/vqe_result.py +1 -1
- classiq/interface/generator/builtin_api_builder.py +0 -5
- classiq/interface/generator/constant.py +2 -3
- classiq/interface/generator/expressions/expression.py +2 -4
- classiq/interface/generator/expressions/qmod_qscalar_proxy.py +1 -1
- classiq/interface/generator/functions/__init__.py +2 -2
- classiq/interface/generator/functions/builtins/__init__.py +15 -0
- classiq/interface/generator/functions/builtins/core_library/__init__.py +14 -0
- classiq/interface/generator/functions/builtins/internal_operators.py +62 -0
- classiq/interface/generator/functions/{core_lib_declarations/quantum_functions/std_lib_functions.py → builtins/open_lib_functions.py} +109 -83
- classiq/interface/generator/functions/builtins/quantum_operators.py +37 -0
- classiq/interface/generator/functions/classical_type.py +2 -4
- classiq/interface/generator/functions/function_declaration.py +2 -2
- classiq/interface/generator/hartree_fock.py +10 -2
- classiq/interface/generator/model/classical_main_validator.py +1 -1
- classiq/interface/generator/model/model.py +1 -1
- classiq/interface/generator/quantum_function_call.py +1 -1
- classiq/interface/generator/types/struct_declaration.py +2 -4
- classiq/interface/model/call_synthesis_data.py +3 -3
- classiq/interface/model/classical_if.py +13 -0
- classiq/interface/model/classical_parameter_declaration.py +2 -3
- classiq/interface/model/control.py +16 -0
- classiq/interface/model/handle_binding.py +3 -2
- classiq/interface/model/invert.py +10 -0
- classiq/interface/model/model.py +2 -1
- classiq/interface/model/power.py +12 -0
- classiq/interface/model/quantum_function_call.py +9 -4
- classiq/interface/model/quantum_if_operation.py +3 -3
- classiq/interface/model/quantum_lambda_function.py +3 -9
- classiq/interface/model/quantum_statement.py +3 -2
- classiq/interface/model/quantum_type.py +2 -4
- classiq/interface/model/quantum_variable_declaration.py +2 -2
- classiq/interface/model/repeat.py +13 -0
- classiq/interface/model/resolvers/function_call_resolver.py +26 -0
- classiq/interface/model/statement_block.py +21 -4
- classiq/interface/model/validations/handles_validator.py +8 -12
- classiq/interface/model/within_apply_operation.py +4 -4
- classiq/interface/server/routes.py +0 -4
- classiq/qmod/builtins/classical_functions.py +9 -9
- classiq/qmod/builtins/functions.py +153 -226
- classiq/qmod/builtins/operations.py +160 -13
- classiq/qmod/native/pretty_printer.py +49 -20
- classiq/qmod/qmod_constant.py +26 -2
- classiq/qmod/qmod_parameter.py +2 -1
- classiq/qmod/qmod_variable.py +48 -15
- classiq/qmod/quantum_callable.py +1 -0
- classiq/qmod/quantum_expandable.py +6 -7
- classiq/qmod/quantum_function.py +4 -0
- classiq/qmod/symbolic.py +2 -2
- {classiq-0.38.0.dist-info → classiq-0.39.0.dist-info}/METADATA +1 -1
- {classiq-0.38.0.dist-info → classiq-0.39.0.dist-info}/RECORD +107 -124
- classiq/applications/benchmarking/__init__.py +0 -9
- classiq/applications/benchmarking/mirror_benchmarking.py +0 -70
- classiq/applications/numpy_utils.py +0 -37
- classiq/applications_model_constructors/__init__.py +0 -25
- classiq/applications_model_constructors/combinatorial_helpers/multiple_comp_basis_sp.py +0 -34
- classiq/applications_model_constructors/libraries/qmci_library.py +0 -107
- classiq/builtin_functions/__init__.py +0 -43
- classiq/builtin_functions/amplitude_loading.py +0 -3
- classiq/builtin_functions/binary_ops.py +0 -1
- classiq/builtin_functions/exponentiation.py +0 -5
- classiq/builtin_functions/qpe.py +0 -4
- classiq/builtin_functions/qsvm.py +0 -7
- classiq/builtin_functions/range_types.py +0 -5
- classiq/builtin_functions/standard_gates.py +0 -1
- classiq/builtin_functions/state_preparation.py +0 -6
- classiq/builtin_functions/suzuki_trotter.py +0 -3
- classiq/interface/generator/functions/core_lib_declarations/quantum_functions/__init__.py +0 -18
- classiq/interface/generator/functions/core_lib_declarations/quantum_operators.py +0 -95
- classiq/model/__init__.py +0 -14
- classiq/model/composite_function_generator.py +0 -33
- classiq/model/function_handler.py +0 -462
- classiq/model/logic_flow.py +0 -149
- classiq/model/logic_flow_change_handler.py +0 -71
- classiq/model/model.py +0 -229
- classiq/quantum_functions/__init__.py +0 -17
- classiq/quantum_functions/annotation_parser.py +0 -205
- classiq/quantum_functions/decorators.py +0 -22
- classiq/quantum_functions/function_library.py +0 -181
- classiq/quantum_functions/function_parser.py +0 -74
- classiq/quantum_functions/quantum_function.py +0 -236
- /classiq/{applications_model_constructors → applications/chemistry}/chemistry_model_constructor.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/__init__.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/allowed_constraints.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/arithmetic/__init__.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/arithmetic/isolation.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/pauli_helpers/__init__.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/pauli_helpers/pauli_sparsing.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/py.typed +0 -0
- /classiq/{applications_model_constructors/combinatorial_helpers/transformations → applications/combinatorial_helpers/solvers}/__init__.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/sympy_utils.py +0 -0
- /classiq/{applications_model_constructors/libraries → applications/combinatorial_helpers/transformations}/__init__.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/penalty.py +0 -0
- /classiq/{applications_model_constructors → applications}/combinatorial_helpers/transformations/sign_seperation.py +0 -0
- /classiq/{applications_model_constructors → applications/grover}/grover_model_constructor.py +0 -0
- /classiq/{interface/generator/functions/core_lib_declarations → applications/libraries}/__init__.py +0 -0
- /classiq/{applications_model_constructors → applications}/libraries/ampltitude_estimation_library.py +0 -0
- /classiq/{applications_model_constructors → applications/qsvm}/qsvm_model_constructor.py +0 -0
- /classiq/interface/generator/functions/{core_lib_declarations/quantum_functions → builtins/core_library}/atomic_quantum_functions.py +0 -0
- /classiq/interface/generator/functions/{core_lib_declarations/quantum_functions → builtins/core_library}/chemistry_functions.py +0 -0
- /classiq/interface/generator/functions/{core_lib_declarations/quantum_functions → builtins/core_library}/exponentiation_functions.py +0 -0
- /classiq/{quantum_register.py → interface/model/quantum_register.py} +0 -0
- {classiq-0.38.0.dist-info → classiq-0.39.0.dist-info}/WHEEL +0 -0
classiq/model/model.py
DELETED
@@ -1,229 +0,0 @@
|
|
1
|
-
"""Model module, implementing facilities for designing models and generating circuits using Classiq platform."""
|
2
|
-
|
3
|
-
from __future__ import annotations
|
4
|
-
|
5
|
-
import logging
|
6
|
-
from typing import Any, Dict, List, Mapping, Optional, cast
|
7
|
-
|
8
|
-
from classiq.interface.chemistry.operator import PauliOperator
|
9
|
-
from classiq.interface.executor.execution_preferences import (
|
10
|
-
ExecutionPreferences,
|
11
|
-
QaeWithQpeEstimationMethod,
|
12
|
-
)
|
13
|
-
from classiq.interface.generator.expressions.enums import Optimizer
|
14
|
-
from classiq.interface.generator.function_params import IOName
|
15
|
-
from classiq.interface.generator.functions import SynthesisNativeFunctionDefinition
|
16
|
-
from classiq.interface.generator.model import (
|
17
|
-
Constraints,
|
18
|
-
Preferences,
|
19
|
-
SynthesisModel as APIModel,
|
20
|
-
)
|
21
|
-
from classiq.interface.generator.model.model import MAIN_FUNCTION_NAME, SerializedModel
|
22
|
-
from classiq.interface.generator.quantum_function_call import (
|
23
|
-
SynthesisQuantumFunctionCall,
|
24
|
-
)
|
25
|
-
|
26
|
-
from classiq.exceptions import ClassiqError, ClassiqValueError
|
27
|
-
from classiq.model import function_handler
|
28
|
-
from classiq.quantum_functions.function_library import FunctionLibrary
|
29
|
-
from classiq.quantum_register import QReg, QRegGenericAlias
|
30
|
-
|
31
|
-
_logger = logging.getLogger(__name__)
|
32
|
-
|
33
|
-
# TODO: Add docstrings for auto generated methods.
|
34
|
-
|
35
|
-
|
36
|
-
ILLEGAL_SETTING_MSG = "Illegal value type provided"
|
37
|
-
|
38
|
-
|
39
|
-
def _pauli_str_to_enums(pauli_str: str) -> str:
|
40
|
-
return ", ".join(f"Pauli.{pauli_term}" for pauli_term in pauli_str)
|
41
|
-
|
42
|
-
|
43
|
-
def _pauli_operator_to_qmod(hamiltonian: PauliOperator) -> str:
|
44
|
-
if not all(isinstance(summand[1], complex) for summand in hamiltonian.pauli_list):
|
45
|
-
raise ClassiqValueError(
|
46
|
-
"Supporting only Hamiltonian with numeric coefficients."
|
47
|
-
)
|
48
|
-
return ", ".join(
|
49
|
-
f"struct_literal(PauliTerm, pauli=[{_pauli_str_to_enums(pauli)}], coefficient={cast(complex, coeff).real})"
|
50
|
-
for pauli, coeff in hamiltonian.pauli_list
|
51
|
-
)
|
52
|
-
|
53
|
-
|
54
|
-
DEFAULT_RESULT_NAME = "result"
|
55
|
-
DEFAULT_AMPLITUDE_ESTIMATION_RESULT_NAME = "estimation"
|
56
|
-
|
57
|
-
|
58
|
-
class Model(function_handler.FunctionHandler):
|
59
|
-
"""Facility to generate circuits, based on the model."""
|
60
|
-
|
61
|
-
def __init__(self, **kwargs: Any) -> None:
|
62
|
-
"""Init self."""
|
63
|
-
super().__init__()
|
64
|
-
self._model = APIModel(**kwargs)
|
65
|
-
|
66
|
-
@classmethod
|
67
|
-
def from_model(cls, model: APIModel) -> Model:
|
68
|
-
return cls(**dict(model))
|
69
|
-
|
70
|
-
@property
|
71
|
-
def _body(
|
72
|
-
self,
|
73
|
-
) -> List[SynthesisQuantumFunctionCall]:
|
74
|
-
return self._model.body
|
75
|
-
|
76
|
-
@property
|
77
|
-
def constraints(self) -> Constraints:
|
78
|
-
"""Get the constraints aggregated in self.
|
79
|
-
|
80
|
-
Returns:
|
81
|
-
The constraints data.
|
82
|
-
"""
|
83
|
-
return self._model.constraints
|
84
|
-
|
85
|
-
@constraints.setter
|
86
|
-
def constraints(self, value: Any) -> None:
|
87
|
-
if not isinstance(value, Constraints):
|
88
|
-
raise ClassiqError(ILLEGAL_SETTING_MSG)
|
89
|
-
self._model.constraints = value
|
90
|
-
|
91
|
-
@property
|
92
|
-
def preferences(self) -> Preferences:
|
93
|
-
"""Get the preferences aggregated in self.
|
94
|
-
|
95
|
-
Returns:
|
96
|
-
The preferences data.
|
97
|
-
"""
|
98
|
-
return self._model.preferences
|
99
|
-
|
100
|
-
@preferences.setter
|
101
|
-
def preferences(self, value: Any) -> None:
|
102
|
-
if not isinstance(value, Preferences):
|
103
|
-
raise ClassiqError(ILLEGAL_SETTING_MSG)
|
104
|
-
self._model.preferences = value
|
105
|
-
|
106
|
-
@property
|
107
|
-
def execution_preferences(self) -> ExecutionPreferences:
|
108
|
-
return self._model.execution_preferences
|
109
|
-
|
110
|
-
@execution_preferences.setter
|
111
|
-
def execution_preferences(self, value: Any) -> None:
|
112
|
-
if not isinstance(value, ExecutionPreferences):
|
113
|
-
raise ClassiqError(ILLEGAL_SETTING_MSG)
|
114
|
-
self._model.execution_preferences = value
|
115
|
-
|
116
|
-
def create_inputs(
|
117
|
-
self, inputs: Mapping[IOName, QRegGenericAlias]
|
118
|
-
) -> Dict[IOName, QReg]:
|
119
|
-
qregs = super().create_inputs(inputs=inputs)
|
120
|
-
self._model.set_inputs(inputs, self.input_wires)
|
121
|
-
return qregs
|
122
|
-
|
123
|
-
def set_outputs(self, outputs: Mapping[IOName, QReg]) -> None:
|
124
|
-
super().set_outputs(outputs=outputs)
|
125
|
-
self._model.set_outputs(outputs, self.output_wires)
|
126
|
-
|
127
|
-
def include_library(self, library: FunctionLibrary) -> None:
|
128
|
-
"""Includes a user-defined custom function library.
|
129
|
-
|
130
|
-
Args:
|
131
|
-
library (FunctionLibrary): The custom function library.
|
132
|
-
"""
|
133
|
-
super().include_library(library=library)
|
134
|
-
# It is important that the .functions list is shared between the library and
|
135
|
-
# the model, as it is modified in-place
|
136
|
-
self._model.functions = library._data
|
137
|
-
library.remove_function_definition(MAIN_FUNCTION_NAME)
|
138
|
-
self._model.functions.append(
|
139
|
-
SynthesisNativeFunctionDefinition(name=MAIN_FUNCTION_NAME)
|
140
|
-
)
|
141
|
-
|
142
|
-
def get_model(self) -> SerializedModel:
|
143
|
-
return self._model.get_model()
|
144
|
-
|
145
|
-
def create_library(self) -> None:
|
146
|
-
self._function_library = FunctionLibrary(*self._model.functions)
|
147
|
-
self._model.functions = self._function_library._data
|
148
|
-
|
149
|
-
def sample(
|
150
|
-
self,
|
151
|
-
execution_params: Optional[Dict[str, float]] = None,
|
152
|
-
) -> None:
|
153
|
-
execution_params = execution_params or dict()
|
154
|
-
|
155
|
-
self._model.classical_execution_code += classical_sample_function(
|
156
|
-
execution_params=execution_params
|
157
|
-
)
|
158
|
-
|
159
|
-
def vqe(
|
160
|
-
self,
|
161
|
-
hamiltonian: PauliOperator,
|
162
|
-
maximize: bool,
|
163
|
-
optimizer: Optimizer,
|
164
|
-
max_iteration: int,
|
165
|
-
initial_point: Optional[List[int]] = None,
|
166
|
-
tolerance: float = 0,
|
167
|
-
step_size: float = 0,
|
168
|
-
skip_compute_variance: bool = False,
|
169
|
-
alpha_cvar: float = 1,
|
170
|
-
) -> None:
|
171
|
-
initial_point = initial_point or []
|
172
|
-
vqe_classical_code = f"""
|
173
|
-
{DEFAULT_RESULT_NAME} = vqe(
|
174
|
-
hamiltonian=[{_pauli_operator_to_qmod(hamiltonian)}],
|
175
|
-
maximize={maximize},
|
176
|
-
initial_point={initial_point},
|
177
|
-
optimizer=Optimizer.{optimizer.name},
|
178
|
-
max_iteration={max_iteration},
|
179
|
-
tolerance={tolerance},
|
180
|
-
step_size={step_size},
|
181
|
-
skip_compute_variance={skip_compute_variance},
|
182
|
-
alpha_cvar={alpha_cvar}
|
183
|
-
)
|
184
|
-
save({{{DEFAULT_RESULT_NAME!r}: {DEFAULT_RESULT_NAME}}})
|
185
|
-
"""
|
186
|
-
|
187
|
-
self._model.classical_execution_code += vqe_classical_code
|
188
|
-
|
189
|
-
def iqae(
|
190
|
-
self,
|
191
|
-
epsilon: float,
|
192
|
-
alpha: float,
|
193
|
-
execution_params: Optional[Dict[str, float]] = None,
|
194
|
-
) -> None:
|
195
|
-
execution_params = execution_params or {}
|
196
|
-
|
197
|
-
iqae_classical_code = f"""
|
198
|
-
{DEFAULT_RESULT_NAME} = iqae(
|
199
|
-
epsilon={epsilon},
|
200
|
-
alpha={alpha},
|
201
|
-
execution_params={execution_params}
|
202
|
-
)
|
203
|
-
save({{{DEFAULT_RESULT_NAME!r}: {DEFAULT_RESULT_NAME}}})
|
204
|
-
"""
|
205
|
-
|
206
|
-
self._model.classical_execution_code += iqae_classical_code
|
207
|
-
|
208
|
-
def post_process_amplitude_estimation(
|
209
|
-
self,
|
210
|
-
estimation_register_size: int,
|
211
|
-
estimation_method: QaeWithQpeEstimationMethod,
|
212
|
-
) -> None:
|
213
|
-
postprocess_classical_code = f"""
|
214
|
-
{DEFAULT_AMPLITUDE_ESTIMATION_RESULT_NAME} = qae_with_qpe_result_post_processing(
|
215
|
-
{estimation_register_size},
|
216
|
-
{estimation_method},
|
217
|
-
{DEFAULT_RESULT_NAME}
|
218
|
-
)
|
219
|
-
save({{{DEFAULT_AMPLITUDE_ESTIMATION_RESULT_NAME!r}: {DEFAULT_AMPLITUDE_ESTIMATION_RESULT_NAME}}})
|
220
|
-
"""
|
221
|
-
|
222
|
-
self._model.classical_execution_code += postprocess_classical_code
|
223
|
-
|
224
|
-
|
225
|
-
def classical_sample_function(execution_params: Dict[str, float]) -> str:
|
226
|
-
return f"""
|
227
|
-
{DEFAULT_RESULT_NAME} = sample({execution_params})
|
228
|
-
save({{{DEFAULT_RESULT_NAME!r}: {DEFAULT_RESULT_NAME}}})
|
229
|
-
"""
|
@@ -1,17 +0,0 @@
|
|
1
|
-
from classiq.quantum_functions.decorators import quantum_function
|
2
|
-
from classiq.quantum_functions.function_library import (
|
3
|
-
QASM3_INTRO,
|
4
|
-
QASM_INTRO,
|
5
|
-
FunctionLibrary,
|
6
|
-
QuantumFunction,
|
7
|
-
QuantumFunctionFactory,
|
8
|
-
)
|
9
|
-
|
10
|
-
__all__ = [
|
11
|
-
"quantum_function",
|
12
|
-
"QASM_INTRO",
|
13
|
-
"QASM3_INTRO",
|
14
|
-
"FunctionLibrary",
|
15
|
-
"QuantumFunction",
|
16
|
-
"QuantumFunctionFactory",
|
17
|
-
]
|
@@ -1,205 +0,0 @@
|
|
1
|
-
# type: ignore # noqa: PGH003
|
2
|
-
# We can either ignore each line individually, or ignore the entire file and wait until mypy can ignore
|
3
|
-
# specific errors per-file.
|
4
|
-
import inspect
|
5
|
-
import sys
|
6
|
-
from types import FunctionType
|
7
|
-
from typing import Any, Dict, List, Tuple, Union, _GenericAlias
|
8
|
-
|
9
|
-
from classiq.interface.generator.register_role import RegisterRole as Role
|
10
|
-
|
11
|
-
from classiq.exceptions import ClassiqQFuncError
|
12
|
-
from classiq.quantum_register import AuxQReg, QReg, QRegGenericAlias, QSFixed, ZeroQReg
|
13
|
-
|
14
|
-
if sys.version_info >= (3, 9):
|
15
|
-
from types import GenericAlias
|
16
|
-
else:
|
17
|
-
GenericAlias = _GenericAlias
|
18
|
-
|
19
|
-
GenericAliasUnion = Union[GenericAlias, _GenericAlias]
|
20
|
-
|
21
|
-
|
22
|
-
class AnnotationParser:
|
23
|
-
def __init__(self, func: FunctionType) -> None:
|
24
|
-
self._func = func
|
25
|
-
|
26
|
-
self.output_types: Dict[str, GenericAlias] = {}
|
27
|
-
|
28
|
-
def parse(self) -> None:
|
29
|
-
annotations = self._func.__annotations__.copy()
|
30
|
-
|
31
|
-
# Todo: remove this `if` after introducing `Inplace`
|
32
|
-
if "return" not in annotations:
|
33
|
-
raise ClassiqQFuncError("Return value annotations not found")
|
34
|
-
|
35
|
-
self.output_values = self._unpack_output_values(annotations.pop("return"))
|
36
|
-
self.input_names, self.input_values = self._unpack_input_values(annotations)
|
37
|
-
|
38
|
-
self._validate()
|
39
|
-
|
40
|
-
def _validate(self) -> None:
|
41
|
-
self._validate_type_hints()
|
42
|
-
self._validate_qubit_amount()
|
43
|
-
self._validate_io_length()
|
44
|
-
self._validate_io_correlation()
|
45
|
-
|
46
|
-
def _validate_type_hints(self) -> None:
|
47
|
-
# Validate type of type-hints
|
48
|
-
if not all(
|
49
|
-
map(
|
50
|
-
self.is_valid_generic_alias_of_qreg,
|
51
|
-
self.input_values + self.output_values,
|
52
|
-
)
|
53
|
-
):
|
54
|
-
raise ClassiqQFuncError("Invalid GenericAlias convection")
|
55
|
-
|
56
|
-
def _validate_qubit_amount(self) -> None:
|
57
|
-
# Validate qubit amount
|
58
|
-
if sum(i.size for i in self.output_values) != sum(
|
59
|
-
i.size for i in self.input_values
|
60
|
-
):
|
61
|
-
raise ClassiqQFuncError(
|
62
|
-
"Input and output values have different amounts of qubits"
|
63
|
-
)
|
64
|
-
|
65
|
-
# Todo: Remove this validation by introducing better heuristics
|
66
|
-
# Or after introducing Inplace
|
67
|
-
def _validate_io_length(self) -> None:
|
68
|
-
# Validate amount of inputs and outputs
|
69
|
-
if len(self.input_values) != len(self.output_values):
|
70
|
-
raise ClassiqQFuncError(
|
71
|
-
"Inputs and outputs must have the same number of QRegs"
|
72
|
-
)
|
73
|
-
|
74
|
-
def _validate_io_correlation(self) -> None:
|
75
|
-
# Validate correspondence between inputs and outputs
|
76
|
-
for input_name, input_type, output_type in zip(
|
77
|
-
self.input_names, self.input_values, self.output_values
|
78
|
-
):
|
79
|
-
# Is arithmetic QReg
|
80
|
-
if issubclass(input_type.__origin__, QSFixed) and input_type != output_type:
|
81
|
-
raise ClassiqQFuncError(
|
82
|
-
f"Arithmetic QReg must be of the same type and size in both the input and the output. Got {input_type} and {output_type}"
|
83
|
-
)
|
84
|
-
|
85
|
-
# Is Auxillary QReg
|
86
|
-
if issubclass(input_type.__origin__, AuxQReg) and input_type != output_type:
|
87
|
-
raise ClassiqQFuncError(
|
88
|
-
f"Auxillary QReg must be of the same type and size in both the input and the output. Got {input_type} and {output_type}"
|
89
|
-
)
|
90
|
-
|
91
|
-
# Is Zero QReg
|
92
|
-
if input_type.__origin__ is ZeroQReg:
|
93
|
-
if output_type.__origin__ is QReg or issubclass(
|
94
|
-
output_type.__origin__, QSFixed
|
95
|
-
):
|
96
|
-
self.output_types[input_name] = output_type
|
97
|
-
else:
|
98
|
-
raise ClassiqQFuncError(
|
99
|
-
"Invalid output type. Any ZeroQReg in the input must have a corresponding QReg in the output"
|
100
|
-
)
|
101
|
-
|
102
|
-
@classmethod
|
103
|
-
def _unpack_output_values(
|
104
|
-
cls, output_value_type_hint: Any
|
105
|
-
) -> Tuple[GenericAlias, ...]:
|
106
|
-
# Handle QReg type hints
|
107
|
-
if cls.is_subclass_qreg(output_value_type_hint):
|
108
|
-
return (cls.to_generic_alias(output_value_type_hint),)
|
109
|
-
|
110
|
-
# Supporting both `typing._GenericAlias` and `types.GenericAlias`
|
111
|
-
if not cls.is_instance_generic_alias(output_value_type_hint):
|
112
|
-
raise ClassiqQFuncError(
|
113
|
-
"Output value type hint must be either a single QReg, `typing.Tuple[QReg, ...]` or, for python>=3.9, `tuple[QReg, ...]`"
|
114
|
-
)
|
115
|
-
|
116
|
-
# Allowing only a tuple of outputs:
|
117
|
-
if not cls.is_tuple_generic_alias(output_value_type_hint):
|
118
|
-
raise ClassiqQFuncError(
|
119
|
-
"Output value type hint must be either Tuple[QReg, ...] or tuple[QReg, ...]"
|
120
|
-
)
|
121
|
-
|
122
|
-
# This line may raise ClassiqQFuncError
|
123
|
-
return tuple(map(cls.to_generic_alias, output_value_type_hint.__args__))
|
124
|
-
|
125
|
-
def _unpack_input_values(
|
126
|
-
self, annotations: Dict[str, Any]
|
127
|
-
) -> Tuple[List[str], Tuple[GenericAlias, ...]]:
|
128
|
-
input_names = list(annotations.keys())
|
129
|
-
input_values = tuple(map(self.to_generic_alias, annotations.values()))
|
130
|
-
return input_names, input_values
|
131
|
-
|
132
|
-
@staticmethod
|
133
|
-
def to_generic_alias(obj: Any) -> GenericAlias:
|
134
|
-
# Handle GenericAlias
|
135
|
-
if isinstance(obj, (QRegGenericAlias, GenericAlias)):
|
136
|
-
return obj
|
137
|
-
# Handle _GenericAlias, for python>3.9, i.e. when GenericAlias != _GenericAlias
|
138
|
-
if isinstance(obj, _GenericAlias):
|
139
|
-
return GenericAlias(obj.__origin__, obj.__args__)
|
140
|
-
# Handle a single QReg (not GenericAlias of QReg)
|
141
|
-
elif inspect.isclass(obj) and issubclass(obj, QReg):
|
142
|
-
return GenericAlias(obj, tuple())
|
143
|
-
|
144
|
-
raise ClassiqQFuncError(f"Invalid type hint object: {obj.__class__.__name__}")
|
145
|
-
|
146
|
-
@staticmethod
|
147
|
-
def is_instance_generic_alias(obj: Any) -> bool:
|
148
|
-
return isinstance(obj, (GenericAlias, _GenericAlias))
|
149
|
-
|
150
|
-
@classmethod
|
151
|
-
def is_subclass_qreg(cls, obj: Any) -> bool:
|
152
|
-
if inspect.isclass(obj):
|
153
|
-
return issubclass(obj, QReg)
|
154
|
-
elif cls.is_instance_generic_alias(obj):
|
155
|
-
return issubclass(obj.__origin__, QReg)
|
156
|
-
return False
|
157
|
-
|
158
|
-
@staticmethod
|
159
|
-
def is_tuple_generic_alias(obj: GenericAliasUnion) -> bool:
|
160
|
-
return obj.__origin__.__name__.lower() == "tuple"
|
161
|
-
|
162
|
-
@staticmethod
|
163
|
-
def is_valid_generic_alias_of_qreg(obj: GenericAlias) -> bool:
|
164
|
-
return isinstance(obj, QRegGenericAlias)
|
165
|
-
|
166
|
-
|
167
|
-
def get_annotation_role(annotation: GenericAlias, default_role: Role) -> Role:
|
168
|
-
"""
|
169
|
-
Note: this function cannot distinguish between inputs and outputs.
|
170
|
-
Thus, for inputs, all 3 options are valid
|
171
|
-
However, for outputs:
|
172
|
-
a) we don't expect to get ZERO
|
173
|
-
b) We treat INPUT as OUTPUT
|
174
|
-
"""
|
175
|
-
ret = None
|
176
|
-
|
177
|
-
if getattr(annotation, "role", None) is not None:
|
178
|
-
ret = annotation.role
|
179
|
-
if getattr(annotation.__origin__, "role", None) is not None:
|
180
|
-
ret = annotation.role
|
181
|
-
|
182
|
-
if issubclass(annotation.__origin__, QReg) and not issubclass(
|
183
|
-
annotation.__origin__, ZeroQReg
|
184
|
-
):
|
185
|
-
ret = default_role
|
186
|
-
|
187
|
-
if issubclass(annotation.__origin__, ZeroQReg) and not issubclass(
|
188
|
-
annotation.__origin__, AuxQReg
|
189
|
-
):
|
190
|
-
ret = Role.ZERO_INPUT
|
191
|
-
|
192
|
-
if issubclass(annotation.__origin__, AuxQReg):
|
193
|
-
ret = Role.AUXILIARY
|
194
|
-
|
195
|
-
# Didn't match anything so far
|
196
|
-
if ret is None:
|
197
|
-
raise ClassiqQFuncError("Invalid annotation role")
|
198
|
-
|
199
|
-
if default_role == Role.INPUT and ret == Role.OUTPUT:
|
200
|
-
raise ClassiqQFuncError("input should not have Role.OUTPUT")
|
201
|
-
|
202
|
-
if default_role == Role.OUTPUT and ret in (Role.ZERO_INPUT, Role.INPUT):
|
203
|
-
raise ClassiqQFuncError("output should not have Role.ZERO / Role.INPUT")
|
204
|
-
|
205
|
-
return ret
|
@@ -1,22 +0,0 @@
|
|
1
|
-
from typing import Callable, Optional, Union, overload
|
2
|
-
|
3
|
-
from classiq.quantum_functions.quantum_function import QuantumFunction
|
4
|
-
|
5
|
-
|
6
|
-
@overload
|
7
|
-
def quantum_function(func: None = None, name: Optional[str] = None) -> Callable: ...
|
8
|
-
|
9
|
-
|
10
|
-
@overload
|
11
|
-
def quantum_function(func: Callable, name: Optional[str] = None) -> QuantumFunction: ...
|
12
|
-
|
13
|
-
|
14
|
-
def quantum_function(
|
15
|
-
func: Optional[Callable] = None, name: Optional[str] = None
|
16
|
-
) -> Union[QuantumFunction, Callable]:
|
17
|
-
if func is None:
|
18
|
-
return lambda func: quantum_function(func, name)
|
19
|
-
else:
|
20
|
-
qf = QuantumFunction()
|
21
|
-
qf.add_implementation(func, name)
|
22
|
-
return qf
|
@@ -1,181 +0,0 @@
|
|
1
|
-
"""Function library module, implementing facilities for adding user defined functions to the Classiq platform."""
|
2
|
-
|
3
|
-
from typing import Any, Dict, List, Tuple, Type, Union
|
4
|
-
|
5
|
-
from more_itertools import locate
|
6
|
-
|
7
|
-
from classiq.interface.generator.functions import (
|
8
|
-
SynthesisForeignFunctionDefinition,
|
9
|
-
SynthesisNativeFunctionDefinition,
|
10
|
-
SynthesisQuantumFunctionDeclaration,
|
11
|
-
)
|
12
|
-
from classiq.interface.generator.model.model import (
|
13
|
-
MAIN_FUNCTION_NAME,
|
14
|
-
ConcreteFunctionDefinition,
|
15
|
-
)
|
16
|
-
from classiq.interface.generator.user_defined_function_params import CustomFunction
|
17
|
-
from classiq.interface.helpers.pydantic_model_helpers import nameables_to_dict
|
18
|
-
|
19
|
-
from classiq.exceptions import ClassiqValueError
|
20
|
-
from classiq.quantum_functions.quantum_function import (
|
21
|
-
QuantumFunction,
|
22
|
-
QuantumFunctionFactory,
|
23
|
-
)
|
24
|
-
|
25
|
-
QASM_INTRO = 'OPENQASM 2.0;\ninclude "qelib1.inc";\n'
|
26
|
-
QASM3_INTRO = 'OPENQASM 3.0;\ninclude "stdgates.inc";\n'
|
27
|
-
|
28
|
-
_INVALID_FUNCTION_LIBRARY_ARGUMENT_ERROR_MSG: str = (
|
29
|
-
"Argument is not a valid FunctionLibrary object"
|
30
|
-
)
|
31
|
-
|
32
|
-
|
33
|
-
class FunctionLibrary:
|
34
|
-
"""Facility to manage functions."""
|
35
|
-
|
36
|
-
def __init__(
|
37
|
-
self,
|
38
|
-
*functions: Union[
|
39
|
-
SynthesisQuantumFunctionDeclaration,
|
40
|
-
QuantumFunction,
|
41
|
-
Type[QuantumFunctionFactory],
|
42
|
-
],
|
43
|
-
) -> None:
|
44
|
-
"""
|
45
|
-
Args:
|
46
|
-
name (:obj:`str`, optional): The name of the function library.
|
47
|
-
*functions (:obj:`SynthesisQuantumFunctionDeclaration`, optional): A list of functions to initialize the object.
|
48
|
-
"""
|
49
|
-
self._data: List[ConcreteFunctionDefinition] = list()
|
50
|
-
self._params: Dict[str, CustomFunction] = dict()
|
51
|
-
self._func_factories: Dict[str, Type[QuantumFunctionFactory]] = dict()
|
52
|
-
|
53
|
-
for f in functions:
|
54
|
-
self.add_function(f)
|
55
|
-
|
56
|
-
if MAIN_FUNCTION_NAME not in self.function_dict:
|
57
|
-
self.add_function(
|
58
|
-
SynthesisNativeFunctionDefinition(name=MAIN_FUNCTION_NAME)
|
59
|
-
)
|
60
|
-
|
61
|
-
def get_function(self, function_name: str) -> CustomFunction:
|
62
|
-
return self._params[function_name]
|
63
|
-
|
64
|
-
def get_function_factory(
|
65
|
-
self, function_factory_name: str
|
66
|
-
) -> Type[QuantumFunctionFactory]:
|
67
|
-
return self._func_factories[function_factory_name]
|
68
|
-
|
69
|
-
def __getitem__(self, key: Any) -> CustomFunction:
|
70
|
-
if isinstance(key, str):
|
71
|
-
return self.get_function(key)
|
72
|
-
else:
|
73
|
-
raise ClassiqValueError("Invalid key")
|
74
|
-
|
75
|
-
def add_function(
|
76
|
-
self,
|
77
|
-
function_data: Union[
|
78
|
-
SynthesisQuantumFunctionDeclaration,
|
79
|
-
QuantumFunction,
|
80
|
-
Type[QuantumFunctionFactory],
|
81
|
-
],
|
82
|
-
override_existing_functions: bool = False,
|
83
|
-
) -> None:
|
84
|
-
"""Adds a function to the function library.
|
85
|
-
|
86
|
-
Args:
|
87
|
-
function_data (SynthesisQuantumFunctionDeclaration): The function data object.
|
88
|
-
override_existing_functions (:obj:`bool`, optional): Defaults to False.
|
89
|
-
|
90
|
-
Returns:
|
91
|
-
None
|
92
|
-
"""
|
93
|
-
if isinstance(function_data, type) and issubclass(
|
94
|
-
function_data, QuantumFunctionFactory
|
95
|
-
):
|
96
|
-
self._func_factories[function_data.__name__] = function_data
|
97
|
-
return
|
98
|
-
if isinstance(function_data, QuantumFunction):
|
99
|
-
function_data = function_data.function_data
|
100
|
-
|
101
|
-
if not isinstance(
|
102
|
-
function_data,
|
103
|
-
(SynthesisForeignFunctionDefinition, SynthesisNativeFunctionDefinition),
|
104
|
-
):
|
105
|
-
raise ClassiqValueError(
|
106
|
-
f"Concrete function definition object expected, got {function_data.__class__.__name__}"
|
107
|
-
)
|
108
|
-
|
109
|
-
function_name = function_data.name
|
110
|
-
if not override_existing_functions and function_name in self.function_dict:
|
111
|
-
raise ClassiqValueError("Cannot override existing functions.")
|
112
|
-
|
113
|
-
if isinstance(function_data, SynthesisNativeFunctionDefinition):
|
114
|
-
for call in filter(
|
115
|
-
lambda i: isinstance(i.function_params, CustomFunction),
|
116
|
-
function_data.body,
|
117
|
-
):
|
118
|
-
if self._data and call.function not in self.function_dict:
|
119
|
-
raise ClassiqValueError(
|
120
|
-
"FunctionLibrary: The function is not found in included library."
|
121
|
-
)
|
122
|
-
|
123
|
-
self._data.append(function_data)
|
124
|
-
self._params[function_name] = self._to_params(function_data)
|
125
|
-
|
126
|
-
@property
|
127
|
-
def function_names(self) -> Tuple[str, ...]:
|
128
|
-
"""Get a tuple of the names of the functions in the library.
|
129
|
-
|
130
|
-
Returns:
|
131
|
-
The names of the functions in the library.
|
132
|
-
"""
|
133
|
-
return tuple(self.function_dict.keys())
|
134
|
-
|
135
|
-
@property
|
136
|
-
def function_factory_names(self) -> Tuple[str, ...]:
|
137
|
-
return tuple(self._func_factories.keys())
|
138
|
-
|
139
|
-
@property
|
140
|
-
def functions(self) -> List[ConcreteFunctionDefinition]:
|
141
|
-
return list(self._data)
|
142
|
-
|
143
|
-
@property
|
144
|
-
def function_dict(self) -> Dict[str, SynthesisQuantumFunctionDeclaration]:
|
145
|
-
return nameables_to_dict(self._data)
|
146
|
-
|
147
|
-
def remove_function_definition(self, name: str) -> None:
|
148
|
-
idx = list(locate(self.functions, lambda func: func.name == name))
|
149
|
-
assert len(idx) == 1
|
150
|
-
self._data.pop(idx[0])
|
151
|
-
|
152
|
-
@staticmethod
|
153
|
-
def _to_params(data: SynthesisQuantumFunctionDeclaration) -> CustomFunction:
|
154
|
-
params = CustomFunction(
|
155
|
-
input_decls=data.inputs,
|
156
|
-
output_decls=data.outputs,
|
157
|
-
)
|
158
|
-
return params
|
159
|
-
|
160
|
-
def __add__(self, other: "FunctionLibrary") -> "FunctionLibrary":
|
161
|
-
if not isinstance(other, FunctionLibrary):
|
162
|
-
raise ClassiqValueError(_INVALID_FUNCTION_LIBRARY_ARGUMENT_ERROR_MSG)
|
163
|
-
joint_library = FunctionLibrary()
|
164
|
-
for library in (self, other):
|
165
|
-
for func in library.functions:
|
166
|
-
if (
|
167
|
-
func.name == MAIN_FUNCTION_NAME
|
168
|
-
or func.name in joint_library.function_names
|
169
|
-
):
|
170
|
-
continue
|
171
|
-
joint_library.add_function(func)
|
172
|
-
return joint_library
|
173
|
-
|
174
|
-
def __iadd__(self, other: "FunctionLibrary") -> "FunctionLibrary":
|
175
|
-
if not isinstance(other, FunctionLibrary):
|
176
|
-
raise ClassiqValueError(_INVALID_FUNCTION_LIBRARY_ARGUMENT_ERROR_MSG)
|
177
|
-
for func in other.functions:
|
178
|
-
if func.name == MAIN_FUNCTION_NAME or func.name in self.function_names:
|
179
|
-
continue
|
180
|
-
self.add_function(func)
|
181
|
-
return self
|