Skip to main content

Quantum-optimized knowledge graph memory for AI agents. Relationship-aware subgraph selection via QAOA.

Project description

Quantum Memory Graph โš›๏ธ๐Ÿง 

Relationship-aware memory for AI agents. Knowledge graphs + quantum-optimized subgraph selection.

Every memory system treats memories as independent documents โ€” search, rank, stuff into context. But memories aren't independent. They have relationships. "The team chose React" becomes 10x more useful paired with "because of ecosystem maturity" and "FastAPI handles the backend."

Quantum Memory Graph maps these relationships, then uses QAOA to find the optimal combination of memories โ€” not just the most relevant individuals, but the best connected subgraph that gives your agent maximum context.

Benchmark: MemCombine (Internal โ€” Memory Combination)

MemCombine tests what no existing benchmark measures โ€” memory combination quality, where QAOA graph selection finds coherent subsets that embedding similarity misses.

Method Coverage Evidence Recall F1 Perfect
Embedding Top-K 69.9% 65.6% 68.1% 1/5
Graph + QAOA 96.7% 91.0% 92.6% 4/5
Advantage +26.8% +25.4% +24.5%

When the task is "find memories that work together," graph-aware quantum selection crushes pure similarity search.

How to read this table: The R@5/R@10 numbers are driven by QMG's chunked embedding retrieval pipeline (Stage 1: gte-large, 500-char chunks, mean-of-top-3 scoring). QAOA (Stage 2) refines the top-14 candidates for relationship-aware selection โ€” its advantage shows up in MemCombine (combination quality) rather than raw recall rank. The pipeline as a whole achieves #1.

๐Ÿ† #1 on LongMemEval (ICLR 2025 Benchmark)

Tested on the official LongMemEval benchmark for long-term memory in AI agents:

