qaoalib 0.1.6__tar.gz → 0.1.8__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.
Files changed (34) hide show
  1. qaoalib-0.1.8/PKG-INFO +55 -0
  2. qaoalib-0.1.8/README.md +41 -0
  3. qaoalib-0.1.8/qaoalib/data/__init__.py +0 -0
  4. qaoalib-0.1.8/qaoalib/data/graph.py +18 -0
  5. qaoalib-0.1.8/qaoalib/data/result.py +26 -0
  6. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/qmc.py +4 -8
  7. qaoalib-0.1.8/qaoalib/qaoa/strategy.py +70 -0
  8. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/utils.py +46 -2
  9. qaoalib-0.1.8/qaoalib/version.py +1 -0
  10. qaoalib-0.1.8/qaoalib.egg-info/PKG-INFO +55 -0
  11. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib.egg-info/SOURCES.txt +4 -0
  12. qaoalib-0.1.6/PKG-INFO +0 -28
  13. qaoalib-0.1.6/README.md +0 -14
  14. qaoalib-0.1.6/qaoalib/version.py +0 -1
  15. qaoalib-0.1.6/qaoalib.egg-info/PKG-INFO +0 -28
  16. {qaoalib-0.1.6 → qaoalib-0.1.8}/LICENSE +0 -0
  17. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/__init__.py +0 -0
  18. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/json.py +0 -0
  19. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/math.py +0 -0
  20. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/__init__.py +0 -0
  21. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/landscape/__init__.py +0 -0
  22. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/landscape/base.py +0 -0
  23. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/landscape/direct_numpy.py +0 -0
  24. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/landscape/hadamard_test.py +0 -0
  25. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/landscape/hybrid_fast.py +0 -0
  26. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/layerwise.py +0 -0
  27. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/qis.py +0 -0
  28. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib/qaoa/utils.py +0 -0
  29. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib.egg-info/dependency_links.txt +0 -0
  30. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib.egg-info/requires.txt +0 -0
  31. {qaoalib-0.1.6 → qaoalib-0.1.8}/qaoalib.egg-info/top_level.txt +0 -0
  32. {qaoalib-0.1.6 → qaoalib-0.1.8}/setup.cfg +0 -0
  33. {qaoalib-0.1.6 → qaoalib-0.1.8}/setup.py +0 -0
  34. {qaoalib-0.1.6 → qaoalib-0.1.8}/test/test.py +0 -0
