Repository navigation
Expand file tree
/
Copy pathtest_real_dwave_quantum.py
More file actions
304 lines (242 loc) · 10.3 KB
/
Copy pathtest_real_dwave_quantum.py
File metadata and controls
304 lines (242 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
from config.mcp_config import MCPConfig
"""
Test Suite for REAL D-Wave Quantum MCP Connector
Tests legitimate D-Wave quantum integration using actual Ocean SDK.
Requires real D-Wave Leap cloud access - NO SIMULATIONS.
Requirements:
- D-Wave Ocean SDK: pip install dwave-ocean-sdk
- D-Wave Leap account: https://cloud.dwavesys.com/leap/
- Valid API token configured
"""
import asyncio
import sys
import logging
from pathlib import Path
# Add project root to path
sys.path.append(str(Path(__file__).parent))
try:
from connectors.dwave_quantum_connector import (
DWaveQuantumConnector,
DWAVE_AVAILABLE,
)
except ImportError:
DWAVE_AVAILABLE = False
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class RealQuantumTest:
"""Test suite for authentic D-Wave quantum connector"""
def __init__(self):
self.connector = None
self.results = []
async def test_connection(self) -> bool:
"""Test connection to D-Wave Leap cloud service"""
logger.info("🔌 Testing D-Wave Leap connection...")
if not DWAVE_AVAILABLE:
logger.error("❌ D-Wave Ocean SDK not available")
logger.info("💡 Install with: pip install dwave-ocean-sdk")
logger.info("💡 Sign up at: https://cloud.dwavesys.com/leap/")
return False
try:
self.connector = DWaveQuantumConnector()
success = await self.connector.connect({})
if success:
logger.info("✅ Connected to D-Wave quantum system")
solver_info = await self.connector.get_solver_info()
logger.info(f"📊 Solver: {solver_info['solver_info']['name']}")
logger.info(f"🔬 Type: {solver_info['solver_info']['type']}")
if solver_info["solver_info"]["type"] == "QPU":
logger.info(
f"⚛️ Qubits: {solver_info['solver_info']['num_qubits']}"
)
logger.info(
f"🔗 Couplers: {solver_info['solver_info']['num_couplers']}"
)
return True
else:
logger.error("❌ Failed to connect to D-Wave")
return False
except Exception as e:
logger.error(f"❌ Connection error: {e}")
return False
async def test_simple_qubo(self) -> bool:
"""Test simple QUBO problem solving"""
logger.info("🧮 Testing simple QUBO problem...")
# Simple QUBO: minimize x0 + x1 - 2*x0*x1
# Optimal solutions: (0,1) or (1,0) with energy -1
qubo = {(0, 0): 1, (1, 1): 1, (0, 1): -2}
try:
result = await self.connector.execute_action(
"solve_qubo", {"qubo": qubo, "num_reads": 50}
)
if result.get("success"):
solution = result["best_solution"]
energy = result["best_energy"]
logger.info(f"✅ QUBO solved")
logger.info(f"📊 Best solution: {solution}")
logger.info(f"⚡ Energy: {energy}")
logger.info(f"🔢 Samples: {result['num_solutions']}")
# Check if we got a good solution
expected_energy = -1
if abs(energy - expected_energy) < 0.1:
logger.info("🎯 Found optimal solution!")
return True
else:
logger.warning(
f"⚠️ Energy {energy} not optimal (expected ~{expected_energy})"
)
return True # Still counts as working
else:
logger.error(f"❌ QUBO failed: {result.get('error')}")
return False
except Exception as e:
logger.error(f"❌ QUBO test error: {e}")
return False
async def test_traveling_salesman(self) -> bool:
"""Test Traveling Salesman Problem"""
logger.info("🗺️ Testing Traveling Salesman Problem...")
# 3-city TSP
cities = ["NYC", "Boston", "Philly"]
distances = {
("NYC", "Boston"): 4,
("NYC", "Philly"): 2,
("Boston", "Philly"): 3,
}
try:
result = await self.connector.execute_action(
"traveling_salesman",
{"cities": cities, "distances": distances, "num_reads": 30},
)
if result.get("success"):
route = result.get("route", [])
total_distance = result.get("total_distance", 0)
logger.info(f"✅ TSP solved")
logger.info(f"🛣️ Route: {' → '.join(route)}")
logger.info(f"📏 Total distance: {total_distance}")
# Verify route is valid (visits all cities)
if set(route) == set(cities):
logger.info("🎯 Valid route found!")
return True
else:
logger.warning("⚠️ Invalid route (missing cities)")
return False
else:
logger.error(f"❌ TSP failed: {result.get('error')}")
return False
except Exception as e:
logger.error(f"❌ TSP test error: {e}")
return False
async def test_maximum_cut(self) -> bool:
"""Test Maximum Cut graph problem"""
logger.info("📊 Testing Maximum Cut problem...")
# Simple triangle graph
edges = [(0, 1), (1, 2), (2, 0)]
weights = {(0, 1): 1, (1, 2): 1, (2, 0): 1}
try:
result = await self.connector.execute_action(
"max_cut",
{"edges": edges, "weights": weights, "num_reads": 30},
)
if result.get("success"):
partition_a = result.get("partition_a", [])
partition_b = result.get("partition_b", [])
cut_value = result.get("cut_value", 0)
logger.info(f"✅ Max-Cut solved")
logger.info(f"🔵 Partition A: {partition_a}")
logger.info(f"🔴 Partition B: {partition_b}")
logger.info(f"✂️ Cut value: {cut_value}")
# For triangle, max cut should be 2
if cut_value >= 2:
logger.info("🎯 Good cut found!")
return True
else:
logger.warning(f"⚠️ Cut value {cut_value} could be better")
return True # Still working
else:
logger.error(f"❌ Max-Cut failed: {result.get('error')}")
return False
except Exception as e:
logger.error(f"❌ Max-Cut test error: {e}")
return False
async def cleanup(self):
"""Clean up connection"""
if self.connector:
await self.connector.disconnect()
logger.info("🔌 Disconnected from D-Wave")
async def run_all_tests(self):
"""Run complete test suite"""
logger.info("🚀 Starting Real D-Wave Quantum Connector Tests")
logger.info("=" * 60)
tests = [
("Connection Test", self.test_connection),
("Simple QUBO", self.test_simple_qubo),
("Traveling Salesman", self.test_traveling_salesman),
("Maximum Cut", self.test_maximum_cut),
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
logger.info(f"\n🧪 Running: {test_name}")
try:
if await test_func():
passed += 1
logger.info(f"✅ {test_name}: PASSED")
else:
logger.error(f"❌ {test_name}: FAILED")
except Exception as e:
logger.error(f"❌ {test_name}: ERROR - {e}")
await self.cleanup()
logger.info("\n" + "=" * 60)
logger.info("🧪 REAL D-WAVE QUANTUM TEST SUMMARY")
logger.info("=" * 60)
logger.info(f"📊 Total Tests: {total}")
logger.info(f"✅ Passed: {passed}")
logger.info(f"❌ Failed: {total - passed}")
logger.info(f"📈 Success Rate: {(passed/total)*100:.1f}%")
if passed == total:
logger.info("🎉 ALL TESTS PASSED! Real quantum computing working!")
else:
logger.warning("⚠️ Some tests failed - check D-Wave Leap setup")
logger.info("\n📋 Requirements for Full Testing:")
logger.info("• D-Wave Ocean SDK: pip install dwave-ocean-sdk")
logger.info("• D-Wave Leap account: https://cloud.dwavesys.com/leap/")
logger.info("• API token configured in environment")
logger.info("• Internet connection for cloud access")
logger.info("\n🔗 Learn More:")
logger.info("• D-Wave Examples: https://github.com/dwave-examples")
logger.info(
"• Advantage2 System: https://github.com/dwave-examples/advantage2.git"
)
logger.info("• Ocean Documentation: https://docs.ocean.dwavesys.com/")
return passed == total
import pytest
@pytest.mark.asyncio
async def test_real_quantum():
"""Test real D-Wave quantum computing"""
logger.info("🚀 Testing REAL D-Wave Quantum Computing")
if not DWAVE_AVAILABLE:
logger.error("❌ D-Wave Ocean SDK not available")
logger.info("Install: pip install dwave-ocean-sdk")
logger.info("Signup: https://cloud.dwavesys.com/leap/")
return False
connector = DWaveQuantumConnector()
# Test connection
success = await connector.connect({})
if not success:
logger.error("❌ Failed to connect to D-Wave")
return False
logger.info("✅ Connected to D-Wave quantum system")
# Test simple QUBO
qubo = {(0, 0): 1, (1, 1): 1, (0, 1): -2}
result = await connector.execute_action(
"solve_qubo", {"qubo": qubo, "num_reads": 50}
)
if result.get("success"):
logger.info(f"✅ QUBO solved: {result['best_solution']}")
logger.info(f"Energy: {result['best_energy']}")
else:
logger.error(f"❌ QUBO failed: {result.get('error')}")
await connector.disconnect()
return result.get("success", False)
if __name__ == "__main__":
asyncio.run(test_real_quantum())