qoro-divi 0.2.0b1__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. divi/__init__.py +1 -2
  2. divi/backends/__init__.py +10 -0
  3. divi/backends/_backend_properties_conversion.py +227 -0
  4. divi/backends/_circuit_runner.py +70 -0
  5. divi/backends/_execution_result.py +70 -0
  6. divi/backends/_parallel_simulator.py +486 -0
  7. divi/backends/_qoro_service.py +663 -0
  8. divi/backends/_qpu_system.py +101 -0
  9. divi/backends/_results_processing.py +133 -0
  10. divi/circuits/__init__.py +13 -0
  11. divi/{exp/cirq → circuits/_cirq}/__init__.py +1 -2
  12. divi/circuits/_cirq/_parser.py +110 -0
  13. divi/circuits/_cirq/_qasm_export.py +78 -0
  14. divi/circuits/_core.py +391 -0
  15. divi/{qasm.py → circuits/_qasm_conversion.py} +73 -14
  16. divi/circuits/_qasm_validation.py +694 -0
  17. divi/qprog/__init__.py +27 -8
  18. divi/qprog/_expectation.py +181 -0
  19. divi/qprog/_hamiltonians.py +281 -0
  20. divi/qprog/algorithms/__init__.py +16 -0
  21. divi/qprog/algorithms/_ansatze.py +368 -0
  22. divi/qprog/algorithms/_custom_vqa.py +263 -0
  23. divi/qprog/algorithms/_pce.py +262 -0
  24. divi/qprog/algorithms/_qaoa.py +579 -0
  25. divi/qprog/algorithms/_vqe.py +262 -0
  26. divi/qprog/batch.py +387 -74
  27. divi/qprog/checkpointing.py +556 -0
  28. divi/qprog/exceptions.py +9 -0
  29. divi/qprog/optimizers.py +1014 -43
  30. divi/qprog/quantum_program.py +243 -412
  31. divi/qprog/typing.py +62 -0
  32. divi/qprog/variational_quantum_algorithm.py +1208 -0
  33. divi/qprog/workflows/__init__.py +10 -0
  34. divi/qprog/{_graph_partitioning.py → workflows/_graph_partitioning.py} +139 -95
  35. divi/qprog/workflows/_qubo_partitioning.py +221 -0
  36. divi/qprog/workflows/_vqe_sweep.py +560 -0
  37. divi/reporting/__init__.py +7 -0
  38. divi/reporting/_pbar.py +127 -0
  39. divi/reporting/_qlogger.py +68 -0
  40. divi/reporting/_reporter.py +155 -0
  41. {qoro_divi-0.2.0b1.dist-info → qoro_divi-0.6.0.dist-info}/METADATA +43 -15
  42. qoro_divi-0.6.0.dist-info/RECORD +47 -0
  43. {qoro_divi-0.2.0b1.dist-info → qoro_divi-0.6.0.dist-info}/WHEEL +1 -1
  44. qoro_divi-0.6.0.dist-info/licenses/LICENSES/.license-header +3 -0
  45. divi/_pbar.py +0 -73
  46. divi/circuits.py +0 -139
  47. divi/exp/cirq/_lexer.py +0 -126
  48. divi/exp/cirq/_parser.py +0 -889
  49. divi/exp/cirq/_qasm_export.py +0 -37
  50. divi/exp/cirq/_qasm_import.py +0 -35
  51. divi/exp/cirq/exception.py +0 -21
  52. divi/exp/scipy/_cobyla.py +0 -342
  53. divi/exp/scipy/pyprima/LICENCE.txt +0 -28
  54. divi/exp/scipy/pyprima/__init__.py +0 -263
  55. divi/exp/scipy/pyprima/cobyla/__init__.py +0 -0
  56. divi/exp/scipy/pyprima/cobyla/cobyla.py +0 -599
  57. divi/exp/scipy/pyprima/cobyla/cobylb.py +0 -849
  58. divi/exp/scipy/pyprima/cobyla/geometry.py +0 -240
  59. divi/exp/scipy/pyprima/cobyla/initialize.py +0 -269
  60. divi/exp/scipy/pyprima/cobyla/trustregion.py +0 -540
  61. divi/exp/scipy/pyprima/cobyla/update.py +0 -331
  62. divi/exp/scipy/pyprima/common/__init__.py +0 -0
  63. divi/exp/scipy/pyprima/common/_bounds.py +0 -41
  64. divi/exp/scipy/pyprima/common/_linear_constraints.py +0 -46
  65. divi/exp/scipy/pyprima/common/_nonlinear_constraints.py +0 -64
  66. divi/exp/scipy/pyprima/common/_project.py +0 -224
  67. divi/exp/scipy/pyprima/common/checkbreak.py +0 -107
  68. divi/exp/scipy/pyprima/common/consts.py +0 -48
  69. divi/exp/scipy/pyprima/common/evaluate.py +0 -101
  70. divi/exp/scipy/pyprima/common/history.py +0 -39
  71. divi/exp/scipy/pyprima/common/infos.py +0 -30
  72. divi/exp/scipy/pyprima/common/linalg.py +0 -452
  73. divi/exp/scipy/pyprima/common/message.py +0 -336
  74. divi/exp/scipy/pyprima/common/powalg.py +0 -131
  75. divi/exp/scipy/pyprima/common/preproc.py +0 -393
  76. divi/exp/scipy/pyprima/common/present.py +0 -5
  77. divi/exp/scipy/pyprima/common/ratio.py +0 -56
  78. divi/exp/scipy/pyprima/common/redrho.py +0 -49
  79. divi/exp/scipy/pyprima/common/selectx.py +0 -346
  80. divi/interfaces.py +0 -25
  81. divi/parallel_simulator.py +0 -258
  82. divi/qlogger.py +0 -119
  83. divi/qoro_service.py +0 -343
  84. divi/qprog/_mlae.py +0 -182
  85. divi/qprog/_qaoa.py +0 -440
  86. divi/qprog/_vqe.py +0 -275
  87. divi/qprog/_vqe_sweep.py +0 -144
  88. divi/utils.py +0 -116
  89. qoro_divi-0.2.0b1.dist-info/RECORD +0 -58
  90. /divi/{qem.py → circuits/qem.py} +0 -0
  91. {qoro_divi-0.2.0b1.dist-info → qoro_divi-0.6.0.dist-info/licenses}/LICENSE +0 -0
  92. {qoro_divi-0.2.0b1.dist-info → qoro_divi-0.6.0.dist-info/licenses}/LICENSES/Apache-2.0.txt +0 -0