qaoalib-0.1.8/PKG-INFO ADDED
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.1
2
+ Name: qaoalib
3
+ Version: 0.1.8
4
+ Summary: A package for QAOA Maxcut calculations
5
+ Home-page: https://github.com/xenoicwyce/qaoalib
6
+ Author: Xinwei Lee
7
+ Author-email: xenoicwyce@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # qaoalib
16
+ A package for QAOA Max-cut Calculations.
17
+
18
+ Packages required:
19
+ - numpy
20
+ - networkx
21
+ - matplotlib
22
+ - qiskit
23
+
24
+ # How to install
25
+ You can install from the PyPI:
26
+ ```
27
+ pip install --upgrade qaoalib
28
+ ```
29
+
30
+ # Usage
31
+ Calculate Max-cut expectation with `Qmc` or `QmcFastKron` (faster version):
32
+ ```
33
+ import networkx as nx
34
+ from qaoalib.qaoa import Qmc, QmcFastKron
35
+
36
+ G = nx.fast_gnp_random_graph(10, 0.5) # Random graph with 10 nodes
37
+ params = [0.2, 0.4, 0.3, 0.5] # 4 params, depth = 2
38
+
39
+ qmc = Qmc(G, params) # or QmcFastKron(G, params)
40
+ qmc.run()
41
+ print(qmc.expectation)
42
+ ```
43
+
44
+ Plot landscapes of the QAOA Max-cut expectation function:
45
+ ```
46
+ import networkx as nx
47
+ from qaoalib.qaoa.landscape import HybridFast
48
+
49
+ G = nx.fast_gnp_random_graph(10, 0.5)
50
+ prev_params = [0.1, 0.2] # previous parameters (gamma1, beta1)
51
+
52
+ ins = HybridFast(G, prev_params) # plots the landscape wrt gamma2 and beta2 with previous parameters given.
53
+ ins.create_grid()
54
+ ins.show_landscape()
55
+ ```
@@ -0,0 +1,41 @@
1
+ # qaoalib
2
+ A package for QAOA Max-cut Calculations.
3
+
4
+ Packages required:
5
+ - numpy
6
+ - networkx
7
+ - matplotlib
8
+ - qiskit
9
+
10
+ # How to install
11
+ You can install from the PyPI:
12
+ ```
13
+ pip install --upgrade qaoalib
14
+ ```
15
+
16
+ # Usage
17
+ Calculate Max-cut expectation with `Qmc` or `QmcFastKron` (faster version):
18
+ ```
19
+ import networkx as nx
20
+ from qaoalib.qaoa import Qmc, QmcFastKron
21
+
22
+ G = nx.fast_gnp_random_graph(10, 0.5) # Random graph with 10 nodes
23
+ params = [0.2, 0.4, 0.3, 0.5] # 4 params, depth = 2
24
+
25
+ qmc = Qmc(G, params) # or QmcFastKron(G, params)
26
+ qmc.run()
27
+ print(qmc.expectation)
28
+ ```
29
+
30
+ Plot landscapes of the QAOA Max-cut expectation function:
31
+ ```
32
+ import networkx as nx
33
+ from qaoalib.qaoa.landscape import HybridFast
34
+
35
+ G = nx.fast_gnp_random_graph(10, 0.5)
36
+ prev_params = [0.1, 0.2] # previous parameters (gamma1, beta1)
37
+
38
+ ins = HybridFast(G, prev_params) # plots the landscape wrt gamma2 and beta2 with previous parameters given.
39
+ ins.create_grid()
40
+ ins.show_landscape()
41
+ ```
File without changes
@@ -0,0 +1,18 @@
1
+ from typing import Sequence
2
+ from pydantic import BaseModel
3
+
4
+
5
+ class GraphData(BaseModel):
6
+ n_nodes: int
7
+ edges: list[Sequence[int]]
8
+ n_edges: int
9
+ shift: float
10
+ true_obj: int
11
+
12
+
13
+ class RegGraphData(GraphData):
14
+ deg: int
15
+
16
+
17
+ class GnpGraphData(GraphData):
18
+ prob: float
@@ -0,0 +1,26 @@
1
+ from typing import Union, Any
2
+ from typing_extensions import TypeAlias
3
+ from pydantic import BaseModel
4
+
5
+ from .graph import RegGraphData, GnpGraphData
6
+
7
+ SingleTrialDict: TypeAlias = dict[int, Any]
8
+ MultipleTrialDict: TypeAlias = dict[int, list[Any]]
9
+
10
+
11
+ class Result(BaseModel):
12
+ graph: Union[RegGraphData, GnpGraphData]
13
+ expectations: SingleTrialDict
14
+ alpha: SingleTrialDict
15
+ initial_params: SingleTrialDict
16
+ opt_params: SingleTrialDict
17
+ nfev: SingleTrialDict
18
+
19
+
20
+ class PfResult(BaseModel):
21
+ graph: Union[RegGraphData, GnpGraphData]
22
+ expectations: MultipleTrialDict
23
+ alpha: MultipleTrialDict
24
+ initial_params: MultipleTrialDict
25
+ opt_params: MultipleTrialDict
26
+ nfev: MultipleTrialDict
@@ -5,6 +5,7 @@ from .qis import qaoa_circuit, sv_backend
5
5
  from .utils import expectation, I, Z
6
6
  from ..math import fast_kron
7
7
 
8
+
8
9
  class Qmc:
9
10
  def __init__(self, G, params):
10
11
  self.graph = G
