classiq 0.60.1__py3-none-any.whl → 0.61.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 +2 -0
- classiq/_internals/client.py +6 -1
- classiq/applications/__init__.py +1 -1
- classiq/applications/chemistry/__init__.py +7 -7
- classiq/applications/chemistry/chemistry_model_constructor.py +17 -6
- classiq/applications/combinatorial_optimization/__init__.py +7 -1
- classiq/applications/combinatorial_optimization/combinatorial_optimization_model_constructor.py +2 -0
- classiq/applications/combinatorial_optimization/combinatorial_problem.py +197 -0
- classiq/applications/finance/finance_model_constructor.py +6 -6
- classiq/applications/grover/grover_model_constructor.py +3 -0
- classiq/applications/libraries/qmci_library.py +1 -10
- classiq/applications/qnn/__init__.py +1 -1
- classiq/applications/qnn/datasets/__init__.py +8 -8
- classiq/applications/qsvm/qsvm.py +1 -1
- classiq/execution/__init__.py +0 -2
- classiq/execution/execution_session.py +6 -0
- classiq/executor.py +1 -1
- classiq/interface/_version.py +1 -1
- classiq/interface/backend/backend_preferences.py +12 -12
- classiq/interface/executor/execution_preferences.py +1 -1
- classiq/interface/generator/application_apis/chemistry_declarations.py +1 -1
- classiq/interface/generator/application_apis/finance_declarations.py +2 -2
- classiq/interface/generator/arith/arithmetic.py +16 -1
- classiq/interface/generator/arith/arithmetic_expression_validator.py +4 -3
- classiq/interface/generator/expressions/expression_constants.py +3 -0
- classiq/interface/generator/generated_circuit_data.py +58 -20
- classiq/interface/generator/model/__init__.py +1 -1
- classiq/interface/generator/model/quantum_register.py +3 -3
- classiq/interface/generator/standard_gates/controlled_standard_gates.py +20 -32
- classiq/interface/ide/visual_model.py +1 -0
- classiq/interface/interface_version.py +1 -1
- classiq/interface/model/model.py +2 -3
- classiq/interface/model/quantum_function_call.py +4 -7
- classiq/interface/model/quantum_function_declaration.py +7 -0
- classiq/interface/model/quantum_lambda_function.py +10 -1
- classiq/interface/model/quantum_type.py +3 -1
- classiq/model_expansions/atomic_expression_functions_defs.py +3 -1
- classiq/model_expansions/capturing/captured_vars.py +24 -8
- classiq/model_expansions/capturing/mangling_utils.py +23 -15
- classiq/model_expansions/evaluators/arg_type_match.py +7 -7
- classiq/model_expansions/expression_evaluator.py +5 -2
- classiq/model_expansions/function_builder.py +21 -4
- classiq/model_expansions/generative_functions.py +12 -90
- classiq/model_expansions/interpreter.py +58 -11
- classiq/model_expansions/quantum_operations/call_emitter.py +19 -10
- classiq/model_expansions/quantum_operations/classicalif.py +1 -1
- classiq/model_expansions/quantum_operations/control.py +5 -31
- classiq/model_expansions/quantum_operations/emitter.py +27 -14
- classiq/model_expansions/quantum_operations/expression_operation.py +3 -5
- classiq/model_expansions/quantum_operations/inplace_binary_operation.py +57 -15
- classiq/model_expansions/quantum_operations/invert.py +1 -6
- classiq/model_expansions/quantum_operations/phase.py +2 -5
- classiq/model_expansions/quantum_operations/power.py +0 -4
- classiq/model_expansions/quantum_operations/quantum_assignment_operation.py +19 -30
- classiq/model_expansions/quantum_operations/quantum_function_call.py +3 -1
- classiq/model_expansions/quantum_operations/shallow_emitter.py +155 -0
- classiq/model_expansions/quantum_operations/within_apply.py +0 -14
- classiq/model_expansions/scope.py +10 -4
- classiq/model_expansions/scope_initialization.py +0 -11
- classiq/model_expansions/sympy_conversion/expression_to_sympy.py +7 -0
- classiq/model_expansions/sympy_conversion/sympy_to_python.py +12 -2
- classiq/model_expansions/transformers/ast_renamer.py +26 -0
- classiq/model_expansions/transformers/var_splitter.py +11 -12
- classiq/model_expansions/visitors/variable_references.py +20 -12
- classiq/qmod/builtins/classical_execution_primitives.py +6 -6
- classiq/qmod/builtins/classical_functions.py +10 -10
- classiq/qmod/builtins/functions/__init__.py +89 -103
- classiq/qmod/builtins/functions/amplitude_estimation.py +1 -1
- classiq/qmod/builtins/functions/arithmetic.py +1 -1
- classiq/qmod/builtins/functions/discrete_sine_cosine_transform.py +6 -6
- classiq/qmod/builtins/functions/grover.py +5 -5
- classiq/qmod/builtins/functions/hea.py +1 -1
- classiq/qmod/builtins/functions/linear_pauli_rotation.py +2 -2
- classiq/qmod/builtins/functions/modular_exponentiation.py +8 -8
- classiq/qmod/builtins/functions/operators.py +1 -1
- classiq/qmod/builtins/functions/qaoa_penalty.py +5 -5
- classiq/qmod/builtins/functions/qft_functions.py +2 -2
- classiq/qmod/builtins/functions/qpe.py +9 -12
- classiq/qmod/builtins/functions/qsvt.py +177 -15
- classiq/qmod/builtins/functions/state_preparation.py +9 -9
- classiq/qmod/builtins/functions/swap_test.py +1 -1
- classiq/qmod/builtins/functions/utility_functions.py +2 -2
- classiq/qmod/builtins/functions/variational.py +2 -2
- classiq/qmod/builtins/operations.py +3 -3
- classiq/qmod/builtins/structs.py +9 -9
- classiq/qmod/native/pretty_printer.py +17 -19
- classiq/qmod/pretty_print/pretty_printer.py +9 -6
- classiq/qmod/qmod_variable.py +2 -5
- classiq/qmod/quantum_expandable.py +18 -4
- classiq/qmod/quantum_function.py +19 -6
- classiq/qmod/semantics/static_semantics_visitor.py +34 -16
- classiq/qmod/semantics/validation/func_call_validation.py +9 -5
- classiq/qmod/semantics/validation/function_name_collisions_validation.py +23 -0
- classiq/qmod/symbolic.py +47 -47
- {classiq-0.60.1.dist-info → classiq-0.61.0.dist-info}/METADATA +1 -1
- {classiq-0.60.1.dist-info → classiq-0.61.0.dist-info}/RECORD +97 -94
- classiq/execution/qaoa.py +0 -86
- {classiq-0.60.1.dist-info → classiq-0.61.0.dist-info}/WHEEL +0 -0
classiq/__init__.py
CHANGED
@@ -28,8 +28,10 @@ from classiq.applications.chemistry import (
|
|
28
28
|
molecule_problem_to_qmod,
|
29
29
|
)
|
30
30
|
from classiq.applications.combinatorial_optimization import (
|
31
|
+
CombinatorialProblem,
|
31
32
|
compute_qaoa_initial_point,
|
32
33
|
construct_combinatorial_optimization_model,
|
34
|
+
execute_qaoa,
|
33
35
|
pyo_model_to_hamiltonian,
|
34
36
|
)
|
35
37
|
from classiq.applications.finance import construct_finance_model
|
classiq/_internals/client.py
CHANGED
@@ -290,7 +290,12 @@ class Client:
|
|
290
290
|
if http_client is None:
|
291
291
|
return self.async_client()
|
292
292
|
else:
|
293
|
-
|
293
|
+
if sys.version_info[0:2] < (3, 10):
|
294
|
+
# remove this `if` and the `_AsyncNullContext` class when we stop
|
295
|
+
# supporting python 3.9
|
296
|
+
return _AsyncNullContext(enter_result=http_client)
|
297
|
+
else:
|
298
|
+
return contextlib.nullcontext(enter_result=http_client)
|
294
299
|
|
295
300
|
def sync_call_api(
|
296
301
|
self,
|
classiq/applications/__init__.py
CHANGED
@@ -19,18 +19,18 @@ from .chemistry_model_constructor import (
|
|
19
19
|
)
|
20
20
|
|
21
21
|
__all__ = [
|
22
|
-
"
|
23
|
-
"
|
22
|
+
"ChemistryExecutionParameters",
|
23
|
+
"FermionicOperator",
|
24
24
|
"GroundStateProblem",
|
25
|
+
"HEAParameters",
|
26
|
+
"HVAParameters",
|
25
27
|
"HamiltonianProblem",
|
26
|
-
"
|
28
|
+
"Molecule",
|
29
|
+
"MoleculeProblem",
|
27
30
|
"PauliOperator",
|
28
|
-
"
|
31
|
+
"PauliOperators",
|
29
32
|
"SummedFermionicOperator",
|
30
33
|
"UCCParameters",
|
31
|
-
"HVAParameters",
|
32
|
-
"HEAParameters",
|
33
|
-
"ChemistryExecutionParameters",
|
34
34
|
"construct_chemistry_model",
|
35
35
|
"molecule_problem_to_qmod",
|
36
36
|
]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# flake8: noqa
|
2
2
|
|
3
|
-
from typing import Optional
|
3
|
+
from typing import Optional, cast
|
4
4
|
from collections.abc import Mapping
|
5
5
|
|
6
6
|
from classiq.interface.chemistry.fermionic_operator import (
|
@@ -53,6 +53,7 @@ from classiq.applications.chemistry.chemistry_execution_parameters import (
|
|
53
53
|
)
|
54
54
|
from classiq.interface.exceptions import ClassiqError
|
55
55
|
from classiq.qmod.utilities import qmod_val_to_expr_str
|
56
|
+
from classiq.qmod.builtins.functions.hea import full_hea
|
56
57
|
|
57
58
|
_LADDER_OPERATOR_TYPE_INDICATOR_TO_QMOD_MAPPING: dict[str, str] = {
|
58
59
|
"+": "PLUS",
|
@@ -489,12 +490,22 @@ def construct_chemistry_model(
|
|
489
490
|
ansatz_parameters: AnsatzParameters,
|
490
491
|
execution_parameters: ChemistryExecutionParameters,
|
491
492
|
) -> SerializedModel:
|
493
|
+
chemistry_functions = [
|
494
|
+
_get_chemistry_quantum_main(
|
495
|
+
chemistry_problem,
|
496
|
+
use_hartree_fock,
|
497
|
+
ansatz_parameters,
|
498
|
+
)
|
499
|
+
]
|
500
|
+
if isinstance(ansatz_parameters, HEAParameters):
|
501
|
+
chemistry_functions.append(
|
502
|
+
cast(
|
503
|
+
NativeFunctionDefinition,
|
504
|
+
full_hea.create_model().function_dict["full_hea"],
|
505
|
+
)
|
506
|
+
)
|
492
507
|
model = Model(
|
493
|
-
functions=
|
494
|
-
_get_chemistry_quantum_main(
|
495
|
-
chemistry_problem, use_hartree_fock, ansatz_parameters
|
496
|
-
),
|
497
|
-
],
|
508
|
+
functions=chemistry_functions,
|
498
509
|
classical_execution_code=_get_chemistry_classical_code(
|
499
510
|
chemistry_problem, execution_parameters
|
500
511
|
),
|
@@ -12,17 +12,23 @@ from .combinatorial_optimization_config import OptimizerConfig, QAOAConfig
|
|
12
12
|
from .combinatorial_optimization_model_constructor import (
|
13
13
|
construct_combinatorial_optimization_model,
|
14
14
|
)
|
15
|
+
from .combinatorial_problem import (
|
16
|
+
CombinatorialProblem,
|
17
|
+
execute_qaoa,
|
18
|
+
)
|
15
19
|
|
16
20
|
__all__ = [
|
21
|
+
"CombinatorialProblem",
|
17
22
|
"EncodingType",
|
18
23
|
"OptimizerConfig",
|
19
24
|
"QAOAConfig",
|
20
25
|
"QSolver",
|
26
|
+
"compute_qaoa_initial_point",
|
21
27
|
"construct_combinatorial_optimization_model",
|
22
28
|
"examples",
|
29
|
+
"execute_qaoa",
|
23
30
|
"get_optimization_solution_from_pyo",
|
24
31
|
"pyo_model_to_hamiltonian",
|
25
|
-
"compute_qaoa_initial_point",
|
26
32
|
]
|
27
33
|
|
28
34
|
|
classiq/applications/combinatorial_optimization/combinatorial_optimization_model_constructor.py
CHANGED
@@ -33,6 +33,7 @@ from classiq.applications.combinatorial_helpers.pauli_helpers.pauli_utils import
|
|
33
33
|
_pauli_terms_to_qmod,
|
34
34
|
)
|
35
35
|
from classiq.applications.combinatorial_optimization import OptimizerConfig, QAOAConfig
|
36
|
+
from classiq.qmod.builtins.functions import qaoa_penalty
|
36
37
|
|
37
38
|
|
38
39
|
def construct_combi_opt_py_model(
|
@@ -106,6 +107,7 @@ def construct_combi_opt_py_model(
|
|
106
107
|
),
|
107
108
|
],
|
108
109
|
),
|
110
|
+
*[f for f in qaoa_penalty.create_model().functions if f.name != "main"],
|
109
111
|
],
|
110
112
|
classical_execution_code=f"""
|
111
113
|
vqe_result = vqe(
|
@@ -0,0 +1,197 @@
|
|
1
|
+
import math
|
2
|
+
import re
|
3
|
+
from typing import Callable, Optional
|
4
|
+
|
5
|
+
import numpy as np
|
6
|
+
import pandas as pd
|
7
|
+
import pyomo.core as pyo
|
8
|
+
import scipy
|
9
|
+
|
10
|
+
from classiq.interface.executor.execution_preferences import ExecutionPreferences
|
11
|
+
from classiq.interface.executor.result import ExecutionDetails
|
12
|
+
from classiq.interface.model.model import SerializedModel
|
13
|
+
|
14
|
+
from classiq.applications.combinatorial_helpers.combinatorial_problem_utils import (
|
15
|
+
pyo_model_to_qmod_problem,
|
16
|
+
)
|
17
|
+
from classiq.execution import ExecutionSession
|
18
|
+
from classiq.qmod.builtins.functions import (
|
19
|
+
RX,
|
20
|
+
allocate,
|
21
|
+
apply_to_all,
|
22
|
+
hadamard_transform,
|
23
|
+
)
|
24
|
+
from classiq.qmod.builtins.operations import phase, repeat
|
25
|
+
from classiq.qmod.cparam import CReal
|
26
|
+
from classiq.qmod.create_model_function import create_model
|
27
|
+
from classiq.qmod.qfunc import qfunc
|
28
|
+
from classiq.qmod.qmod_parameter import CArray
|
29
|
+
from classiq.qmod.qmod_variable import Output, QVar
|
30
|
+
from classiq.synthesis import SerializedQuantumProgram, synthesize
|
31
|
+
|
32
|
+
|
33
|
+
class CombinatorialProblem:
|
34
|
+
def __init__(
|
35
|
+
self,
|
36
|
+
pyo_model: pyo.ConcreteModel,
|
37
|
+
num_layers: int,
|
38
|
+
penalty_factor: int = 1,
|
39
|
+
):
|
40
|
+
self.problem_vars_, self.cost_func_ = pyo_model_to_qmod_problem(
|
41
|
+
pyo_model, penalty_factor
|
42
|
+
)
|
43
|
+
self.num_layers_ = num_layers
|
44
|
+
self.model_ = None
|
45
|
+
self.qprog_ = None
|
46
|
+
self.es_ = None
|
47
|
+
self.optimized_params_ = None
|
48
|
+
|
49
|
+
def get_model(self) -> SerializedModel:
|
50
|
+
@qfunc
|
51
|
+
def main(
|
52
|
+
params: CArray[CReal, self.num_layers_ * 2], # type:ignore[valid-type]
|
53
|
+
v: Output[self.problem_vars_], # type:ignore[name-defined]
|
54
|
+
) -> None:
|
55
|
+
allocate(v.size, v)
|
56
|
+
hadamard_transform(v)
|
57
|
+
repeat(
|
58
|
+
self.num_layers_,
|
59
|
+
lambda i: [ # type:ignore[arg-type]
|
60
|
+
phase(
|
61
|
+
-self.cost_func_(v), params[i]
|
62
|
+
), # type:ignore[func-returns-value]
|
63
|
+
apply_to_all(lambda q: RX(params[self.num_layers_ + i], q), v),
|
64
|
+
],
|
65
|
+
)
|
66
|
+
|
67
|
+
self.model_ = create_model(main) # type:ignore[assignment]
|
68
|
+
return self.model_ # type:ignore[return-value]
|
69
|
+
|
70
|
+
def get_qprog(self) -> SerializedQuantumProgram:
|
71
|
+
if self.model_ is None:
|
72
|
+
self.get_model()
|
73
|
+
self.qprog_ = synthesize(self.model_) # type:ignore[assignment,arg-type]
|
74
|
+
return self.qprog_ # type:ignore[return-value]
|
75
|
+
|
76
|
+
def optimize(
|
77
|
+
self,
|
78
|
+
execution_preferences: Optional[ExecutionPreferences] = None,
|
79
|
+
maxiter: int = 20,
|
80
|
+
cost_trace: Optional[list[float]] = None,
|
81
|
+
quantile: float = 1.0,
|
82
|
+
) -> list[float]:
|
83
|
+
if self.qprog_ is None:
|
84
|
+
self.get_qprog()
|
85
|
+
self.es_ = ExecutionSession(
|
86
|
+
self.qprog_, execution_preferences # type:ignore[assignment,arg-type]
|
87
|
+
)
|
88
|
+
|
89
|
+
def estimate_cost_wrapper(params: np.ndarray) -> float:
|
90
|
+
cost = self.es_.estimate_cost( # type:ignore[attr-defined]
|
91
|
+
lambda state: self.cost_func_(state["v"]),
|
92
|
+
{"params": params.tolist()},
|
93
|
+
quantile=quantile,
|
94
|
+
)
|
95
|
+
if cost_trace is not None:
|
96
|
+
cost_trace.append(cost)
|
97
|
+
return cost
|
98
|
+
|
99
|
+
initial_params = (
|
100
|
+
np.concatenate(
|
101
|
+
(
|
102
|
+
np.linspace(0, 1, self.num_layers_),
|
103
|
+
np.linspace(1, 0, self.num_layers_),
|
104
|
+
)
|
105
|
+
)
|
106
|
+
* math.pi
|
107
|
+
)
|
108
|
+
self.optimized_params_ = scipy.optimize.minimize(
|
109
|
+
estimate_cost_wrapper,
|
110
|
+
x0=initial_params,
|
111
|
+
method="COBYLA",
|
112
|
+
options={"maxiter": maxiter},
|
113
|
+
).x.tolist()
|
114
|
+
return self.optimized_params_ # type:ignore[return-value]
|
115
|
+
|
116
|
+
def get_results(self) -> pd.DataFrame:
|
117
|
+
if self.optimized_params_ is None:
|
118
|
+
self.optimize()
|
119
|
+
assert self.es_ is not None
|
120
|
+
res = self.es_.sample( # type:ignore[unreachable]
|
121
|
+
{"params": self.optimized_params_}
|
122
|
+
)
|
123
|
+
parsed_result = [
|
124
|
+
{
|
125
|
+
"solution": {
|
126
|
+
key: value
|
127
|
+
for key, value in sampled.state["v"].items()
|
128
|
+
if not re.match(".*_slack_var_.*", key)
|
129
|
+
},
|
130
|
+
"probability": sampled.shots / res.num_shots,
|
131
|
+
"cost": self.cost_func_(sampled.state["v"]),
|
132
|
+
}
|
133
|
+
for sampled in res.parsed_counts
|
134
|
+
]
|
135
|
+
return pd.DataFrame.from_records(parsed_result)
|
136
|
+
|
137
|
+
|
138
|
+
def execute_qaoa(
|
139
|
+
problem_vars: type[QVar],
|
140
|
+
cost_func: Callable,
|
141
|
+
num_layers: int,
|
142
|
+
maxiter: int,
|
143
|
+
execution_preferences: Optional[ExecutionPreferences] = None,
|
144
|
+
) -> tuple[SerializedModel, SerializedQuantumProgram, ExecutionDetails]:
|
145
|
+
"""
|
146
|
+
Implements a simple QAOA algorithm, including the creation and synthesis of the QAOA
|
147
|
+
ansatz and the classical optimization loop.
|
148
|
+
|
149
|
+
Args:
|
150
|
+
problem_vars: the quantum type (scalar, array, or struct) of the problem variable(s)
|
151
|
+
cost_func: the arithmetic expression that evaluates the cost given an instance of the problem_vars type
|
152
|
+
num_layers: the number of layers of the QAOA ansatz
|
153
|
+
maxiter: the maximum number of iterations for the classical optimization loop
|
154
|
+
execution_preferences: the execution settings for running the QAOA ansatz
|
155
|
+
|
156
|
+
Returns:
|
157
|
+
a tuple containing the model of the QAOA ansatz, the corresponding synthesized quantum program,
|
158
|
+
and the result of the execution with the optimized parameters
|
159
|
+
"""
|
160
|
+
|
161
|
+
@qfunc
|
162
|
+
def main(
|
163
|
+
params: CArray[CReal, num_layers * 2], # type:ignore[valid-type]
|
164
|
+
v: Output[problem_vars], # type:ignore[valid-type]
|
165
|
+
) -> None:
|
166
|
+
allocate(v.size, v) # type:ignore[attr-defined]
|
167
|
+
hadamard_transform(v)
|
168
|
+
repeat(
|
169
|
+
num_layers,
|
170
|
+
lambda i: [ # type:ignore[arg-type]
|
171
|
+
phase(-cost_func(v), params[i]), # type:ignore[func-returns-value]
|
172
|
+
apply_to_all(lambda q: RX(params[num_layers + i], q), v),
|
173
|
+
],
|
174
|
+
)
|
175
|
+
|
176
|
+
model = create_model(main)
|
177
|
+
qprog = synthesize(model)
|
178
|
+
|
179
|
+
with ExecutionSession(qprog, execution_preferences) as es:
|
180
|
+
initial_params = (
|
181
|
+
np.concatenate(
|
182
|
+
(np.linspace(0, 1, num_layers), np.linspace(1, 0, num_layers))
|
183
|
+
)
|
184
|
+
* math.pi
|
185
|
+
)
|
186
|
+
final_params = scipy.optimize.minimize(
|
187
|
+
lambda params: es.estimate_cost(
|
188
|
+
lambda state: cost_func(state["v"]),
|
189
|
+
{"params": params.tolist()},
|
190
|
+
),
|
191
|
+
x0=initial_params,
|
192
|
+
method="COBYLA",
|
193
|
+
options={"maxiter": maxiter},
|
194
|
+
).x.tolist()
|
195
|
+
result = es.sample({"params": final_params})
|
196
|
+
|
197
|
+
return model, qprog, result
|
@@ -15,7 +15,7 @@ from classiq.interface.model.native_function_definition import NativeFunctionDef
|
|
15
15
|
from classiq.interface.model.port_declaration import PortDeclaration
|
16
16
|
from classiq.interface.model.quantum_function_call import QuantumFunctionCall
|
17
17
|
from classiq.interface.model.quantum_lambda_function import QuantumLambdaFunction
|
18
|
-
from classiq.interface.model.quantum_type import
|
18
|
+
from classiq.interface.model.quantum_type import QuantumNumeric
|
19
19
|
from classiq.interface.model.variable_declaration_statement import (
|
20
20
|
VariableDeclarationStatement,
|
21
21
|
)
|
@@ -68,8 +68,10 @@ def construct_finance_model(
|
|
68
68
|
positional_arg_declarations=[
|
69
69
|
PortDeclaration(
|
70
70
|
name="phase_port",
|
71
|
-
quantum_type=
|
72
|
-
|
71
|
+
quantum_type=QuantumNumeric(
|
72
|
+
size=Expression(expr=f"{phase_port_size}"),
|
73
|
+
is_signed=Expression(expr="False"),
|
74
|
+
fraction_digits=Expression(expr=f"{phase_port_size}"),
|
73
75
|
),
|
74
76
|
direction=PortDeclarationDirection.Output,
|
75
77
|
),
|
@@ -84,10 +86,8 @@ def construct_finance_model(
|
|
84
86
|
],
|
85
87
|
),
|
86
88
|
QuantumFunctionCall(
|
87
|
-
function="
|
89
|
+
function="allocate",
|
88
90
|
positional_args=[
|
89
|
-
Expression(expr=f"{phase_port_size}"),
|
90
|
-
Expression(expr="False"),
|
91
91
|
Expression(expr=f"{phase_port_size}"),
|
92
92
|
HandleBinding(name="phase_port"),
|
93
93
|
],
|
@@ -19,6 +19,7 @@ from classiq.interface.model.variable_declaration_statement import (
|
|
19
19
|
)
|
20
20
|
|
21
21
|
from classiq import RegisterUserInput
|
22
|
+
from classiq.qmod.builtins.functions.grover import grover_search, phase_oracle
|
22
23
|
|
23
24
|
_OUTPUT_VARIABLE_NAME = "result"
|
24
25
|
|
@@ -151,6 +152,8 @@ def construct_grover_model(
|
|
151
152
|
),
|
152
153
|
],
|
153
154
|
),
|
155
|
+
*[f for f in grover_search.create_model().functions if f.name != "main"],
|
156
|
+
*[f for f in phase_oracle.create_model().functions if f.name != "main"],
|
154
157
|
],
|
155
158
|
)
|
156
159
|
return grover_model.get_model()
|
@@ -1,7 +1,3 @@
|
|
1
|
-
from typing import cast
|
2
|
-
|
3
|
-
from classiq.interface.model.native_function_definition import NativeFunctionDefinition
|
4
|
-
|
5
1
|
from classiq.qmod.builtins.functions import Z, amplitude_estimation
|
6
2
|
from classiq.qmod.qfunc import qfunc
|
7
3
|
from classiq.qmod.qmod_variable import QArray, QBit, QNum
|
@@ -22,9 +18,4 @@ def qmci(
|
|
22
18
|
)
|
23
19
|
|
24
20
|
|
25
|
-
QMCI_LIBRARY = [
|
26
|
-
cast(
|
27
|
-
NativeFunctionDefinition,
|
28
|
-
qmci.create_model().function_dict["qmci"],
|
29
|
-
),
|
30
|
-
]
|
21
|
+
QMCI_LIBRARY = [func for func in qmci.create_model().functions if func.name != "main"]
|
@@ -13,19 +13,19 @@ from ..datasets.dataset_xor import DatasetXor
|
|
13
13
|
from ..datasets.datasets_utils import state_to_label, state_to_weights
|
14
14
|
|
15
15
|
__all__ = [
|
16
|
-
"builtin_datasets",
|
17
|
-
"DatasetNot",
|
18
|
-
"DATASET_NOT",
|
19
16
|
"DATALOADER_NOT",
|
20
|
-
"
|
21
|
-
"DATASET_XOR",
|
17
|
+
"DATALOADER_SUBSET_PARITY",
|
22
18
|
"DATALOADER_XOR",
|
23
|
-
"
|
19
|
+
"DATASET_NOT",
|
24
20
|
"DATASET_SUBSET_PARITY",
|
25
|
-
"
|
21
|
+
"DATASET_XOR",
|
22
|
+
"DatasetNot",
|
26
23
|
"DatasetParity",
|
27
|
-
"
|
24
|
+
"DatasetSubsetParity",
|
25
|
+
"DatasetXor",
|
26
|
+
"builtin_datasets",
|
28
27
|
"state_to_label",
|
28
|
+
"state_to_weights",
|
29
29
|
]
|
30
30
|
|
31
31
|
|
classiq/execution/__init__.py
CHANGED
@@ -10,7 +10,6 @@ from ..interface.executor.vqe_result import VQESolverResult
|
|
10
10
|
from .execution_session import ExecutionSession
|
11
11
|
from .iqcc import generate_iqcc_token, generate_iqcc_token_async
|
12
12
|
from .jobs import ExecutionJob, get_execution_jobs, get_execution_jobs_async
|
13
|
-
from .qaoa import execute_qaoa
|
14
13
|
from .qnn import execute_qnn
|
15
14
|
|
16
15
|
__all__ = (
|
@@ -25,7 +24,6 @@ __all__ = (
|
|
25
24
|
"get_execution_jobs",
|
26
25
|
"get_execution_jobs_async",
|
27
26
|
"ExecutionSession",
|
28
|
-
"execute_qaoa",
|
29
27
|
"execute_qnn",
|
30
28
|
"generate_iqcc_token",
|
31
29
|
"generate_iqcc_token_async",
|
@@ -1,4 +1,5 @@
|
|
1
1
|
import json
|
2
|
+
import random
|
2
3
|
from functools import cached_property
|
3
4
|
from types import TracebackType
|
4
5
|
from typing import Any, Callable, Optional, Union, cast
|
@@ -168,6 +169,9 @@ class ExecutionSession:
|
|
168
169
|
# because cmain is expected in some cases
|
169
170
|
self.program.model.classical_execution_code = "dummy"
|
170
171
|
|
172
|
+
self._random_seed = self.program.model.execution_preferences.random_seed
|
173
|
+
self._rng = random.Random(self._random_seed) # noqa: S311
|
174
|
+
|
171
175
|
self._async_client = client().async_client()
|
172
176
|
|
173
177
|
def __enter__(self) -> "ExecutionSession":
|
@@ -197,6 +201,8 @@ class ExecutionSession:
|
|
197
201
|
self, classical_execution_code: str, primitives_input: PrimitivesInput
|
198
202
|
) -> ExecutionJob:
|
199
203
|
execution_input = self._execution_input.copy()
|
204
|
+
execution_input["execution_preferences"]["random_seed"] = self._random_seed
|
205
|
+
self._random_seed = self._rng.randint(0, 2**32 - 1)
|
200
206
|
execution_input["classical_execution_code"] = classical_execution_code
|
201
207
|
# The use of `model_dump_json` is necessary for complex numbers serialization
|
202
208
|
execution_input["primitives_input"] = json.loads(
|
classiq/executor.py
CHANGED
classiq/interface/_version.py
CHANGED
@@ -513,25 +513,25 @@ BackendPreferencesTypes = Union[
|
|
513
513
|
]
|
514
514
|
|
515
515
|
__all__ = [
|
516
|
+
"AQTBackendPreferences",
|
517
|
+
"AliceBobBackendNames",
|
518
|
+
"AliceBobBackendPreferences",
|
519
|
+
"AmazonBraketBackendNames",
|
520
|
+
"AwsBackendPreferences",
|
516
521
|
"AzureBackendPreferences",
|
517
522
|
"AzureCredential",
|
518
523
|
"AzureQuantumBackendNames",
|
519
524
|
"ClassiqBackendPreferences",
|
525
|
+
"ClassiqNvidiaBackendNames",
|
520
526
|
"ClassiqSimulatorBackendNames",
|
527
|
+
"GCPBackendPreferences",
|
521
528
|
"IBMBackendPreferences",
|
522
529
|
"IBMBackendProvider",
|
523
|
-
"
|
524
|
-
"AmazonBraketBackendNames",
|
525
|
-
"IonqBackendPreferences",
|
526
|
-
"IonqBackendNames",
|
527
|
-
"ClassiqNvidiaBackendNames",
|
530
|
+
"IQCCBackendPreferences",
|
528
531
|
"IntelBackendNames",
|
529
|
-
"GCPBackendPreferences",
|
530
|
-
"AliceBobBackendPreferences",
|
531
|
-
"AliceBobBackendNames",
|
532
|
-
"OQCBackendPreferences",
|
533
|
-
"OQCBackendNames",
|
534
532
|
"IntelBackendPreferences",
|
535
|
-
"
|
536
|
-
"
|
533
|
+
"IonqBackendNames",
|
534
|
+
"IonqBackendPreferences",
|
535
|
+
"OQCBackendNames",
|
536
|
+
"OQCBackendPreferences",
|
537
537
|
]
|
@@ -61,7 +61,7 @@ MOLECULE_GROUND_STATE_SOLUTION_POST_PROCESS = ClassicalFunctionDeclaration(
|
|
61
61
|
)
|
62
62
|
|
63
63
|
__all__ = [
|
64
|
-
"MOLECULE_PROBLEM_TO_HAMILTONIAN",
|
65
64
|
"FOCK_HAMILTONIAN_PROBLEM_TO_HAMILTONIAN",
|
66
65
|
"MOLECULE_GROUND_STATE_SOLUTION_POST_PROCESS",
|
66
|
+
"MOLECULE_PROBLEM_TO_HAMILTONIAN",
|
67
67
|
]
|
@@ -98,8 +98,8 @@ GAUSSIAN_FINANCE_POST_PROCESS = ClassicalFunctionDeclaration(
|
|
98
98
|
)
|
99
99
|
|
100
100
|
__all__ = [
|
101
|
-
"LOG_NORMAL_FINANCE_FUNCTION",
|
102
101
|
"GAUSSIAN_FINANCE_FUNCTION",
|
103
|
-
"LOG_NORMAL_FINANCE_POST_PROCESS",
|
104
102
|
"GAUSSIAN_FINANCE_POST_PROCESS",
|
103
|
+
"LOG_NORMAL_FINANCE_FUNCTION",
|
104
|
+
"LOG_NORMAL_FINANCE_POST_PROCESS",
|
105
105
|
]
|
@@ -9,6 +9,9 @@ from classiq.interface.generator.arith import arithmetic_expression_parser
|
|
9
9
|
from classiq.interface.generator.arith.arithmetic_expression_abc import (
|
10
10
|
ArithmeticExpressionABC,
|
11
11
|
)
|
12
|
+
from classiq.interface.generator.arith.arithmetic_expression_validator import (
|
13
|
+
is_constant,
|
14
|
+
)
|
12
15
|
from classiq.interface.generator.arith.arithmetic_param_getters import (
|
13
16
|
id2op,
|
14
17
|
operation_allows_target,
|
@@ -17,7 +20,9 @@ from classiq.interface.generator.arith.arithmetic_result_builder import (
|
|
17
20
|
ArithmeticResultBuilder,
|
18
21
|
)
|
19
22
|
from classiq.interface.generator.arith.register_user_input import RegisterArithmeticInfo
|
23
|
+
from classiq.interface.generator.expressions.expression import Expression
|
20
24
|
from classiq.interface.model.quantum_type import (
|
25
|
+
QuantumNumeric,
|
21
26
|
QuantumType,
|
22
27
|
quantum_var_to_register,
|
23
28
|
register_info_to_quantum_type,
|
@@ -28,6 +33,10 @@ ARITHMETIC_EXPRESSION_RESULT_NAME: Final[str] = "expression_result"
|
|
28
33
|
ARITHMETIC_EXPRESSION_GARBAGE_NAME: Final[str] = "expression_garbage"
|
29
34
|
|
30
35
|
|
36
|
+
def is_zero(expr: str) -> bool:
|
37
|
+
return is_constant(expr) and float(expr) == 0
|
38
|
+
|
39
|
+
|
31
40
|
class Arithmetic(ArithmeticExpressionABC):
|
32
41
|
target: Optional[RegisterArithmeticInfo] = None
|
33
42
|
inputs_to_save: set[str] = pydantic.Field(default_factory=set)
|
@@ -99,7 +108,13 @@ def get_arithmetic_params(
|
|
99
108
|
|
100
109
|
def compute_arithmetic_result_type(
|
101
110
|
expr_str: str, var_types: dict[str, QuantumType], machine_precision: int
|
102
|
-
) ->
|
111
|
+
) -> QuantumNumeric:
|
112
|
+
if is_zero(expr_str):
|
113
|
+
return QuantumNumeric(
|
114
|
+
size=Expression(expr="1"),
|
115
|
+
is_signed=Expression(expr="False"),
|
116
|
+
fraction_digits=Expression(expr="0"),
|
117
|
+
)
|
103
118
|
arith_param = get_arithmetic_params(expr_str, var_types, machine_precision)
|
104
119
|
return register_info_to_quantum_type(
|
105
120
|
arith_param.outputs[ARITHMETIC_EXPRESSION_RESULT_NAME]
|