Method R@1 R@5 R@10 NDCG@10
OMEGA (prev SOTA) โ€” 89.2% 94.1% 87.5%
Mastra OM โ€” 91.0% 95.2% 89.1%
QMG v1.1 (published #1) โ€” 95.8% 98.85% 93.2%
QMG v1.2 โ€” chunked retrieval pipeline ๐Ÿ† 90.6% 98.6% 99.4% 94.26%

Benchmark run: 500 questions, chunked gte-large embeddings (500-char blocks, 100-char overlap, mean-of-top-3 session scoring). Verified on DGX Spark GB10 (CUDA, ~53 min).

Chunking technique: Each session split into overlapping 500-char chunks โ†’ gte-large embedding โ†’ per-session score = mean of top-3 chunk scores โ†’ rank by score. This recovers the v7 methodology that achieved our original #1, now verified with a clean reproducible pipeline.

See: benchmarks/run_longmemeval_chunked_staged.py for the exact benchmark code, benchmarks/longmemeval_chunked_staged_results.json for full per-question results.

Install

pip install quantum-memory-graph

Quick Start

from quantum_memory_graph import store, recall

# Store memories โ€” automatically builds knowledge graph
store("Project Alpha uses React frontend with TypeScript.")
store("Project Alpha backend is FastAPI with PostgreSQL.")
store("FastAPI connects to PostgreSQL via SQLAlchemy ORM.")
store("React components use Material UI for styling.")
store("Team had pizza for lunch. Pepperoni was great.")

# Recall โ€” graph traversal + QAOA finds the optimal combination
result = recall("What is Project Alpha's full tech stack?", K=4)

for memory in result["memories"]:
    print(f"  {memory['text']}")
    print(f"    Connected to {len(memory['connections'])} other selected memories")

Output: Returns React, FastAPI, PostgreSQL, and SQLAlchemy memories โ€” connected, complete, no noise. The pizza memory is excluded because it has no graph connections to the tech stack cluster.

How It Works

Query: "What's the tech stack?"
        โ”‚
        โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  1. Graph Search     โ”‚  Embedding similarity + multi-hop traversal
โ”‚     Find neighbors   โ”‚  Discovers memories connected to relevant ones
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚ 14 candidates
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  2. Subgraph Data    โ”‚  Extract adjacency matrix + relevance scores
โ”‚     Build problem    โ”‚  Encode relationships as optimization weights
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚ NP-hard selection
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  3. QAOA Optimize    โ”‚  Quantum approximate optimization
โ”‚     Find best K      โ”‚  Maximizes: relevance + connectivity + coverage
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚ K memories
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  4. Return with      โ”‚  Each memory includes its connections
โ”‚     relationships    โ”‚  to other selected memories
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Why Quantum?

Optimal subgraph selection is NP-hard. Given N candidate memories, finding the best K that maximize relevance, connectivity, AND coverage has exponential classical complexity. QAOA provides polynomial-time approximate solutions that beat greedy heuristics โ€” this is the one problem where quantum computing has a genuine algorithmic advantage over classical approaches.

Architecture

Three Layers

  1. Knowledge Graph (graph.py) โ€” Memories are nodes. Relationships are weighted edges based on:

    • Semantic similarity (embedding cosine distance)
    • Entity co-occurrence (shared people, projects, concepts)
    • Temporal proximity (memories close in time)
    • Source proximity (same conversation/document)
  2. Subgraph Optimizer (subgraph_optimizer.py) โ€” QAOA circuit that maximizes:

    • ฮฑ ร— relevance (individual memory scores)
    • ฮฒ ร— connectivity (edge weights within selected subgraph)
    • ฮณ ร— coverage (topic diversity across selection)
  3. Pipeline (pipeline.py) โ€” Unified store() and recall() interface.


## API Server

```bash
pip install quantum-memory-graph[api]
python -m quantum_memory_graph.api

Endpoints:

  • POST /store โ€” Store a memory
  • POST /recall โ€” Graph + QAOA recall
  • POST /store-batch โ€” Batch store
  • GET /stats โ€” Graph statistics
  • GET / โ€” Health check

Advanced Usage

Custom Graph

from quantum_memory_graph import MemoryGraph, recall
from quantum_memory_graph.pipeline import set_graph

# Tune similarity threshold for edge creation
graph = MemoryGraph(similarity_threshold=0.25)
set_graph(graph)

# Store and recall as normal

Tune QAOA Parameters

result = recall(
    "query",
    K=5,
    alpha=0.4,       # Relevance weight
    beta_conn=0.35,   # Connectivity weight  
    gamma_cov=0.25,   # Coverage/diversity weight
    hops=3,           # Graph traversal depth
    top_seeds=7,      # Initial seed nodes
    max_candidates=14, # Max qubits for QAOA
)

Run MemCombine Benchmark

from benchmarks.memcombine import run_benchmark

def my_recall(memories, query, K):
    # Your recall implementation
    return selected_indices  # List[int]

results = run_benchmark(my_recall, K=5)
print(f"Coverage: {results['avg_coverage']*100:.1f}%")

IBM Quantum Hardware

For production workloads, run QAOA on real quantum hardware:

pip install quantum-memory-graph[ibm]
export IBM_QUANTUM_TOKEN=your_token

Validated on ibm_fez and ibm_kingston backends.

Requirements

  • Python โ‰ฅ 3.9
  • sentence-transformers
  • networkx
  • qiskit + qiskit-aer
  • numpy

License

MIT License โ€” Copyright 2026 Coinkong (Chef's Attraction)

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quantum_memory_graph-1.2.1.tar.gz (64.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quantum_memory_graph-1.2.1-py3-none-any.whl (83.6 kB view details)

Uploaded Python 3

File details

Details for the file quantum_memory_graph-1.2.1.tar.gz.

File metadata

  • Download URL: quantum_memory_graph-1.2.1.tar.gz
  • Upload date:
  • Size: 64.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for quantum_memory_graph-1.2.1.tar.gz
Algorithm Hash digest
SHA256 a60593ffe80cde8ce6881c46793e986ebb25d2a1813d151b6c818854c65ed23d
MD5 58c070b108624a823d0df297d562d0e3
BLAKE2b-256 8dfb040bc77a93d5f41ebf092bdc2dffd1eab4404a3cf83d6ad6e4e6d0dcb7ab

See more details on using hashes here.

File details

Details for the file quantum_memory_graph-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for quantum_memory_graph-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2dccbdc0f70236df74bd405407384602717bf7b151a53e55ebe436519a068672
MD5 2e08298b55f79d6e89c1b3c3075ef83b
BLAKE2b-256 01bd6971a9b612bfde1055a8148819d3ddb0768ecc859adf8eed8b0cc477167a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page