quantumflow-sdk 0.1.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.
- api/__init__.py +1 -0
- api/auth.py +208 -0
- api/main.py +403 -0
- api/models.py +137 -0
- api/routes/__init__.py +1 -0
- api/routes/auth_routes.py +234 -0
- api/routes/teleport_routes.py +415 -0
- db/__init__.py +15 -0
- db/crud.py +319 -0
- db/database.py +93 -0
- db/models.py +197 -0
- quantumflow/__init__.py +47 -0
- quantumflow/algorithms/__init__.py +48 -0
- quantumflow/algorithms/compression/__init__.py +7 -0
- quantumflow/algorithms/compression/amplitude_amplification.py +189 -0
- quantumflow/algorithms/compression/qft_compression.py +133 -0
- quantumflow/algorithms/compression/token_compression.py +261 -0
- quantumflow/algorithms/cryptography/__init__.py +6 -0
- quantumflow/algorithms/cryptography/qkd.py +205 -0
- quantumflow/algorithms/cryptography/qrng.py +231 -0
- quantumflow/algorithms/machine_learning/__init__.py +7 -0
- quantumflow/algorithms/machine_learning/qnn.py +276 -0
- quantumflow/algorithms/machine_learning/qsvm.py +249 -0
- quantumflow/algorithms/machine_learning/vqe.py +229 -0
- quantumflow/algorithms/optimization/__init__.py +7 -0
- quantumflow/algorithms/optimization/grover.py +223 -0
- quantumflow/algorithms/optimization/qaoa.py +251 -0
- quantumflow/algorithms/optimization/quantum_annealing.py +237 -0
- quantumflow/algorithms/utility/__init__.py +6 -0
- quantumflow/algorithms/utility/circuit_optimizer.py +194 -0
- quantumflow/algorithms/utility/error_correction.py +330 -0
- quantumflow/api/__init__.py +1 -0
- quantumflow/api/routes/__init__.py +4 -0
- quantumflow/api/routes/billing_routes.py +520 -0
- quantumflow/backends/__init__.py +33 -0
- quantumflow/backends/base_backend.py +184 -0
- quantumflow/backends/braket_backend.py +345 -0
- quantumflow/backends/ibm_backend.py +112 -0
- quantumflow/backends/simulator_backend.py +86 -0
- quantumflow/billing/__init__.py +25 -0
- quantumflow/billing/models.py +126 -0
- quantumflow/billing/stripe_service.py +619 -0
- quantumflow/core/__init__.py +12 -0
- quantumflow/core/entanglement.py +164 -0
- quantumflow/core/memory.py +147 -0
- quantumflow/core/quantum_backprop.py +394 -0
- quantumflow/core/quantum_compressor.py +309 -0
- quantumflow/core/teleportation.py +386 -0
- quantumflow/integrations/__init__.py +107 -0
- quantumflow/integrations/autogen_tools.py +501 -0
- quantumflow/integrations/crewai_agents.py +425 -0
- quantumflow/integrations/crewai_tools.py +407 -0
- quantumflow/integrations/langchain_memory.py +385 -0
- quantumflow/integrations/langchain_tools.py +366 -0
- quantumflow/integrations/mcp_server.py +575 -0
- quantumflow_sdk-0.1.0.dist-info/METADATA +190 -0
- quantumflow_sdk-0.1.0.dist-info/RECORD +60 -0
- quantumflow_sdk-0.1.0.dist-info/WHEEL +5 -0
- quantumflow_sdk-0.1.0.dist-info/entry_points.txt +2 -0
- quantumflow_sdk-0.1.0.dist-info/top_level.txt +3 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"""
|
|
2
|
+
LangChain Tools for QuantumFlow.
|
|
3
|
+
|
|
4
|
+
Provides quantum-powered tools for LangChain agents:
|
|
5
|
+
- Token compression for context optimization
|
|
6
|
+
- Quantum gradient computation
|
|
7
|
+
- Quantum memory operations
|
|
8
|
+
- Entanglement for context linking
|
|
9
|
+
- Grover's search for fast retrieval
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from typing import Any, Optional, Type, List
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from langchain.tools import BaseTool
|
|
17
|
+
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
18
|
+
from pydantic import BaseModel, Field
|
|
19
|
+
LANGCHAIN_AVAILABLE = True
|
|
20
|
+
except ImportError:
|
|
21
|
+
LANGCHAIN_AVAILABLE = False
|
|
22
|
+
# Create dummy classes for type hints
|
|
23
|
+
class BaseTool:
|
|
24
|
+
pass
|
|
25
|
+
class BaseModel:
|
|
26
|
+
pass
|
|
27
|
+
class CallbackManagerForToolRun:
|
|
28
|
+
pass
|
|
29
|
+
def Field(*args, **kwargs):
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
from quantumflow.core.quantum_compressor import QuantumCompressor
|
|
33
|
+
from quantumflow.core.quantum_backprop import QuantumBackprop
|
|
34
|
+
from quantumflow.core.entanglement import Entangler
|
|
35
|
+
from quantumflow.core.memory import QuantumMemory
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _check_langchain():
|
|
39
|
+
"""Check if LangChain is available."""
|
|
40
|
+
if not LANGCHAIN_AVAILABLE:
|
|
41
|
+
raise ImportError(
|
|
42
|
+
"LangChain is not installed. "
|
|
43
|
+
"Install it with: pip install langchain langchain-core"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# ============== Input Schemas ==============
|
|
48
|
+
|
|
49
|
+
if LANGCHAIN_AVAILABLE:
|
|
50
|
+
class CompressInput(BaseModel):
|
|
51
|
+
"""Input for quantum compression."""
|
|
52
|
+
text: str = Field(description="Text to compress using quantum encoding")
|
|
53
|
+
compression_level: int = Field(default=1, description="Compression level (1-3)")
|
|
54
|
+
|
|
55
|
+
class GradientInput(BaseModel):
|
|
56
|
+
"""Input for quantum gradient computation."""
|
|
57
|
+
input_values: List[float] = Field(description="Input state values")
|
|
58
|
+
target_values: List[float] = Field(description="Target state values")
|
|
59
|
+
weights: List[float] = Field(description="Current weight values")
|
|
60
|
+
|
|
61
|
+
class MemoryInput(BaseModel):
|
|
62
|
+
"""Input for quantum memory operations."""
|
|
63
|
+
operation: str = Field(description="Operation: 'store', 'retrieve', or 'search'")
|
|
64
|
+
key: str = Field(description="Memory key")
|
|
65
|
+
values: Optional[List[float]] = Field(default=None, description="Values to store")
|
|
66
|
+
|
|
67
|
+
class EntangleInput(BaseModel):
|
|
68
|
+
"""Input for quantum entanglement."""
|
|
69
|
+
contexts: List[str] = Field(description="List of contexts to entangle")
|
|
70
|
+
|
|
71
|
+
class SearchInput(BaseModel):
|
|
72
|
+
"""Input for quantum search."""
|
|
73
|
+
query: str = Field(description="Search query")
|
|
74
|
+
database: List[str] = Field(description="Database of items to search")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# ============== Tools ==============
|
|
78
|
+
|
|
79
|
+
class QuantumCompressTool(BaseTool):
|
|
80
|
+
"""
|
|
81
|
+
Quantum Token Compression Tool.
|
|
82
|
+
|
|
83
|
+
Uses quantum amplitude encoding to compress text tokens,
|
|
84
|
+
achieving up to 53% reduction in token count.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
name: str = "quantum_compress"
|
|
88
|
+
description: str = (
|
|
89
|
+
"Compress text using quantum amplitude encoding. "
|
|
90
|
+
"Useful for reducing context size while preserving information. "
|
|
91
|
+
"Achieves 53% token reduction on average."
|
|
92
|
+
)
|
|
93
|
+
args_schema: Type[BaseModel] = CompressInput if LANGCHAIN_AVAILABLE else None
|
|
94
|
+
|
|
95
|
+
compressor: Optional[QuantumCompressor] = None
|
|
96
|
+
|
|
97
|
+
def __init__(self, backend: str = "simulator", **kwargs):
|
|
98
|
+
_check_langchain()
|
|
99
|
+
super().__init__(**kwargs)
|
|
100
|
+
self.compressor = QuantumCompressor(backend=backend)
|
|
101
|
+
|
|
102
|
+
def _run(
|
|
103
|
+
self,
|
|
104
|
+
text: str,
|
|
105
|
+
compression_level: int = 1,
|
|
106
|
+
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
107
|
+
) -> str:
|
|
108
|
+
"""Compress text using quantum encoding."""
|
|
109
|
+
# Tokenize (simple word-based for demo)
|
|
110
|
+
words = text.split()
|
|
111
|
+
tokens = [hash(w) % 10000 for w in words]
|
|
112
|
+
|
|
113
|
+
if len(tokens) < 2:
|
|
114
|
+
return f"Text too short to compress. Original: {text}"
|
|
115
|
+
|
|
116
|
+
result = self.compressor.compress(
|
|
117
|
+
tokens=tokens,
|
|
118
|
+
compression_level=compression_level,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
f"Compressed {result.input_token_count} tokens to {result.n_qubits} qubits. "
|
|
123
|
+
f"Compression ratio: {result.compression_ratio:.2f}x "
|
|
124
|
+
f"({result.compression_percentage:.1f}% reduction). "
|
|
125
|
+
f"Tokens saved: {result.tokens_saved}"
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class QuantumGradientTool(BaseTool):
|
|
130
|
+
"""
|
|
131
|
+
Quantum Gradient Computation Tool.
|
|
132
|
+
|
|
133
|
+
Uses quantum teleportation protocol for backpropagation,
|
|
134
|
+
achieving 97.78% similarity with classical gradients.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
name: str = "quantum_gradient"
|
|
138
|
+
description: str = (
|
|
139
|
+
"Compute gradients using quantum teleportation protocol. "
|
|
140
|
+
"Useful for optimizing neural network weights with quantum speedup. "
|
|
141
|
+
"Achieves 97.78% similarity with classical gradients."
|
|
142
|
+
)
|
|
143
|
+
args_schema: Type[BaseModel] = GradientInput if LANGCHAIN_AVAILABLE else None
|
|
144
|
+
|
|
145
|
+
backprop: Optional[QuantumBackprop] = None
|
|
146
|
+
|
|
147
|
+
def __init__(self, backend: str = "simulator", **kwargs):
|
|
148
|
+
_check_langchain()
|
|
149
|
+
super().__init__(**kwargs)
|
|
150
|
+
self.backprop = QuantumBackprop(backend=backend)
|
|
151
|
+
|
|
152
|
+
def _run(
|
|
153
|
+
self,
|
|
154
|
+
input_values: List[float],
|
|
155
|
+
target_values: List[float],
|
|
156
|
+
weights: List[float],
|
|
157
|
+
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
158
|
+
) -> str:
|
|
159
|
+
"""Compute quantum gradients."""
|
|
160
|
+
result = self.backprop.compute_gradient(
|
|
161
|
+
input_state=np.array(input_values),
|
|
162
|
+
target_state=np.array(target_values),
|
|
163
|
+
weights=np.array(weights),
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
f"Gradients: {result.gradients.tolist()}. "
|
|
168
|
+
f"Direction: {result.gradient_direction}. "
|
|
169
|
+
f"Magnitude: {result.gradient_magnitude:.4f}. "
|
|
170
|
+
f"Classical similarity: {abs(result.similarity):.2%}"
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class QuantumMemoryTool(BaseTool):
|
|
175
|
+
"""
|
|
176
|
+
Quantum Memory Tool.
|
|
177
|
+
|
|
178
|
+
Store and retrieve data using quantum-compressed memory
|
|
179
|
+
with O(log n) complexity.
|
|
180
|
+
"""
|
|
181
|
+
|
|
182
|
+
name: str = "quantum_memory"
|
|
183
|
+
description: str = (
|
|
184
|
+
"Store, retrieve, or search data in quantum memory. "
|
|
185
|
+
"Uses O(log n) quantum memory vs O(n) classical. "
|
|
186
|
+
"Operations: 'store', 'retrieve', 'search'"
|
|
187
|
+
)
|
|
188
|
+
args_schema: Type[BaseModel] = MemoryInput if LANGCHAIN_AVAILABLE else None
|
|
189
|
+
|
|
190
|
+
memory: Optional[QuantumMemory] = None
|
|
191
|
+
|
|
192
|
+
def __init__(self, backend: str = "simulator", **kwargs):
|
|
193
|
+
_check_langchain()
|
|
194
|
+
super().__init__(**kwargs)
|
|
195
|
+
self.memory = QuantumMemory(backend=backend)
|
|
196
|
+
|
|
197
|
+
def _run(
|
|
198
|
+
self,
|
|
199
|
+
operation: str,
|
|
200
|
+
key: str,
|
|
201
|
+
values: Optional[List[float]] = None,
|
|
202
|
+
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
203
|
+
) -> str:
|
|
204
|
+
"""Execute quantum memory operation."""
|
|
205
|
+
if operation == "store":
|
|
206
|
+
if not values:
|
|
207
|
+
return "Error: values required for store operation"
|
|
208
|
+
slot = self.memory.store(key, values)
|
|
209
|
+
return (
|
|
210
|
+
f"Stored {len(values)} values under key '{key}'. "
|
|
211
|
+
f"Qubits used: {slot.compressed.n_qubits if slot.compressed else 'N/A'}. "
|
|
212
|
+
f"Compression: {slot.compressed.compression_ratio:.2f}x" if slot.compressed else ""
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
elif operation == "retrieve":
|
|
216
|
+
try:
|
|
217
|
+
values = self.memory.retrieve(key)
|
|
218
|
+
return f"Retrieved from '{key}': {values[:5]}..." if len(values) > 5 else f"Retrieved: {values}"
|
|
219
|
+
except KeyError:
|
|
220
|
+
return f"Key '{key}' not found in quantum memory"
|
|
221
|
+
|
|
222
|
+
elif operation == "search":
|
|
223
|
+
stats = self.memory.get_stats()
|
|
224
|
+
return f"Memory stats: {stats.total_items} items, {stats.compression_ratio:.2f}x compression"
|
|
225
|
+
|
|
226
|
+
return f"Unknown operation: {operation}"
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class QuantumEntangleTool(BaseTool):
|
|
230
|
+
"""
|
|
231
|
+
Quantum Entanglement Tool.
|
|
232
|
+
|
|
233
|
+
Create entangled states from multiple contexts
|
|
234
|
+
for parallel processing and correlation.
|
|
235
|
+
"""
|
|
236
|
+
|
|
237
|
+
name: str = "quantum_entangle"
|
|
238
|
+
description: str = (
|
|
239
|
+
"Create quantum entanglement between multiple contexts. "
|
|
240
|
+
"Useful for linking related information and parallel processing. "
|
|
241
|
+
"Creates Bell pairs (2 contexts) or GHZ states (3+ contexts)."
|
|
242
|
+
)
|
|
243
|
+
args_schema: Type[BaseModel] = EntangleInput if LANGCHAIN_AVAILABLE else None
|
|
244
|
+
|
|
245
|
+
entangler: Optional[Entangler] = None
|
|
246
|
+
|
|
247
|
+
def __init__(self, backend: str = "simulator", **kwargs):
|
|
248
|
+
_check_langchain()
|
|
249
|
+
super().__init__(**kwargs)
|
|
250
|
+
self.entangler = Entangler(backend=backend)
|
|
251
|
+
|
|
252
|
+
def _run(
|
|
253
|
+
self,
|
|
254
|
+
contexts: List[str],
|
|
255
|
+
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
256
|
+
) -> str:
|
|
257
|
+
"""Create entangled state from contexts."""
|
|
258
|
+
if len(contexts) < 2:
|
|
259
|
+
return "Error: Need at least 2 contexts to entangle"
|
|
260
|
+
|
|
261
|
+
# Convert string contexts to numeric representations
|
|
262
|
+
numeric_contexts = []
|
|
263
|
+
for ctx in contexts:
|
|
264
|
+
values = [ord(c) / 255.0 for c in ctx[:10]]
|
|
265
|
+
if len(values) < 2:
|
|
266
|
+
values = values + [0.5] * (2 - len(values))
|
|
267
|
+
numeric_contexts.append(values)
|
|
268
|
+
|
|
269
|
+
if len(contexts) == 2:
|
|
270
|
+
state = self.entangler.entangle_contexts(numeric_contexts[0], numeric_contexts[1])
|
|
271
|
+
else:
|
|
272
|
+
state = self.entangler.create_ghz_state(len(contexts))
|
|
273
|
+
|
|
274
|
+
return (
|
|
275
|
+
f"Created entangled state with {state.n_qubits} qubits, "
|
|
276
|
+
f"{state.n_parties} parties. "
|
|
277
|
+
f"Entropy: {state.entropy:.4f}. "
|
|
278
|
+
f"Maximally entangled: {state.is_maximally_entangled}"
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class QuantumSearchTool(BaseTool):
|
|
283
|
+
"""
|
|
284
|
+
Quantum Search Tool (Grover's Algorithm).
|
|
285
|
+
|
|
286
|
+
Search through unstructured data with quadratic speedup.
|
|
287
|
+
"""
|
|
288
|
+
|
|
289
|
+
name: str = "quantum_search"
|
|
290
|
+
description: str = (
|
|
291
|
+
"Search through a database using Grover's quantum algorithm. "
|
|
292
|
+
"Provides quadratic speedup: O(sqrt(N)) vs O(N) classical. "
|
|
293
|
+
"Best for unstructured search problems."
|
|
294
|
+
)
|
|
295
|
+
args_schema: Type[BaseModel] = SearchInput if LANGCHAIN_AVAILABLE else None
|
|
296
|
+
|
|
297
|
+
def __init__(self, backend: str = "simulator", **kwargs):
|
|
298
|
+
_check_langchain()
|
|
299
|
+
super().__init__(**kwargs)
|
|
300
|
+
|
|
301
|
+
def _run(
|
|
302
|
+
self,
|
|
303
|
+
query: str,
|
|
304
|
+
database: List[str],
|
|
305
|
+
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
306
|
+
) -> str:
|
|
307
|
+
"""Search database using Grover's algorithm."""
|
|
308
|
+
import math
|
|
309
|
+
from quantumflow.algorithms.optimization import GroverSearch
|
|
310
|
+
|
|
311
|
+
# Find matching indices
|
|
312
|
+
query_lower = query.lower()
|
|
313
|
+
marked_indices = [i for i, item in enumerate(database) if query_lower in item.lower()]
|
|
314
|
+
|
|
315
|
+
if not marked_indices:
|
|
316
|
+
return f"No matches found for '{query}' in database of {len(database)} items"
|
|
317
|
+
|
|
318
|
+
# Calculate n_qubits needed
|
|
319
|
+
n_qubits = max(2, math.ceil(math.log2(len(database))))
|
|
320
|
+
|
|
321
|
+
grover = GroverSearch(backend="simulator")
|
|
322
|
+
result = grover.search(n_qubits=n_qubits, marked_states=marked_indices)
|
|
323
|
+
|
|
324
|
+
matches = [database[i] for i in marked_indices]
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
f"Found {len(matches)} match(es) for '{query}'. "
|
|
328
|
+
f"Matches: {matches[:3]}{'...' if len(matches) > 3 else ''}. "
|
|
329
|
+
f"Quantum iterations: {result.iterations}. "
|
|
330
|
+
f"Success probability: {result.probability:.2%}"
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
# ============== Toolkit ==============
|
|
335
|
+
|
|
336
|
+
def get_quantum_toolkit(backend: str = "simulator") -> List[BaseTool]:
|
|
337
|
+
"""
|
|
338
|
+
Get all quantum tools for LangChain.
|
|
339
|
+
|
|
340
|
+
Args:
|
|
341
|
+
backend: Quantum backend to use ("simulator", "ibm")
|
|
342
|
+
|
|
343
|
+
Returns:
|
|
344
|
+
List of quantum tools for use with LangChain agents
|
|
345
|
+
|
|
346
|
+
Example:
|
|
347
|
+
from langchain.agents import initialize_agent, AgentType
|
|
348
|
+
from langchain.llms import OpenAI
|
|
349
|
+
from quantumflow.integrations import get_quantum_toolkit
|
|
350
|
+
|
|
351
|
+
tools = get_quantum_toolkit()
|
|
352
|
+
llm = OpenAI(temperature=0)
|
|
353
|
+
agent = initialize_agent(
|
|
354
|
+
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
|
355
|
+
)
|
|
356
|
+
agent.run("Compress this text using quantum encoding: ...")
|
|
357
|
+
"""
|
|
358
|
+
_check_langchain()
|
|
359
|
+
|
|
360
|
+
return [
|
|
361
|
+
QuantumCompressTool(backend=backend),
|
|
362
|
+
QuantumGradientTool(backend=backend),
|
|
363
|
+
QuantumMemoryTool(backend=backend),
|
|
364
|
+
QuantumEntangleTool(backend=backend),
|
|
365
|
+
QuantumSearchTool(backend=backend),
|
|
366
|
+
]
|