@@ -28,6 +29,7 @@ class Qmc:
28
29
 
29
30
  self.expectation = expectation(self.graph, self.result)
30
31
 
32
+
31
33
  class QmcFastKron(Qmc):
32
34
  def __init__(self, G, params):
33
35
  super().__init__(G, params)
@@ -38,14 +40,8 @@ class QmcFastKron(Qmc):
38
40
  edge = (u,v)
39
41
  kron_list = [Z if i in edge else I for i in range(len(self.graph.nodes))]
40
42
  kron_list.reverse()
41
- sum_ += d.get("weight",1) * (1 - (sv.conj().T @ fast_kron(kron_list, sv)).item().real)
42
-
43
-
44
- # for edge in self.graph.edges:
45
- # kron_list = [Z if i in edge else I for i in range(len(self.graph.nodes))]
46
- # kron_list.reverse()
47
- # sum_ += (sv.conj().T @ fast_kron(kron_list, sv)).item().real
48
-
43
+ weight = d.get('weight', 1)
44
+ sum_ += weight * (1 - (sv.conj().T @ fast_kron(kron_list, sv)).item().real)
49
45
  # return a single expectation value
50
46
  return sum_/2
51
47
 
@@ -0,0 +1,70 @@
1
+ import numpy as np
2
+ from typing import TYPE_CHECKING, Optional, Union, Sequence
3
+ from typing_extensions import TypeAlias
4
+ # from .params import Params
5
+
6
+ if TYPE_CHECKING:
7
+ import numpy.typing as npt
8
+
9
+ Params: TypeAlias = Union[Sequence[float], "npt.NDArray[np.float_]"]
10
+
11
+
12
+ def interp():
13
+ ...
14
+
15
+ def bilinear(
16
+ prev_params: Params,
17
+ pp_params: Params,
18
+ gamma_bound: Optional[tuple[float, float]] = None,
19
+ beta_bound: Optional[tuple[float, float]] = None,
20
+ ) -> "npt.NDArray[np.float_]":
21
+ if gamma_bound is None:
22
+ gamma_bound = (0, np.pi)
23
+ if beta_bound is None:
24
+ beta_bound = (0, np.pi/2)
25
+
26
+ prev_params = np.asarray(prev_params)
27
+ pp_params = np.asarray(pp_params) # prev_prev_params (p-2)
28
+
29
+ # split into gamma and beta
30
+ prev_gb = np.split(prev_params, 2)
31
+ pp_gb = np.split(pp_params, 2)
32
+
33
+ new_gb = []
34
+ for i in range(2):
35
+ delta2 = prev_gb[i][-2] - pp_gb[i][-1]
36
+ pp_gb[i] = np.hstack([pp_gb[i], prev_gb[i][-1] - delta2])
37
+
38
+ diff = prev_gb[i] - pp_gb[i]
39
+ new_x = prev_gb[i] + diff
40
+
41
+ delta3 = new_x[-1] - new_x[-2]
42
+ new_x = np.hstack([new_x, new_x[-1] + delta3])
43
+
44
+ if i == 0:
45
+ new_x = np.clip(new_x, *gamma_bound)
46
+ else:
47
+ new_x = np.clip(new_x, *beta_bound)
48
+
49
+ new_gb.append(new_x)
50
+
51
+ return np.hstack(new_gb)
52
+
53
+
54
+ def params_fixing(
55
+ prev_params: Params,
56
+ gamma_bound: Optional[tuple[float, float]] = None,
57
+ beta_bound: Optional[tuple[float, float]] = None,
58
+ ) -> "npt.NDArray[np.float_]":
59
+ if gamma_bound is None:
60
+ gamma_bound = (0, np.pi)
61
+ if beta_bound is None:
62
+ beta_bound = (0, np.pi/2)
63
+
64
+ prev_params = np.asarray(prev_params)
65
+ gamma, beta = np.split(prev_params)
66
+
67
+ gamma = np.hstack([gamma, np.random.uniform(*gamma_bound)])
68
+ beta = np.hstack([beta, np.random.uniform(*beta_bound)])
69
+
70
+ return np.hstack([gamma, beta])
@@ -1,7 +1,10 @@
1
+ from typing import Union, Literal, Optional
1
2
  import warnings
