classiq 0.93.0__py3-none-any.whl → 0.94.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of classiq might be problematic. Click here for more details.
- classiq/__init__.py +6 -19
- classiq/_analyzer_extras/_ipywidgets_async_extension.py +7 -7
- classiq/_analyzer_extras/interactive_hardware.py +19 -12
- classiq/_internals/api_wrapper.py +38 -52
- classiq/_internals/async_utils.py +4 -7
- classiq/_internals/authentication/auth0.py +3 -3
- classiq/_internals/authentication/device.py +4 -4
- classiq/_internals/authentication/password_manager.py +13 -13
- classiq/_internals/authentication/token_manager.py +4 -5
- classiq/_internals/client.py +17 -44
- classiq/_internals/config.py +1 -2
- classiq/_internals/help.py +1 -2
- classiq/_internals/host_checker.py +3 -3
- classiq/_internals/jobs.py +14 -14
- classiq/_internals/type_validation.py +3 -3
- classiq/analyzer/analyzer.py +18 -18
- classiq/analyzer/rb.py +17 -8
- classiq/applications/chemistry/__init__.py +0 -30
- classiq/applications/chemistry/op_utils.py +4 -4
- classiq/applications/chemistry/problems.py +3 -3
- classiq/applications/chemistry/ucc.py +1 -2
- classiq/applications/chemistry/z2_symmetries.py +4 -4
- classiq/applications/combinatorial_helpers/allowed_constraints.py +1 -3
- classiq/applications/combinatorial_helpers/arithmetic/arithmetic_expression.py +2 -1
- classiq/applications/combinatorial_helpers/combinatorial_problem_utils.py +2 -2
- classiq/applications/combinatorial_helpers/encoding_mapping.py +2 -3
- classiq/applications/combinatorial_helpers/encoding_utils.py +2 -2
- classiq/applications/combinatorial_helpers/optimization_model.py +3 -4
- classiq/applications/combinatorial_helpers/pauli_helpers/pauli_sparsing.py +2 -2
- classiq/applications/combinatorial_helpers/pyomo_utils.py +8 -8
- classiq/applications/combinatorial_helpers/sympy_utils.py +1 -3
- classiq/applications/combinatorial_helpers/transformations/encoding.py +3 -3
- classiq/applications/combinatorial_helpers/transformations/fixed_variables.py +1 -2
- classiq/applications/combinatorial_optimization/combinatorial_optimization_config.py +2 -3
- classiq/applications/combinatorial_optimization/combinatorial_optimization_model_constructor.py +4 -6
- classiq/applications/combinatorial_optimization/combinatorial_problem.py +15 -10
- classiq/applications/hamiltonian/pauli_decomposition.py +6 -4
- classiq/applications/iqae/iqae.py +8 -8
- classiq/applications/qnn/datasets/dataset_base_classes.py +6 -6
- classiq/applications/qnn/datasets/dataset_parity.py +6 -6
- classiq/applications/qnn/qlayer.py +8 -7
- classiq/applications/qnn/torch_utils.py +3 -4
- classiq/applications/qnn/types.py +2 -1
- classiq/applications/qsp/qsp.py +5 -4
- classiq/applications/qsvm/qsvm_data_generation.py +1 -2
- classiq/evaluators/classical_expression.py +0 -4
- classiq/evaluators/parameter_types.py +7 -8
- classiq/evaluators/qmod_annotated_expression.py +24 -26
- classiq/evaluators/qmod_expression_visitors/qmod_expression_evaluator.py +14 -14
- classiq/evaluators/qmod_expression_visitors/qmod_expression_simplifier.py +2 -1
- classiq/evaluators/qmod_expression_visitors/sympy_wrappers.py +8 -8
- classiq/evaluators/qmod_node_evaluators/classical_function_evaluation.py +4 -4
- classiq/evaluators/qmod_node_evaluators/list_evaluation.py +2 -2
- classiq/evaluators/qmod_node_evaluators/numeric_attrs_utils.py +3 -3
- classiq/evaluators/qmod_node_evaluators/subscript_evaluation.py +9 -9
- classiq/evaluators/qmod_node_evaluators/utils.py +6 -6
- classiq/evaluators/qmod_type_inference/classical_type_inference.py +9 -10
- classiq/evaluators/qmod_type_inference/quantum_type_inference.py +5 -5
- classiq/execution/execution_session.py +18 -19
- classiq/execution/jobs.py +26 -26
- classiq/execution/qnn.py +1 -2
- classiq/execution/user_budgets.py +52 -7
- classiq/executor.py +1 -3
- classiq/interface/_version.py +1 -1
- classiq/interface/analyzer/analysis_params.py +4 -4
- classiq/interface/analyzer/cytoscape_graph.py +3 -3
- classiq/interface/analyzer/result.py +4 -4
- classiq/interface/applications/qsvm.py +5 -8
- classiq/interface/ast_node.py +3 -3
- classiq/interface/backend/backend_preferences.py +16 -16
- classiq/interface/backend/ionq/ionq_quantum_program.py +5 -5
- classiq/interface/chemistry/ansatz_library.py +3 -5
- classiq/interface/chemistry/operator.py +3 -3
- classiq/interface/combinatorial_optimization/examples/knapsack.py +2 -4
- classiq/interface/combinatorial_optimization/examples/tsp_digraph.py +1 -2
- classiq/interface/compression_utils.py +2 -3
- classiq/interface/debug_info/debug_info.py +7 -7
- classiq/interface/exceptions.py +2 -3
- classiq/interface/execution/iqcc.py +1 -3
- classiq/interface/execution/primitives.py +6 -6
- classiq/interface/executor/estimate_cost.py +1 -1
- classiq/interface/executor/execution_preferences.py +3 -5
- classiq/interface/executor/execution_request.py +10 -10
- classiq/interface/executor/execution_result.py +1 -2
- classiq/interface/executor/quantum_code.py +8 -8
- classiq/interface/executor/result.py +28 -18
- classiq/interface/executor/user_budget.py +2 -3
- classiq/interface/executor/vqe_result.py +5 -6
- classiq/interface/generator/ansatz_library.py +6 -8
- classiq/interface/generator/application_apis/__init__.py +0 -2
- classiq/interface/generator/arith/arithmetic.py +2 -2
- classiq/interface/generator/arith/arithmetic_arg_type_validator.py +2 -3
- classiq/interface/generator/arith/arithmetic_expression_abc.py +4 -5
- classiq/interface/generator/arith/arithmetic_expression_parser.py +11 -4
- classiq/interface/generator/arith/arithmetic_expression_validator.py +12 -15
- classiq/interface/generator/arith/arithmetic_operations.py +4 -6
- classiq/interface/generator/arith/arithmetic_param_getters.py +70 -107
- classiq/interface/generator/arith/arithmetic_result_builder.py +4 -4
- classiq/interface/generator/arith/ast_node_rewrite.py +8 -4
- classiq/interface/generator/arith/binary_ops.py +7 -36
- classiq/interface/generator/arith/logical_ops.py +2 -3
- classiq/interface/generator/arith/number_utils.py +2 -2
- classiq/interface/generator/arith/register_user_input.py +2 -2
- classiq/interface/generator/arith/unary_ops.py +2 -2
- classiq/interface/generator/circuit_code/circuit_code.py +8 -10
- classiq/interface/generator/circuit_code/types_and_constants.py +1 -1
- classiq/interface/generator/complex_type.py +2 -2
- classiq/interface/generator/copy.py +1 -3
- classiq/interface/generator/expressions/atomic_expression_functions.py +0 -5
- classiq/interface/generator/expressions/evaluated_expression.py +2 -3
- classiq/interface/generator/expressions/expression.py +2 -2
- classiq/interface/generator/expressions/proxies/classical/classical_array_proxy.py +4 -7
- classiq/interface/generator/function_param_list.py +0 -20
- classiq/interface/generator/function_params.py +5 -6
- classiq/interface/generator/functions/classical_function_declaration.py +2 -2
- classiq/interface/generator/functions/classical_type.py +3 -3
- classiq/interface/generator/functions/type_modifier.py +0 -14
- classiq/interface/generator/functions/type_name.py +2 -2
- classiq/interface/generator/generated_circuit_data.py +12 -13
- classiq/interface/generator/hamiltonian_evolution/exponentiation.py +2 -4
- classiq/interface/generator/hardware/hardware_data.py +8 -8
- classiq/interface/generator/hardware_efficient_ansatz.py +8 -8
- classiq/interface/generator/mcu.py +3 -3
- classiq/interface/generator/mcx.py +3 -3
- classiq/interface/generator/model/constraints.py +34 -5
- classiq/interface/generator/model/preferences/preferences.py +15 -21
- classiq/interface/generator/model/quantum_register.py +7 -10
- classiq/interface/generator/noise_properties.py +3 -7
- classiq/interface/generator/parameters.py +1 -1
- classiq/interface/generator/partitioned_register.py +1 -2
- classiq/interface/generator/preferences/qasm_to_qmod_params.py +11 -0
- classiq/interface/generator/qsvm.py +2 -2
- classiq/interface/generator/quantum_function_call.py +8 -11
- classiq/interface/generator/quantum_program.py +12 -15
- classiq/interface/generator/range_types.py +3 -3
- classiq/interface/generator/slice_parsing_utils.py +4 -5
- classiq/interface/generator/standard_gates/standard_gates.py +2 -4
- classiq/interface/generator/state_preparation/state_preparation.py +6 -8
- classiq/interface/generator/synthesis_execution_parameter.py +1 -3
- classiq/interface/generator/synthesis_metadata/synthesis_execution_data.py +2 -3
- classiq/interface/generator/transpiler_basis_gates.py +2 -4
- classiq/interface/generator/types/builtin_enum_declarations.py +0 -136
- classiq/interface/generator/types/compilation_metadata.py +12 -1
- classiq/interface/generator/types/enum_declaration.py +2 -1
- classiq/interface/generator/validations/flow_graph.py +3 -3
- classiq/interface/generator/visitor.py +10 -12
- classiq/interface/hardware.py +2 -2
- classiq/interface/helpers/classproperty.py +2 -2
- classiq/interface/helpers/custom_encoders.py +2 -1
- classiq/interface/helpers/custom_pydantic_types.py +1 -1
- classiq/interface/helpers/text_utils.py +1 -4
- classiq/interface/ide/visual_model.py +5 -5
- classiq/interface/jobs.py +3 -3
- classiq/interface/model/allocate.py +4 -4
- classiq/interface/model/block.py +2 -2
- classiq/interface/model/bounds.py +3 -3
- classiq/interface/model/control.py +1 -1
- classiq/interface/model/inplace_binary_operation.py +2 -2
- classiq/interface/model/model.py +4 -4
- classiq/interface/model/parameter.py +1 -3
- classiq/interface/model/port_declaration.py +1 -1
- classiq/interface/model/quantum_expressions/quantum_expression.py +1 -2
- classiq/interface/model/quantum_function_call.py +3 -6
- classiq/interface/model/quantum_function_declaration.py +1 -0
- classiq/interface/model/quantum_lambda_function.py +4 -4
- classiq/interface/model/quantum_statement.py +4 -4
- classiq/interface/model/quantum_type.py +14 -14
- classiq/interface/model/validation_handle.py +2 -3
- classiq/interface/model/variable_declaration_statement.py +2 -2
- classiq/interface/pretty_print/expression_to_qmod.py +3 -4
- classiq/interface/server/routes.py +0 -4
- classiq/interface/source_reference.py +3 -4
- classiq/model_expansions/arithmetic.py +6 -7
- classiq/model_expansions/arithmetic_compute_result_attrs.py +4 -5
- classiq/model_expansions/capturing/captured_vars.py +3 -3
- classiq/model_expansions/capturing/mangling_utils.py +1 -2
- classiq/model_expansions/closure.py +12 -11
- classiq/model_expansions/function_builder.py +14 -6
- classiq/model_expansions/generative_functions.py +1 -4
- classiq/model_expansions/interpreters/base_interpreter.py +2 -6
- classiq/model_expansions/interpreters/generative_interpreter.py +5 -3
- classiq/model_expansions/quantum_operations/allocate.py +4 -4
- classiq/model_expansions/quantum_operations/assignment_result_processor.py +2 -4
- classiq/model_expansions/quantum_operations/call_emitter.py +31 -37
- classiq/model_expansions/quantum_operations/declarative_call_emitter.py +2 -2
- classiq/model_expansions/quantum_operations/emitter.py +3 -5
- classiq/model_expansions/quantum_operations/expression_evaluator.py +3 -3
- classiq/model_expansions/quantum_operations/skip_control_verifier.py +1 -2
- classiq/model_expansions/quantum_operations/variable_decleration.py +2 -2
- classiq/model_expansions/scope.py +7 -7
- classiq/model_expansions/scope_initialization.py +4 -0
- classiq/model_expansions/visitors/symbolic_param_inference.py +3 -3
- classiq/model_expansions/visitors/uncomputation_signature_inference.py +317 -0
- classiq/model_expansions/visitors/variable_references.py +15 -14
- classiq/open_library/functions/__init__.py +6 -0
- classiq/open_library/functions/discrete_sine_cosine_transform.py +5 -5
- classiq/open_library/functions/grover.py +8 -10
- classiq/open_library/functions/modular_exponentiation.py +96 -8
- classiq/qmod/__init__.py +5 -2
- classiq/qmod/builtins/classical_execution_primitives.py +4 -11
- classiq/qmod/builtins/classical_functions.py +1 -42
- classiq/qmod/builtins/enums.py +0 -136
- classiq/qmod/builtins/functions/__init__.py +0 -13
- classiq/qmod/builtins/functions/allocation.py +4 -4
- classiq/qmod/builtins/functions/arithmetic.py +22 -27
- classiq/qmod/builtins/functions/standard_gates.py +27 -27
- classiq/qmod/builtins/operations.py +35 -58
- classiq/qmod/builtins/structs.py +2 -58
- classiq/qmod/cfunc.py +3 -2
- classiq/qmod/classical_function.py +2 -1
- classiq/qmod/cparam.py +2 -8
- classiq/qmod/create_model_function.py +7 -7
- classiq/qmod/declaration_inferrer.py +33 -30
- classiq/qmod/model_state_container.py +2 -2
- classiq/qmod/native/pretty_printer.py +25 -14
- classiq/qmod/pretty_print/expression_to_python.py +5 -3
- classiq/qmod/pretty_print/pretty_printer.py +39 -17
- classiq/qmod/python_classical_type.py +40 -13
- classiq/qmod/qfunc.py +139 -16
- classiq/qmod/qmod_constant.py +2 -2
- classiq/qmod/qmod_parameter.py +5 -2
- classiq/qmod/qmod_variable.py +47 -43
- classiq/qmod/quantum_callable.py +18 -13
- classiq/qmod/quantum_expandable.py +31 -26
- classiq/qmod/quantum_function.py +51 -32
- classiq/qmod/semantics/annotation/call_annotation.py +2 -2
- classiq/qmod/semantics/error_manager.py +5 -6
- classiq/qmod/semantics/lambdas.py +1 -2
- classiq/qmod/semantics/validation/types_validation.py +1 -2
- classiq/qmod/symbolic.py +2 -4
- classiq/qmod/utilities.py +13 -10
- classiq/qmod/write_qmod.py +3 -4
- classiq/quantum_program.py +1 -3
- classiq/synthesis.py +11 -7
- {classiq-0.93.0.dist-info → classiq-0.94.1.dist-info}/METADATA +2 -3
- {classiq-0.93.0.dist-info → classiq-0.94.1.dist-info}/RECORD +238 -260
- classiq/applications/chemistry/ansatz_parameters.py +0 -29
- classiq/applications/chemistry/chemistry_execution_parameters.py +0 -16
- classiq/applications/chemistry/chemistry_model_constructor.py +0 -532
- classiq/applications/chemistry/ground_state_problem.py +0 -42
- classiq/evaluators/qmod_expression_visitors/qmod_expression_bwc.py +0 -129
- classiq/interface/chemistry/elements.py +0 -120
- classiq/interface/chemistry/fermionic_operator.py +0 -208
- classiq/interface/chemistry/ground_state_problem.py +0 -132
- classiq/interface/chemistry/ground_state_result.py +0 -8
- classiq/interface/chemistry/molecule.py +0 -71
- classiq/interface/generator/application_apis/chemistry_declarations.py +0 -69
- classiq/interface/generator/application_apis/entangler_declarations.py +0 -29
- classiq/interface/generator/chemistry_function_params.py +0 -50
- classiq/interface/generator/entangler_params.py +0 -72
- classiq/interface/generator/entanglers.py +0 -14
- classiq/interface/generator/hartree_fock.py +0 -26
- classiq/interface/generator/hva.py +0 -22
- classiq/interface/generator/linear_pauli_rotations.py +0 -92
- classiq/interface/generator/qft.py +0 -37
- classiq/interface/generator/ucc.py +0 -74
- classiq/interface/helpers/backward_compatibility.py +0 -9
- classiq/model_expansions/transformers/type_modifier_inference.py +0 -392
- classiq/qmod/builtins/functions/chemistry.py +0 -123
- {classiq-0.93.0.dist-info → classiq-0.94.1.dist-info}/WHEEL +0 -0
- {classiq-0.93.0.dist-info → classiq-0.94.1.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from collections.abc import Sequence
|
|
2
|
-
from typing import
|
|
2
|
+
from typing import cast
|
|
3
3
|
|
|
4
4
|
from openfermion import MolecularData
|
|
5
5
|
from openfermion.ops import FermionOperator
|
|
@@ -31,7 +31,7 @@ class FermionHamiltonianProblem:
|
|
|
31
31
|
self,
|
|
32
32
|
fermion_hamiltonian: FermionOperator,
|
|
33
33
|
n_particles: tuple[int, int],
|
|
34
|
-
n_orbitals:
|
|
34
|
+
n_orbitals: int | None = None,
|
|
35
35
|
) -> None:
|
|
36
36
|
"""
|
|
37
37
|
Initializes a `FermionHamiltonianProblem` from the fermion hamiltonian, number
|
|
@@ -115,7 +115,7 @@ class FermionHamiltonianProblem:
|
|
|
115
115
|
cls,
|
|
116
116
|
molecule: MolecularData,
|
|
117
117
|
first_active_index: int = 0,
|
|
118
|
-
remove_orbitlas:
|
|
118
|
+
remove_orbitlas: Sequence[int] | None = None,
|
|
119
119
|
op_compression_tol: float = 1e-13,
|
|
120
120
|
) -> "FermionHamiltonianProblem":
|
|
121
121
|
"""
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from collections.abc import Sequence
|
|
2
2
|
from itertools import chain, combinations, product
|
|
3
3
|
from math import factorial
|
|
4
|
-
from typing import Union
|
|
5
4
|
|
|
6
5
|
from openfermion.ops.operators.fermion_operator import FermionOperator
|
|
7
6
|
from openfermion.ops.operators.qubit_operator import QubitOperator
|
|
@@ -17,7 +16,7 @@ from classiq.qmod.builtins.structs import (
|
|
|
17
16
|
def get_ucc_hamiltonians(
|
|
18
17
|
problem: FermionHamiltonianProblem,
|
|
19
18
|
mapper: FermionToQubitMapper,
|
|
20
|
-
excitations:
|
|
19
|
+
excitations: int | Sequence[int],
|
|
21
20
|
) -> list[SparsePauliOp]:
|
|
22
21
|
"""
|
|
23
22
|
Computes the UCC hamiltonians of the given problem in the desired excitations,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from collections.abc import Sequence
|
|
2
2
|
from functools import cached_property
|
|
3
|
-
from typing import TYPE_CHECKING, Any
|
|
3
|
+
from typing import TYPE_CHECKING, Any
|
|
4
4
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
from openfermion.ops.operators.fermion_operator import FermionOperator
|
|
@@ -38,7 +38,7 @@ class Z2SymTaperMapper(FermionToQubitMapper):
|
|
|
38
38
|
generators: Sequence[QubitOperator],
|
|
39
39
|
x_ops: Sequence[QubitOperator],
|
|
40
40
|
method: MappingMethod = MappingMethod.JORDAN_WIGNER,
|
|
41
|
-
sector:
|
|
41
|
+
sector: Sequence[int] | None = None,
|
|
42
42
|
tol: float = 1e-14,
|
|
43
43
|
) -> None:
|
|
44
44
|
"""
|
|
@@ -192,7 +192,7 @@ class Z2SymTaperMapper(FermionToQubitMapper):
|
|
|
192
192
|
generators = _get_z2_symmetries_generators(qubit_op, n_qubits)
|
|
193
193
|
x_ops = _get_x_ops_for_generators(generators, n_qubits)
|
|
194
194
|
|
|
195
|
-
sector:
|
|
195
|
+
sector: list[int] | None = None
|
|
196
196
|
if sector_from_hartree_fock:
|
|
197
197
|
from classiq.applications.chemistry.hartree_fock import get_hf_state
|
|
198
198
|
|
|
@@ -252,7 +252,7 @@ def _get_x_ops_for_generators(
|
|
|
252
252
|
# we look for a column in the Z-part of the matrix which is populated only with
|
|
253
253
|
# 0s except for a 1 in the current generator: a X operation in this column's
|
|
254
254
|
# qubit will anti-commute with the current generator and commute with all others
|
|
255
|
-
found_col:
|
|
255
|
+
found_col: int | None = None
|
|
256
256
|
for col in range(n_qubits):
|
|
257
257
|
if (
|
|
258
258
|
generators[row, n_qubits + col] == 1
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
from typing import Optional
|
|
2
|
-
|
|
3
1
|
from sympy import Add, Expr, LessThan, Symbol
|
|
4
2
|
|
|
5
3
|
from classiq.interface.combinatorial_optimization.encoding_types import EncodingType
|
|
@@ -8,7 +6,7 @@ _INEQUALITY_UPPER_LIMIT = 1
|
|
|
8
6
|
|
|
9
7
|
|
|
10
8
|
def is_constraint_sum_less_than_one(
|
|
11
|
-
expression: Expr, encoding_type:
|
|
9
|
+
expression: Expr, encoding_type: EncodingType | None = None
|
|
12
10
|
) -> bool:
|
|
13
11
|
# tests the case: x_1 + ... + x_n <= 1
|
|
14
12
|
return (
|
|
@@ -2,7 +2,6 @@ from typing import Any
|
|
|
2
2
|
|
|
3
3
|
import sympy
|
|
4
4
|
from pyomo.core.expr.sympy_tools import PyomoSympyBimap
|
|
5
|
-
from scipy.optimize import differential_evolution
|
|
6
5
|
from sympy import Expr
|
|
7
6
|
|
|
8
7
|
from classiq.applications.combinatorial_helpers import encoding_utils
|
|
@@ -17,6 +16,8 @@ def sympy_lambdify(*args: Any, **kwargs: Any) -> Any:
|
|
|
17
16
|
def multivariate_extremum(
|
|
18
17
|
expr: Expr, symbols_map: PyomoSympyBimap, is_min: bool
|
|
19
18
|
) -> float:
|
|
19
|
+
from scipy.optimize import differential_evolution
|
|
20
|
+
|
|
20
21
|
if expr.is_number:
|
|
21
22
|
return float(expr)
|
|
22
23
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import itertools
|
|
2
|
+
from collections.abc import Callable
|
|
2
3
|
from functools import partial
|
|
3
|
-
from typing import Callable, Union
|
|
4
4
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
import pyomo.environ as pyo
|
|
@@ -96,7 +96,7 @@ def _decode_vector_str(
|
|
|
96
96
|
) # reverse qubit order
|
|
97
97
|
|
|
98
98
|
|
|
99
|
-
def _evaluate_operator(operator: SparsePauliOp, state:
|
|
99
|
+
def _evaluate_operator(operator: SparsePauliOp, state: list[int] | str) -> float:
|
|
100
100
|
if isinstance(state, list):
|
|
101
101
|
state = "".join([str(x) for x in state])
|
|
102
102
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import itertools
|
|
2
2
|
from dataclasses import dataclass, field
|
|
3
|
-
from typing import Optional, Union
|
|
4
3
|
|
|
5
4
|
import pyomo.environ as pyo
|
|
6
5
|
from pyomo.core.base.var import VarData
|
|
@@ -47,7 +46,7 @@ class EncodingMapping:
|
|
|
47
46
|
self,
|
|
48
47
|
original_var: VarData,
|
|
49
48
|
encoding_expr: pyo.Expression,
|
|
50
|
-
encodings_vars:
|
|
49
|
+
encodings_vars: list[VarData] | None = None,
|
|
51
50
|
) -> None:
|
|
52
51
|
if encodings_vars is None:
|
|
53
52
|
encodings_vars = list(identify_variables(encoding_expr))
|
|
@@ -73,7 +72,7 @@ class EncodingMapping:
|
|
|
73
72
|
def get_encoding_vars(self, original_var: VarData) -> list[VarData]:
|
|
74
73
|
return self.get_var_expr_mapping(original_var).encodings_vars
|
|
75
74
|
|
|
76
|
-
def get_original_var(self, encoding_var: VarData) ->
|
|
75
|
+
def get_original_var(self, encoding_var: VarData) -> VarData | None:
|
|
77
76
|
for original_var in self.original_vars:
|
|
78
77
|
if pyomo_utils.contains(encoding_var, self.get_encoding_vars(original_var)):
|
|
79
78
|
return original_var
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import math
|
|
2
2
|
from itertools import filterfalse
|
|
3
|
-
from typing import Any,
|
|
3
|
+
from typing import Any, cast
|
|
4
4
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
import pyomo.environ as pyo
|
|
@@ -97,7 +97,7 @@ def encode_constraints(
|
|
|
97
97
|
|
|
98
98
|
def deal_with_trivial_boolean_constraint(
|
|
99
99
|
constraint: _ComponentBase,
|
|
100
|
-
constraint_expression:
|
|
100
|
+
constraint_expression: bool | Expr,
|
|
101
101
|
model: pyo.ConcreteModel,
|
|
102
102
|
) -> None:
|
|
103
103
|
# using '==' on purpose since comparing against sympy's True
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import copy
|
|
2
2
|
from functools import cached_property
|
|
3
3
|
from itertools import filterfalse
|
|
4
|
-
from typing import Optional, Union
|
|
5
4
|
|
|
6
5
|
import pyomo.environ as pyo
|
|
7
6
|
import sympy
|
|
@@ -49,8 +48,8 @@ class OptimizationModel:
|
|
|
49
48
|
self,
|
|
50
49
|
model: ConcreteModel,
|
|
51
50
|
qsolver: QSolver,
|
|
52
|
-
penalty_energy:
|
|
53
|
-
encoding_type:
|
|
51
|
+
penalty_energy: float | None,
|
|
52
|
+
encoding_type: EncodingType | None = None,
|
|
54
53
|
) -> None:
|
|
55
54
|
assert model.nobjectives() == 1, "model must have a single objective"
|
|
56
55
|
model_copy = copy.deepcopy(model)
|
|
@@ -130,7 +129,7 @@ class OptimizationModel:
|
|
|
130
129
|
|
|
131
130
|
def _get_penalty_term(
|
|
132
131
|
self, constraints: list[_GeneralConstraintData]
|
|
133
|
-
) ->
|
|
132
|
+
) -> int | Expression:
|
|
134
133
|
normalized_penalty_term = penalty.get_penalty_expression(constraints)
|
|
135
134
|
return self.penalty_energy * normalized_penalty_term * self.sign
|
|
136
135
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Any
|
|
1
|
+
from typing import Any
|
|
2
2
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
|
|
@@ -23,7 +23,7 @@ class SparsePauliOp:
|
|
|
23
23
|
new_coeffs = np.concatenate([self.coeffs, other.coeffs])
|
|
24
24
|
return SparsePauliOp(new_paulis, new_coeffs)
|
|
25
25
|
|
|
26
|
-
def __mul__(self, other:
|
|
26
|
+
def __mul__(self, other: int | float | complex) -> "SparsePauliOp":
|
|
27
27
|
"""Scalar multiplication of a SparsePauliOp."""
|
|
28
28
|
if not isinstance(other, (int, float, complex)):
|
|
29
29
|
raise ValueError("Can only multiply by scalar values.")
|
|
@@ -8,7 +8,7 @@ from enum import Enum
|
|
|
8
8
|
from functools import reduce
|
|
9
9
|
from operator import mul
|
|
10
10
|
from types import CodeType
|
|
11
|
-
from typing import Any,
|
|
11
|
+
from typing import Any, TypeVar
|
|
12
12
|
|
|
13
13
|
import pydantic
|
|
14
14
|
import pyomo.core.expr.numeric_expr as pyo_expr
|
|
@@ -132,7 +132,7 @@ def _delete_element_by_value(dict_: dict, value: Any) -> None:
|
|
|
132
132
|
del dict_[k]
|
|
133
133
|
|
|
134
134
|
|
|
135
|
-
def get_name(component:
|
|
135
|
+
def get_name(component: IndexedComponent | ComponentData) -> str:
|
|
136
136
|
if isinstance(component, IndexedComponent):
|
|
137
137
|
return component._name # constraint.name returns "'{name}'"
|
|
138
138
|
else:
|
|
@@ -141,8 +141,8 @@ def get_name(component: Union[IndexedComponent, ComponentData]) -> str:
|
|
|
141
141
|
|
|
142
142
|
class FixedSympy2PyomoVisitor(Sympy2PyomoVisitor):
|
|
143
143
|
def beforeChild( # noqa: N802
|
|
144
|
-
self, node:
|
|
145
|
-
) -> tuple[bool,
|
|
144
|
+
self, node: sympy.Expr | None, child: sympy.Expr, child_idx: int | None
|
|
145
|
+
) -> tuple[bool, int | float | None]:
|
|
146
146
|
if not child._args:
|
|
147
147
|
item = self.object_map.getPyomoSymbol(child, None)
|
|
148
148
|
if item is None:
|
|
@@ -300,7 +300,7 @@ def _get_array_sizes(vars: list[VarData]) -> dict[str, tuple[int, ...]]:
|
|
|
300
300
|
|
|
301
301
|
def _get_indices_dimensions(
|
|
302
302
|
indices: set[tuple[int, ...]], *, strict: bool
|
|
303
|
-
) ->
|
|
303
|
+
) -> tuple[int, ...] | None:
|
|
304
304
|
indices_list = list(indices)
|
|
305
305
|
if len(indices) == 0:
|
|
306
306
|
return None
|
|
@@ -365,7 +365,7 @@ def _get_qmod_field_type(var_name: str, var_data: VarData) -> type[QVar]:
|
|
|
365
365
|
|
|
366
366
|
|
|
367
367
|
def evaluate_objective(
|
|
368
|
-
var_mapping: dict[Any,
|
|
368
|
+
var_mapping: dict[Any, str | tuple[str, tuple[int, ...]]],
|
|
369
369
|
sympy_expr: sympy.Expr,
|
|
370
370
|
code_expr: CodeType,
|
|
371
371
|
struct_obj: Any,
|
|
@@ -411,7 +411,7 @@ def is_index_var(var: VarData) -> bool:
|
|
|
411
411
|
)
|
|
412
412
|
|
|
413
413
|
|
|
414
|
-
def index_as_tuple(index:
|
|
414
|
+
def index_as_tuple(index: int | tuple[int, ...]) -> tuple[int, ...]:
|
|
415
415
|
if isinstance(index, int):
|
|
416
416
|
return (index,)
|
|
417
417
|
return index
|
|
@@ -435,7 +435,7 @@ def add_var_domain_constraints(model: ConcreteModel) -> Iterator[None]:
|
|
|
435
435
|
model.del_component("var_domain_constraints")
|
|
436
436
|
|
|
437
437
|
|
|
438
|
-
def _get_var_domain_constraint(var: VarData) ->
|
|
438
|
+
def _get_var_domain_constraint(var: VarData) -> ExpressionBase | None:
|
|
439
439
|
bounds = var.bounds
|
|
440
440
|
if (
|
|
441
441
|
type(bounds) is not tuple
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
from typing import Optional
|
|
2
|
-
|
|
3
1
|
import pyomo.core as pyo
|
|
4
2
|
from pyomo.core.base.var import VarData
|
|
5
3
|
from pyomo.core.expr.sympy_tools import Pyomo2SympyVisitor, PyomoSympyBimap
|
|
@@ -14,7 +12,7 @@ def sympyify_vars(variables: list[VarData]) -> PyomoSympyBimap:
|
|
|
14
12
|
|
|
15
13
|
|
|
16
14
|
def sympyify_expression(
|
|
17
|
-
expression: pyo.Expression, symbols_map:
|
|
15
|
+
expression: pyo.Expression, symbols_map: PyomoSympyBimap | None = None
|
|
18
16
|
) -> Expr:
|
|
19
17
|
if symbols_map is None:
|
|
20
18
|
symbols_map = PyomoSympyBimap()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import copy
|
|
2
|
+
from collections.abc import Callable
|
|
2
3
|
from itertools import chain, product
|
|
3
|
-
from typing import Callable, Union
|
|
4
4
|
|
|
5
5
|
import pyomo.environ as pyo
|
|
6
6
|
from pyomo.core.base.var import VarData
|
|
@@ -25,7 +25,7 @@ def _make_invalid_encoding_type_error(
|
|
|
25
25
|
)
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def encoding_length(var: VarData, encoding_type:
|
|
28
|
+
def encoding_length(var: VarData, encoding_type: EncodingType | None) -> int:
|
|
29
29
|
if encoding_type is None:
|
|
30
30
|
return 1
|
|
31
31
|
|
|
@@ -172,7 +172,7 @@ class ModelEncoder:
|
|
|
172
172
|
def encode_expr(
|
|
173
173
|
self,
|
|
174
174
|
expr: pyo.Expression,
|
|
175
|
-
substitution_dict:
|
|
175
|
+
substitution_dict: dict[int, pyo.Expression] | None = None,
|
|
176
176
|
) -> pyo.Expression:
|
|
177
177
|
if substitution_dict is None:
|
|
178
178
|
substitution_dict = self.vars_encoding_mapping.substitution_dict
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import copy
|
|
2
|
-
from typing import Union
|
|
3
2
|
|
|
4
3
|
from pyomo.core import ConcreteModel, Var
|
|
5
4
|
from pyomo.core.base.constraint import _GeneralConstraintData
|
|
@@ -125,7 +124,7 @@ def add_fixed_variables_to_solution(
|
|
|
125
124
|
return solution_with_fixed
|
|
126
125
|
|
|
127
126
|
|
|
128
|
-
def _get_value_if_exists(var: VarData) ->
|
|
127
|
+
def _get_value_if_exists(var: VarData) -> int | VarData:
|
|
129
128
|
return var.value if var.value is not None else var
|
|
130
129
|
|
|
131
130
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import dataclasses
|
|
2
2
|
from dataclasses import dataclass
|
|
3
|
-
from typing import Optional
|
|
4
3
|
|
|
5
4
|
from classiq.interface.executor.optimizer_preferences import CostType, OptimizerType
|
|
6
5
|
|
|
@@ -14,10 +13,10 @@ class QAOAConfig:
|
|
|
14
13
|
@dataclass
|
|
15
14
|
class OptimizerConfig:
|
|
16
15
|
opt_type: OptimizerType = OptimizerType.COBYLA
|
|
17
|
-
max_iteration:
|
|
16
|
+
max_iteration: int | None = None
|
|
18
17
|
tolerance: float = 0.0
|
|
19
18
|
step_size: float = 0.0
|
|
20
19
|
skip_compute_variance: bool = False
|
|
21
20
|
cost_type: CostType = CostType.CVAR
|
|
22
21
|
alpha_cvar: float = 1.0
|
|
23
|
-
initial_point:
|
|
22
|
+
initial_point: list[float] | None = dataclasses.field(default=None)
|
classiq/applications/combinatorial_optimization/combinatorial_optimization_model_constructor.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
from typing import Optional
|
|
2
|
-
|
|
3
1
|
from pyomo import environ as pyo
|
|
4
2
|
from pyomo.core import Objective, maximize
|
|
5
3
|
|
|
@@ -47,8 +45,8 @@ with set_global_declarative_switch():
|
|
|
47
45
|
|
|
48
46
|
def construct_combi_opt_py_model(
|
|
49
47
|
pyo_model: pyo.ConcreteModel,
|
|
50
|
-
qaoa_config:
|
|
51
|
-
optimizer_config:
|
|
48
|
+
qaoa_config: QAOAConfig | None = None,
|
|
49
|
+
optimizer_config: OptimizerConfig | None = None,
|
|
52
50
|
) -> Model:
|
|
53
51
|
if qaoa_config is None:
|
|
54
52
|
qaoa_config = QAOAConfig()
|
|
@@ -137,8 +135,8 @@ save({{"vqe_result": vqe_result, "hamiltonian": hamiltonian}})
|
|
|
137
135
|
|
|
138
136
|
def construct_combinatorial_optimization_model(
|
|
139
137
|
pyo_model: pyo.ConcreteModel,
|
|
140
|
-
qaoa_config:
|
|
141
|
-
optimizer_config:
|
|
138
|
+
qaoa_config: QAOAConfig | None = None,
|
|
139
|
+
optimizer_config: OptimizerConfig | None = None,
|
|
142
140
|
) -> SerializedModel:
|
|
143
141
|
converted_pyo_model = convert_pyomo_to_global_presentation(pyo_model)
|
|
144
142
|
model = construct_combi_opt_py_model(
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import math
|
|
2
2
|
import re
|
|
3
|
-
from
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from typing import TYPE_CHECKING, cast
|
|
4
5
|
|
|
5
6
|
import numpy as np
|
|
6
|
-
import pandas as pd
|
|
7
7
|
import pyomo.core as pyo
|
|
8
|
-
import scipy
|
|
9
8
|
|
|
10
9
|
from classiq.interface.executor.execution_preferences import ExecutionPreferences
|
|
11
10
|
from classiq.interface.executor.result import ExecutionDetails
|
|
@@ -29,6 +28,9 @@ from classiq.qmod.qmod_parameter import CArray
|
|
|
29
28
|
from classiq.qmod.qmod_variable import Output, QVar
|
|
30
29
|
from classiq.synthesis import synthesize
|
|
31
30
|
|
|
31
|
+
if TYPE_CHECKING:
|
|
32
|
+
import pandas as pd
|
|
33
|
+
|
|
32
34
|
|
|
33
35
|
class CombinatorialProblem:
|
|
34
36
|
def __init__(
|
|
@@ -62,8 +64,8 @@ class CombinatorialProblem:
|
|
|
62
64
|
|
|
63
65
|
def get_model(
|
|
64
66
|
self,
|
|
65
|
-
constraints:
|
|
66
|
-
preferences:
|
|
67
|
+
constraints: Constraints | None = None,
|
|
68
|
+
preferences: Preferences | None = None,
|
|
67
69
|
) -> SerializedModel:
|
|
68
70
|
@qfunc
|
|
69
71
|
def main(
|
|
@@ -93,10 +95,10 @@ class CombinatorialProblem:
|
|
|
93
95
|
|
|
94
96
|
def optimize(
|
|
95
97
|
self,
|
|
96
|
-
execution_preferences:
|
|
98
|
+
execution_preferences: ExecutionPreferences | None = None,
|
|
97
99
|
maxiter: int = 20,
|
|
98
100
|
quantile: float = 1.0,
|
|
99
|
-
initial_params:
|
|
101
|
+
initial_params: list[float] | None = None,
|
|
100
102
|
) -> list[float]:
|
|
101
103
|
if self.qprog_ is None:
|
|
102
104
|
self.get_qprog()
|
|
@@ -131,10 +133,12 @@ class CombinatorialProblem:
|
|
|
131
133
|
self._es = _es
|
|
132
134
|
return _optimized_params
|
|
133
135
|
|
|
134
|
-
def sample_uniform(self) -> pd.DataFrame:
|
|
136
|
+
def sample_uniform(self) -> "pd.DataFrame":
|
|
135
137
|
return self.sample([0] * self.num_layers_ * 2)
|
|
136
138
|
|
|
137
|
-
def sample(self, params: list) -> pd.DataFrame:
|
|
139
|
+
def sample(self, params: list) -> "pd.DataFrame":
|
|
140
|
+
import pandas as pd
|
|
141
|
+
|
|
138
142
|
assert self._es is not None
|
|
139
143
|
res = self._es.sample({"params": params})
|
|
140
144
|
parsed_result = [
|
|
@@ -161,7 +165,7 @@ def execute_qaoa(
|
|
|
161
165
|
cost_func: Callable,
|
|
162
166
|
num_layers: int,
|
|
163
167
|
maxiter: int,
|
|
164
|
-
execution_preferences:
|
|
168
|
+
execution_preferences: ExecutionPreferences | None = None,
|
|
165
169
|
) -> tuple[SerializedModel, QuantumProgram, ExecutionDetails]:
|
|
166
170
|
"""
|
|
167
171
|
Implements a simple QAOA algorithm, including the creation and synthesis of the QAOA
|
|
@@ -178,6 +182,7 @@ def execute_qaoa(
|
|
|
178
182
|
a tuple containing the model of the QAOA ansatz, the corresponding synthesized quantum program,
|
|
179
183
|
and the result of the execution with the optimized parameters
|
|
180
184
|
"""
|
|
185
|
+
import scipy
|
|
181
186
|
|
|
182
187
|
@qfunc
|
|
183
188
|
def main(
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
from typing import Union
|
|
2
|
-
|
|
3
1
|
import numpy as np
|
|
4
2
|
from sympy import fwht
|
|
5
3
|
|
|
@@ -56,7 +54,7 @@ def _coefficents_for_set(mat: np.ndarray, set_index: int) -> list:
|
|
|
56
54
|
|
|
57
55
|
def _get_signed_coefficient(
|
|
58
56
|
c: complex, k: int, i: int, is_hermitian: bool
|
|
59
|
-
) ->
|
|
57
|
+
) -> complex | float:
|
|
60
58
|
# correct from iY to Y
|
|
61
59
|
coef = complex((1j) ** ((i & k).bit_count()) * c)
|
|
62
60
|
if is_hermitian:
|
|
@@ -131,7 +129,7 @@ def _sparse_pauli_to_list(operator: SparsePauliOp) -> list[PauliTerm]:
|
|
|
131
129
|
|
|
132
130
|
# return matrix from hamiltonian
|
|
133
131
|
def hamiltonian_to_matrix(
|
|
134
|
-
hamiltonian:
|
|
132
|
+
hamiltonian: list[PauliTerm] | SparsePauliOp,
|
|
135
133
|
) -> np.ndarray:
|
|
136
134
|
if isinstance(hamiltonian, SparsePauliOp):
|
|
137
135
|
hamiltonian = _sparse_pauli_to_list(hamiltonian)
|
|
@@ -143,3 +141,7 @@ def hamiltonian_to_matrix(
|
|
|
143
141
|
matrix += p.coefficient * pauli_string_to_mat(p.pauli)
|
|
144
142
|
|
|
145
143
|
return matrix
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def pauli_operator_to_matrix(pauli_op: SparsePauliOp) -> np.ndarray:
|
|
147
|
+
return hamiltonian_to_matrix(_sparse_pauli_to_list(pauli_op))
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Literal,
|
|
1
|
+
from typing import Literal, cast
|
|
2
2
|
|
|
3
3
|
from classiq.interface.applications.iqae.generic_iqae import GenericIQAE
|
|
4
4
|
from classiq.interface.applications.iqae.iqae_result import (
|
|
@@ -46,15 +46,15 @@ class IQAE:
|
|
|
46
46
|
self,
|
|
47
47
|
state_prep_op: QCallable[QArray[QBit, Literal["problem_vars_size"]], QBit],
|
|
48
48
|
problem_vars_size: int,
|
|
49
|
-
constraints:
|
|
50
|
-
preferences:
|
|
49
|
+
constraints: Constraints | None = None,
|
|
50
|
+
preferences: Preferences | None = None,
|
|
51
51
|
) -> None:
|
|
52
52
|
self._state_prep_op = state_prep_op
|
|
53
53
|
self._problem_vars_size: int = problem_vars_size
|
|
54
|
-
self._constraints:
|
|
55
|
-
self._preferences:
|
|
56
|
-
self._model:
|
|
57
|
-
self._qprog:
|
|
54
|
+
self._constraints: Constraints | None = constraints
|
|
55
|
+
self._preferences: Preferences | None = preferences
|
|
56
|
+
self._model: SerializedModel | None = None
|
|
57
|
+
self._qprog: QuantumProgram | None = None
|
|
58
58
|
|
|
59
59
|
"""
|
|
60
60
|
Args:
|
|
@@ -133,7 +133,7 @@ class IQAE:
|
|
|
133
133
|
self,
|
|
134
134
|
epsilon: float,
|
|
135
135
|
alpha: float,
|
|
136
|
-
execution_preferences:
|
|
136
|
+
execution_preferences: ExecutionPreferences | None = None,
|
|
137
137
|
) -> IQAEResult:
|
|
138
138
|
"""
|
|
139
139
|
Executes IQAE's quantum program with the provided epsilon, alpha, and execution
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
2
|
from collections.abc import Sequence
|
|
3
|
-
from typing import Literal,
|
|
3
|
+
from typing import Literal, TypeVar
|
|
4
4
|
|
|
5
5
|
from torch import Tensor, tensor
|
|
6
6
|
from torch.utils.data import Dataset
|
|
@@ -16,8 +16,8 @@ class MyDataset(Dataset, ABC):
|
|
|
16
16
|
def __init__(
|
|
17
17
|
self,
|
|
18
18
|
n: int = 2,
|
|
19
|
-
transform:
|
|
20
|
-
target_transform:
|
|
19
|
+
transform: Transform | None = None,
|
|
20
|
+
target_transform: Transform | None = None,
|
|
21
21
|
) -> None:
|
|
22
22
|
self._n = n
|
|
23
23
|
self.transform = transform
|
|
@@ -55,9 +55,9 @@ class MySubsetDataset(MyDataset, ABC):
|
|
|
55
55
|
def __init__(
|
|
56
56
|
self,
|
|
57
57
|
n: int = 2,
|
|
58
|
-
subset:
|
|
59
|
-
transform:
|
|
60
|
-
target_transform:
|
|
58
|
+
subset: list[int] | Literal["all"] = "all",
|
|
59
|
+
transform: Transform | None = None,
|
|
60
|
+
target_transform: Transform | None = None,
|
|
61
61
|
) -> None:
|
|
62
62
|
super().__init__(n, transform, target_transform)
|
|
63
63
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Literal
|
|
1
|
+
from typing import Literal
|
|
2
2
|
|
|
3
3
|
from classiq.applications.qnn.datasets.dataset_base_classes import MySubsetDataset
|
|
4
4
|
from classiq.applications.qnn.types import DataAndLabel, Transform
|
|
@@ -8,10 +8,10 @@ class DatasetSubsetParity(MySubsetDataset):
|
|
|
8
8
|
def __init__(
|
|
9
9
|
self,
|
|
10
10
|
n: int = 2,
|
|
11
|
-
subset:
|
|
11
|
+
subset: list[int] | Literal["all"] = "all",
|
|
12
12
|
add_readout_qubit: bool = True,
|
|
13
|
-
transform:
|
|
14
|
-
target_transform:
|
|
13
|
+
transform: Transform | None = None,
|
|
14
|
+
target_transform: Transform | None = None,
|
|
15
15
|
) -> None:
|
|
16
16
|
super().__init__(n, subset, transform, target_transform)
|
|
17
17
|
|
|
@@ -35,7 +35,7 @@ class DatasetParity(DatasetSubsetParity):
|
|
|
35
35
|
self,
|
|
36
36
|
n: int = 2,
|
|
37
37
|
add_readout_qubit: bool = True,
|
|
38
|
-
transform:
|
|
39
|
-
target_transform:
|
|
38
|
+
transform: Transform | None = None,
|
|
39
|
+
target_transform: Transform | None = None,
|
|
40
40
|
) -> None:
|
|
41
41
|
super().__init__(n, "all", add_readout_qubit, transform, target_transform)
|
|
@@ -2,7 +2,8 @@ import functools
|
|
|
2
2
|
import inspect
|
|
3
3
|
import os
|
|
4
4
|
import typing
|
|
5
|
-
from
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any, overload
|
|
6
7
|
|
|
7
8
|
import more_itertools
|
|
8
9
|
import torch
|
|
@@ -41,14 +42,14 @@ from classiq.execution.qnn import _MAX_ARGUMENTS_SIZE, _execute_qnn_sample
|
|
|
41
42
|
|
|
42
43
|
class QLayerFunction(torch.autograd.Function):
|
|
43
44
|
@staticmethod
|
|
44
|
-
def forward(
|
|
45
|
+
def forward(
|
|
45
46
|
ctx: Any,
|
|
46
47
|
inputs: Tensor,
|
|
47
48
|
weights: Tensor,
|
|
48
49
|
quantum_program: QuantumProgram,
|
|
49
50
|
execute: ExecuteFunction,
|
|
50
51
|
post_process: PostProcessFunction,
|
|
51
|
-
epsilon:
|
|
52
|
+
epsilon: float | None = EPSILON,
|
|
52
53
|
) -> Tensor:
|
|
53
54
|
"""
|
|
54
55
|
This function receives:
|
|
@@ -105,7 +106,7 @@ class QLayerFunction(torch.autograd.Function):
|
|
|
105
106
|
@staticmethod
|
|
106
107
|
def backward( # type: ignore[override]
|
|
107
108
|
ctx: Any, grad_output: Tensor
|
|
108
|
-
) -> tuple[
|
|
109
|
+
) -> tuple[Tensor | None, Tensor | None, None, None, None, None]:
|
|
109
110
|
"""
|
|
110
111
|
grad_output: Tensor
|
|
111
112
|
is of shape (ctx.batch_size, ctx.num_out_features)
|
|
@@ -159,7 +160,7 @@ class QLayer(nn.Module):
|
|
|
159
160
|
execute: ExecuteFunction,
|
|
160
161
|
post_process: PostProcessFunction,
|
|
161
162
|
# Optional parameters:
|
|
162
|
-
head_start:
|
|
163
|
+
head_start: float | Tensor | None = None,
|
|
163
164
|
# Experimental parameters:
|
|
164
165
|
calc_num_out_features: CalcNumOutFeatures = calc_num_out_features_single_output,
|
|
165
166
|
epsilon: float = EPSILON,
|
|
@@ -172,7 +173,7 @@ class QLayer(nn.Module):
|
|
|
172
173
|
post_process: PostProcessFunction,
|
|
173
174
|
/,
|
|
174
175
|
# Optional parameters:
|
|
175
|
-
head_start:
|
|
176
|
+
head_start: float | Tensor | None = None,
|
|
176
177
|
# Experimental parameters:
|
|
177
178
|
calc_num_out_features: CalcNumOutFeatures = calc_num_out_features_single_output,
|
|
178
179
|
epsilon: float = EPSILON,
|
|
@@ -207,7 +208,7 @@ class QLayer(nn.Module):
|
|
|
207
208
|
circuit: Circuit,
|
|
208
209
|
execute: ExecuteFunction,
|
|
209
210
|
post_process: PostProcessFunction,
|
|
210
|
-
head_start:
|
|
211
|
+
head_start: float | Tensor | None = None,
|
|
211
212
|
calc_num_out_features: CalcNumOutFeatures = calc_num_out_features_single_output,
|
|
212
213
|
epsilon: float = EPSILON,
|
|
213
214
|
) -> None:
|