QAOA-powered memory management for AI agents — clustering, compaction, and recall
Project description
Quantum Agent Memory
QAOA-powered memory management for AI agents. Uses the Quantum Approximate Optimization Algorithm to solve three fundamental memory operations — clustering, compaction, and recall — as combinatorial optimization problems on quantum circuits. Runs on the Qiskit Aer simulator out of the box; optionally submits to IBM Quantum hardware.
What It Does
| Layer | Problem | Formulation |
|---|---|---|
| 1. Clustering | Group memories into semantically coherent clusters | Balanced graph cut via QAOA — 4-dimension cost matrix (temporal, relational, categorical, recency) |
| 2. Compaction | Select optimal K memories to keep from M total | Subset selection with budget constraint penalty — maximizes coverage, coherence, value, and recency |
| 3. Recall | Find the best combination of K memories for a query | Context-aware selection — individual relevance + pairwise synergy + diversity + recency |
Each layer compares QAOA against classical baselines (brute-force, greedy, top-K) and reports approximation ratios.
Requirements
- Python 3.10+
- Qiskit 2.0+, Qiskit Aer 0.15+, NumPy
Quick Start
git clone https://github.com/Dustin-a11y/quantum-agent-memory.git
cd quantum-agent-memory
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
python -m quantum_agent_memory benchmark
Expected Output
╔══════════════════════════════════════════════════╗
║ QUANTUM AGENT MEMORY — BENCHMARK REPORT ║
╠══════════════════════════════════════════════════╣
║ Layer 1: Clustering ║
║ n= 8: Quantum ~100% optimal [0.9s] ║
║ n=10: Quantum ~95-100% optimal [1.8s] ║
║ n=12: Quantum ~92-100% optimal [2.5s] ║
║ Layer 2: Compaction ║
║ M=10,K=5: Quantum ~100% optimal ║
║ M=12,K=6: Quantum ~100% optimal ║
║ Layer 3: Recall ║
║ Query 1: Quantum ~100% | Top-K ~95% ║
║ Query 2: Quantum ~99% | Top-K ~93% ║
║ Summary: Avg accuracy ~98%, 3 layers tested ║
╚══════════════════════════════════════════════════╝
Results are also saved as JSON to results/benchmark_TIMESTAMP.json.
Optional: Live Mem0 Integration
If you have a Mem0 instance running, point the benchmark at it:
pip install requests
python -m quantum_agent_memory benchmark --mem0-url http://localhost:8500
This fetches real agent memories instead of using the bundled demo data.
Optional: IBM Quantum Hardware
To run on real quantum processors (free tier: 10 min/month):
- Sign up at quantum.ibm.com
- Copy your API token from Account Settings
- Run:
pip install qiskit-ibm-runtime
python -m quantum_agent_memory submit --ibm-token YOUR_TOKEN
This transpiles and submits all 3 circuit types to the least-busy IBM backend.
Project Structure
quantum_agent_memory/
├── __init__.py # Package metadata
├── __main__.py # CLI entry point
├── cost_function.py # 4-dimension cost matrix builder
├── clustering.py # Layer 1: QAOA balanced graph-cut clustering
├── compaction.py # Layer 2: QAOA subset selection with budget penalty
├── recall.py # Layer 3: QAOA context-aware memory retrieval
├── benchmark.py # Full 3-layer benchmark runner
└── demo_data.py # 20 synthetic test memories
Use in Any Agent Framework
The quantum memory system is a standalone Python library — no OpenClaw dependency. Call it from any agent, any framework:
from quantum_agent_memory.recall import run_recall
from quantum_agent_memory.compaction import run_compaction
from quantum_agent_memory.clustering import run_clustering
# Your memories — any list of dicts with a "memory" key
memories = [
{"memory": "User prefers dark mode", "agent_id": "assistant"},
{"memory": "Project deadline is Friday", "agent_id": "planner"},
{"memory": "API key rotated on March 1", "agent_id": "security"},
# ... more memories
]
# Quantum recall — find best K memories for a query
result = run_recall(memories, query="What's the project status?", K=3)
print(result["selected"]) # Optimal memory combination via QAOA
# Quantum compaction — pick K best memories to keep
result = run_compaction(memories, K=5)
print(result["keep"]) # Memories to retain
print(result["archive"]) # Memories safe to archive
# Quantum clustering — group related memories
result = run_clustering(memories)
print(result["clusters"]) # Grouped by semantic similarity
REST API
Or run the API server and call it over HTTP from any language:
python scripts/quantum_api.py
curl -X POST http://localhost:8501/quantum-recall \
-H "Content-Type: application/json" \
-d '{"query": "project status", "k": 3}'
Works with LangChain, CrewAI, AutoGen, OpenClaw, or any agent framework that can make HTTP calls or import Python.
How It Works
Cost Function (Shared)
All three layers build on a 4-dimension pairwise cost matrix:
- Temporal coherence (25%) — memories close in time cluster together
- Relational coherence (30%) — shared entities/words (Jaccard similarity)
- Categorical coherence (25%) — same agent, source, or batch
- Recency weighting (20%) — newer memories prioritized
QAOA Encoding
- Each memory maps to one qubit
- Cost terms encode as ZZ interactions in the problem Hamiltonian
- Budget/balance constraints use quadratic penalty terms
- Grid search over (γ, β) parameters; p=1 QAOA depth
- Measured bitstrings decoded as memory assignments
License
MIT — Copyright 2026 Coinkong (Chef's Attraction)
Running on Real Quantum Hardware
By default, the benchmark runs on the Qiskit Aer simulator (free, local, perfect results). To run on real IBM quantum processors:
Setup (one time)
- Sign up at quantum.ibm.com (free — 10 min QPU/month)
- Copy your API token from Account Settings
- Save it:
export IBM_QUANTUM_TOKEN="your-token-here"
Submit to real hardware
python -m quantum_agent_memory submit --ibm-token $IBM_QUANTUM_TOKEN
Scheduled hardware runs
For ongoing real-hardware data collection, set up a cron job:
# Run 10x daily on real IBM quantum hardware
crontab -e
# Add: 0 0,2,5,7,10,12,14,17,19,22 * * * python /path/to/quantum_agent_memory/ibm_cron.py
QPU Budget
- Free tier: 10 minutes/month
- Each run uses ~1 second of QPU time
- 10 runs/day = ~5 minutes/month (50% of free budget)
- Plenty of room for experimentation
Why real hardware matters
- Simulator gives perfect results; real hardware has noise
- Real hardware data validates your quantum advantage claims
- IBM job IDs are timestamped proof of quantum computing usage
- Accumulating real-hardware results builds your quantum portfolio
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quantum_agent_memory-0.1.0.tar.gz.
File metadata
- Download URL: quantum_agent_memory-0.1.0.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e89a30fb4d8ee05e389933018fa389a03ed88f8591d6f2c23c8002d1892337a
|
|
| MD5 |
689cf2ea03c9921c970036dc3dc3fb14
|
|
| BLAKE2b-256 |
fe3500470ba6ff77d5096d36538d2e305e2c2c9384440c81b68fddbdce0ef346
|
File details
Details for the file quantum_agent_memory-0.1.0-py3-none-any.whl.
File metadata
- Download URL: quantum_agent_memory-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b716a3aa53e4d2167b5c7796908519d3e87b2bb2052de7bb09cdce4d17fb5d6
|
|
| MD5 |
51e0cd9fe6efcb687c48a2ed06d00db7
|
|
| BLAKE2b-256 |
38597dcbc27f4b8d874974352f6a66d9cf17345141ab505a19c1297f02b9f7c6
|