2
3
  import numpy as np
3
4
  import networkx as nx
4
5
 
6
+ from .data.graph import RegGraphData, GnpGraphData
7
+
5
8
 
6
9
  def get_info(filename, indicator, mode='get-numeric'):
7
10
  try:
@@ -122,7 +125,7 @@ def _reg_params(degree, node, seed=None):
122
125
 
123
126
  return list(G.edges), shift, cost
124
127
 
125
- def load_data_prototype(graph_type, G=None, **data_kw):
128
+ def load_data_prototype(graph_type, G=None, solve_brute=True, **data_kw):
126
129
  data = {
127
130
  'node': 0,
128
131
  'edges': [],
@@ -148,7 +151,8 @@ def load_data_prototype(graph_type, G=None, **data_kw):
148
151
  data['edges'] = list(G.edges)
149
152
  data['n_edge'] = len(G.edges)
150
153
  data['shift'] = -data['n_edge']/2.0
151
- data['true_obj'], _ = maxcut_brute(G)
154
+ if solve_brute:
155
+ data['true_obj'], _ = maxcut_brute(G)
152
156
 
153
157
  if graph_type == 'reg':
154
158
  data['degree'] = len(list(G.neighbors(0)))
@@ -260,3 +264,43 @@ def post_process(data):
260
264
  for p, arr in data['expectations'].items():
261
265
  data['energies'][p] = -(np.asarray(arr) + data['shift'])
262
266
  data['alpha'][p] = np.asarray(arr) / data['true_obj']
267
+
268
+ def convert_graph(
269
+ G: nx.Graph,
270
+ graph_type: Literal['reg', 'gnp'],
271
+ prob: Optional[float] = None,
272
+ solve_brute: bool = True,
273
+ ) -> Union[RegGraphData, GnpGraphData]:
274
+
275
+ if solve_brute:
276
+ true_obj, _ = maxcut_brute(G)
277
+ else:
278
+ true_obj = 0
279
+
280
+ n_nodes = len(G.nodes)
281
+ edges = list(G.edges)
282
+ n_edges = len(G.edges)
283
+ shift = -n_edges / 2.0
284
+
285
+ if graph_type == 'reg':
286
+ deg = len(list(G.neighbors(0)))
287
+ return RegGraphData(
288
+ deg=deg,
289
+ n_nodes=n_nodes,
290
+ edges=edges,
291
+ n_edges=n_edges,
292
+ shift=shift,
293
+ true_obj=true_obj,
294
+ )
295
+
296
+ elif graph_type == 'gnp':
297
+ if prob is None:
298
+ raise ValueError('`prob` must be passed for Gnp graphs.')
299
+ return GnpGraphData(
300
+ prob=prob,
301
+ n_nodes=n_nodes,
302
+ edges=edges,
303
+ n_edges=n_edges,
304
+ shift=shift,
305
+ true_obj=true_obj,
306
+ )
@@ -0,0 +1 @@
1
+ __version__ = '0.1.8'
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.1
2
+ Name: qaoalib
3
+ Version: 0.1.8
4
+ Summary: A package for QAOA Maxcut calculations
5
+ Home-page: https://github.com/xenoicwyce/qaoalib
6
+ Author: Xinwei Lee
7
+ Author-email: xenoicwyce@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # qaoalib
16
+ A package for QAOA Max-cut Calculations.
17
+
18
+ Packages required:
19
+ - numpy
20
+ - networkx
21
+ - matplotlib
22
+ - qiskit
23
+
24
+ # How to install
25
+ You can install from the PyPI:
26
+ ```
27
+ pip install --upgrade qaoalib
28
+ ```
29
+
30
+ # Usage
31
+ Calculate Max-cut expectation with `Qmc` or `QmcFastKron` (faster version):
32
+ ```
33
+ import networkx as nx
34
+ from qaoalib.qaoa import Qmc, QmcFastKron
35
+
36
+ G = nx.fast_gnp_random_graph(10, 0.5) # Random graph with 10 nodes
37
+ params = [0.2, 0.4, 0.3, 0.5] # 4 params, depth = 2
38
+
39
+ qmc = Qmc(G, params) # or QmcFastKron(G, params)
40
+ qmc.run()
41
+ print(qmc.expectation)
42
+ ```
43
+
44
+ Plot landscapes of the QAOA Max-cut expectation function:
45
+ ```
46
+ import networkx as nx
47
+ from qaoalib.qaoa.landscape import HybridFast
48
+
49
+ G = nx.fast_gnp_random_graph(10, 0.5)
50
+ prev_params = [0.1, 0.2] # previous parameters (gamma1, beta1)
51
+
52
+ ins = HybridFast(G, prev_params) # plots the landscape wrt gamma2 and beta2 with previous parameters given.
53
+ ins.create_grid()
54
+ ins.show_landscape()
55
+ ```
@@ -11,10 +11,14 @@ qaoalib.egg-info/SOURCES.txt
11
11
  qaoalib.egg-info/dependency_links.txt
12
12
  qaoalib.egg-info/requires.txt
13
13
  qaoalib.egg-info/top_level.txt
14
+ qaoalib/data/__init__.py
15
+ qaoalib/data/graph.py
16
+ qaoalib/data/result.py
14
17
  qaoalib/qaoa/__init__.py
15
18
  qaoalib/qaoa/layerwise.py
16
19
  qaoalib/qaoa/qis.py
17
20
  qaoalib/qaoa/qmc.py
21
+ qaoalib/qaoa/strategy.py
18
22
  qaoalib/qaoa/utils.py
19
23
  qaoalib/qaoa/landscape/__init__.py
20
24
  qaoalib/qaoa/landscape/base.py
qaoalib-0.1.6/PKG-INFO DELETED
@@ -1,28 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: qaoalib
3
- Version: 0.1.6
4
- Summary: A package for QAOA Maxcut calculations
5
- Home-page: https://github.com/xenoicwyce/qaoalib
6
- Author: Xinwei Lee
7
- Author-email: xenoicwyce@gmail.com
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.6
12
- Description-Content-Type: text/markdown
13
- License-File: LICENSE
14
-
15
- # qaoalib
16
- A package for QAOA Maxcut Calculations.
17
-
18
- Packages required:
19
- - numpy
20
- - networkx
21
- - matplotlib
22
- - qiskit
23
-
24
- # How to install
25
- You can install from the PyPI:
26
- ```
27
- pip install --upgrade qaoalib
28
- ```
qaoalib-0.1.6/README.md DELETED
@@ -1,14 +0,0 @@
1
- # qaoalib
2
- A package for QAOA Maxcut Calculations.
3
-
4
- Packages required:
5
- - numpy
6
- - networkx
7
- - matplotlib
8
- - qiskit
9
-
10
- # How to install
11
- You can install from the PyPI:
12
- ```
13
- pip install --upgrade qaoalib
14
- ```
@@ -1 +0,0 @@
1
- __version__ = '0.1.6'
@@ -1,28 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: qaoalib
3
- Version: 0.1.6
4
- Summary: A package for QAOA Maxcut calculations
5
- Home-page: https://github.com/xenoicwyce/qaoalib
6
- Author: Xinwei Lee
7
- Author-email: xenoicwyce@gmail.com
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.6
12
- Description-Content-Type: text/markdown
13
- License-File: LICENSE
14
-
15
- # qaoalib
16
- A package for QAOA Maxcut Calculations.
17
-
18
- Packages required:
19
- - numpy
20
- - networkx
21
- - matplotlib
22
- - qiskit
23
-
24
- # How to install
25
- You can install from the PyPI:
26
- ```
27
- pip install --upgrade qaoalib
28
- ```
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes