luna-quantum 1.0.8rc2__cp314-cp314-macosx_10_12_x86_64.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 luna-quantum might be problematic. Click here for more details.
- luna_quantum/__init__.py +121 -0
- luna_quantum/__init__.pyi +85 -0
- luna_quantum/_core.cpython-314-darwin.so +0 -0
- luna_quantum/_core.pyi +4185 -0
- luna_quantum/algorithms/__init__.py +1 -0
- luna_quantum/aqm_overwrites/__init__.py +3 -0
- luna_quantum/aqm_overwrites/model.py +184 -0
- luna_quantum/backends/__init__.py +1 -0
- luna_quantum/client/__init__.py +0 -0
- luna_quantum/client/controllers/__init__.py +4 -0
- luna_quantum/client/controllers/luna_http_client.py +37 -0
- luna_quantum/client/controllers/luna_platform_client.py +256 -0
- luna_quantum/client/controllers/luna_q.py +67 -0
- luna_quantum/client/controllers/luna_solve.py +129 -0
- luna_quantum/client/error/__init__.py +0 -0
- luna_quantum/client/error/luna_api_key_invalid_error.py +10 -0
- luna_quantum/client/error/luna_api_key_missing_error.py +10 -0
- luna_quantum/client/error/luna_error.py +2 -0
- luna_quantum/client/error/luna_server_error.py +20 -0
- luna_quantum/client/error/timeout_error.py +12 -0
- luna_quantum/client/error/transformation_error.py +18 -0
- luna_quantum/client/error/utils/__init__.py +0 -0
- luna_quantum/client/error/utils/http_error_utils.py +112 -0
- luna_quantum/client/interfaces/__init__.py +4 -0
- luna_quantum/client/interfaces/clients/__init__.py +25 -0
- luna_quantum/client/interfaces/clients/circuit_rest_client_i.py +68 -0
- luna_quantum/client/interfaces/clients/info_rest_client_i.py +53 -0
- luna_quantum/client/interfaces/clients/model_rest_client_i.py +139 -0
- luna_quantum/client/interfaces/clients/qpu_token_rest_client_i.py +364 -0
- luna_quantum/client/interfaces/clients/rest_client_i.py +21 -0
- luna_quantum/client/interfaces/clients/solve_job_rest_client_i.py +201 -0
- luna_quantum/client/interfaces/clients/users_rest_client_i.py +29 -0
- luna_quantum/client/interfaces/services/__init__.py +0 -0
- luna_quantum/client/interfaces/services/luna_q_i.py +34 -0
- luna_quantum/client/interfaces/services/luna_solve_i.py +72 -0
- luna_quantum/client/interfaces/services/service_i.py +56 -0
- luna_quantum/client/rest_client/__init__.py +15 -0
- luna_quantum/client/rest_client/circuit_rest_client.py +107 -0
- luna_quantum/client/rest_client/info_rest_client.py +74 -0
- luna_quantum/client/rest_client/model_rest_client.py +216 -0
- luna_quantum/client/rest_client/qpu_token_rest_client.py +508 -0
- luna_quantum/client/rest_client/solve_job_rest_client.py +286 -0
- luna_quantum/client/rest_client/users_rest_client.py +35 -0
- luna_quantum/client/schemas/__init__.py +26 -0
- luna_quantum/client/schemas/circuit.py +48 -0
- luna_quantum/client/schemas/create/__init__.py +15 -0
- luna_quantum/client/schemas/create/circuit.py +30 -0
- luna_quantum/client/schemas/create/optimization.py +39 -0
- luna_quantum/client/schemas/create/qpu_token.py +22 -0
- luna_quantum/client/schemas/create/qpu_token_time_quota.py +35 -0
- luna_quantum/client/schemas/create/qpu_token_time_quota_update.py +24 -0
- luna_quantum/client/schemas/create/qubo.py +19 -0
- luna_quantum/client/schemas/create/solve_job_create.py +43 -0
- luna_quantum/client/schemas/enums/__init__.py +0 -0
- luna_quantum/client/schemas/enums/call_style.py +13 -0
- luna_quantum/client/schemas/enums/circuit.py +42 -0
- luna_quantum/client/schemas/enums/model_format.py +11 -0
- luna_quantum/client/schemas/enums/problem.py +50 -0
- luna_quantum/client/schemas/enums/qpu_token_type.py +20 -0
- luna_quantum/client/schemas/enums/sense.py +8 -0
- luna_quantum/client/schemas/enums/status.py +40 -0
- luna_quantum/client/schemas/enums/timeframe.py +11 -0
- luna_quantum/client/schemas/error_message.py +14 -0
- luna_quantum/client/schemas/model_metadata.py +35 -0
- luna_quantum/client/schemas/qpu_token/__init__.py +0 -0
- luna_quantum/client/schemas/qpu_token/qpu_token.py +154 -0
- luna_quantum/client/schemas/qpu_token/qpu_token_source.py +19 -0
- luna_quantum/client/schemas/qpu_token/qpu_token_time_quota.py +30 -0
- luna_quantum/client/schemas/qpu_token/token_provider.py +132 -0
- luna_quantum/client/schemas/representation.py +19 -0
- luna_quantum/client/schemas/solution.py +106 -0
- luna_quantum/client/schemas/solve_job.py +50 -0
- luna_quantum/client/schemas/solver_info.py +11 -0
- luna_quantum/client/schemas/user.py +11 -0
- luna_quantum/client/schemas/wrappers/__init__.py +5 -0
- luna_quantum/client/schemas/wrappers/datetime_wrapper.py +32 -0
- luna_quantum/client/utils/__init__.py +0 -0
- luna_quantum/client/utils/qpu_token_utils.py +147 -0
- luna_quantum/config.py +11 -0
- luna_quantum/decorators.py +248 -0
- luna_quantum/errors.py +34 -0
- luna_quantum/errors.pyi +287 -0
- luna_quantum/exceptions/__init__.py +0 -0
- luna_quantum/exceptions/base_luna_quantum_error.py +2 -0
- luna_quantum/exceptions/patch_class_field_exists_error.py +10 -0
- luna_quantum/factories/__init__.py +4 -0
- luna_quantum/factories/luna_solve_client_factory.py +100 -0
- luna_quantum/factories/usecase_factory.py +457 -0
- luna_quantum/py.typed +0 -0
- luna_quantum/solve/__init__.py +13 -0
- luna_quantum/solve/default_token.py +304 -0
- luna_quantum/solve/domain/__init__.py +0 -0
- luna_quantum/solve/domain/abstract/__init__.py +4 -0
- luna_quantum/solve/domain/abstract/luna_algorithm.py +205 -0
- luna_quantum/solve/domain/abstract/qpu_token_backend.py +34 -0
- luna_quantum/solve/domain/model_metadata.py +56 -0
- luna_quantum/solve/domain/solve_job.py +196 -0
- luna_quantum/solve/errors/__init__.py +0 -0
- luna_quantum/solve/errors/incompatible_backend_error.py +15 -0
- luna_quantum/solve/errors/model_metadata_missing_error.py +11 -0
- luna_quantum/solve/errors/solve_base_error.py +5 -0
- luna_quantum/solve/errors/token_missing_error.py +11 -0
- luna_quantum/solve/interfaces/__init__.py +0 -0
- luna_quantum/solve/interfaces/algorithm_i.py +49 -0
- luna_quantum/solve/interfaces/backend_i.py +28 -0
- luna_quantum/solve/interfaces/usecases/__init__.py +55 -0
- luna_quantum/solve/interfaces/usecases/model_delete_usecase_i.py +27 -0
- luna_quantum/solve/interfaces/usecases/model_fetch_metadata_usecase_i.py +33 -0
- luna_quantum/solve/interfaces/usecases/model_get_solutions_usecase_i.py +33 -0
- luna_quantum/solve/interfaces/usecases/model_get_solve_jobs_usecase_i.py +33 -0
- luna_quantum/solve/interfaces/usecases/model_load_by_id_usecase_i.py +32 -0
- luna_quantum/solve/interfaces/usecases/model_load_by_metadata_usecase_i.py +37 -0
- luna_quantum/solve/interfaces/usecases/model_load_metadata_by_hash_usecase_i.py +38 -0
- luna_quantum/solve/interfaces/usecases/model_save_usecase_i.py +36 -0
- luna_quantum/solve/interfaces/usecases/solve_job_cancel_usecase_i.py +33 -0
- luna_quantum/solve/interfaces/usecases/solve_job_create_usecase_i.py +44 -0
- luna_quantum/solve/interfaces/usecases/solve_job_delete_usecase_i.py +32 -0
- luna_quantum/solve/interfaces/usecases/solve_job_fetch_updates_usecase_i.py +38 -0
- luna_quantum/solve/interfaces/usecases/solve_job_get_result_usecase_i.py +63 -0
- luna_quantum/solve/parameters/__init__.py +0 -0
- luna_quantum/solve/parameters/algorithms/__init__.py +51 -0
- luna_quantum/solve/parameters/algorithms/base_params/__init__.py +24 -0
- luna_quantum/solve/parameters/algorithms/base_params/decomposer.py +57 -0
- luna_quantum/solve/parameters/algorithms/base_params/qaoa_circuit_params.py +95 -0
- luna_quantum/solve/parameters/algorithms/base_params/quantum_annealing_params.py +79 -0
- luna_quantum/solve/parameters/algorithms/base_params/scipy_optimizer.py +122 -0
- luna_quantum/solve/parameters/algorithms/base_params/simulated_annealing_params.py +106 -0
- luna_quantum/solve/parameters/algorithms/base_params/tabu_kerberos_params.py +39 -0
- luna_quantum/solve/parameters/algorithms/base_params/tabu_search_params.py +129 -0
- luna_quantum/solve/parameters/algorithms/flexible_parameter_algorithm.py +59 -0
- luna_quantum/solve/parameters/algorithms/genetic_algorithms/__init__.py +4 -0
- luna_quantum/solve/parameters/algorithms/genetic_algorithms/qaga.py +131 -0
- luna_quantum/solve/parameters/algorithms/genetic_algorithms/saga.py +139 -0
- luna_quantum/solve/parameters/algorithms/lq_fda/__init__.py +3 -0
- luna_quantum/solve/parameters/algorithms/lq_fda/fujits_da_base.py +85 -0
- luna_quantum/solve/parameters/algorithms/lq_fda/fujitsu_da_v3c.py +155 -0
- luna_quantum/solve/parameters/algorithms/optimization_solvers/__init__.py +3 -0
- luna_quantum/solve/parameters/algorithms/optimization_solvers/scip.py +51 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/__init__.py +19 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/kerberos.py +149 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/leap_hybrid_bqm.py +75 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/leap_hybrid_cqm.py +75 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/parallel_tempering_qpu.py +139 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/population_annealing_qpu.py +109 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/qbsolv_like_qpu.py +111 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/quantum_annealing.py +121 -0
- luna_quantum/solve/parameters/algorithms/quantum_annealing/repeated_reverse_quantum_annealing.py +174 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/__init__.py +6 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/flex_qaoa/__init__.py +26 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/flex_qaoa/config.py +80 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/flex_qaoa/flex_qaoa.py +226 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/flex_qaoa/optimizers.py +99 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/flex_qaoa/pipeline.py +87 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/qaoa.py +102 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/qaoa_fo.py +69 -0
- luna_quantum/solve/parameters/algorithms/quantum_gate/vqe.py +108 -0
- luna_quantum/solve/parameters/algorithms/search_algorithms/__init__.py +5 -0
- luna_quantum/solve/parameters/algorithms/search_algorithms/dialectic_search.py +136 -0
- luna_quantum/solve/parameters/algorithms/search_algorithms/qbsolv_like_tabu_search.py +117 -0
- luna_quantum/solve/parameters/algorithms/search_algorithms/tabu_search.py +126 -0
- luna_quantum/solve/parameters/algorithms/simulated_annealing/__init__.py +13 -0
- luna_quantum/solve/parameters/algorithms/simulated_annealing/parallel_tempering.py +131 -0
- luna_quantum/solve/parameters/algorithms/simulated_annealing/population_annealing.py +95 -0
- luna_quantum/solve/parameters/algorithms/simulated_annealing/qbsolv_like_simulated_annealing.py +141 -0
- luna_quantum/solve/parameters/algorithms/simulated_annealing/repeated_reverse_simulated_annealing.py +172 -0
- luna_quantum/solve/parameters/algorithms/simulated_annealing/simulated_annealing.py +126 -0
- luna_quantum/solve/parameters/backends/__init__.py +22 -0
- luna_quantum/solve/parameters/backends/aqarios.py +17 -0
- luna_quantum/solve/parameters/backends/aws/__init__.py +11 -0
- luna_quantum/solve/parameters/backends/aws/aws.py +36 -0
- luna_quantum/solve/parameters/backends/aws/aws_backend_base.py +74 -0
- luna_quantum/solve/parameters/backends/aws/ionq.py +43 -0
- luna_quantum/solve/parameters/backends/aws/iqm.py +31 -0
- luna_quantum/solve/parameters/backends/aws/rigetti.py +31 -0
- luna_quantum/solve/parameters/backends/dwave.py +17 -0
- luna_quantum/solve/parameters/backends/dwave_qpu.py +166 -0
- luna_quantum/solve/parameters/backends/fda.py +17 -0
- luna_quantum/solve/parameters/backends/ibm.py +138 -0
- luna_quantum/solve/parameters/backends/qctrl.py +103 -0
- luna_quantum/solve/parameters/backends/zib.py +17 -0
- luna_quantum/solve/parameters/constants.py +11 -0
- luna_quantum/solve/parameters/mixins/__init__.py +0 -0
- luna_quantum/solve/parameters/mixins/fujitsu_common_params_mixin.py +239 -0
- luna_quantum/solve/parameters/mixins/fujitsu_v2_mixin.py +70 -0
- luna_quantum/solve/parameters/mixins/qbsolv_like_mixin.py +60 -0
- luna_quantum/solve/use_cases/__init__.py +119 -0
- luna_quantum/solve/use_cases/arbitrage_edge_based.py +50 -0
- luna_quantum/solve/use_cases/arbitrage_node_based.py +55 -0
- luna_quantum/solve/use_cases/base.py +7 -0
- luna_quantum/solve/use_cases/binary_integer_linear_programming.py +54 -0
- luna_quantum/solve/use_cases/binary_paint_shop_problem.py +37 -0
- luna_quantum/solve/use_cases/credit_scoring_feature_selection.py +40 -0
- luna_quantum/solve/use_cases/dynamic_portfolio_optimization.py +64 -0
- luna_quantum/solve/use_cases/exact_cover.py +51 -0
- luna_quantum/solve/use_cases/flight_gate_assignment.py +79 -0
- luna_quantum/solve/use_cases/graph_coloring.py +42 -0
- luna_quantum/solve/use_cases/graph_isomorphism.py +52 -0
- luna_quantum/solve/use_cases/graph_partitioning.py +46 -0
- luna_quantum/solve/use_cases/hamiltonian_cycle.py +49 -0
- luna_quantum/solve/use_cases/induced_subgraph_isomorphism.py +50 -0
- luna_quantum/solve/use_cases/job_shop_scheduling.py +44 -0
- luna_quantum/solve/use_cases/k_medoids_clustering.py +49 -0
- luna_quantum/solve/use_cases/knapsack_integer_weights.py +56 -0
- luna_quantum/solve/use_cases/linear_regression.py +60 -0
- luna_quantum/solve/use_cases/lmwcs.py +84 -0
- luna_quantum/solve/use_cases/longest_path.py +50 -0
- luna_quantum/solve/use_cases/market_graph_clustering.py +61 -0
- luna_quantum/solve/use_cases/max2sat.py +54 -0
- luna_quantum/solve/use_cases/max3sat.py +55 -0
- luna_quantum/solve/use_cases/max_clique.py +60 -0
- luna_quantum/solve/use_cases/max_cut.py +48 -0
- luna_quantum/solve/use_cases/max_independent_set.py +37 -0
- luna_quantum/solve/use_cases/minimal_maximal_matching.py +54 -0
- luna_quantum/solve/use_cases/minimal_spanning_tree.py +90 -0
- luna_quantum/solve/use_cases/minimum_vertex_cover.py +45 -0
- luna_quantum/solve/use_cases/number_partitioning.py +32 -0
- luna_quantum/solve/use_cases/portfolio_optimization.py +46 -0
- luna_quantum/solve/use_cases/portfolio_optimization_ib_tv.py +63 -0
- luna_quantum/solve/use_cases/quadratic_assignment.py +49 -0
- luna_quantum/solve/use_cases/quadratic_knapsack.py +48 -0
- luna_quantum/solve/use_cases/satellite_scheduling.py +73 -0
- luna_quantum/solve/use_cases/sensor_placement.py +58 -0
- luna_quantum/solve/use_cases/set_cover.py +56 -0
- luna_quantum/solve/use_cases/set_packing.py +54 -0
- luna_quantum/solve/use_cases/set_partitioning.py +52 -0
- luna_quantum/solve/use_cases/subgraph_isomorphism.py +55 -0
- luna_quantum/solve/use_cases/subset_sum.py +37 -0
- luna_quantum/solve/use_cases/support_vector_machine.py +64 -0
- luna_quantum/solve/use_cases/traffic_flow.py +35 -0
- luna_quantum/solve/use_cases/travelling_salesman_problem.py +53 -0
- luna_quantum/solve/use_cases/type_aliases.py +9 -0
- luna_quantum/solve/use_cases/weighted_max_cut.py +37 -0
- luna_quantum/solve/usecases/__init__.py +45 -0
- luna_quantum/solve/usecases/model_delete_usecase.py +49 -0
- luna_quantum/solve/usecases/model_fetch_metadata_usecase.py +50 -0
- luna_quantum/solve/usecases/model_get_solution_usecase.py +59 -0
- luna_quantum/solve/usecases/model_get_solve_jobs_usecase.py +62 -0
- luna_quantum/solve/usecases/model_load_by_id_usecase.py +47 -0
- luna_quantum/solve/usecases/model_load_by_metadata_usecase.py +52 -0
- luna_quantum/solve/usecases/model_load_metadata_by_hash_usecase.py +51 -0
- luna_quantum/solve/usecases/model_save_usecase.py +63 -0
- luna_quantum/solve/usecases/solve_job_cancel_usecase.py +51 -0
- luna_quantum/solve/usecases/solve_job_create_usecase.py +112 -0
- luna_quantum/solve/usecases/solve_job_delete_usecase.py +38 -0
- luna_quantum/solve/usecases/solve_job_fetch_updates_usecase.py +49 -0
- luna_quantum/solve/usecases/solve_job_get_result_usecase.py +95 -0
- luna_quantum/transformations.py +18 -0
- luna_quantum/transformations.pyi +371 -0
- luna_quantum/translator.py +23 -0
- luna_quantum/translator.pyi +869 -0
- luna_quantum/util/__init__.py +0 -0
- luna_quantum/util/active_waiting.py +79 -0
- luna_quantum/util/class_patcher.py +164 -0
- luna_quantum/util/debug_info.py +52 -0
- luna_quantum/util/log_utils.py +187 -0
- luna_quantum/util/pretty_base.py +67 -0
- luna_quantum/util/pydantic_utils.py +38 -0
- luna_quantum/utils.py +3 -0
- luna_quantum/utils.pyi +67 -0
- luna_quantum-1.0.8rc2.dist-info/METADATA +36 -0
- luna_quantum-1.0.8rc2.dist-info/RECORD +264 -0
- luna_quantum-1.0.8rc2.dist-info/WHEEL +4 -0
- luna_quantum-1.0.8rc2.dist-info/licenses/LICENSE +176 -0
- luna_quantum-1.0.8rc2.dist-info/licenses/NOTICE +13 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from luna_quantum.solve.use_cases.base import UseCase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SupportVectorMachine(UseCase):
|
|
9
|
+
r"""
|
|
10
|
+
# Support Vector Machine.
|
|
11
|
+
|
|
12
|
+
Description
|
|
13
|
+
-----------
|
|
14
|
+
|
|
15
|
+
In machine learning, support vector machines are supervised learning models that
|
|
16
|
+
perform linear classification in such a way that the seperating hyperplane is as far
|
|
17
|
+
away from each data point as possible.
|
|
18
|
+
|
|
19
|
+
Note that, in this implementation, the model always assumes the separating
|
|
20
|
+
hyperplane to be unbiased, i.e. it goes through the origin.
|
|
21
|
+
|
|
22
|
+
Q-Bit Interpretation
|
|
23
|
+
--------------------
|
|
24
|
+
|
|
25
|
+
For interpretation, the qubit vector has to be cut into N sublists of length K
|
|
26
|
+
(specified below). The sum of each of the products of each of these sublists and the
|
|
27
|
+
precision vector gives a lagrangian multiplier for each data point.
|
|
28
|
+
|
|
29
|
+
Links
|
|
30
|
+
-----
|
|
31
|
+
|
|
32
|
+
[Wikipedia](https://en.wikipedia.org/wiki/Support-vector_machine)
|
|
33
|
+
|
|
34
|
+
[Transformation](https://doi.org/10.1038/s41598-021-89461-4)
|
|
35
|
+
|
|
36
|
+
Attributes
|
|
37
|
+
----------
|
|
38
|
+
### X: List[List[float]]
|
|
39
|
+
\n Training data set in form of a nested list.
|
|
40
|
+
\n All inner lists have to be of the same length.
|
|
41
|
+
\n (e.g. 3 data points with 2 features:
|
|
42
|
+
\n _[[1.1, 4.23], [0.1, -2.4], [-2.3, 1.11]]_
|
|
43
|
+
|
|
44
|
+
### Y: List[int]
|
|
45
|
+
\n Classification labels of the training data set.
|
|
46
|
+
\n e.g. for 3 data points:
|
|
47
|
+
\n _[-1, 1, 1]_
|
|
48
|
+
|
|
49
|
+
### K: int
|
|
50
|
+
\n Length of the precision vector.
|
|
51
|
+
\n As the problem outputs are supposed to be real values but the qubo only
|
|
52
|
+
gives a binary vector, we need a precision vector, consisting of positive powers
|
|
53
|
+
of 2, to simulate real values. This parameter determines the length of this
|
|
54
|
+
vector.
|
|
55
|
+
\n (e.g. for K = 5, the precision vector is _[0.25, 0.5, 1, 2, 4]_)
|
|
56
|
+
\n This parameter also determines the size of the qubo matrix together with the
|
|
57
|
+
number of data points _N_:
|
|
58
|
+
\n _size = N * K_
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
name: Literal["SVM"] = "SVM"
|
|
62
|
+
X: list[list[float]]
|
|
63
|
+
Y: list[int]
|
|
64
|
+
K: int = 5
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from luna_quantum.solve.use_cases.base import UseCase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TrafficFlow(UseCase):
|
|
9
|
+
r"""
|
|
10
|
+
# Traffic Flow Optimization.
|
|
11
|
+
|
|
12
|
+
Description
|
|
13
|
+
-----------
|
|
14
|
+
|
|
15
|
+
The Traffic Flow Optimization problem tries to minimize the total time for a given
|
|
16
|
+
set of cars to travel between their individual sources and destinations. This is
|
|
17
|
+
achieved by minimizing the number of overlapping segments between assigned routes
|
|
18
|
+
for each car.
|
|
19
|
+
|
|
20
|
+
Links
|
|
21
|
+
-----
|
|
22
|
+
|
|
23
|
+
[Description and Transformation](https://www.frontiersin.org/articles/10.3389/fict.2017.00029/full)
|
|
24
|
+
|
|
25
|
+
Attributes
|
|
26
|
+
----------
|
|
27
|
+
### car_routes: List[List[List[int]]]
|
|
28
|
+
\n The route segments of each possible route for each car.
|
|
29
|
+
\n (e.g. for two cars, where car 1 can take either route 0, 1, 2 or route 0, 3,
|
|
30
|
+
4 and car 2 can take either route 3, 0, 5 or route 6, 7, 5:
|
|
31
|
+
\n _[[[0, 1, 2], [0, 3, 4]], [[3, 0, 5], [6, 7, 5]]]_
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
name: Literal["TFO"] = "TFO"
|
|
35
|
+
car_routes: list[list[list[int]]]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from pydantic import Field
|
|
6
|
+
|
|
7
|
+
from luna_quantum.solve.use_cases.base import UseCase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TravellingSalesmanProblem(UseCase):
|
|
11
|
+
r"""
|
|
12
|
+
# Travelling Salesman.
|
|
13
|
+
|
|
14
|
+
Description
|
|
15
|
+
-----------
|
|
16
|
+
|
|
17
|
+
The Travelling Salesman problem, either for a directed or undirected graph, asks the
|
|
18
|
+
following: given a graph, where the edges are labeled with the distances between the
|
|
19
|
+
corresponding nodes, what is the shortest possible route that visits each node
|
|
20
|
+
exactly once and returns to the origin node?
|
|
21
|
+
|
|
22
|
+
Links
|
|
23
|
+
-----
|
|
24
|
+
|
|
25
|
+
[Wikipedia](https://en.wikipedia.org/wiki/Travelling_salesman_problem)
|
|
26
|
+
|
|
27
|
+
[Transformation](https://arxiv.org/pdf/1302.5843.pdf)
|
|
28
|
+
|
|
29
|
+
Attributes
|
|
30
|
+
----------
|
|
31
|
+
graph: Dict[int, Dict[int, Dict[str, float]]]
|
|
32
|
+
\n Problem graph for the travelling salesman problem in form of nested
|
|
33
|
+
dictionaries.
|
|
34
|
+
\n (e.g. fully connected graph with 3 nodes:
|
|
35
|
+
\n _{0: {1: {}, 2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}}_
|
|
36
|
+
\n or _networkx.to_dict_of_dicts(networkx.complete_graph(3))_ )
|
|
37
|
+
|
|
38
|
+
A: Optional[float]
|
|
39
|
+
\n Positive penalty value which enforces that each node is visited exactly once.
|
|
40
|
+
\n if _None_, will be calculated with the equation: _A = B * _max_weight_ + 1_
|
|
41
|
+
\n Default: _None_
|
|
42
|
+
|
|
43
|
+
B: Optional[float]
|
|
44
|
+
\n Positive penalty value (_B * _max_weight_) < A_) which helps find the
|
|
45
|
+
shortest route.
|
|
46
|
+
\n Default: _1.0_
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
name: Literal["TSP"] = "TSP"
|
|
50
|
+
graph: dict[str, dict[str, dict[str, float]]] = Field(name="graph") # type: ignore[call-overload]
|
|
51
|
+
directed: bool | None = False
|
|
52
|
+
B: float | None = 1.0
|
|
53
|
+
A: float | None = None
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from luna_quantum.solve.use_cases.base import UseCase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class WeightedMaxCut(UseCase):
|
|
9
|
+
r"""
|
|
10
|
+
# Weighted Maximum Cut.
|
|
11
|
+
|
|
12
|
+
Description
|
|
13
|
+
-----------
|
|
14
|
+
|
|
15
|
+
The Weighted Maximum Cut problem tries to find a cut that maximizes the weight of
|
|
16
|
+
intersecting edges in an undirected weighted graph.
|
|
17
|
+
|
|
18
|
+
Links
|
|
19
|
+
-----
|
|
20
|
+
|
|
21
|
+
[Wikipedia](https://en.wikipedia.org/wiki/Maximum_cut)
|
|
22
|
+
|
|
23
|
+
[Transformation](https://arxiv.org/pdf/2009.05008.pdf)
|
|
24
|
+
|
|
25
|
+
Attributes
|
|
26
|
+
----------
|
|
27
|
+
### graph: Dict[int, Dict[int, Dict[str, float]]]
|
|
28
|
+
\n Problem graph for the weighted maximum cut problem in form of nested
|
|
29
|
+
dictionaries.
|
|
30
|
+
\n Every edge has to have an assigned weight.
|
|
31
|
+
\n (e.g. fully connected graph with 3 nodes and edge weights:
|
|
32
|
+
\n _{0: {1: {"weight": 1}, 2: {"weight": 1}}, 1: {0: {"weight": 1},
|
|
33
|
+
2: {"weight": 1}}, 2: {0: {"weight": 1}, 1: {"weight": 1}}}_ )
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
name: Literal["WMC"] = "WMC"
|
|
37
|
+
graph: dict[int, dict[int, dict[str, float]]]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from luna_quantum.solve.usecases.model_delete_usecase import ModelDeleteUseCase
|
|
2
|
+
from luna_quantum.solve.usecases.model_fetch_metadata_usecase import (
|
|
3
|
+
ModelFetchMetadataUseCase,
|
|
4
|
+
)
|
|
5
|
+
from luna_quantum.solve.usecases.model_get_solution_usecase import (
|
|
6
|
+
ModelGetSolutionUseCase,
|
|
7
|
+
)
|
|
8
|
+
from luna_quantum.solve.usecases.model_get_solve_jobs_usecase import (
|
|
9
|
+
ModelGetSolveJobsUseCase,
|
|
10
|
+
)
|
|
11
|
+
from luna_quantum.solve.usecases.model_load_by_id_usecase import (
|
|
12
|
+
ModelLoadByIdUseCase,
|
|
13
|
+
)
|
|
14
|
+
from luna_quantum.solve.usecases.model_load_by_metadata_usecase import (
|
|
15
|
+
ModelLoadByMetadataUseCase,
|
|
16
|
+
)
|
|
17
|
+
from luna_quantum.solve.usecases.model_load_metadata_by_hash_usecase import (
|
|
18
|
+
ModelLoadMetadataByHashUseCase,
|
|
19
|
+
)
|
|
20
|
+
from luna_quantum.solve.usecases.model_save_usecase import ModelSaveUseCase
|
|
21
|
+
from luna_quantum.solve.usecases.solve_job_cancel_usecase import SolveJobCancelUseCase
|
|
22
|
+
from luna_quantum.solve.usecases.solve_job_create_usecase import SolveJobCreateUseCase
|
|
23
|
+
from luna_quantum.solve.usecases.solve_job_delete_usecase import SolveJobDeleteUseCase
|
|
24
|
+
from luna_quantum.solve.usecases.solve_job_fetch_updates_usecase import (
|
|
25
|
+
SolveJobFetchUpdatesUseCase,
|
|
26
|
+
)
|
|
27
|
+
from luna_quantum.solve.usecases.solve_job_get_result_usecase import (
|
|
28
|
+
SolveJobGetResultUseCase,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"ModelDeleteUseCase",
|
|
33
|
+
"ModelFetchMetadataUseCase",
|
|
34
|
+
"ModelGetSolutionUseCase",
|
|
35
|
+
"ModelGetSolveJobsUseCase",
|
|
36
|
+
"ModelLoadByIdUseCase",
|
|
37
|
+
"ModelLoadByMetadataUseCase",
|
|
38
|
+
"ModelLoadMetadataByHashUseCase",
|
|
39
|
+
"ModelSaveUseCase",
|
|
40
|
+
"SolveJobCancelUseCase",
|
|
41
|
+
"SolveJobCreateUseCase",
|
|
42
|
+
"SolveJobDeleteUseCase",
|
|
43
|
+
"SolveJobFetchUpdatesUseCase",
|
|
44
|
+
"SolveJobGetResultUseCase",
|
|
45
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from luna_quantum.aqm_overwrites import Model
|
|
2
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
3
|
+
from luna_quantum.solve.interfaces.usecases.model_delete_usecase_i import (
|
|
4
|
+
IModelDeleteUseCase,
|
|
5
|
+
)
|
|
6
|
+
from luna_quantum.util.log_utils import progress
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ModelDeleteUseCase(IModelDeleteUseCase):
|
|
10
|
+
"""
|
|
11
|
+
Handle the deletion of models.
|
|
12
|
+
|
|
13
|
+
This class facilitates the execution of the model delete method
|
|
14
|
+
for a given Model.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
client : ILunaSolve
|
|
19
|
+
The client instance used for invoking the model delete method.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
client: ILunaSolve
|
|
23
|
+
|
|
24
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
25
|
+
self.client = client
|
|
26
|
+
|
|
27
|
+
@progress(total=None, desc="Deleting model...")
|
|
28
|
+
def __call__(self, model: Model) -> None:
|
|
29
|
+
"""
|
|
30
|
+
Delete the given Model from the client.
|
|
31
|
+
|
|
32
|
+
If the Model does not have metadata, this method does nothing. It is assumed
|
|
33
|
+
that the model was not stored in the client.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
model : Model
|
|
38
|
+
The Model instance for which the model delete method is called.
|
|
39
|
+
|
|
40
|
+
Returns
|
|
41
|
+
-------
|
|
42
|
+
None
|
|
43
|
+
This method does not return a value.
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
if model.metadata is None:
|
|
47
|
+
return
|
|
48
|
+
|
|
49
|
+
self.client.model.delete(model_id=model.metadata.id)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from luna_quantum.aqm_overwrites import Model
|
|
2
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
3
|
+
from luna_quantum.solve.domain.model_metadata import ModelMetadata
|
|
4
|
+
from luna_quantum.solve.interfaces.usecases.model_fetch_metadata_usecase_i import (
|
|
5
|
+
IModelFetchMetadataUseCase,
|
|
6
|
+
)
|
|
7
|
+
from luna_quantum.util.log_utils import progress
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ModelFetchMetadataUseCase(IModelFetchMetadataUseCase):
|
|
11
|
+
"""Use case for fetching metadata of an AQ model.
|
|
12
|
+
|
|
13
|
+
The `ModelFetchMetadataUseCase` works with a given client interface to
|
|
14
|
+
retrieve metadata for a specified AQ model.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
client : ILunaSolve
|
|
19
|
+
Client used to fetch the metadata for an AQ model.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
client: ILunaSolve
|
|
23
|
+
|
|
24
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
25
|
+
self.client = client
|
|
26
|
+
|
|
27
|
+
@progress(total=None, desc="Fetching model...")
|
|
28
|
+
def __call__(self, model: Model) -> ModelMetadata:
|
|
29
|
+
"""
|
|
30
|
+
Fetch the metadata of the given Model.
|
|
31
|
+
|
|
32
|
+
Load the metadata associated with the given Model instance from the client.
|
|
33
|
+
The metadata is validated before returning it. The metadata is not written to
|
|
34
|
+
the Model instance.
|
|
35
|
+
|
|
36
|
+
Parameters
|
|
37
|
+
----------
|
|
38
|
+
model : Model
|
|
39
|
+
The Model instance to be associated with metadata.
|
|
40
|
+
|
|
41
|
+
Returns
|
|
42
|
+
-------
|
|
43
|
+
ModelMetadata
|
|
44
|
+
The metadata associated with the provided Model.
|
|
45
|
+
"""
|
|
46
|
+
metadata_schema = self.client.model.get_metadata_by_hash(
|
|
47
|
+
model_hash=model.__hash__()
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return ModelMetadata.model_validate(metadata_schema.model_dump())
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from luna_quantum._core import Solution
|
|
2
|
+
from luna_quantum.aqm_overwrites import Model
|
|
3
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
4
|
+
from luna_quantum.client.schemas.enums.status import StatusEnum
|
|
5
|
+
from luna_quantum.solve.interfaces.usecases.model_get_solutions_usecase_i import (
|
|
6
|
+
IModelGetSolutionUseCase,
|
|
7
|
+
)
|
|
8
|
+
from luna_quantum.util.log_utils import progress
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ModelGetSolutionUseCase(IModelGetSolutionUseCase):
|
|
12
|
+
"""
|
|
13
|
+
Use case for retrieving solutions of a model.
|
|
14
|
+
|
|
15
|
+
Handles the process of interacting with the client to get all solutions
|
|
16
|
+
for a specific AQ model based on its metadata and associated ID.
|
|
17
|
+
|
|
18
|
+
Attributes
|
|
19
|
+
----------
|
|
20
|
+
client : ILunaSolve
|
|
21
|
+
The client responsible for fetching solutions from the external source.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
client: ILunaSolve
|
|
25
|
+
|
|
26
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
27
|
+
self.client = client
|
|
28
|
+
|
|
29
|
+
@progress(total=None, desc="Loading solution...")
|
|
30
|
+
def __call__(self, model: Model) -> list[Solution]:
|
|
31
|
+
"""
|
|
32
|
+
Load solutions of the given Model input.
|
|
33
|
+
|
|
34
|
+
This function retrieves and returns a list of ISolution instances for the
|
|
35
|
+
given Model. If the Model does not have metadata, an empty list is returned.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
model : Model
|
|
40
|
+
The input Model object whose associated solutions are to be retrieved.
|
|
41
|
+
|
|
42
|
+
Returns
|
|
43
|
+
-------
|
|
44
|
+
list[Solution]
|
|
45
|
+
A list of Solution objects associated with the given Model.
|
|
46
|
+
"""
|
|
47
|
+
if model.metadata is None:
|
|
48
|
+
return []
|
|
49
|
+
|
|
50
|
+
model_id = model.metadata.id
|
|
51
|
+
|
|
52
|
+
solve_jobs = self.client.solve_job.get_all(model_id=model_id)
|
|
53
|
+
|
|
54
|
+
# TODO THIS IS SUPER INEFFICIENT # noqa: FIX002, TD002, TD004
|
|
55
|
+
return [
|
|
56
|
+
self.client.solve_job.get_solution(solve_job_id=s.id)
|
|
57
|
+
for s in solve_jobs
|
|
58
|
+
if s.status is StatusEnum.DONE
|
|
59
|
+
]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
from luna_quantum.aqm_overwrites import Model
|
|
4
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
5
|
+
from luna_quantum.solve.domain.solve_job import SolveJob
|
|
6
|
+
from luna_quantum.solve.interfaces.usecases.model_get_solve_jobs_usecase_i import (
|
|
7
|
+
IModelGetSolveJobUseCase,
|
|
8
|
+
)
|
|
9
|
+
from luna_quantum.util.log_utils import progress
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from luna_quantum.client.schemas.solve_job import SolveJobSchema
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ModelGetSolveJobsUseCase(IModelGetSolveJobUseCase):
|
|
16
|
+
"""
|
|
17
|
+
Handle the retrieval of solve jobs for a given Model.
|
|
18
|
+
|
|
19
|
+
This class is responsible for interacting with the client to fetch solve jobs
|
|
20
|
+
related to a given Model. It ensures that the required metadata is present
|
|
21
|
+
before performing operations with the client.
|
|
22
|
+
|
|
23
|
+
Attributes
|
|
24
|
+
----------
|
|
25
|
+
client : ILunaSolve
|
|
26
|
+
The client used to communicate with the solve job backend or service.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
client: ILunaSolve
|
|
30
|
+
|
|
31
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
32
|
+
self.client = client
|
|
33
|
+
|
|
34
|
+
@progress(total=None, desc="Loading solve job...")
|
|
35
|
+
def __call__(self, model: Model) -> list[SolveJob]:
|
|
36
|
+
"""
|
|
37
|
+
Load solve jobs from the given Model instance.
|
|
38
|
+
|
|
39
|
+
Fetches and validates all solve jobs associated with the given Model's
|
|
40
|
+
metadata and returns them as a list of SolveJob objects. If metadata is
|
|
41
|
+
missing in the Model, an empty list is returned.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
model : Model
|
|
46
|
+
The Model instance whose associated solve jobs are to be fetched.
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
-------
|
|
50
|
+
List[SolveJob]
|
|
51
|
+
A list of SolveJob objects derived from the fetched solve jobs..
|
|
52
|
+
"""
|
|
53
|
+
if model.metadata is None:
|
|
54
|
+
return []
|
|
55
|
+
|
|
56
|
+
model_id = model.metadata.id
|
|
57
|
+
|
|
58
|
+
solutions: list[SolveJobSchema] = self.client.solve_job.get_all(
|
|
59
|
+
model_id=model_id
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
return [SolveJob.model_validate(s.model_dump()) for s in solutions]
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from luna_quantum.aqm_overwrites import Model
|
|
2
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
3
|
+
from luna_quantum.solve.interfaces.usecases.model_load_by_id_usecase_i import (
|
|
4
|
+
IModelLoadByIdUseCase,
|
|
5
|
+
)
|
|
6
|
+
from luna_quantum.util.log_utils import progress
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ModelLoadByIdUseCase(IModelLoadByIdUseCase):
|
|
10
|
+
"""
|
|
11
|
+
Load an Model by its identifier.
|
|
12
|
+
|
|
13
|
+
This class interacts with a client implementing ILunaSolve to fetch and load
|
|
14
|
+
model models, providing their metadata and content.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
client : ILunaSolve
|
|
19
|
+
The client used for fetching the model models.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
client: ILunaSolve
|
|
23
|
+
|
|
24
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
25
|
+
self.client = client
|
|
26
|
+
|
|
27
|
+
@progress(total=None, desc="Retrieving model...")
|
|
28
|
+
def __call__(self, model_id: str) -> Model:
|
|
29
|
+
"""
|
|
30
|
+
Retrieve a Model object based on the given model id.
|
|
31
|
+
|
|
32
|
+
This method fetches a model schema using the provided model identifier, converts
|
|
33
|
+
it to a Model object, and validates its metadata before returning it.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
model_id : str
|
|
38
|
+
The unique identifier of the model to retrieve.
|
|
39
|
+
|
|
40
|
+
Returns
|
|
41
|
+
-------
|
|
42
|
+
Model
|
|
43
|
+
The retrieved model, with associated metadata validated and populated.
|
|
44
|
+
"""
|
|
45
|
+
aq_model: Model = self.client.model.get_model(model_id=model_id)
|
|
46
|
+
|
|
47
|
+
return aq_model
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from luna_quantum.aqm_overwrites import Model
|
|
2
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
3
|
+
from luna_quantum.solve.domain.model_metadata import ModelMetadata
|
|
4
|
+
from luna_quantum.solve.interfaces.usecases.model_load_by_metadata_usecase_i import (
|
|
5
|
+
IModelLoadByMetadataUseCase,
|
|
6
|
+
)
|
|
7
|
+
from luna_quantum.util.log_utils import progress
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ModelLoadByMetadataUseCase(IModelLoadByMetadataUseCase):
|
|
11
|
+
"""
|
|
12
|
+
Loads an model model using metadata.
|
|
13
|
+
|
|
14
|
+
The purpose of this class is to load an model model from a given metadata
|
|
15
|
+
object using the specified client. It retrieves the model and attaches the metadata
|
|
16
|
+
to it.
|
|
17
|
+
|
|
18
|
+
Attributes
|
|
19
|
+
----------
|
|
20
|
+
client : ILunaSolve
|
|
21
|
+
The client responsible for interacting with the model backend.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
client: ILunaSolve
|
|
25
|
+
|
|
26
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
27
|
+
self.client = client
|
|
28
|
+
|
|
29
|
+
@progress(total=None, desc="Retrieving model...")
|
|
30
|
+
def __call__(self, model_metadata: ModelMetadata) -> Model:
|
|
31
|
+
"""
|
|
32
|
+
Callable to retrieve and update an Model instance.
|
|
33
|
+
|
|
34
|
+
This method interacts with the client to fetch an Model based on the
|
|
35
|
+
provided ModelMetadata, updates the model's metadata, and then returns
|
|
36
|
+
the updated Model instance.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
model_metadata : ModelMetadata
|
|
41
|
+
The metadata object containing the ID and relevant data for fetching
|
|
42
|
+
the corresponding Model.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
-------
|
|
46
|
+
Model
|
|
47
|
+
The fetched and updated Model instance.
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
aq_model: Model = self.client.model.get_model(model_id=model_metadata.id)
|
|
51
|
+
|
|
52
|
+
return aq_model
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
2
|
+
from luna_quantum.solve.domain.model_metadata import ModelMetadata
|
|
3
|
+
from luna_quantum.solve.interfaces.usecases import (
|
|
4
|
+
IModelLoadMetadataByHashUseCase,
|
|
5
|
+
)
|
|
6
|
+
from luna_quantum.util.log_utils import progress
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ModelLoadMetadataByHashUseCase(IModelLoadMetadataByHashUseCase):
|
|
10
|
+
"""
|
|
11
|
+
Load metadata for an AQ model using a hash.
|
|
12
|
+
|
|
13
|
+
This class interacts with the client to retrieve and validate the metadata
|
|
14
|
+
of an AQ model by providing its unique hash value.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
client : ILunaSolve
|
|
19
|
+
The client used to retrieve AQ model metadata.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
client: ILunaSolve
|
|
23
|
+
|
|
24
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
25
|
+
self.client = client
|
|
26
|
+
|
|
27
|
+
@progress(total=None, desc="Retrieving model metadata...")
|
|
28
|
+
def __call__(self, model_hash: int) -> ModelMetadata:
|
|
29
|
+
"""
|
|
30
|
+
Retrieve the metadata for a specific model using its unique hash.
|
|
31
|
+
|
|
32
|
+
This callable retrieves the model metadata by communicating with a client
|
|
33
|
+
based on the provided model hash. It validates the
|
|
34
|
+
retrieved metadata against the ModelMetadata schema and returns a
|
|
35
|
+
validated object.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
model_hash : int
|
|
40
|
+
The unique hash identifier for the model whose metadata is to be
|
|
41
|
+
retrieved.
|
|
42
|
+
|
|
43
|
+
Returns
|
|
44
|
+
-------
|
|
45
|
+
ModelMetadata
|
|
46
|
+
A validated object containing metadata information about the model.
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
aq_model_schema = self.client.model.get_metadata_by_hash(model_hash=model_hash)
|
|
50
|
+
|
|
51
|
+
return ModelMetadata.model_validate(aq_model_schema.model_dump())
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
from luna_quantum.aqm_overwrites import Model
|
|
4
|
+
from luna_quantum.client.interfaces.services.luna_solve_i import ILunaSolve
|
|
5
|
+
from luna_quantum.solve.domain.model_metadata import ModelMetadata
|
|
6
|
+
from luna_quantum.solve.interfaces.usecases.model_save_usecase_i import (
|
|
7
|
+
IModelSaveUseCase,
|
|
8
|
+
)
|
|
9
|
+
from luna_quantum.util.log_utils import progress
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from luna_quantum.client.schemas.model_metadata import ModelMetadataSchema
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ModelSaveUseCase(IModelSaveUseCase):
|
|
16
|
+
"""
|
|
17
|
+
Represents a use case for saving an Model instance.
|
|
18
|
+
|
|
19
|
+
Provides functionality to interface with a client implementation for
|
|
20
|
+
retrieving or creating model metadata and saving model information. The
|
|
21
|
+
metadata retrieved is not written to the model instance.
|
|
22
|
+
|
|
23
|
+
Attributes
|
|
24
|
+
----------
|
|
25
|
+
client : ILunaSolve
|
|
26
|
+
The client used to perform operations related to saving and retrieving
|
|
27
|
+
Model metadata, such as fetching metadata by hash or creating new
|
|
28
|
+
metadata.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
client: ILunaSolve
|
|
32
|
+
|
|
33
|
+
def __init__(self, client: ILunaSolve) -> None:
|
|
34
|
+
self.client = client
|
|
35
|
+
|
|
36
|
+
@progress(total=None, desc="Saving model...")
|
|
37
|
+
def __call__(self, model: Model) -> ModelMetadata:
|
|
38
|
+
"""
|
|
39
|
+
Retrieve model metadata, if the model does not exist create and retrieve it.
|
|
40
|
+
|
|
41
|
+
This function attempts to fetch the metadata of the given `Model` instance
|
|
42
|
+
from the model client. If the metadata is not found, the model is saved
|
|
43
|
+
with the client and the metadata is retrieved again.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
model : Model
|
|
48
|
+
The model instance for which metadata is being retrieved or created.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
ModelMetadata
|
|
53
|
+
The validated metadata associated with the given model instance.
|
|
54
|
+
"""
|
|
55
|
+
metadata: ModelMetadataSchema
|
|
56
|
+
try:
|
|
57
|
+
metadata = self.client.model.get_metadata_by_hash(
|
|
58
|
+
model_hash=model.__hash__()
|
|
59
|
+
)
|
|
60
|
+
except Exception: # TODO(Llewellyn): make more specific to # noqa: FIX002
|
|
61
|
+
# Not found exception
|
|
62
|
+
metadata = self.client.model.create(model=model)
|
|
63
|
+
return ModelMetadata.model_validate(metadata.model_dump())
|