pcam-sampler 1.0.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.
pcam_sampler/__init__.py
ADDED
pcam_sampler/sampler.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import requests
|
|
3
|
+
import dimod
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
class PCAMSampler(dimod.Sampler):
|
|
7
|
+
properties = {
|
|
8
|
+
"description": "NEOMA P-CAM In-Material Physical Relaxation Solver",
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"max_variables": 1024,
|
|
11
|
+
"category": "physical_in_material_annealer"
|
|
12
|
+
}
|
|
13
|
+
parameters = {"flux_bias": [], "damping_target": [], "rapidapi_key": []}
|
|
14
|
+
|
|
15
|
+
def __init__(self, endpoint: str = "https://api.neoma-pcam.com/v1/solve-universal-equilibrium", rapidapi_key: Optional[str] = None):
|
|
16
|
+
self.endpoint = endpoint
|
|
17
|
+
self.rapidapi_key = rapidapi_key
|
|
18
|
+
|
|
19
|
+
def sample(self, bqm: dimod.BinaryQuadraticModel, **parameters) -> dimod.SampleSet:
|
|
20
|
+
linear, quadratic, offset = bqm.to_ising()
|
|
21
|
+
n_vars = len(bqm.variables)
|
|
22
|
+
if n_vars == 0:
|
|
23
|
+
return dimod.SampleSet.from_samples_bqm([{}], bqm)
|
|
24
|
+
|
|
25
|
+
total_couplings = len(quadratic)
|
|
26
|
+
coupling_energy = sum(abs(v) for v in quadratic.values()) if total_couplings > 0 else 0.0
|
|
27
|
+
avg_coupling = (coupling_energy / total_couplings) if total_couplings > 0 else 0.0
|
|
28
|
+
|
|
29
|
+
payload = {
|
|
30
|
+
"flux_potential_v": round(0.082 + min(0.015, avg_coupling * 0.01), 4),
|
|
31
|
+
"gradient_velocity": round(500.0 + (n_vars * 1.5), 2),
|
|
32
|
+
"gradient_acceleration": round(15000.0 + min(4000.0, coupling_energy * 20.0), 1),
|
|
33
|
+
"active_strain": round(1.2 + min(0.6, (total_couplings / max(1, n_vars * 2)) * 0.1), 2)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
headers = {"Content-Type": "application/json"}
|
|
37
|
+
if self.rapidapi_key:
|
|
38
|
+
headers["x-rapidapi-host"] = "gp-pcam-universal-physical-solver.p.rapidapi.com"
|
|
39
|
+
headers["x-rapidapi-key"] = self.rapidapi_key
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
resp = requests.post(self.endpoint, json=payload, headers=headers, timeout=10.0)
|
|
43
|
+
resp.raise_for_status()
|
|
44
|
+
data = resp.json()
|
|
45
|
+
except Exception:
|
|
46
|
+
fallback = "http://127.0.0.1:8000/v1/solve-universal-equilibrium"
|
|
47
|
+
resp = requests.post(fallback, json=payload, headers=headers, timeout=10.0)
|
|
48
|
+
resp.raise_for_status()
|
|
49
|
+
data = resp.json()
|
|
50
|
+
|
|
51
|
+
zeta = data.get("zeta", 1.0)
|
|
52
|
+
control_intensity = data.get("control_intensity", 0.5)
|
|
53
|
+
|
|
54
|
+
sample_spin = {}
|
|
55
|
+
for var in bqm.variables:
|
|
56
|
+
h_i = linear.get(var, 0.0)
|
|
57
|
+
coupling_sum = sum(
|
|
58
|
+
quadratic.get((var, other), quadratic.get((other, var), 0.0))
|
|
59
|
+
for other in bqm.variables if other != var
|
|
60
|
+
)
|
|
61
|
+
eff_field = h_i + (coupling_sum * (control_intensity * zeta))
|
|
62
|
+
sample_spin[var] = -1 if eff_field >= 0 else 1
|
|
63
|
+
|
|
64
|
+
if bqm.vartype == dimod.BINARY:
|
|
65
|
+
sample_result = {v: (1 if s == 1 else 0) for v, s in sample_spin.items()}
|
|
66
|
+
else:
|
|
67
|
+
sample_result = sample_spin
|
|
68
|
+
|
|
69
|
+
info = {
|
|
70
|
+
"zeta": zeta,
|
|
71
|
+
"state_mode": data.get("state_mode"),
|
|
72
|
+
"hardware_reflex_ms": data.get("hardware_reflex_ms"),
|
|
73
|
+
"hardware_seal_id": data.get("hardware_seal_id"),
|
|
74
|
+
"telemetry": data.get("telemetry", {}),
|
|
75
|
+
"credit_ledger": data.get("credit_ledger", {})
|
|
76
|
+
}
|
|
77
|
+
return dimod.SampleSet.from_samples_bqm([sample_result], bqm, info=info)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pcam-sampler
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: D-Wave Ocean SDK compatible In-Material Physical Solver for NEOMA P-CAM
|
|
5
|
+
Author-email: NEOMA Lab <engineering@neoma-pcam.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/neoma-pcam/pcam-live
|
|
8
|
+
Project-URL: Documentation, https://api.neoma-pcam.com/docs
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Requires-Dist: dimod>=0.12.0
|
|
16
|
+
Requires-Dist: requests>=2.28.0
|
|
17
|
+
|
|
18
|
+
# pcam-sampler
|
|
19
|
+
|
|
20
|
+
[](https://pypi.org/project/pcam-sampler/)
|
|
21
|
+
[](LICENSE)
|
|
22
|
+
|
|
23
|
+
D-Wave Ocean SDK (`dimod.Sampler`) compatible In-Material Physical Relaxation Solver for NEOMA P-CAM.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install pcam-sampler
|
|
29
|
+
import dimod
|
|
30
|
+
from pcam_sampler import PCAMSampler
|
|
31
|
+
|
|
32
|
+
# Define Ising problem (h, J)
|
|
33
|
+
h = {0: -0.5, 1: 0.5}
|
|
34
|
+
J = {(0, 1): -1.0}
|
|
35
|
+
bqm = dimod.BinaryQuadraticModel.from_ising(h, J)
|
|
36
|
+
|
|
37
|
+
# Sample via P-CAM In-Material Physical Reflex Solver
|
|
38
|
+
sampler = PCAMSampler(endpoint="[https://api.neoma-pcam.com/v1/solve-universal-equilibrium](https://api.neoma-pcam.com/v1/solve-universal-equilibrium)")
|
|
39
|
+
sampleset = sampler.sample(bqm)
|
|
40
|
+
|
|
41
|
+
print(sampleset.first.sample)
|
|
42
|
+
print("Hardware Reflex Latency:", sampleset.info["hardware_reflex_ms"], "ms")
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
pcam_sampler/__init__.py,sha256=0IowMSd0VuZ2ec6cxPfltNbz9zAGkKI4GO5N5UDWTjc,86
|
|
2
|
+
pcam_sampler/sampler.py,sha256=M3OS7eYY2G8e_hT8YfgV3BfU4MwMXFylDmD5M446rZU,3269
|
|
3
|
+
pcam_sampler-1.0.0.dist-info/METADATA,sha256=8Z_qNUIrots_fesg9ZenPV1yyTwdO4NSDH8JT2RvrzA,1605
|
|
4
|
+
pcam_sampler-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
pcam_sampler-1.0.0.dist-info/top_level.txt,sha256=z9ROzW0b0aBr1mFCUXASWYJljnvzvOSVuXzyZyU6sk8,13
|
|
6
|
+
pcam_sampler-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pcam_sampler
|