eqc-models 0.10.0__py3-none-any.whl → 0.10.3__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 (69) hide show
  1. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/__init__.py +2 -1
  2. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/algorithms/penaltymultiplier.py +20 -7
  3. eqc_models-0.10.3.data/platlib/eqc_models/assignment/setpartition.py +4 -0
  4. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/base.py +44 -4
  5. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/polyeval.c +163 -163
  6. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/polyeval.cpython-310-darwin.so +0 -0
  7. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/polynomial.py +7 -3
  8. eqc_models-0.10.3.data/platlib/eqc_models/combinatorics/__init__.py +6 -0
  9. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/combinatorics/setcover.py +35 -17
  10. {eqc_models-0.10.0.data/platlib/eqc_models/assignment → eqc_models-0.10.3.data/platlib/eqc_models/combinatorics}/setpartition.py +2 -0
  11. eqc_models-0.10.3.data/platlib/eqc_models/graph/__init__.py +6 -0
  12. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/graph/maxkcut.py +26 -68
  13. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/solvers/qciclient.py +11 -2
  14. {eqc_models-0.10.0.dist-info → eqc_models-0.10.3.dist-info}/METADATA +3 -2
  15. eqc_models-0.10.3.dist-info/RECORD +59 -0
  16. {eqc_models-0.10.0.dist-info → eqc_models-0.10.3.dist-info}/WHEEL +1 -1
  17. eqc_models-0.10.0.data/platlib/eqc_models/base.py +0 -115
  18. eqc_models-0.10.0.data/platlib/eqc_models/communitydetection.py +0 -25
  19. eqc_models-0.10.0.data/platlib/eqc_models/eqcdirectsolver.py +0 -61
  20. eqc_models-0.10.0.data/platlib/eqc_models/graph/__init__.py +0 -5
  21. eqc_models-0.10.0.data/platlib/eqc_models/graphs.py +0 -28
  22. eqc_models-0.10.0.data/platlib/eqc_models/maxcut.py +0 -113
  23. eqc_models-0.10.0.data/platlib/eqc_models/maxkcut.py +0 -185
  24. eqc_models-0.10.0.data/platlib/eqc_models/quadraticmodel.py +0 -131
  25. eqc_models-0.10.0.data/platlib/eqc_models/solvers/eqcdirect.py +0 -160
  26. eqc_models-0.10.0.dist-info/RECORD +0 -65
  27. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/compile_extensions.py +0 -0
  28. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/algorithms/__init__.py +0 -0
  29. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/algorithms/base.py +0 -0
  30. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/allocation/__init__.py +0 -0
  31. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/allocation/allocation.py +0 -0
  32. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/allocation/portbase.py +0 -0
  33. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/allocation/portmomentum.py +0 -0
  34. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/assignment/__init__.py +0 -0
  35. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/assignment/qap.py +0 -0
  36. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/__init__.py +0 -0
  37. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/constraints.py +0 -0
  38. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/operators.py +0 -0
  39. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/polyeval.pyx +0 -0
  40. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/base/quadratic.py +0 -0
  41. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/decoding.py +0 -0
  42. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/graph/base.py +0 -0
  43. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/graph/hypergraph.py +0 -0
  44. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/graph/maxcut.py +0 -0
  45. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/graph/partition.py +0 -0
  46. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/__init__.py +0 -0
  47. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/classifierbase.py +0 -0
  48. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/classifierqboost.py +0 -0
  49. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/classifierqsvm.py +0 -0
  50. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/clustering.py +0 -0
  51. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/clusteringbase.py +0 -0
  52. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/cvqboost_hamiltonian.pyx +0 -0
  53. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/cvqboost_hamiltonian_c_func.c +0 -0
  54. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/cvqboost_hamiltonian_c_func.h +0 -0
  55. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/decomposition.py +0 -0
  56. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/forecast.py +0 -0
  57. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/forecastbase.py +0 -0
  58. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/regressor.py +0 -0
  59. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/regressorbase.py +0 -0
  60. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/ml/reservoir.py +0 -0
  61. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/sequence/__init__.py +0 -0
  62. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/sequence/tsp.py +0 -0
  63. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/solvers/__init__.py +0 -0
  64. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/utilities/__init__.py +0 -0
  65. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/utilities/fileio.py +0 -0
  66. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/utilities/polynomial.py +0 -0
  67. {eqc_models-0.10.0.data → eqc_models-0.10.3.data}/platlib/eqc_models/utilities/qplib.py +0 -0
  68. {eqc_models-0.10.0.dist-info → eqc_models-0.10.3.dist-info/licenses}/LICENSE.txt +0 -0
  69. {eqc_models-0.10.0.dist-info → eqc_models-0.10.3.dist-info}/top_level.txt +0 -0