@@ -1,37 +0,0 @@
1
- # SPDX-FileCopyrightText: 2025 Qoro Quantum Ltd <divi@qoroquantum.de>
2
- #
3
- # SPDX-License-Identifier: Apache-2.0
4
-
5
- from typing import Any
6
-
7
- from cirq import ops
8
- from cirq.protocols.qasm import QasmArgs
9
-
10
-
11
- def patched_format_field(self, value: Any, spec: str) -> str:
12
- """Method of string.Formatter that specifies the output of format()."""
13
- if isinstance(value, (float, int)):
14
- if isinstance(value, float):
15
- value = round(value, self.precision)
16
- if spec == "half_turns":
17
- value = f"pi*{value}" if value != 0 else "0"
18
- spec = ""
19
-
20
- elif isinstance(value, ops.Qid):
21
- value = self.qubit_id_map[value]
22
-
23
- elif isinstance(value, str) and spec == "meas":
24
- value = self.meas_key_id_map[value]
25
- spec = ""
26
-
27
- from sympy import Expr, pi
28
-
29
- if isinstance(value, Expr):
30
- if spec == "half_turns":
31
- value *= pi
32
- return str(value)
33
-
34
- return super(QasmArgs, self).format_field(value, spec)
35
-
36
-
37
- QasmArgs.format_field = patched_format_field
@@ -1,35 +0,0 @@
1
- # Copyright 2018 The Cirq Developers
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from __future__ import annotations
16
-
17
- from typing import TYPE_CHECKING
18
-
19
- from ._parser import QasmParser
20
-
21
- if TYPE_CHECKING:
22
- import cirq
23
-
24
-
25
- def cirq_circuit_from_qasm(qasm: str) -> cirq.Circuit:
26
- """Parses an OpenQASM string to `cirq.Circuit`.
27
-
28
- Args:
29
- qasm: The OpenQASM string
30
-
31
- Returns:
32
- The parsed circuit
33
- """
34
-
35
- return QasmParser().parse(qasm).circuit
@@ -1,21 +0,0 @@
1
- # Copyright 2018 The Cirq Developers
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from __future__ import annotations
16
-
17
-
18
- class QasmException(Exception):
19
- def __init__(self, message: str) -> None:
20
- super().__init__(message)
21
- self.message = message
divi/exp/scipy/_cobyla.py DELETED
@@ -1,342 +0,0 @@
1
- """
2
- Interface to Constrained Optimization By Linear Approximation
3
-
4
- Functions
5
- ---------
6
- .. autosummary::
7
- :toctree: generated/
8
-
9
- fmin_cobyla
10
-
11
- """
12
-
13
- from inspect import signature
14
-
15
- import numpy as np
16
- from scipy.optimize._constraints import NonlinearConstraint
17
- from scipy.optimize._optimize import (
18
- OptimizeResult,
19
- _check_unknown_options,
20
- _prepare_scalar_function,
21
- )
22
-
23
- __all__ = ["fmin_cobyla"]
24
-
25
-
26
- def fmin_cobyla(
27
- func,
28
- x0,
29
- cons,
30
- args=(),
31
- consargs=None,
32
- rhobeg=1.0,
33
- rhoend=1e-4,
34
- maxfun=1000,
35
- disp=None,
36
- catol=2e-4,
37
- *,
38
- callback=None,
39
- ):
40
- """
41
- Minimize a function using the Constrained Optimization By Linear
42
- Approximation (COBYLA) method. This method uses the pure-python implementation
43
- of the algorithm from PRIMA.
44
-
45
- Parameters
46
- ----------
47
- func : callable
48
- Function to minimize. In the form func(x, \\*args).
49
- x0 : ndarray
50
- Initial guess.
51
- cons : sequence
52
- Constraint functions; must all be ``>=0`` (a single function
53
- if only 1 constraint). Each function takes the parameters `x`
54
- as its first argument, and it can return either a single number or
55
- an array or list of numbers.
56
- args : tuple, optional
57
- Extra arguments to pass to function.
58
- consargs : tuple, optional
59
- Extra arguments to pass to constraint functions (default of None means
60
- use same extra arguments as those passed to func).
61
- Use ``()`` for no extra arguments.
62
- rhobeg : float, optional
63
- Reasonable initial changes to the variables.
64
- rhoend : float, optional
65
- Final accuracy in the optimization (not precisely guaranteed). This
66
- is a lower bound on the size of the trust region.
67
- disp : {0, 1, 2, 3}, optional
68
- Controls the frequency of output; 0 implies no output.
69
- maxfun : int, optional
70
- Maximum number of function evaluations.
71
- catol : float, optional
72
- Absolute tolerance for constraint violations.
73
- callback : callable, optional
74
- Called after each iteration, as ``callback(x)``, where ``x`` is the
75
- current parameter vector.
76
-
77
- Returns
78
- -------
79
- x : ndarray
80
- The argument that minimises `f`.
81
-
82
- See also
83
- --------
84
- minimize: Interface to minimization algorithms for multivariate
85
- functions. See the 'COBYLA' `method` in particular.
86
-
87
- Notes
88
- -----
89
- This algorithm is based on linear approximations to the objective
90
- function and each constraint. We briefly describe the algorithm.
91
-
92
- Suppose the function is being minimized over k variables. At the
93
- jth iteration the algorithm has k+1 points v_1, ..., v_(k+1),
94
- an approximate solution x_j, and a radius RHO_j.
95
- (i.e., linear plus a constant) approximations to the objective
96
- function and constraint functions such that their function values
97
- agree with the linear approximation on the k+1 points v_1,.., v_(k+1).
98
- This gives a linear program to solve (where the linear approximations
99
- of the constraint functions are constrained to be non-negative).
100
-
101
- However, the linear approximations are likely only good
102
- approximations near the current simplex, so the linear program is
103
- given the further requirement that the solution, which
104
- will become x_(j+1), must be within RHO_j from x_j. RHO_j only
105
- decreases, never increases. The initial RHO_j is rhobeg and the
106
- final RHO_j is rhoend. In this way COBYLA's iterations behave
107
- like a trust region algorithm.
108
-
109
- Additionally, the linear program may be inconsistent, or the
110
- approximation may give poor improvement. For details about
111
- how these issues are resolved, as well as how the points v_i are
112
- updated, refer to the source code or the references below.
113
-
114
- .. versionchanged:: 1.16.0
115
- The original Powell implementation was replaced by a pure
116
- Python version from the PRIMA package, with bug fixes and
117
- improvements being made.
118
-
119
-
120
- References
121
- ----------
122
- Powell M.J.D. (1994), "A direct search optimization method that models
123
- the objective and constraint functions by linear interpolation.", in
124
- Advances in Optimization and Numerical Analysis, eds. S. Gomez and
125
- J-P Hennart, Kluwer Academic (Dordrecht), pp. 51-67
126
-
127
- Powell M.J.D. (1998), "Direct search algorithms for optimization
128
- calculations", Acta Numerica 7, 287-336
129
-
130
- Powell M.J.D. (2007), "A view of algorithms for optimization without
131
- derivatives", Cambridge University Technical Report DAMTP 2007/NA03
132
-
133
- Zhang Z. (2023), "PRIMA: Reference Implementation for Powell's Methods with
134
- Modernization and Amelioration", https://www.libprima.net,
135
- :doi:`10.5281/zenodo.8052654`
136
-
137
- Examples
138
- --------
139
- Minimize the objective function f(x,y) = x*y subject
140
- to the constraints x**2 + y**2 < 1 and y > 0::
141
-
142
- >>> def objective(x):
143
- ... return x[0]*x[1]
144
- ...
145
- >>> def constr1(x):
146
- ... return 1 - (x[0]**2 + x[1]**2)
147
- ...
148
- >>> def constr2(x):
149
- ... return x[1]
150
- ...
151
- >>> from scipy.optimize import fmin_cobyla
152
- >>> fmin_cobyla(objective, [0.0, 0.1], [constr1, constr2], rhoend=1e-7)
153
- array([-0.70710685, 0.70710671])
154
-
155
- The exact solution is (-sqrt(2)/2, sqrt(2)/2).
156
-
157
-
158
-
159
- """
160
- err = (
161
- "cons must be a sequence of callable functions or a single"
162
- " callable function."
163
- )
164
- try:
165
- len(cons)
166
- except TypeError as e:
167
- if callable(cons):
168
- cons = [cons]
169
- else:
170
- raise TypeError(err) from e
171
- else:
172
- for thisfunc in cons:
173
- if not callable(thisfunc):
174
- raise TypeError(err)
175
-
176
- if consargs is None:
177
- consargs = args
178
-
179
- # build constraints
180
- nlcs = []
181
- for con in cons:
182
- # Use default argument, otherwise the last `con` is captured by all wrapped_con
183
- def wrapped_con(x, confunc=con):
184
- return confunc(x, *consargs)
185
-
186
- nlcs.append(NonlinearConstraint(wrapped_con, 0, np.inf))
187
-
188
- # options
189
- opts = {
190
- "rhobeg": rhobeg,
191
- "tol": rhoend,
192
- "disp": disp,
193
- "maxiter": maxfun,
194
- "catol": catol,
195
- "callback": callback,
196
- }
197
-
198
- sol = _minimize_cobyla(func, x0, args, constraints=nlcs, **opts)
199
- if disp and not sol["success"]:
200
- print(f"COBYLA failed to find a solution: {sol.message}")
201
- return sol["x"]
202
-
203
-
204
- def _minimize_cobyla(
205
- fun,
206
- x0,
207
- args=(),
208
- constraints=(),
209
- rhobeg=1.0,
210
- tol=1e-4,
211
- maxiter=1000,
212
- disp=0,
213
- catol=None,
214
- f_target=-np.inf,
215
- callback=None,
216
- bounds=None,
217
- **unknown_options,
218
- ):
219
- """
220
- Minimize a scalar function of one or more variables using the
221
- Constrained Optimization BY Linear Approximation (COBYLA) algorithm.
222
- This method uses the pure-python implementation of the algorithm from PRIMA.
223
-
224
- Options
225
- -------
226
- rhobeg : float
227
- Reasonable initial changes to the variables.
228
- tol : float
229
- Final accuracy in the optimization (not precisely guaranteed).
230
- This is a lower bound on the size of the trust region.
231
- disp : int
232
- Controls the frequency of output:
233
- 0. (default) There will be no printing
234
- 1. A message will be printed to the screen at the end of iteration, showing
235
- the best vector of variables found and its objective function value
236
- 2. in addition to 1, each new value of RHO is printed to the screen,
237
- with the best vector of variables so far and its objective function
238
- value.
239
- 3. in addition to 2, each function evaluation with its variables will
240
- be printed to the screen.
241
- maxiter : int
242
- Maximum number of function evaluations.
243
- catol : float
244
- Tolerance (absolute) for constraint violations
245
- f_target : float
246
- Stop if the objective function is less than `f_target`.
247
-
248
- .. versionchanged:: 1.16.0
249
- The original Powell implementation was replaced by a pure
250
- Python version from the PRIMA package, with bug fixes and
251
- improvements being made.
252
-
253
-
254
- References
255
- ----------
256
- Zhang Z. (2023), "PRIMA: Reference Implementation for Powell's Methods with
257
- Modernization and Amelioration", https://www.libprima.net,
258
- :doi:`10.5281/zenodo.8052654`
259
- """
260
- from .pyprima import minimize
261
- from .pyprima.common.infos import FTARGET_ACHIEVED, SMALL_TR_RADIUS
262
- from .pyprima.common.message import get_info_string
263
-
264
- # _check_unknown_options(unknown_options)
265
- rhoend = tol
266
- iprint = disp if disp is not None else 0
267
- if iprint != 0 and iprint != 1 and iprint != 2 and iprint != 3:
268
- raise ValueError(
269
- f"disp argument to minimize must be 0, 1, 2, or 3,\
270
- received {iprint}"
271
- )
272
-
273
- # create the ScalarFunction, cobyla doesn't require derivative function
274
- def _jac(x, *args):
275
- return None
276
-
277
- sf = _prepare_scalar_function(fun, x0, args=args, jac=_jac)
278
-
279
- if callback is not None:
280
- sig = signature(callback)
281
- if set(sig.parameters) == {"intermediate_result"}:
282
-
283
- def wrapped_callback_intermediate(x, f, nf, tr, cstrv, nlconstrlist):
284
- intermediate_result = OptimizeResult(
285
- x=np.copy(x), fun=f, nfev=nf, nit=tr, maxcv=cstrv
286
- )
287
- callback(intermediate_result=intermediate_result)
288
-
289
- else:
290
-
291
- def wrapped_callback_intermediate(x, f, nf, tr, cstrv, nlconstrlist):
292
- callback(np.copy(x))
293
-
294
- def wrapped_callback(x, f, nf, tr, cstrv, nlconstrlist):
295
- try:
296
- wrapped_callback_intermediate(x, f, nf, tr, cstrv, nlconstrlist)
297
- return False
298
- except StopIteration:
299
- return True
300
-
301
- else:
302
- wrapped_callback = None
303
-
304
- ctol = catol if catol is not None else np.sqrt(np.finfo(float).eps)
305
- options = {
306
- "rhobeg": rhobeg,
307
- "rhoend": rhoend,
308
- "maxfev": maxiter,
309
- "iprint": iprint,
310
- "ctol": ctol,
311
- "ftarget": f_target,
312
- }
313
-
314
- result = minimize(
315
- sf.fun,
316
- x0,
317
- method="cobyla",
318
- bounds=bounds,
319
- constraints=constraints,
320
- callback=wrapped_callback,
321
- options=options,
322
- )
323
-
324
- if result.cstrv > ctol:
325
- success = False
326
- message = (
327
- "Did not converge to a solution satisfying the constraints. See "
328
- "`maxcv` for the magnitude of the violation."
329
- )
330
- else:
331
- success = result.info == SMALL_TR_RADIUS or result.info == FTARGET_ACHIEVED
332
- message = get_info_string("COBYLA", result.info)
333
-
334
- return OptimizeResult(
335
- x=result.x,
336
- status=result.info,
337
- success=success,
338
- message=message,
339
- nfev=result.nf,
340
- fun=result.f,
341
- maxcv=result.cstrv,
342
- )
@@ -1,28 +0,0 @@
1
- BSD 3-Clause License
2
-
3
- Copyright (c) 2020--2025, Zaikun ZHANG ( https://www.zhangzk.net )
4
-
5
- Redistribution and use in source and binary forms, with or without
6
- modification, are permitted provided that the following conditions are met:
7
-
8
- 1. Redistributions of source code must retain the above copyright notice, this
9
- list of conditions and the following disclaimer.
10
-
11
- 2. Redistributions in binary form must reproduce the above copyright notice,
12
- this list of conditions and the following disclaimer in the documentation
13
- and/or other materials provided with the distribution.
14
-
15
- 3. Neither the name of the copyright holder nor the names of its
16
- contributors may be used to endorse or promote products derived from
17
- this software without specific prior written permission.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.