laser-mind-client 0.1.1__tar.gz
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.
- laser_mind_client-0.1.1/LICENSE +201 -0
- laser_mind_client-0.1.1/PKG-INFO +245 -0
- laser_mind_client-0.1.1/README.md +228 -0
- laser_mind_client-0.1.1/laser_mind_client/__init__.py +1 -0
- laser_mind_client-0.1.1/laser_mind_client/laser_mind_client.py +170 -0
- laser_mind_client-0.1.1/laser_mind_client.egg-info/PKG-INFO +245 -0
- laser_mind_client-0.1.1/laser_mind_client.egg-info/SOURCES.txt +10 -0
- laser_mind_client-0.1.1/laser_mind_client.egg-info/dependency_links.txt +1 -0
- laser_mind_client-0.1.1/laser_mind_client.egg-info/requires.txt +4 -0
- laser_mind_client-0.1.1/laser_mind_client.egg-info/top_level.txt +1 -0
- laser_mind_client-0.1.1/pyproject.toml +32 -0
- laser_mind_client-0.1.1/setup.cfg +4 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: laser-mind-client
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A client python API for accessing LightSolver's capabilities
|
|
5
|
+
Author-email: Assaf Kalinski <assaf@lightsolver.com>
|
|
6
|
+
Project-URL: Homepage, https://lightsolver.com
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: numpy>=1.21.5
|
|
14
|
+
Requires-Dist: laser-mind-client-meta>=0.0.9
|
|
15
|
+
Requires-Dist: ls_api_clients>=0.4.6
|
|
16
|
+
Requires-Dist: ls-packers>=0.2.0
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## LightSolver Platform Client
|
|
20
|
+
The LightSolver Platform Client is a Python package designed to interface with the LightSolver Cloud to facilitate solving Quadratic Unconstrained Binary Optimization (QUBO) problems.
|
|
21
|
+
|
|
22
|
+
This package is designated for internal access to features during the development process, as well as serves as a prototype for future versions of the production LightSolver Platform Client.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
- **QUBO Problem Solving:** The `solve_qubo` function accepts a QUBO problem, represented either as a 2D array (matrix) or an adjacency list, and returns the solution.
|
|
26
|
+
- **Synchronous and Asynchronous Operation:** Users can choose between blocking (synchronous) and non-blocking (asynchronous) modes for QUBO problem solving.
|
|
27
|
+
- **Flexible Installation:** Compatible with both Windows and Ubuntu systems.
|
|
28
|
+
|
|
29
|
+
### Solve QUBO
|
|
30
|
+
The `solve_qubo` function handles the computation of QUBO problems, either represented by a 2D array (matrix) or by an adjacency list. For code samples, see the **Usage** section.
|
|
31
|
+
|
|
32
|
+
#### Input Matrix Validity
|
|
33
|
+
- The matrix must be square.
|
|
34
|
+
- The matrix supports int or float cell values.
|
|
35
|
+
|
|
36
|
+
#### Return Value
|
|
37
|
+
A dictionary with the following fields:
|
|
38
|
+
```
|
|
39
|
+
- 'id': Unique identifier of the solution.
|
|
40
|
+
- 'solution': The solution as a Python list() of 1s and 0s.
|
|
41
|
+
- 'objval: The objective value of the solution.
|
|
42
|
+
- 'solverRunningTime': Time spent by the solver to calculate the problem.
|
|
43
|
+
- 'receivedTime': Timestamp when the request was received by the server.
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Synchronous and Asynchronous Usage
|
|
47
|
+
- **Synchronous Mode (Default):** The `waitForSolution` flag is set to **True** by default. The function blocks operations until a result is received.
|
|
48
|
+
- **Asynchronous Mode:** Set `waitForSolution` to **False**. The function returns immediately with a token object, allowing the script to continue while the server processes the QUBO problem.
|
|
49
|
+
|
|
50
|
+
## Setting Up
|
|
51
|
+
|
|
52
|
+
### Prerequisites
|
|
53
|
+
- Valid credentials for connecting to the LightSolver Cloud.
|
|
54
|
+
- LightSolver Client code project - please use this link to download.
|
|
55
|
+
- Python 3.10.
|
|
56
|
+
- Operating System: Linux or Windows. Tested on Ubuntu 20.04 and Windows 11.
|
|
57
|
+
- Highly Recommended: Use a virtual environment before installing laser-mind-client (Please see detailed action further below under the relevant OS).
|
|
58
|
+
|
|
59
|
+
### Installation
|
|
60
|
+
Complete the installation on Windows or Ubuntu as described below.
|
|
61
|
+
For further assistance with setup or connection issues, contact support@lightsolver.com.
|
|
62
|
+
|
|
63
|
+
#### Windows
|
|
64
|
+
1. Press the windows key, type "cmd", right click on the result and select "Run as administrator".
|
|
65
|
+
2. Navigate to the root folder where you unzipped and plan to use the LightSolver Client:
|
|
66
|
+
```
|
|
67
|
+
cd <your project folder>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
3. (Recommended) Set the LightSolver credentials using environment variables (will remove the need to provide credentials for every usage).
|
|
71
|
+
Run the following command:
|
|
72
|
+
```sh
|
|
73
|
+
python setup_env.py
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
4. (Recommended) Create a virtual environment:
|
|
77
|
+
```sh
|
|
78
|
+
python -m venv .venv
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
5. (Recommended) Activate the new virtual environment:
|
|
82
|
+
```sh
|
|
83
|
+
.venv\Scripts\activate
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
6. Install the laser-mind-client package. This command instructs pip to install the package from a local folder instead of searching online:
|
|
87
|
+
```sh
|
|
88
|
+
pip install --no-cache-dir --find-links=.\packages laser-mind-client
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
7. (Recommended) Test LightSolver credentials
|
|
92
|
+
Run the following command:
|
|
93
|
+
```sh
|
|
94
|
+
python test_env.py
|
|
95
|
+
```
|
|
96
|
+
if test credentials failed , please reinstall credentials using following commands:
|
|
97
|
+
- Exit virtual environment , by running :
|
|
98
|
+
```sh
|
|
99
|
+
deactivate
|
|
100
|
+
```
|
|
101
|
+
- Run the following command:
|
|
102
|
+
```sh
|
|
103
|
+
python setup_env.py
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
#### Ubuntu
|
|
108
|
+
1. Open new terminal window
|
|
109
|
+
2. Navigate to the root folder where you unzipped and plan to use the LightSolver Client:
|
|
110
|
+
```
|
|
111
|
+
cd <your project folder>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
3. (Recommended) Set the LightSolver credentials using environment variables (will remove the need to provide credentials for every usage).
|
|
115
|
+
Run the following command:
|
|
116
|
+
```sh
|
|
117
|
+
python3 setup_env.py
|
|
118
|
+
source ~/.bash_profile
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
4. (Recommended) Create a virtual environment:
|
|
122
|
+
```sh
|
|
123
|
+
python3 -m venv .venv
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
5. (Recommended) Activate the new virtual environment:
|
|
127
|
+
```sh
|
|
128
|
+
chmod 755 .venv/bin/activate
|
|
129
|
+
source .venv/bin/activate
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
6. Install the laser-mind-client package. This command instructs pip to install the package from a local folder instead of searching online:
|
|
133
|
+
```sh
|
|
134
|
+
pip install --no-cache-dir --find-links=./packages laser-mind-client
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
7. (Recommended) Test LightSolver credentials
|
|
138
|
+
Run the following command:
|
|
139
|
+
```sh
|
|
140
|
+
python3 test_env.py
|
|
141
|
+
```
|
|
142
|
+
if test credentials failed , please reinstall credentials using following commands:
|
|
143
|
+
- Exit virtual environment , by running :
|
|
144
|
+
```sh
|
|
145
|
+
deactivate
|
|
146
|
+
```
|
|
147
|
+
- Run the following command:
|
|
148
|
+
```sh
|
|
149
|
+
python3 setup_env.py
|
|
150
|
+
source ~/.bash_profile
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Authentication
|
|
154
|
+
Initialization of the `LaserMind` class automatically forms a secure and authenticated connection with the LightSolver Cloud.
|
|
155
|
+
Subsequent calls by the same user are similarly secure and authenticated.
|
|
156
|
+
|
|
157
|
+
## Usage
|
|
158
|
+
To begin solving any QUBO problem:
|
|
159
|
+
1. Create an instance of the ```LaserMind``` class. This class represents the client that requests solutions from the LightSolver Cloud.
|
|
160
|
+
2. Call the ```solve_qubo``` function using either a matrix or an adjacency list.
|
|
161
|
+
**Note:** You may either provide a value for ```matrixData``` or for ```edgeList```, but not both.
|
|
162
|
+
|
|
163
|
+
### Solve QUBO Matrix Example
|
|
164
|
+
This example creates a matrix representing a QUBO problem and solves it using the LightSolver Platform Client.
|
|
165
|
+
The `solve_qubo` function is used with the following parameters:
|
|
166
|
+
- ```matrixData```: A 2D array representing the QUBO problem.
|
|
167
|
+
- ```timeout```: The required time limit for the calculation in seconds.
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import numpy
|
|
171
|
+
from laser_mind_client import LaserMind
|
|
172
|
+
|
|
173
|
+
# Create a mock QUBO problem
|
|
174
|
+
quboProblemData = numpy.random.randint(-1, 2, (10,10))
|
|
175
|
+
|
|
176
|
+
# Symmetrize the matrix
|
|
177
|
+
quboProblemData = (quboProblemData + quboProblemData.T) // 2
|
|
178
|
+
|
|
179
|
+
# Connect to the LightSolver Cloud
|
|
180
|
+
lsClient = LaserMind()
|
|
181
|
+
|
|
182
|
+
res = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1)
|
|
183
|
+
|
|
184
|
+
print(res)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Solve QUBO Adjacency List Example
|
|
188
|
+
This example describes a QUBO problem using an adjacency list. This is useful for sparse matrices.
|
|
189
|
+
The `solve_qubo` function is used with the following parameters:
|
|
190
|
+
- ```edgeList```: The adjacency list representing the QUBO problem.
|
|
191
|
+
- ```timeout```: The required time limit for the calculation in seconds.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from laser_mind_client import LaserMind
|
|
196
|
+
|
|
197
|
+
# Create a mock QUBO problem
|
|
198
|
+
quboListData = [
|
|
199
|
+
[1,1,5],
|
|
200
|
+
[1,2,-6],
|
|
201
|
+
[2,2,3],
|
|
202
|
+
[2,3,-1],
|
|
203
|
+
[3,10,1]]
|
|
204
|
+
|
|
205
|
+
# Connect to the LightSolver Cloud
|
|
206
|
+
lsClient = LaserMind()
|
|
207
|
+
|
|
208
|
+
res = lsClient.solve_qubo(edgeList=quboListData, timeout=1)
|
|
209
|
+
|
|
210
|
+
print(res)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Solve QUBO Matrix using Asynchronous Flow
|
|
214
|
+
This example demonstratse how to solve a QUBO problem asynchronously using the LightSolver Platform Client.
|
|
215
|
+
Begin by creating a matrix to represent your QUBO problem.
|
|
216
|
+
The `solve_qubo` function is used with the following parameters:
|
|
217
|
+
- `matrixData`: A 2D array representing the QUBO problem.
|
|
218
|
+
- `timeout`: The desired time limit for the calculation in seconds.
|
|
219
|
+
- `waitForSolution`: A boolean flag set to `False` to indicate non-blocking mode.
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
import numpy
|
|
223
|
+
from laser_mind_client import LaserMind
|
|
224
|
+
|
|
225
|
+
# Create a mock QUBO problem
|
|
226
|
+
quboProblemData = numpy.random.randint(-1, 2, (10,10))
|
|
227
|
+
|
|
228
|
+
# Symmetrize our matrix
|
|
229
|
+
quboProblemData = (quboProblemData + quboProblemData.T) // 2
|
|
230
|
+
|
|
231
|
+
# Connect to the LightSolver Cloud
|
|
232
|
+
lsClient = LaserMind()
|
|
233
|
+
|
|
234
|
+
# Request a solution to the QUBO problem and get the request token for future retrieval.
|
|
235
|
+
# This call does not block operations until the problem is solved.
|
|
236
|
+
requestToken = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1, waitForSolution=False)
|
|
237
|
+
|
|
238
|
+
# You can run other code here that is not dependant on the request, while the server processes your request.
|
|
239
|
+
|
|
240
|
+
# Retrieve the solution using the get_solution_sync method.
|
|
241
|
+
# This blocks operations until the solution is acquired.
|
|
242
|
+
res = lsClient.get_solution_sync(requestToken)
|
|
243
|
+
|
|
244
|
+
print(res)
|
|
245
|
+
```
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
|
|
2
|
+
## LightSolver Platform Client
|
|
3
|
+
The LightSolver Platform Client is a Python package designed to interface with the LightSolver Cloud to facilitate solving Quadratic Unconstrained Binary Optimization (QUBO) problems.
|
|
4
|
+
|
|
5
|
+
This package is designated for internal access to features during the development process, as well as serves as a prototype for future versions of the production LightSolver Platform Client.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
- **QUBO Problem Solving:** The `solve_qubo` function accepts a QUBO problem, represented either as a 2D array (matrix) or an adjacency list, and returns the solution.
|
|
9
|
+
- **Synchronous and Asynchronous Operation:** Users can choose between blocking (synchronous) and non-blocking (asynchronous) modes for QUBO problem solving.
|
|
10
|
+
- **Flexible Installation:** Compatible with both Windows and Ubuntu systems.
|
|
11
|
+
|
|
12
|
+
### Solve QUBO
|
|
13
|
+
The `solve_qubo` function handles the computation of QUBO problems, either represented by a 2D array (matrix) or by an adjacency list. For code samples, see the **Usage** section.
|
|
14
|
+
|
|
15
|
+
#### Input Matrix Validity
|
|
16
|
+
- The matrix must be square.
|
|
17
|
+
- The matrix supports int or float cell values.
|
|
18
|
+
|
|
19
|
+
#### Return Value
|
|
20
|
+
A dictionary with the following fields:
|
|
21
|
+
```
|
|
22
|
+
- 'id': Unique identifier of the solution.
|
|
23
|
+
- 'solution': The solution as a Python list() of 1s and 0s.
|
|
24
|
+
- 'objval: The objective value of the solution.
|
|
25
|
+
- 'solverRunningTime': Time spent by the solver to calculate the problem.
|
|
26
|
+
- 'receivedTime': Timestamp when the request was received by the server.
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Synchronous and Asynchronous Usage
|
|
30
|
+
- **Synchronous Mode (Default):** The `waitForSolution` flag is set to **True** by default. The function blocks operations until a result is received.
|
|
31
|
+
- **Asynchronous Mode:** Set `waitForSolution` to **False**. The function returns immediately with a token object, allowing the script to continue while the server processes the QUBO problem.
|
|
32
|
+
|
|
33
|
+
## Setting Up
|
|
34
|
+
|
|
35
|
+
### Prerequisites
|
|
36
|
+
- Valid credentials for connecting to the LightSolver Cloud.
|
|
37
|
+
- LightSolver Client code project - please use this link to download.
|
|
38
|
+
- Python 3.10.
|
|
39
|
+
- Operating System: Linux or Windows. Tested on Ubuntu 20.04 and Windows 11.
|
|
40
|
+
- Highly Recommended: Use a virtual environment before installing laser-mind-client (Please see detailed action further below under the relevant OS).
|
|
41
|
+
|
|
42
|
+
### Installation
|
|
43
|
+
Complete the installation on Windows or Ubuntu as described below.
|
|
44
|
+
For further assistance with setup or connection issues, contact support@lightsolver.com.
|
|
45
|
+
|
|
46
|
+
#### Windows
|
|
47
|
+
1. Press the windows key, type "cmd", right click on the result and select "Run as administrator".
|
|
48
|
+
2. Navigate to the root folder where you unzipped and plan to use the LightSolver Client:
|
|
49
|
+
```
|
|
50
|
+
cd <your project folder>
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
3. (Recommended) Set the LightSolver credentials using environment variables (will remove the need to provide credentials for every usage).
|
|
54
|
+
Run the following command:
|
|
55
|
+
```sh
|
|
56
|
+
python setup_env.py
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
4. (Recommended) Create a virtual environment:
|
|
60
|
+
```sh
|
|
61
|
+
python -m venv .venv
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
5. (Recommended) Activate the new virtual environment:
|
|
65
|
+
```sh
|
|
66
|
+
.venv\Scripts\activate
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
6. Install the laser-mind-client package. This command instructs pip to install the package from a local folder instead of searching online:
|
|
70
|
+
```sh
|
|
71
|
+
pip install --no-cache-dir --find-links=.\packages laser-mind-client
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
7. (Recommended) Test LightSolver credentials
|
|
75
|
+
Run the following command:
|
|
76
|
+
```sh
|
|
77
|
+
python test_env.py
|
|
78
|
+
```
|
|
79
|
+
if test credentials failed , please reinstall credentials using following commands:
|
|
80
|
+
- Exit virtual environment , by running :
|
|
81
|
+
```sh
|
|
82
|
+
deactivate
|
|
83
|
+
```
|
|
84
|
+
- Run the following command:
|
|
85
|
+
```sh
|
|
86
|
+
python setup_env.py
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
#### Ubuntu
|
|
91
|
+
1. Open new terminal window
|
|
92
|
+
2. Navigate to the root folder where you unzipped and plan to use the LightSolver Client:
|
|
93
|
+
```
|
|
94
|
+
cd <your project folder>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
3. (Recommended) Set the LightSolver credentials using environment variables (will remove the need to provide credentials for every usage).
|
|
98
|
+
Run the following command:
|
|
99
|
+
```sh
|
|
100
|
+
python3 setup_env.py
|
|
101
|
+
source ~/.bash_profile
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
4. (Recommended) Create a virtual environment:
|
|
105
|
+
```sh
|
|
106
|
+
python3 -m venv .venv
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
5. (Recommended) Activate the new virtual environment:
|
|
110
|
+
```sh
|
|
111
|
+
chmod 755 .venv/bin/activate
|
|
112
|
+
source .venv/bin/activate
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
6. Install the laser-mind-client package. This command instructs pip to install the package from a local folder instead of searching online:
|
|
116
|
+
```sh
|
|
117
|
+
pip install --no-cache-dir --find-links=./packages laser-mind-client
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
7. (Recommended) Test LightSolver credentials
|
|
121
|
+
Run the following command:
|
|
122
|
+
```sh
|
|
123
|
+
python3 test_env.py
|
|
124
|
+
```
|
|
125
|
+
if test credentials failed , please reinstall credentials using following commands:
|
|
126
|
+
- Exit virtual environment , by running :
|
|
127
|
+
```sh
|
|
128
|
+
deactivate
|
|
129
|
+
```
|
|
130
|
+
- Run the following command:
|
|
131
|
+
```sh
|
|
132
|
+
python3 setup_env.py
|
|
133
|
+
source ~/.bash_profile
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Authentication
|
|
137
|
+
Initialization of the `LaserMind` class automatically forms a secure and authenticated connection with the LightSolver Cloud.
|
|
138
|
+
Subsequent calls by the same user are similarly secure and authenticated.
|
|
139
|
+
|
|
140
|
+
## Usage
|
|
141
|
+
To begin solving any QUBO problem:
|
|
142
|
+
1. Create an instance of the ```LaserMind``` class. This class represents the client that requests solutions from the LightSolver Cloud.
|
|
143
|
+
2. Call the ```solve_qubo``` function using either a matrix or an adjacency list.
|
|
144
|
+
**Note:** You may either provide a value for ```matrixData``` or for ```edgeList```, but not both.
|
|
145
|
+
|
|
146
|
+
### Solve QUBO Matrix Example
|
|
147
|
+
This example creates a matrix representing a QUBO problem and solves it using the LightSolver Platform Client.
|
|
148
|
+
The `solve_qubo` function is used with the following parameters:
|
|
149
|
+
- ```matrixData```: A 2D array representing the QUBO problem.
|
|
150
|
+
- ```timeout```: The required time limit for the calculation in seconds.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
import numpy
|
|
154
|
+
from laser_mind_client import LaserMind
|
|
155
|
+
|
|
156
|
+
# Create a mock QUBO problem
|
|
157
|
+
quboProblemData = numpy.random.randint(-1, 2, (10,10))
|
|
158
|
+
|
|
159
|
+
# Symmetrize the matrix
|
|
160
|
+
quboProblemData = (quboProblemData + quboProblemData.T) // 2
|
|
161
|
+
|
|
162
|
+
# Connect to the LightSolver Cloud
|
|
163
|
+
lsClient = LaserMind()
|
|
164
|
+
|
|
165
|
+
res = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1)
|
|
166
|
+
|
|
167
|
+
print(res)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Solve QUBO Adjacency List Example
|
|
171
|
+
This example describes a QUBO problem using an adjacency list. This is useful for sparse matrices.
|
|
172
|
+
The `solve_qubo` function is used with the following parameters:
|
|
173
|
+
- ```edgeList```: The adjacency list representing the QUBO problem.
|
|
174
|
+
- ```timeout```: The required time limit for the calculation in seconds.
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from laser_mind_client import LaserMind
|
|
179
|
+
|
|
180
|
+
# Create a mock QUBO problem
|
|
181
|
+
quboListData = [
|
|
182
|
+
[1,1,5],
|
|
183
|
+
[1,2,-6],
|
|
184
|
+
[2,2,3],
|
|
185
|
+
[2,3,-1],
|
|
186
|
+
[3,10,1]]
|
|
187
|
+
|
|
188
|
+
# Connect to the LightSolver Cloud
|
|
189
|
+
lsClient = LaserMind()
|
|
190
|
+
|
|
191
|
+
res = lsClient.solve_qubo(edgeList=quboListData, timeout=1)
|
|
192
|
+
|
|
193
|
+
print(res)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Solve QUBO Matrix using Asynchronous Flow
|
|
197
|
+
This example demonstratse how to solve a QUBO problem asynchronously using the LightSolver Platform Client.
|
|
198
|
+
Begin by creating a matrix to represent your QUBO problem.
|
|
199
|
+
The `solve_qubo` function is used with the following parameters:
|
|
200
|
+
- `matrixData`: A 2D array representing the QUBO problem.
|
|
201
|
+
- `timeout`: The desired time limit for the calculation in seconds.
|
|
202
|
+
- `waitForSolution`: A boolean flag set to `False` to indicate non-blocking mode.
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import numpy
|
|
206
|
+
from laser_mind_client import LaserMind
|
|
207
|
+
|
|
208
|
+
# Create a mock QUBO problem
|
|
209
|
+
quboProblemData = numpy.random.randint(-1, 2, (10,10))
|
|
210
|
+
|
|
211
|
+
# Symmetrize our matrix
|
|
212
|
+
quboProblemData = (quboProblemData + quboProblemData.T) // 2
|
|
213
|
+
|
|
214
|
+
# Connect to the LightSolver Cloud
|
|
215
|
+
lsClient = LaserMind()
|
|
216
|
+
|
|
217
|
+
# Request a solution to the QUBO problem and get the request token for future retrieval.
|
|
218
|
+
# This call does not block operations until the problem is solved.
|
|
219
|
+
requestToken = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1, waitForSolution=False)
|
|
220
|
+
|
|
221
|
+
# You can run other code here that is not dependant on the request, while the server processes your request.
|
|
222
|
+
|
|
223
|
+
# Retrieve the solution using the get_solution_sync method.
|
|
224
|
+
# This blocks operations until the solution is acquired.
|
|
225
|
+
res = lsClient.get_solution_sync(requestToken)
|
|
226
|
+
|
|
227
|
+
print(res)
|
|
228
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from laser_mind_client.laser_mind_client import LaserMind
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import logging
|
|
3
|
+
import time
|
|
4
|
+
import numpy
|
|
5
|
+
|
|
6
|
+
from ls_api_clients import LSAPIClient
|
|
7
|
+
from ls_packers import float_array_as_int
|
|
8
|
+
from ls_packers import numpy_array_to_triu_flat
|
|
9
|
+
from laser_mind_client_meta import MessageKeys
|
|
10
|
+
|
|
11
|
+
logging.basicConfig(
|
|
12
|
+
filename="laser-mind.log",
|
|
13
|
+
level=logging.INFO,
|
|
14
|
+
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
|
|
15
|
+
|
|
16
|
+
def symmetrize(matrix):
|
|
17
|
+
"""
|
|
18
|
+
Symmetrizes a given matrix in numpy array form
|
|
19
|
+
"""
|
|
20
|
+
if (matrix == matrix.T).all():
|
|
21
|
+
# do nothing if the matrix is already symmetric
|
|
22
|
+
return matrix
|
|
23
|
+
result = (matrix + matrix.T) * 0.5
|
|
24
|
+
return result
|
|
25
|
+
|
|
26
|
+
class LaserMind:
|
|
27
|
+
"""
|
|
28
|
+
## A client for accessing LightSolver's computaion capabilities via web services.
|
|
29
|
+
"""
|
|
30
|
+
POLL_MAX_RETRIES = 100000
|
|
31
|
+
POLL_DELAY_SECS = 0.5
|
|
32
|
+
|
|
33
|
+
def __init__(self,
|
|
34
|
+
username = None,
|
|
35
|
+
password = None,
|
|
36
|
+
states_per_call=3):
|
|
37
|
+
if username == None:
|
|
38
|
+
if 'LS_USER' in os.environ:
|
|
39
|
+
username = os.environ['LS_USER']
|
|
40
|
+
else:
|
|
41
|
+
raise Exception("the 'username' parameter cannot be None if the LS_USER environment variable is not set.")
|
|
42
|
+
if password == None:
|
|
43
|
+
if 'LS_PASS' in os.environ:
|
|
44
|
+
password = os.environ['LS_PASS']
|
|
45
|
+
else:
|
|
46
|
+
raise Exception("the 'password' parameter cannot be None if the LS_PASS environment variable is not set.")
|
|
47
|
+
|
|
48
|
+
self.states_per_call = states_per_call
|
|
49
|
+
logging.info('LightSolver connection init started')
|
|
50
|
+
self.apiClient = LSAPIClient(username, password)
|
|
51
|
+
logging.info('LightSolver connection init finished')
|
|
52
|
+
|
|
53
|
+
def get_solution_by_id(self, solutionId, timestamp):
|
|
54
|
+
"""
|
|
55
|
+
Retrieve a previously requested solution from the LightSolver cloud.
|
|
56
|
+
|
|
57
|
+
- `solutionId` : the solution id received when requesting a solution.
|
|
58
|
+
- `timestamp` : the timestamp received when requesting a solution.
|
|
59
|
+
"""
|
|
60
|
+
result = self.apiClient.SendResultRequest(solutionId, timestamp)
|
|
61
|
+
return result
|
|
62
|
+
|
|
63
|
+
def get_solution_sync(self, requestInfo):
|
|
64
|
+
"""
|
|
65
|
+
Waits for a solution to be available and downloads it.
|
|
66
|
+
|
|
67
|
+
- `requestInfo` : a dictionary containing 'id' and 'reqTime' keys needed for retrieving the solution.
|
|
68
|
+
"""
|
|
69
|
+
for try_num in range(1, self.POLL_MAX_RETRIES):
|
|
70
|
+
result = self.get_solution_by_id(requestInfo['id'], requestInfo['reqTime'])
|
|
71
|
+
if result != None:
|
|
72
|
+
result["receivedTime"] = requestInfo["receivedTime"]
|
|
73
|
+
logging.info(f"got solution for {requestInfo}, try #{try_num}")
|
|
74
|
+
return result
|
|
75
|
+
time.sleep((self.POLL_DELAY_SECS))
|
|
76
|
+
|
|
77
|
+
logging.warning(f"got timeout for {requestInfo}")
|
|
78
|
+
raise FileNotFoundError(f"Exceeded max retries when attempting to find {requestInfo['id']}")
|
|
79
|
+
|
|
80
|
+
def make_command_input(self, matrixData = None, edgeList = None, timeout = 10):
|
|
81
|
+
"""
|
|
82
|
+
Creates the message payload for a request input.
|
|
83
|
+
"""
|
|
84
|
+
commandInput = {}
|
|
85
|
+
|
|
86
|
+
if matrixData is not None:
|
|
87
|
+
varCount = len(matrixData)
|
|
88
|
+
if varCount > 10000 or varCount < 10:
|
|
89
|
+
raise(ValueError("The total number of variables must be between 10-10000"))
|
|
90
|
+
if type(matrixData) == numpy.ndarray:
|
|
91
|
+
matrixData = symmetrize(matrixData)
|
|
92
|
+
if matrixData.dtype == numpy.float32 or matrixData.dtype == numpy.float64:
|
|
93
|
+
triuFlat = float_array_as_int(numpy_array_to_triu_flat(matrixData))
|
|
94
|
+
commandInput[MessageKeys.FLOAT_DATA_AS_INT] = True
|
|
95
|
+
else:
|
|
96
|
+
triuFlat = numpy_array_to_triu_flat(matrixData)
|
|
97
|
+
else:
|
|
98
|
+
validationArr = [len(matrixData[i]) != varCount for i in range(varCount)]
|
|
99
|
+
if numpy.array(validationArr).any():
|
|
100
|
+
raise(ValueError("The input must be a square matrix"))
|
|
101
|
+
triuFlat = numpy_array_to_triu_flat(symmetrize(numpy.array(matrixData)))
|
|
102
|
+
commandInput[MessageKeys.QUBO_MATRIX] = triuFlat.tolist()
|
|
103
|
+
elif edgeList is not None:
|
|
104
|
+
if type(edgeList) == numpy.ndarray:
|
|
105
|
+
varCount = numpy.max(edgeList[:,0:2])
|
|
106
|
+
edgeList = edgeList.tolist()
|
|
107
|
+
else:
|
|
108
|
+
varCount = numpy.max(numpy.array(edgeList)[:,0:2])
|
|
109
|
+
if varCount > 10000 or varCount < 10:
|
|
110
|
+
raise(ValueError("The total number of variables must be between 10-10000"))
|
|
111
|
+
commandInput[MessageKeys.QUBO_EDGE_LIST] = edgeList
|
|
112
|
+
else:
|
|
113
|
+
raise Exception("You must provide either a QUBO matrix or a QUBO edge list")
|
|
114
|
+
|
|
115
|
+
commandInput[MessageKeys.ALGO_RUN_TIMEOUT] = timeout
|
|
116
|
+
return commandInput, int(varCount)
|
|
117
|
+
|
|
118
|
+
def upload_qubo_input(self, matrixData = None, edgeList = None, timeout = 10, inputPath = None):
|
|
119
|
+
"""
|
|
120
|
+
Uploads the given input to the lightsolver cloud for later processing.
|
|
121
|
+
|
|
122
|
+
- `matrixData` : (optional) The matrix data of the target problem, must be a symmetric matrix. if given, the edge list in the vortex parameters is ignored.
|
|
123
|
+
- `edgeList` : (optional) The edge list describing Ising matrix of the target problem. if the matrixData parameter is given, this parameter is ignored.
|
|
124
|
+
- `timeout` : (optional) the running timeout, in seconds for the algorithm, must be in the range 0.001 - 60 (default: 10).
|
|
125
|
+
- `inputPath` : (optional) The the path to a pre-uploaded input file if not given a random string is used returned.
|
|
126
|
+
|
|
127
|
+
Returns a dictionary with the 'data' key being a dictionary representing the solution using the following keys:
|
|
128
|
+
- `iid` : The id of the uploaded file.
|
|
129
|
+
- `varCount` : The amount number of variables of the problem.
|
|
130
|
+
|
|
131
|
+
"""
|
|
132
|
+
commandInput, varCount = self.make_command_input(matrixData, edgeList, timeout)
|
|
133
|
+
|
|
134
|
+
iid = self.apiClient.upload_command_input(commandInput, inputPath)
|
|
135
|
+
return iid, varCount
|
|
136
|
+
|
|
137
|
+
def solve_qubo(self, matrixData = None, edgeList = None, inputPath = None, timeout = 10, waitForSolution = True):
|
|
138
|
+
"""
|
|
139
|
+
Solves a qubo problem using the optimized algorithm.
|
|
140
|
+
|
|
141
|
+
- `matrixData` : (optional) The matrix data of the target problem, must be a symmetric matrix. if given, the edge list in the vortex parameters is ignored.
|
|
142
|
+
- `edgeList` : (optional) The edge list describing Ising matrix of the target problem. if the matrixData parameter is given, this parameter is ignored.
|
|
143
|
+
- `inputPath` : (optional) The the path to a pre-uploaded input file, the upload can be done using the upload_qubo_input() method of this class.
|
|
144
|
+
- `timeout` : (optional) the running timeout, in seconds for the algorithm, must be in the range 0.001 - 60 (default: 10).
|
|
145
|
+
- `waitForSolution` : (optional) When set to True it waits for the solution, else returns with retrieval info (default: True).
|
|
146
|
+
|
|
147
|
+
Returns a dictionary with the 'data' key being a dictionary representing the solution using the following keys:
|
|
148
|
+
- `objval` : The objective value.
|
|
149
|
+
- `solution` : The optimal solution found.
|
|
150
|
+
"""
|
|
151
|
+
command_name = MessageKeys.QUBO_COMMAND_NAME
|
|
152
|
+
if inputPath == None:
|
|
153
|
+
iid, varCount = self.upload_qubo_input(matrixData, edgeList, timeout)
|
|
154
|
+
else:
|
|
155
|
+
iid = inputPath
|
|
156
|
+
varCount = 10000
|
|
157
|
+
|
|
158
|
+
requestInput = {
|
|
159
|
+
MessageKeys.QUBO_INPUT_PATH : iid,
|
|
160
|
+
MessageKeys.ALGO_RUN_TIMEOUT : timeout,
|
|
161
|
+
MessageKeys.VAR_COUNT_KEY : varCount
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
response = self.apiClient.SendCommandRequest(command_name, requestInput)
|
|
165
|
+
logging.info(f"got response {response}")
|
|
166
|
+
if not waitForSolution:
|
|
167
|
+
return response
|
|
168
|
+
result = self.get_solution_sync(response)
|
|
169
|
+
return result
|
|
170
|
+
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: laser-mind-client
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A client python API for accessing LightSolver's capabilities
|
|
5
|
+
Author-email: Assaf Kalinski <assaf@lightsolver.com>
|
|
6
|
+
Project-URL: Homepage, https://lightsolver.com
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: numpy>=1.21.5
|
|
14
|
+
Requires-Dist: laser-mind-client-meta>=0.0.9
|
|
15
|
+
Requires-Dist: ls_api_clients>=0.4.6
|
|
16
|
+
Requires-Dist: ls-packers>=0.2.0
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## LightSolver Platform Client
|
|
20
|
+
The LightSolver Platform Client is a Python package designed to interface with the LightSolver Cloud to facilitate solving Quadratic Unconstrained Binary Optimization (QUBO) problems.
|
|
21
|
+
|
|
22
|
+
This package is designated for internal access to features during the development process, as well as serves as a prototype for future versions of the production LightSolver Platform Client.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
- **QUBO Problem Solving:** The `solve_qubo` function accepts a QUBO problem, represented either as a 2D array (matrix) or an adjacency list, and returns the solution.
|
|
26
|
+
- **Synchronous and Asynchronous Operation:** Users can choose between blocking (synchronous) and non-blocking (asynchronous) modes for QUBO problem solving.
|
|
27
|
+
- **Flexible Installation:** Compatible with both Windows and Ubuntu systems.
|
|
28
|
+
|
|
29
|
+
### Solve QUBO
|
|
30
|
+
The `solve_qubo` function handles the computation of QUBO problems, either represented by a 2D array (matrix) or by an adjacency list. For code samples, see the **Usage** section.
|
|
31
|
+
|
|
32
|
+
#### Input Matrix Validity
|
|
33
|
+
- The matrix must be square.
|
|
34
|
+
- The matrix supports int or float cell values.
|
|
35
|
+
|
|
36
|
+
#### Return Value
|
|
37
|
+
A dictionary with the following fields:
|
|
38
|
+
```
|
|
39
|
+
- 'id': Unique identifier of the solution.
|
|
40
|
+
- 'solution': The solution as a Python list() of 1s and 0s.
|
|
41
|
+
- 'objval: The objective value of the solution.
|
|
42
|
+
- 'solverRunningTime': Time spent by the solver to calculate the problem.
|
|
43
|
+
- 'receivedTime': Timestamp when the request was received by the server.
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Synchronous and Asynchronous Usage
|
|
47
|
+
- **Synchronous Mode (Default):** The `waitForSolution` flag is set to **True** by default. The function blocks operations until a result is received.
|
|
48
|
+
- **Asynchronous Mode:** Set `waitForSolution` to **False**. The function returns immediately with a token object, allowing the script to continue while the server processes the QUBO problem.
|
|
49
|
+
|
|
50
|
+
## Setting Up
|
|
51
|
+
|
|
52
|
+
### Prerequisites
|
|
53
|
+
- Valid credentials for connecting to the LightSolver Cloud.
|
|
54
|
+
- LightSolver Client code project - please use this link to download.
|
|
55
|
+
- Python 3.10.
|
|
56
|
+
- Operating System: Linux or Windows. Tested on Ubuntu 20.04 and Windows 11.
|
|
57
|
+
- Highly Recommended: Use a virtual environment before installing laser-mind-client (Please see detailed action further below under the relevant OS).
|
|
58
|
+
|
|
59
|
+
### Installation
|
|
60
|
+
Complete the installation on Windows or Ubuntu as described below.
|
|
61
|
+
For further assistance with setup or connection issues, contact support@lightsolver.com.
|
|
62
|
+
|
|
63
|
+
#### Windows
|
|
64
|
+
1. Press the windows key, type "cmd", right click on the result and select "Run as administrator".
|
|
65
|
+
2. Navigate to the root folder where you unzipped and plan to use the LightSolver Client:
|
|
66
|
+
```
|
|
67
|
+
cd <your project folder>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
3. (Recommended) Set the LightSolver credentials using environment variables (will remove the need to provide credentials for every usage).
|
|
71
|
+
Run the following command:
|
|
72
|
+
```sh
|
|
73
|
+
python setup_env.py
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
4. (Recommended) Create a virtual environment:
|
|
77
|
+
```sh
|
|
78
|
+
python -m venv .venv
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
5. (Recommended) Activate the new virtual environment:
|
|
82
|
+
```sh
|
|
83
|
+
.venv\Scripts\activate
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
6. Install the laser-mind-client package. This command instructs pip to install the package from a local folder instead of searching online:
|
|
87
|
+
```sh
|
|
88
|
+
pip install --no-cache-dir --find-links=.\packages laser-mind-client
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
7. (Recommended) Test LightSolver credentials
|
|
92
|
+
Run the following command:
|
|
93
|
+
```sh
|
|
94
|
+
python test_env.py
|
|
95
|
+
```
|
|
96
|
+
if test credentials failed , please reinstall credentials using following commands:
|
|
97
|
+
- Exit virtual environment , by running :
|
|
98
|
+
```sh
|
|
99
|
+
deactivate
|
|
100
|
+
```
|
|
101
|
+
- Run the following command:
|
|
102
|
+
```sh
|
|
103
|
+
python setup_env.py
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
#### Ubuntu
|
|
108
|
+
1. Open new terminal window
|
|
109
|
+
2. Navigate to the root folder where you unzipped and plan to use the LightSolver Client:
|
|
110
|
+
```
|
|
111
|
+
cd <your project folder>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
3. (Recommended) Set the LightSolver credentials using environment variables (will remove the need to provide credentials for every usage).
|
|
115
|
+
Run the following command:
|
|
116
|
+
```sh
|
|
117
|
+
python3 setup_env.py
|
|
118
|
+
source ~/.bash_profile
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
4. (Recommended) Create a virtual environment:
|
|
122
|
+
```sh
|
|
123
|
+
python3 -m venv .venv
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
5. (Recommended) Activate the new virtual environment:
|
|
127
|
+
```sh
|
|
128
|
+
chmod 755 .venv/bin/activate
|
|
129
|
+
source .venv/bin/activate
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
6. Install the laser-mind-client package. This command instructs pip to install the package from a local folder instead of searching online:
|
|
133
|
+
```sh
|
|
134
|
+
pip install --no-cache-dir --find-links=./packages laser-mind-client
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
7. (Recommended) Test LightSolver credentials
|
|
138
|
+
Run the following command:
|
|
139
|
+
```sh
|
|
140
|
+
python3 test_env.py
|
|
141
|
+
```
|
|
142
|
+
if test credentials failed , please reinstall credentials using following commands:
|
|
143
|
+
- Exit virtual environment , by running :
|
|
144
|
+
```sh
|
|
145
|
+
deactivate
|
|
146
|
+
```
|
|
147
|
+
- Run the following command:
|
|
148
|
+
```sh
|
|
149
|
+
python3 setup_env.py
|
|
150
|
+
source ~/.bash_profile
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Authentication
|
|
154
|
+
Initialization of the `LaserMind` class automatically forms a secure and authenticated connection with the LightSolver Cloud.
|
|
155
|
+
Subsequent calls by the same user are similarly secure and authenticated.
|
|
156
|
+
|
|
157
|
+
## Usage
|
|
158
|
+
To begin solving any QUBO problem:
|
|
159
|
+
1. Create an instance of the ```LaserMind``` class. This class represents the client that requests solutions from the LightSolver Cloud.
|
|
160
|
+
2. Call the ```solve_qubo``` function using either a matrix or an adjacency list.
|
|
161
|
+
**Note:** You may either provide a value for ```matrixData``` or for ```edgeList```, but not both.
|
|
162
|
+
|
|
163
|
+
### Solve QUBO Matrix Example
|
|
164
|
+
This example creates a matrix representing a QUBO problem and solves it using the LightSolver Platform Client.
|
|
165
|
+
The `solve_qubo` function is used with the following parameters:
|
|
166
|
+
- ```matrixData```: A 2D array representing the QUBO problem.
|
|
167
|
+
- ```timeout```: The required time limit for the calculation in seconds.
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import numpy
|
|
171
|
+
from laser_mind_client import LaserMind
|
|
172
|
+
|
|
173
|
+
# Create a mock QUBO problem
|
|
174
|
+
quboProblemData = numpy.random.randint(-1, 2, (10,10))
|
|
175
|
+
|
|
176
|
+
# Symmetrize the matrix
|
|
177
|
+
quboProblemData = (quboProblemData + quboProblemData.T) // 2
|
|
178
|
+
|
|
179
|
+
# Connect to the LightSolver Cloud
|
|
180
|
+
lsClient = LaserMind()
|
|
181
|
+
|
|
182
|
+
res = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1)
|
|
183
|
+
|
|
184
|
+
print(res)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Solve QUBO Adjacency List Example
|
|
188
|
+
This example describes a QUBO problem using an adjacency list. This is useful for sparse matrices.
|
|
189
|
+
The `solve_qubo` function is used with the following parameters:
|
|
190
|
+
- ```edgeList```: The adjacency list representing the QUBO problem.
|
|
191
|
+
- ```timeout```: The required time limit for the calculation in seconds.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from laser_mind_client import LaserMind
|
|
196
|
+
|
|
197
|
+
# Create a mock QUBO problem
|
|
198
|
+
quboListData = [
|
|
199
|
+
[1,1,5],
|
|
200
|
+
[1,2,-6],
|
|
201
|
+
[2,2,3],
|
|
202
|
+
[2,3,-1],
|
|
203
|
+
[3,10,1]]
|
|
204
|
+
|
|
205
|
+
# Connect to the LightSolver Cloud
|
|
206
|
+
lsClient = LaserMind()
|
|
207
|
+
|
|
208
|
+
res = lsClient.solve_qubo(edgeList=quboListData, timeout=1)
|
|
209
|
+
|
|
210
|
+
print(res)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Solve QUBO Matrix using Asynchronous Flow
|
|
214
|
+
This example demonstratse how to solve a QUBO problem asynchronously using the LightSolver Platform Client.
|
|
215
|
+
Begin by creating a matrix to represent your QUBO problem.
|
|
216
|
+
The `solve_qubo` function is used with the following parameters:
|
|
217
|
+
- `matrixData`: A 2D array representing the QUBO problem.
|
|
218
|
+
- `timeout`: The desired time limit for the calculation in seconds.
|
|
219
|
+
- `waitForSolution`: A boolean flag set to `False` to indicate non-blocking mode.
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
import numpy
|
|
223
|
+
from laser_mind_client import LaserMind
|
|
224
|
+
|
|
225
|
+
# Create a mock QUBO problem
|
|
226
|
+
quboProblemData = numpy.random.randint(-1, 2, (10,10))
|
|
227
|
+
|
|
228
|
+
# Symmetrize our matrix
|
|
229
|
+
quboProblemData = (quboProblemData + quboProblemData.T) // 2
|
|
230
|
+
|
|
231
|
+
# Connect to the LightSolver Cloud
|
|
232
|
+
lsClient = LaserMind()
|
|
233
|
+
|
|
234
|
+
# Request a solution to the QUBO problem and get the request token for future retrieval.
|
|
235
|
+
# This call does not block operations until the problem is solved.
|
|
236
|
+
requestToken = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1, waitForSolution=False)
|
|
237
|
+
|
|
238
|
+
# You can run other code here that is not dependant on the request, while the server processes your request.
|
|
239
|
+
|
|
240
|
+
# Retrieve the solution using the get_solution_sync method.
|
|
241
|
+
# This blocks operations until the solution is acquired.
|
|
242
|
+
res = lsClient.get_solution_sync(requestToken)
|
|
243
|
+
|
|
244
|
+
print(res)
|
|
245
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
laser_mind_client/__init__.py
|
|
5
|
+
laser_mind_client/laser_mind_client.py
|
|
6
|
+
laser_mind_client.egg-info/PKG-INFO
|
|
7
|
+
laser_mind_client.egg-info/SOURCES.txt
|
|
8
|
+
laser_mind_client.egg-info/dependency_links.txt
|
|
9
|
+
laser_mind_client.egg-info/requires.txt
|
|
10
|
+
laser_mind_client.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
laser_mind_client
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "laser-mind-client"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Assaf Kalinski", email="assaf@lightsolver.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "A client python API for accessing LightSolver's capabilities"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.8"
|
|
14
|
+
dependencies = [
|
|
15
|
+
'numpy>=1.21.5',
|
|
16
|
+
'laser-mind-client-meta>=0.0.9',
|
|
17
|
+
'ls_api_clients>=0.4.6',
|
|
18
|
+
'ls-packers>=0.2.0'
|
|
19
|
+
]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"License :: OSI Approved :: MIT License",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
"Homepage" = "https://lightsolver.com"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
include = ["laser_mind_client"]
|
|
32
|
+
|