@@ -1,28 +0,0 @@
1
- from typing import List
2
- import networkx as nx
3
- from .quadraticmodel import QuadraticModel
4
-
5
- class GraphModel(QuadraticModel):
6
-
7
- def __init__(self, G : nx.Graph):
8
- self.G = G
9
-
10
- class TwoPartitionModel(GraphModel):
11
- """ Create a model where the variables are node-based """
12
-
13
- @property
14
- def variables(self) -> List[str]:
15
- """ Provide a variable name to index lookup, order enforced by sorting the list before returning """
16
- names = [node.name for node in self.G.nodes]
17
- names.sort()
18
- return names
19
-
20
- class EdgeModel(GraphModel):
21
- """ Create a model where the variables are edge-based """
22
-
23
- @property
24
- def variables(self) -> List[str]:
25
- """ Provide a variable name to index lookup, order enforced by sorting the list before returning """
26
- names = [f"({u},{v})" for u, v in self.G.edges]
27
- names.sort()
28
- return names
@@ -1,113 +0,0 @@
1
- import networkx as nx
2
- import numpy as np
3
- from .graphs import TwoPartitionModel
4
-
5
- class MaxCutModel(TwoPartitionModel):
6
-
7
- def build(self):
8
- variables = self.variables
9
- n = len(variables)
10
- self.domains = np.ones((n,))
11
-
12
- J = np.zeros((n+1, n+1), dtype=np.float32)
13
- h = np.zeros((n+1,1), dtype=np.float32)
14
- for u, v in G.edges:
15
- J[u, v] += 1
16
- J[v, u] += 1
17
- J[u, u] = 1
18
- J[v, v] = 1
19
- h[u] -= 1
20
- h[v] -= 1
21
- J *= 1/t**2
22
- h *= 1/t
23
- H = np.hstack([h, J])
24
- return H
25
-
26
- @property
27
- def J(self) -> np.ndarray:
28
- if getattr(self, "_J", None) is None:
29
- self.build()
30
- return self._J
31
-
32
- @property
33
- def C(self) -> np.ndarray:
34
- if getattr(self, "C", None) is None:
35
- self.build()
36
- return self._C
37
-
38
- def get_graph(n, d):
39
- """ Produce a repeatable graph with parameters n and d """
40
-
41
- seed = n * d
42
- return nx.random_graphs.random_regular_graph(d, n, seed)
43
-
44
- def get_partition_graph(G, solution):
45
- """
46
- Build the partitioned graph, counting cut size
47
-
48
- :parameters: G : nx.DiGraph, solution : np.ndarray
49
- :returns: nx.DiGraph, int
50
-
51
- """
52
-
53
- cut_size = 0
54
- Gprime = nx.DiGraph()
55
- Gprime.add_nodes_from(G.nodes)
56
- for i, j in G.edges:
57
- if solution[i] != solution[j]:
58
- cut_size+=1
59
- else:
60
- Gprime.add_edge(i, j)
61
- return Gprime, cut_size
62
-
63
- def determine_solution(G, solution):
64
- """
65
- Use a simple bisection method to determine the binary solution. Uses
66
- the cut size as the metric.
67
-
68
- Returns the partitioned graph and solution.
69
-
70
- :parameters: G : nx.DiGraph, solution : np.ndarray
71
- :returns: nx.DiGraph, np.ndarray
72
-
73
- """
74
-
75
- solution = np.array(solution)
76
- lower = np.min(solution)
77
- upper = np.max(solution)
78
- best_cut_size = 0
79
- best_graph = G
80
- best_solution = None
81
- while upper > lower + 0.0001:
82
- middle = (lower + upper) / 2
83
- test_solution = (solution>=middle).astype(np.int32)
84
- Gprime, cut_size = get_partition_graph(G, test_solution)
85
- if cut_size > best_cut_size:
86
- best_cut_size = cut_size
87
- lower = middle
88
- best_solution = test_solution
89
- best_graph = Gprime
90
- else:
91
- upper = middle
92
- return best_graph, best_solution
93
-
94
- def get_maxcut_H(G, t):
95
- """
96
- Return a Hamiltonian representing the Maximum Cut Problem. Scale the problem using `t`.
97
- Automatically adds a slack qudit.
98
-
99
- """
100
- n = len(G.nodes)
101
- J = np.zeros((n+1, n+1), dtype=np.float32)
102
- h = np.zeros((n+1,1), dtype=np.float32)
103
- for u, v in G.edges:
104
- J[u, v] += 1
105
- J[v, u] += 1
106
- J[u, u] = 1
107
- J[v, v] = 1
108
- h[u] -= 1
109
- h[v] -= 1
110
- J *= 1/t**2
111
- h *= 1/t
112
- H = np.hstack([h, J])
113
- return H
@@ -1,185 +0,0 @@
1
- import numpy as np
2
- import networkx as nx
3
- from .quadraticmodel import QuadraticModel
4
-
5
-
6
- class MaxKCut(QuadraticModel):
7
-
8
- def __init__(self, G : nx.Graph, k : int):
9
- self.G = G
10
- self.node_map = list(G.nodes)
11
- self.k = k
12
- self.partitions = []
13
- self._lhs = None
14
- self._rhs = None
15
- self._objective = None
16
- self._J = None
17
- self._C = None
18
-
19
- def decode(self, solution: np.ndarray) -> np.ndarray:
20
- """ Override the default decoding to use a the max cut metric to determine a solution """
21
-
22
- # only one partition per node can be selected
23
- # rather than the same cutoff per node, use the max value per partition
24
- decoded_solution = np.zeros_like(solution, dtype=np.int32)
25
- k = self.k
26
- for i, u in enumerate(self.node_map):
27
- idx = slice(k*i, k*(i+1))
28
- spins = solution[idx]
29
- mx = np.max(spins)
30
- for j in range(k):
31
- if spins[j] == mx:
32
- decoded_solution[k*i+j] = 1
33
- break
34
- return decoded_solution
35
-
36
- def partition(self, solution):
37
- """ Return a dictionary with the partition number of each node """
38
- k = self.k
39
- n = len(self.node_map)
40
- partition_num = {}
41
- for i, u in enumerate(self.node_map):
42
- for j in range(k):
43
- if solution[i*k+j] == 1:
44
- partition_num[u] = j+1
45
- return partition_num
46
-
47
- def getCutSize(self, partition):
48
- cut_size = 0
49
- for u, v in self.G.edges:
50
- if partition[u]!=partition[v]:
51
- cut_size += 1
52
- return cut_size
53
-
54
- def _build_objective(self):
55
-
56
- node_map = self.node_map
57
- G = self.G
58
- m = len(G.nodes)
59
- n = self.k * m
60
- # construct the quadratic portion of the objective
61
- # the linear portion is 0
62
- objective = np.zeros((n, n), dtype=np.float32)
63
- # increment the joint variable terms indicating the nodes are in different sets
64
- pairs = [(i, j) for i in range(self.k) for j in range(self.k) if i!=j]
65
- for u, v in G.edges:
66
- i = node_map.index(u)
67
- j = node_map.index(v)
68
- ibase = i * self.k
69
- jbase = j * self.k
70
- for incr1, incr2 in pairs:
71
- idx1 = ibase + incr1
72
- idx2 = jbase + incr2
73
- objective[idx1, idx2] += -1
74
- self._objective = (np.zeros((n, 1)), objective)
75
-
76
- def _build_constraints(self):
77
-
78
- node_map = self.node_map
79
- G = self.G
80
- m = len(G.nodes)
81
- n = self.k * m
82
-
83
- # build the constraints
84
- A = np.zeros((m, n))
85
- b = np.ones((m,))
86
- for u in G.nodes:
87
- i = node_map.index(u)
88
- ibase = i * self.k
89
- A[i, ibase:ibase+self.k] = 1
90
- self._lhs = A
91
- self._rhs = b
92
-
93
- def build(self, multiplier=None):
94
- """ Create the constraints and objective and Hamiltonian """
95
-
96
- # there are k * m variables in this problem where m is the number of nodes in the graph
97
- node_map = self.node_map
98
- G = self.G
99
- m = len(G.nodes)
100
- n = self.k * m
101
- self.domains = np.ones((n,))
102
-
103
- self._build_objective()
104
- if multiplier is None:
105
- multiplier = np.max(np.abs(self._objective[1]))
106
- self._build_constraints()
107
-
108
- self._C, self._J = self.buildH(multiplier)
109
- self.sum_constraint = m
110
-
111
- def buildH(self, multiplier):
112
- """ Combine the objective and penalties using the multiplier """
113
-
114
- objC, objJ = self.objective
115
- lhs, rhs = self.constraints
116
- Pq = lhs.T@lhs
117
- Pl = -2 * rhs.T@lhs
118
- offset = rhs.T@rhs
119
- n = self.n
120
- J = np.zeros((n, n), np.float32)
121
- C = np.zeros([n, 1], np.float32)
122
- C += objC
123
- J[:,:] += objJ
124
- C += multiplier * Pl.reshape((n, 1))
125
- J[:,:] += multiplier * Pq
126
- return C, J
127
-
128
- @property
129
- def constraints(self):
130
- """ Return LHS, RHS in numpy matrix format """
131
- if self._rhs is None:
132
- self.build()
133
- return self._lhs, self._rhs
134
-
135
- @property
136
- def objective(self):
137
- """ Return the quadratic objective as NxN+1 matrix """
138
-
139
- if self._objective is None:
140
- self.build()
141
- return self._objective
142
-
143
- @property
144
- def H(self):
145
- """ Return the Hamiltonian as parts C, J """
146
-
147
- if self._C is None:
148
- self.build()
149
- return self._C, self._J
150
-
151
- class WeightedMaxKCut(MaxKCut):
152
-
153
- def __init__(self, G: nx.Graph, k: int, weight_label : str = "weight"):
154
- super().__init__(G, k)
155
-
156
- self.weight_label = weight_label
157
-
158
- def _build_objective(self):
159
-
160
- node_map = self.node_map
161
- G = self.G
162
- m = len(G.nodes)
163
- n = self.k * m
164
- # construct the quadratic portion of the objective
165
- # the linear portion is 0
166
- objective = np.zeros((n, n), dtype=np.float32)
167
- # increment the joint variable terms indicating the nodes are in different sets
168
- pairs = [(i, j) for i in range(self.k) for j in range(self.k) if i!=j]
169
- for u, v in G.edges:
170
- i = node_map.index(u)
171
- j = node_map.index(v)
172
- ibase = i * self.k
173
- jbase = j * self.k
174
- for incr1, incr2 in pairs:
175
- idx1 = ibase + incr1
176
- idx2 = jbase + incr2
177
- objective[idx1, idx2] += G[u][v][self.weight_label]
178
- self._objective = (np.zeros((n, 1)), objective)
179
-
180
- def getCutSize(self, partition):
181
- cut_size = 0
182
- for u, v in self.G.edges:
183
- if partition[u]!=partition[v]:
184
- cut_size += self.G[u][v][self.weight_label]
185
- return cut_size
@@ -1,131 +0,0 @@
1
- from typing import Tuple
2
- import numpy as np
3
- from .base import EqcModel
4
-
5
- class QuadraticMixIn:
6
- C = None
7
- J = None
8
-
9
- def encode(self, levels:int=200, norm_value:float=None, dtype=np.float32) -> np.ndarray:
10
- """
11
- Encode Hamiltonian into the domain of the device
12
-
13
- The encoding method can be tuned using levels or norm_value. The parameter
14
- levels and the member self.domains are used to generate a vector t such that
15
- $$
16
- x = ts
17
- $$
18
- thus,
19
- $$
20
- h^Tx + x^TJx = h^T(ts) + (ts)^TJ(ts) = (th)^Ts + s^T(t\\cross tJ)s
21
- $$
22
-
23
- """
24
-
25
- n = max(self.domains.shape)
26
- J = self.J
27
- C = self.C
28
- if norm_value is None:
29
- max_level = levels - 1
30
- multipliers = (self.domains) / max_level
31
- J = np.array(np.outer(multipliers, multipliers) * J)
32
- # h = np.multiply(h[:,0], multipliers)
33
- C *= multipliers
34
- C = C.reshape((n, 1))
35
- else:
36
- # normalize the operator
37
- max_val_J = np.max(np.abs(J))
38
- max_val_C = np.max(np.abs(C))
39
- if max_val_J > max_val_C:
40
- max_val = max_val_J
41
- else:
42
- max_val = max_val_C
43
- C /= max_val
44
- C *= norm_value
45
- J /= max_val
46
- J *= norm_value
47
- # make J symmetric
48
- J += J.T
49
- J /= 2
50
- # return operator in hJ format
51
- H = np.hstack([C, J]).astype(dtype)
52
-
53
- if self.machine_slacks > 0:
54
- machine_slacks = self.machine_slacks
55
- n = H.shape[0]
56
- Hslack = np.zeros((n+machine_slacks, n+machine_slacks+1), dtype=dtype)
57
- Hslack[:n, :n+1] = H
58
- H = Hslack
59
-
60
- return np.array(H)
61
-
62
- @property
63
- def sparse(self) -> Tuple[np.ndarray, np.ndarray]:
64
- """ Put the linear and quadratic terms in a sparse format
65
-
66
- :returns: coefficients : List, indices : List
67
- """
68
- C, J = self.H
69
- n = self.n
70
- indices = []
71
- coefficients = []
72
- # build a key (ordered tuple of indices) of length 2 for each element
73
- for i in range(n):
74
- if C[i,0] != 0:
75
- key = (0, i+1)
76
- indices.append(key)
77
- coefficients.append(C[i,0])
78
- # make J upper triangular
79
- J = np.triu(J) + np.tril(J, -1).T
80
- for i in range(n):
81
- for j in range(i, n):
82
- val = J[i, j]
83
- if val != 0:
84
- key = (i+1, j+1)
85
- indices.append(key)
86
- coefficients.append(val)
87
- return np.array(coefficients, dtype=np.float32), np.array(indices, dtype=np.int32)
88
-
89
- def evaluate(self, solution: np.ndarray, decode:bool=False, levels:int=200) -> float:
90
- """
91
- Evaluate the solution using the original operator. The decode
92
- and levels parameters control the decoding of the solution. Without
93
- specifying decode, the evaluation of the operator is done with the
94
- solution provided.
95
-
96
- """
97
- H = self.H
98
- h, J = H[:, 0], H[:, 1:]
99
- if decode:
100
- sol = self.decode(solution)
101
- else:
102
- sol = solution
103
- return np.squeeze(sol.T@J@sol + h.T@sol)
104
-
105
- class QuadraticModel(QuadraticMixIn, EqcModel):
106
- """ Provides a quadratic operator and device sum constraint support """
107
-
108
- def __init__(self, C : np.ndarray, J : np.ndarray, sum_constraint : float):
109
- self._C = C
110
- self._J = J
111
- self._sum_constraint = sum_constraint
112
-
113
- @property
114
- def H(self):
115
- return self._C, self._J
116
-
117
- def check_constraint(self, solution: np.array) -> bool:
118
- """ Evaluate the solution against the original sum constraint """
119
- return np.sum(solution) == self.sum_constraint
120
-
121
- @property
122
- def sum_constraint(self) -> int:
123
- """ Integer value which all qudits must sum to.
124
- The value must be less than or equal to n * base for
125
- the model to make sense. """
126
-
127
- return self._sum_constraint
128
-
129
- @sum_constraint.setter
130
- def sum_constraint(self, value : int):
131
- self._sum_constraint = value
@@ -1,160 +0,0 @@
1
- # (C) Quantum Computing Inc., 2024.
2
- from typing import Dict
3
- import logging
4
- import numpy as np
5
- from eqc_direct.client import EqcClient
6
- from eqc_models.base.base import ModelSolver, EqcModel
7
-
8
- log = logging.getLogger(name=__name__)
9
-
10
-
11
- class EqcDirectMixin:
12
- """
13
- This class provides an instance method and property that
14
- manage the direct connection to a QCi device.
15
-
16
- """
17
-
18
- ip_addr = None
19
- port = None
20
-
21
- def connect(self, ip_addr: str, port: str) -> str:
22
- """
23
- Explicitly set device address; if environment is
24
- configured with the connection, this call is not required.
25
-
26
- Parameters
27
- ------------
28
-
29
- ip_addr: The IP address of the device.
30
-
31
- port: The port number of the device.
32
-
33
- Parameters
34
- ------------
35
- The status.
36
-
37
- """
38
- self.ip_addr = ip_addr
39
- self.port = port
40
- client = self.client
41
- return client.system_status()["status_desc"]
42
-
43
- @property
44
- def client(self):
45
- params = {}
46
- if self.ip_addr is not None:
47
- params["ip_address"] = self.ip_addr
48
- if self.port is not None:
49
- params["port"] = self.port
50
- return EqcClient(**params)
51
-
52
-
53
- class EqcDirectSolver(ModelSolver, EqcDirectMixin):
54
- """
55
- This class provides an instance method for direct submission
56
- of jobs to QCi devices.
57
-
58
- """
59
-
60
- def solve(
61
- self,
62
- model: EqcModel,
63
- relaxation_schedule: int = 2,
64
- precision: float = 1.0,
65
- ) -> Dict:
66
- """Parameters
67
- -------------
68
- model: An EqcModel instance.
69
-
70
- relaxation_schedule: A predefined schedule indicator which
71
- sets parameters on the device to control the sampling through
72
- photon measurement; default is 2.
73
-
74
- precision: A value which, when not None, indicates
75
- the numerical precision desired in the solution: 1 for
76
- integer, 0.1 for tenths place, 0.01 for hundreths and None for
77
- raw; default is 1.0.
78
-
79
- Returns
80
- ---------
81
- Json response from the solver.
82
-
83
- """
84
- poly_coefficients, poly_indices = model.sparse
85
- # print(poly_indices)
86
- if model.machine_slacks > 0:
87
- # add a single 0 coefficient entry as the next-highest index
88
- highest_idx = int(np.max(poly_indices))
89
- # print("POLY HIGHEST", highest_idx)
90
- for i in range(model.machine_slacks):
91
- addtl_index = [0 for i in range(len(poly_indices[0]))]
92
- addtl_index[-1] = highest_idx + i + 1
93
- poly_indices = poly_indices.tolist() + [addtl_index]
94
- poly_coefficients = poly_coefficients.tolist() + [0]
95
- # print(poly_indices)
96
- scval = model.sum_constraint
97
-
98
- client = self.client
99
- lock_id, start_ts, end_ts = client.wait_for_lock()
100
- log.debug(
101
- "Got device lock id %s. Wait time %f",
102
- lock_id,
103
- end_ts - start_ts,
104
- )
105
- resp = None
106
- try:
107
- log.debug(
108
- "Calling device with parameters relaxation_schedule %d sum_constraint %s lock_id %s solution_precision %f",
109
- relaxation_schedule,
110
- scval,
111
- lock_id,
112
- precision,
113
- )
114
- resp = client.process_job(
115
- poly_coefficients=poly_coefficients,
116
- poly_indices=poly_indices,
117
- relaxation_schedule=relaxation_schedule,
118
- sum_constraint=scval,
119
- lock_id=lock_id,
120
- solution_precision=precision,
121
- )
122
- log.debug("Received response with status %s", resp["err_desc"])
123
- log.debug(
124
- "Runtime %f resulting in energy %f",
125
- resp["runtime"],
126
- resp["energy"],
127
- )
128
- log.debug(
129
- "Distillation runtime %s resulting in energy %f",
130
- resp["distilled_runtime"],
131
- resp["distilled_energy"],
132
- )
133
- finally:
134
- client.release_lock(lock_id=lock_id)
135
- if resp is not None:
136
- solution = resp["solution"]
137
- energy = resp["energy"]
138
- runtime = resp["runtime"]
139
- dirac3_sol = np.array(solution)
140
- log.debug(
141
- "Energy %f Runtime %f Solution Size %i Solution Sum %f",
142
- energy,
143
- runtime,
144
- len(dirac3_sol),
145
- sum(dirac3_sol),
146
- )
147
- else:
148
- raise RuntimeError("FAILED TO GET RESPONSE")
149
- return resp
150
-
151
-
152
- class Dirac3DirectSolver(EqcDirectSolver):
153
-
154
- """
155
- Naming this for when when other devices are available and have
156
- different requirements. For instance, Dirac-3 requires the
157
- summation constraint parameter, but others might not. The same
158
- could be true for relaxation schedule.
159
-
160
- """
@@ -1,65 +0,0 @@
1
- eqc_models-0.10.0.data/platlib/compile_extensions.py,sha256=ivFZ87WFzBITU7IwK_72C41MDNMortJIF8tKevm2aW4,1946
2
- eqc_models-0.10.0.data/platlib/eqc_models/__init__.py,sha256=njh-tvfgnAIVBGw9FXFv6XQOAgwrnBG23dU9dipa1Jg,585
3
- eqc_models-0.10.0.data/platlib/eqc_models/base.py,sha256=YWS9kjmh-ZEZqpByDaik4fLjeGbwKLLlqbVMsHto6Bs,3524
4
- eqc_models-0.10.0.data/platlib/eqc_models/communitydetection.py,sha256=-zqESm8MFDdaw_a39Kg6Wuu5Tvman3JOeUH4bbPXB48,891
5
- eqc_models-0.10.0.data/platlib/eqc_models/decoding.py,sha256=G3JgbIFzvZ3DIKW0kZ1JeTMIZmrc8hy9kzdbX2Xv5Og,637
6
- eqc_models-0.10.0.data/platlib/eqc_models/eqcdirectsolver.py,sha256=zoR8VHD9OFEBxKO9GW8sPLtuqVfWMubg4SDFCQKr3fc,2444
7
- eqc_models-0.10.0.data/platlib/eqc_models/graphs.py,sha256=8gWGwPKhAFoddYFRiJ770CNHrbudy_UVqgsv2BrHVDA,895
8
- eqc_models-0.10.0.data/platlib/eqc_models/maxcut.py,sha256=ohD08acRcmDhnxRI5MN1wvY6k81xUs8PRpsAzO5kQXI,2899
9
- eqc_models-0.10.0.data/platlib/eqc_models/maxkcut.py,sha256=fzittwUftQljSqe9yBS042-SXeyj2AmLTDuW8yc0JFw,5812
10
- eqc_models-0.10.0.data/platlib/eqc_models/quadraticmodel.py,sha256=vhWMNmKxzxaV6kdb9TTI-SqJMUEvKMLHfAOD6BVYBwc,4242
11
- eqc_models-0.10.0.data/platlib/eqc_models/algorithms/__init__.py,sha256=lzQoMMSs2QTqp5suEYNvcQaSEN16d0LwIsZ1mRstumU,135
12
- eqc_models-0.10.0.data/platlib/eqc_models/algorithms/base.py,sha256=qfQQTwuVyewn7Qpg1_SVoD_7TsdA8vdwSKGqOeZSmNI,204
13
- eqc_models-0.10.0.data/platlib/eqc_models/algorithms/penaltymultiplier.py,sha256=gV48irx3q9yvMsCRqv2fa8DGQEOUkgVNGbZdNzI-3Tc,7057
14
- eqc_models-0.10.0.data/platlib/eqc_models/allocation/__init__.py,sha256=op_udrapTlWrakTuQId3M0ggo-Y1w4nGJLPcYKOQ-8I,239
15
- eqc_models-0.10.0.data/platlib/eqc_models/allocation/allocation.py,sha256=PAQn4M75Zegeoy0i6r3hycWs4TKeRglHk05TvwZOoLk,15506
16
- eqc_models-0.10.0.data/platlib/eqc_models/allocation/portbase.py,sha256=BTnYYduHydPbrE2yQr_Sgv3XJACH_GpIJstuMrfGqCU,3269
17
- eqc_models-0.10.0.data/platlib/eqc_models/allocation/portmomentum.py,sha256=oMod63rNDC-01dLZjmhUb24SN3_GVgfc6CItgQL_obI,3756
18
- eqc_models-0.10.0.data/platlib/eqc_models/assignment/__init__.py,sha256=CX_QYl8P1meajV8gcVs6hm07VhoLVe8uu436t7-1XQo,86
19
- eqc_models-0.10.0.data/platlib/eqc_models/assignment/qap.py,sha256=WMiQQmTORsgi2w7kbmMA1xo-93dESLECWqYTF-zkmTs,2963
20
- eqc_models-0.10.0.data/platlib/eqc_models/assignment/setpartition.py,sha256=-DJjPgsSMslEjfo3iDdXbKHQ9Lfc3cHlNc-9DtkJnW0,6202
21
- eqc_models-0.10.0.data/platlib/eqc_models/base/__init__.py,sha256=RwZguuucmsuOSVqfPLBqj-wzinBOzcgO-2ifFU-DCqg,2885
22
- eqc_models-0.10.0.data/platlib/eqc_models/base/base.py,sha256=kpnOb1dw0aaQyassIYKNlS6rIH1fNvZMI-NKPosAEMk,4856
23
- eqc_models-0.10.0.data/platlib/eqc_models/base/constraints.py,sha256=KMPyCdt_8GMWLnVEdpzw1T1WqXm2f7Lh629ClUH7XDY,8271
24
- eqc_models-0.10.0.data/platlib/eqc_models/base/operators.py,sha256=9nCeN6fRP-_YBfs-Gm57D-O_376qcOQiqGDuVlSlf00,7392
25
- eqc_models-0.10.0.data/platlib/eqc_models/base/polyeval.c,sha256=4Rbo0snmzpBfk3EL1u5NLsYt_vni5wk_4p9XwsszFkg,445633
26
- eqc_models-0.10.0.data/platlib/eqc_models/base/polyeval.cpython-310-darwin.so,sha256=p9WsRiRpK18WJUHT4-x3s2VPr92_-DF5TLLPOuvwl_o,101024
27
- eqc_models-0.10.0.data/platlib/eqc_models/base/polyeval.pyx,sha256=76Bf99Jt1_rLh5byrZxAjavE2F4_yCysirViqOBFIXw,2547
28
- eqc_models-0.10.0.data/platlib/eqc_models/base/polynomial.py,sha256=dlWqL-PgSzV1UX-1S-MDwEL4sp3vs3h0vdRkv4JYfLo,12981
29
- eqc_models-0.10.0.data/platlib/eqc_models/base/quadratic.py,sha256=BHZKniUvSq30pZgzguVasdO39BCEYue_wi5AIklPGS8,8062
30
- eqc_models-0.10.0.data/platlib/eqc_models/combinatorics/setcover.py,sha256=v42_pB2gPF2YkVllCqqjw58t97y5AAddfWITIsESZI8,2730
31
- eqc_models-0.10.0.data/platlib/eqc_models/graph/__init__.py,sha256=Cpo4jb630U57J0lS_iZddE_W3v9N4ijUb7xZRIYuguo,95
32
- eqc_models-0.10.0.data/platlib/eqc_models/graph/base.py,sha256=j48OwhorZ5jTlTysTmMiXCeiAdj6jylBREk3NxCpFLI,2072
33
- eqc_models-0.10.0.data/platlib/eqc_models/graph/hypergraph.py,sha256=ABvutT0NOdIEpUF4TjUzboE4Y_J5iUZyj6-AzKr4R28,13268
34
- eqc_models-0.10.0.data/platlib/eqc_models/graph/maxcut.py,sha256=o8xVsAwTa9jcpmsIoCQ5z7HSstVdraT8TENomdT519o,4132
35
- eqc_models-0.10.0.data/platlib/eqc_models/graph/maxkcut.py,sha256=OFZwmC3wWpIJWVHOb9wkUeka0g0bOHrwf-mx0VZvt58,5807
36
- eqc_models-0.10.0.data/platlib/eqc_models/graph/partition.py,sha256=HMpRRipLp14x8pHucY-g6fU7v0PGoy1pf_KpzbanfD0,5800
37
- eqc_models-0.10.0.data/platlib/eqc_models/ml/__init__.py,sha256=CLfraacr0FrD5ynxlNB6cyNy0lpbavcQT45TvkDrNvY,369
38
- eqc_models-0.10.0.data/platlib/eqc_models/ml/classifierbase.py,sha256=1yXufiUGpaiBjI2evMiwfYn-Zr-SnWwCnvtREqprtJw,2184
39
- eqc_models-0.10.0.data/platlib/eqc_models/ml/classifierqboost.py,sha256=5Dcpd0DjpMmlUULyZFKKWm4UuZpeMaknWKViiIjwc0U,18978
40
- eqc_models-0.10.0.data/platlib/eqc_models/ml/classifierqsvm.py,sha256=b6TdwlghR84UoQpfUThwMwNF_wmkABbCqOn6T1uY2dA,6636
41
- eqc_models-0.10.0.data/platlib/eqc_models/ml/clustering.py,sha256=yhh6jlgwZiQVn9h2pnYBlzVTCocK7rzsEurSQxCn5FQ,9486
42
- eqc_models-0.10.0.data/platlib/eqc_models/ml/clusteringbase.py,sha256=AvQbt6jeocBAGCuAmXHKyr3wLWqYUtBDq9PbsEOSlSc,2632
43
- eqc_models-0.10.0.data/platlib/eqc_models/ml/cvqboost_hamiltonian.pyx,sha256=3PMmEJ_xfmmWXGfire0t-WASnmKj6-CblufgQ2NTARo,2111
44
- eqc_models-0.10.0.data/platlib/eqc_models/ml/cvqboost_hamiltonian_c_func.c,sha256=ZoKgm_uGjTewhk4W6s-x8QoFuZO0KVkxILIFh6JKsoI,1851
45
- eqc_models-0.10.0.data/platlib/eqc_models/ml/cvqboost_hamiltonian_c_func.h,sha256=aOImMG5pziUnZxGpDXyWjLrvcY7ZdsczwwSQ2ay4T88,272
46
- eqc_models-0.10.0.data/platlib/eqc_models/ml/decomposition.py,sha256=Nsnq0GmINpbGOszndgCHXqKdECNpiNiampBrHPU_Gjg,8943
47
- eqc_models-0.10.0.data/platlib/eqc_models/ml/forecast.py,sha256=fFcBxQK9ZryfEuyvlr9HXicHoZRzLFybimYYttzhI9E,7403
48
- eqc_models-0.10.0.data/platlib/eqc_models/ml/forecastbase.py,sha256=s-6nUMvtYqG07r7MmmkFVj8_QqgeGkD-HVoEEDTE2bk,3654
49
- eqc_models-0.10.0.data/platlib/eqc_models/ml/regressor.py,sha256=LA1woXCnefG0wfoOxilX1kszgCUmfNcbQgs_WZ4Ai0o,5683
50
- eqc_models-0.10.0.data/platlib/eqc_models/ml/regressorbase.py,sha256=H5E8-8pUji1oH5JRdS37LJQoHuKt2JU8eXCZp-ZaEM4,2109
51
- eqc_models-0.10.0.data/platlib/eqc_models/ml/reservoir.py,sha256=cPRvpCaWLYTBkui35jCssHcOPQgSQZallrG6Ac9djVI,2827
52
- eqc_models-0.10.0.data/platlib/eqc_models/sequence/__init__.py,sha256=VXlYufO3GYFsM00oii9Cite2WsQEF8XTwRcjLPH_Zlg,92
53
- eqc_models-0.10.0.data/platlib/eqc_models/sequence/tsp.py,sha256=YM641FTyK5NkgRGxHrU1QmMkEU0gf77nEmIElTqa6Qw,7680
54
- eqc_models-0.10.0.data/platlib/eqc_models/solvers/__init__.py,sha256=hTjJI4F0FNDgQlK_oRHJxLIkg6of3I1-nEqU_3RL4Gk,542
55
- eqc_models-0.10.0.data/platlib/eqc_models/solvers/eqcdirect.py,sha256=RzcRz3e3a0nJaR_qZ1td3NRKHT1NKPei_QNYExN3ooI,4898
56
- eqc_models-0.10.0.data/platlib/eqc_models/solvers/qciclient.py,sha256=Vnfqa1N6JSzigb-mbqXj6g0r-1wOkeYzmEmctId1VCo,26975
57
- eqc_models-0.10.0.data/platlib/eqc_models/utilities/__init__.py,sha256=SI2U7JKmPWSiq-F1WcSyfd7l9V6nbOZv_p8quMAZaT0,340
58
- eqc_models-0.10.0.data/platlib/eqc_models/utilities/fileio.py,sha256=alWPTfjGFx6Iio9HZAAWtYcLmZsBBifg6S6_YbFMQhk,1088
59
- eqc_models-0.10.0.data/platlib/eqc_models/utilities/polynomial.py,sha256=blXfu7Ehz9lT4nEmIinRzJOL27_qUHSbQ57zxmwDJCA,4735
60
- eqc_models-0.10.0.data/platlib/eqc_models/utilities/qplib.py,sha256=Do-MjmCFdI5HyDOAjfoz4_5lugySLMBlMAWDLUWx2OA,15796
61
- eqc_models-0.10.0.dist-info/LICENSE.txt,sha256=8eh0oqsNNVR1Jk-13gkqRRSo2axtUU5kp2KzH4f9u3U,11354
62
- eqc_models-0.10.0.dist-info/METADATA,sha256=vFFt3ENmDo-0sFt3-9V_ffKaZm2y9COhcC0N_AVbUvM,7107
63
- eqc_models-0.10.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
64
- eqc_models-0.10.0.dist-info/top_level.txt,sha256=9ZfFeKNEvkRlKWoUnfcZ9TzmTdgdsuPEnTPy11Hqf4Q,30
65
- eqc_models-0.10.0.dist-info/RECORD,,