Skip to content

User Guide

Complete user guide for building quantum-classical hybrid AI applications with QuantumLangChain.

Table of Contents

  1. Basic Concepts
  2. Building Your First Quantum Chain
  3. Working with Quantum Memory
  4. Multi-Agent Collaboration
  5. Advanced Vector Search
  6. Tool Integration
  7. Context Management
  8. Prompt Engineering
  9. Performance Optimization
  10. Troubleshooting

Basic Concepts

Quantum-Classical Hybrid Computing

QuantumLangChain combines classical AI techniques with quantum computing principles:

  • Superposition: Execute multiple reasoning paths simultaneously
  • Entanglement: Create correlated states between components
  • Interference: Amplify correct solutions and suppress incorrect ones
  • Measurement: Collapse quantum states to classical results

Core Architecture

from quantumlangchain import QuantumLangChain, QuantumConfig

# Initialize with quantum configuration
config = QuantumConfig(
    num_qubits=8,
    backend_type="qiskit",
    decoherence_threshold=0.1
)

qlc = QuantumLangChain(config=config)

Quantum States

Components can exist in various quantum states:

  • Coherent: Well-defined quantum state
  • Superposition: Multiple states simultaneously
  • Entangled: Correlated with other components
  • Decoherent: Quantum properties degraded

Building Your First Quantum Chain

Simple Query Processing

import asyncio
from quantumlangchain import QLChain, QuantumMemory
from quantumlangchain.backends import QiskitBackend

async def basic_chain_example():
    # Setup components
    backend = QiskitBackend()
    memory = QuantumMemory(
        classical_dim=768,
        quantum_dim=4,
        backend=backend
    )

    # Create quantum chain
    chain = QLChain(
        memory=memory,
        backend=backend,
        config={
            'enable_superposition': True,
            'max_parallel_branches': 4,
            'coherence_threshold': 0.8
        }
    )

    # Execute with quantum enhancement
    result = await chain.arun(
        "What are the implications of quantum computing for AI?",
        quantum_enhanced=True,
        parallel_branches=3
    )

    print(f"Answer: {result['output']}")
    print(f"Quantum coherence: {result['quantum_coherence']}")
    print(f"Execution branches: {result['execution_branches']}")

# Run the example
asyncio.run(basic_chain_example())

Chain Configuration Options

# Advanced chain configuration
chain_config = {
    'enable_superposition': True,
    'max_parallel_branches': 8,
    'coherence_threshold': 0.8,
    'decoherence_mitigation': True,
    'quantum_error_correction': True,
    'optimization_level': 2,
    'shots': 2048,
    'reasoning_depth': 5,
    'entanglement_enabled': True
}

chain = QLChain(memory=memory, backend=backend, config=chain_config)

Working with Quantum Memory

Memory Storage and Retrieval

async def memory_example():
    memory = QuantumMemory(classical_dim=768, quantum_dim=6)

    # Store information with quantum enhancement
    await memory.store(
        "quantum_computing_basics",
        "Quantum computing uses qubits that can exist in superposition...",
        quantum_enhanced=True
    )

    # Create entangled memories
    await memory.store("ai_applications", "AI applications include...", quantum_enhanced=True)
    await memory.store("quantum_ai", "Quantum AI combines...", quantum_enhanced=True)

    entanglement_id = await memory.entangle_memories([
        "quantum_computing_basics",
        "ai_applications", 
        "quantum_ai"
    ])

    # Quantum similarity search
    results = await memory.similarity_search(
        "How does quantum computing enhance AI?",
        top_k=3
    )

    for result in results:
        print(f"Memory: {result['key']}")
        print(f"Similarity: {result['similarity']:.3f}")
        print(f"Content: {result['content'][:100]}...")
        print()

asyncio.run(memory_example())

Memory Snapshots and Reversibility

async def memory_snapshot_example():
    memory = QuantumMemory(classical_dim=512, quantum_dim=4)

    # Store initial state
    await memory.store("fact1", "The sky is blue")
    await memory.store("fact2", "Water boils at 100°C")

    # Create snapshot
    snapshot_id = await memory.create_memory_snapshot()
    print(f"Created snapshot: {snapshot_id}")

    # Modify memory
    await memory.store("fact1", "The sky is green")  # Incorrect update
    await memory.store("fact3", "Grass is purple")   # Add incorrect fact

    # Verify current state
    fact1 = await memory.retrieve("fact1")
    print(f"Current fact1: {fact1}")  # Will show "The sky is green"

    # Restore from snapshot
    success = await memory.restore_memory_snapshot(snapshot_id)
    if success:
        print("Memory restored successfully")

        # Verify restored state
        fact1 = await memory.retrieve("fact1")
        print(f"Restored fact1: {fact1}")  # Will show "The sky is blue"

        fact3 = await memory.retrieve("fact3")
        print(f"Fact3 exists: {fact3 is not None}")  # Will be False

asyncio.run(memory_snapshot_example())

Multi-Agent Collaboration

Setting Up Entangled Agents

from quantumlangchain import EntangledAgents, AgentRole
from quantumlangchain.backends import QiskitBackend

async def multi_agent_example():
    backend = QiskitBackend()

    # Define agent roles
    agent_configs = [
        {
            'agent_id': 'researcher',
            'role': AgentRole.RESEARCHER,
            'capabilities': ['web_search', 'document_analysis', 'fact_checking'],
            'quantum_weight': 1.0,
            'specialization': 'scientific_research'
        },
        {
            'agent_id': 'analyst',
            'role': AgentRole.ANALYST,
            'capabilities': ['data_analysis', 'pattern_recognition', 'synthesis'],
            'quantum_weight': 0.9,
            'specialization': 'data_interpretation'
        },
        {
            'agent_id': 'creative',
            'role': AgentRole.CREATIVE,
            'capabilities': ['ideation', 'storytelling', 'creative_synthesis'],
            'quantum_weight': 0.8,
            'specialization': 'creative_solutions'
        }
    ]

    # Initialize entangled agent system
    agents = EntangledAgents(agent_configs=agent_configs, backend=backend)

    # Collaborative problem solving
    problem = """
    Design an innovative solution for reducing plastic waste in oceans
    while considering economic feasibility and environmental impact.
    """

    result = await agents.collaborative_solve(
        problem=problem,
        max_iterations=5,
        enable_interference=True
    )

    print("Collaborative Solution:")
    print(f"Final solution: {result['solution']}")
    print(f"Confidence: {result['confidence']:.3f}")
    print(f"Agent contributions:")

    for agent_id, contribution in result['contributions'].items():
        print(f"  {agent_id}: {contribution['summary']}")
        print(f"    Confidence: {contribution['confidence']:.3f}")
        print(f"    Quantum coherence: {contribution['quantum_coherence']:.3f}")

asyncio.run(multi_agent_example())

Parallel Agent Execution

async def parallel_agent_example():
    agents = EntangledAgents(agent_configs=[...])  # Same config as above

    problem = "Analyze the potential impact of quantum computing on cybersecurity"

    # Run agents in parallel
    results = await agents.run_parallel_agents(
        agent_ids=['researcher', 'analyst', 'creative'],
        problem=problem
    )

    # Compare individual results
    for result in results:
        print(f"\nAgent: {result['agent_id']}")
        print(f"Approach: {result['approach']}")
        print(f"Key insights: {result['insights']}")
        print(f"Execution time: {result['execution_time']:.2f}s")
        print(f"Quantum enhancement: {result['quantum_enhanced']}")

asyncio.run(parallel_agent_example())

Quantum-Enhanced ChromaDB

from quantumlangchain.vectorstores import HybridChromaDB
import asyncio

async def vector_search_example():
    # Initialize quantum-enhanced ChromaDB
    vectorstore = HybridChromaDB(
        collection_name="quantum_research",
        persist_directory="./chroma_db",
        config={
            'quantum_enhanced': True,
            'entanglement_enabled': True,
            'coherence_threshold': 0.7
        }
    )

    # Add documents with quantum enhancement
    documents = [
        "Quantum computing leverages quantum mechanical phenomena...",
        "Artificial intelligence encompasses machine learning algorithms...",
        "Quantum machine learning combines quantum computing with AI...",
        "Neural networks are computational models inspired by biological neurons...",
        "Quantum entanglement allows for instantaneous correlations..."
    ]

    metadatas = [
        {'topic': 'quantum_computing', 'difficulty': 'advanced'},
        {'topic': 'ai', 'difficulty': 'intermediate'},
        {'topic': 'quantum_ai', 'difficulty': 'expert'},
        {'topic': 'neural_networks', 'difficulty': 'intermediate'},
        {'topic': 'quantum_physics', 'difficulty': 'advanced'}
    ]

    doc_ids = await vectorstore.add_documents(
        documents=documents,
        metadatas=metadatas,
        quantum_enhanced=True
    )

    # Create document entanglements
    entanglement_id = await vectorstore.entangle_documents(
        doc_ids=doc_ids[:3],  # Entangle first 3 documents
        entanglement_strength=0.9
    )

    # Quantum similarity search
    results = await vectorstore.quantum_similarity_search(
        query="How does quantum computing enhance machine learning?",
        k=3,
        quantum_algorithm="amplitude_amplification"
    )

    print("Quantum Search Results:")
    for doc, score in results:
        print(f"Score: {score:.3f}")
        print(f"Content: {doc.page_content[:100]}...")
        print(f"Metadata: {doc.metadata}")
        print(f"Quantum coherence: {doc.quantum_coherence:.3f}")
        print()

asyncio.run(vector_search_example())

FAISS with Quantum Algorithms

from quantumlangchain.vectorstores import QuantumFAISS
import numpy as np

async def faiss_quantum_example():
    # Initialize quantum FAISS
    vectorstore = QuantumFAISS(
        dimension=768,
        index_type="IVFFlat",
        metric="L2",
        nlist=100,
        config={
            'quantum_enhancement': True,
            'grovers_enabled': True,
            'amplitude_amplification': True
        }
    )

    # Generate sample vectors
    vectors = np.random.rand(1000, 768).astype(np.float32)
    ids = [f"doc_{i}" for i in range(1000)]
    metadatas = [{'category': f'cat_{i%10}', 'importance': np.random.rand()} 
                 for i in range(1000)]

    # Add vectors with quantum enhancement
    await vectorstore.add_vectors(
        vectors=vectors,
        ids=ids,
        metadatas=metadatas,
        quantum_enhanced=True
    )

    # Define target condition for amplitude amplification
    def high_importance_condition(metadata):
        return metadata.get('importance', 0) > 0.8

    # Amplitude amplification search
    query_vector = np.random.rand(768).astype(np.float32)
    results = await vectorstore.amplitude_amplification_search(
        query_vector=query_vector,
        target_condition=high_importance_condition,
        k=5,
        iterations=3
    )

    print("Amplitude Amplification Results:")
    for vector_id, score, metadata in results:
        print(f"ID: {vector_id}, Score: {score:.3f}, Importance: {metadata['importance']:.3f}")

asyncio.run(faiss_quantum_example())

Tool Integration

Quantum Tool Execution

from quantumlangchain.tools import QuantumToolExecutor
import requests
import json

async def tool_integration_example():
    executor = QuantumToolExecutor()

    # Define tools
    def web_search(query: str) -> dict:
        # Simulate web search
        return {
            'results': [
                {'title': f'Result for {query}', 'url': 'https://example.com', 'snippet': '...'},
                {'title': f'Another result for {query}', 'url': 'https://example2.com', 'snippet': '...'}
            ]
        }

    def analyze_sentiment(text: str) -> dict:
        # Simulate sentiment analysis
        return {
            'sentiment': 'positive',
            'confidence': 0.85,
            'emotions': ['joy', 'excitement']
        }

    def summarize_text(text: str, max_length: int = 100) -> dict:
        # Simulate text summarization
        return {
            'summary': f"Summary of: {text[:50]}...",
            'original_length': len(text),
            'summary_length': max_length
        }

    # Register tools
    executor.register_tool(
        name="web_search",
        function=web_search,
        description="Search the web for information",
        quantum_enhanced=True,
        parallel_execution=True
    )

    executor.register_tool(
        name="sentiment_analysis",
        function=analyze_sentiment,
        description="Analyze sentiment of text",
        quantum_enhanced=True,
        entanglement_enabled=True
    )

    executor.register_tool(
        name="text_summarization",
        function=summarize_text,
        description="Summarize long text",
        quantum_enhanced=False
    )

    # Execute tools in quantum superposition
    tool_configs = [
        {'name': 'web_search', 'args': ['quantum computing AI']},
        {'name': 'sentiment_analysis', 'args': ['Quantum computing is revolutionary!']},
        {'name': 'text_summarization', 'args': ['This is a long text that needs summarization...']}
    ]

    # Superposition execution
    def measurement_function(results):
        # Measure based on result quality
        scores = []
        for result in results:
            if result.success:
                confidence = result.result.get('confidence', 0.5)
                scores.append(confidence)
            else:
                scores.append(0.0)
        return max(scores)

    result = await executor.execute_quantum_superposition_tools(
        tool_configs=tool_configs,
        measurement_function=measurement_function
    )

    print(f"Selected tool: {result.tool_name}")
    print(f"Result: {result.result}")
    print(f"Quantum enhanced: {result.quantum_enhanced}")

asyncio.run(tool_integration_example())

Tool Chaining

async def tool_chain_example():
    executor = QuantumToolExecutor()

    # Register tools (same as above)
    # ...

    # Create tool chain
    executor.create_tool_chain(
        chain_name="research_and_analyze",
        tool_names=["web_search", "sentiment_analysis", "text_summarization"]
    )

    # Execute chain with result propagation
    results = await executor.execute_tool_chain(
        chain_name="research_and_analyze",
        initial_input="quantum computing breakthroughs",
        propagate_results=True
    )

    print("Tool Chain Results:")
    for i, result in enumerate(results):
        print(f"Step {i+1}: {result.tool_name}")
        print(f"  Success: {result.success}")
        print(f"  Time: {result.execution_time:.3f}s")
        print(f"  Result: {str(result.result)[:100]}...")
        print()

asyncio.run(tool_chain_example())

Context Management

Quantum Context Windows

from quantumlangchain.context import QuantumContextManager, ContextScope

async def context_management_example():
    context_manager = QuantumContextManager()

    # Create context windows
    conversation_window = context_manager.create_context_window(
        window_id="main_conversation",
        max_size=50,
        coherence_threshold=0.8
    )

    research_window = context_manager.create_context_window(
        window_id="research_context",
        max_size=20,
        coherence_threshold=0.9
    )

    # Set context at different scopes
    await context_manager.set_context(
        scope=ContextScope.GLOBAL,
        key="user_preferences",
        value={"language": "en", "detail_level": "high"},
        quantum_enhanced=False
    )

    await context_manager.set_context(
        scope=ContextScope.SESSION,
        key="current_topic",
        value="quantum computing",
        quantum_enhanced=True,
        window_id="main_conversation"
    )

    await context_manager.set_context(
        scope=ContextScope.CONVERSATION,
        key="research_focus",
        value="quantum algorithms",
        quantum_enhanced=True,
        window_id="research_context"
    )

    # Create context entanglement
    entanglement_id = await context_manager.entangle_contexts(
        context_keys=[
            (ContextScope.SESSION, "current_topic"),
            (ContextScope.CONVERSATION, "research_focus")
        ],
        entanglement_strength=0.9
    )

    # Create snapshot
    snapshot_id = await context_manager.create_snapshot(
        scope=ContextScope.SESSION,
        include_windows=True
    )

    print(f"Created context snapshot: {snapshot_id}")
    print(f"Context entanglement: {entanglement_id}")

    # Retrieve context with quantum search
    current_topic = await context_manager.get_context(
        scope=ContextScope.SESSION,
        key="current_topic",
        quantum_search=True
    )

    print(f"Current topic: {current_topic}")

asyncio.run(context_management_example())

Prompt Engineering

Quantum Prompt Chains

from quantumlangchain.prompts import QPromptChain, PromptType

async def prompt_engineering_example():
    prompt_chain = QPromptChain()

    # Add prompts with different quantum weights
    system_prompt_id = prompt_chain.add_prompt(
        content="You are an expert quantum computing assistant. Provide accurate, detailed information.",
        prompt_type=PromptType.SYSTEM,
        quantum_weight=1.0,
        metadata={'priority': 'high', 'domain': 'quantum_computing'}
    )

    research_prompt_id = prompt_chain.add_prompt(
        content="Analyze the following quantum computing concept from a research perspective: {concept}",
        prompt_type=PromptType.USER,
        quantum_weight=0.9,
        conditions={'mode': 'research'},
        metadata={'style': 'academic', 'depth': 'detailed'}
    )

    practical_prompt_id = prompt_chain.add_prompt(
        content="Explain how {concept} can be applied in practical quantum computing applications.",
        prompt_type=PromptType.USER,
        quantum_weight=0.8,
        conditions={'mode': 'practical'},
        metadata={'style': 'practical', 'audience': 'developers'}
    )

    creative_prompt_id = prompt_chain.add_prompt(
        content="Imagine innovative ways {concept} could revolutionize quantum computing in the future.",
        prompt_type=PromptType.USER,
        quantum_weight=0.7,
        conditions={'mode': 'creative'},
        metadata={'style': 'creative', 'perspective': 'futuristic'}
    )

    # Create superposition group for dynamic selection
    prompt_chain.create_superposition_group(
        group_name="response_styles",
        prompt_ids=[research_prompt_id, practical_prompt_id, creative_prompt_id],
        selection_method="quantum_interference"
    )

    # Entangle related prompts
    entanglement_id = prompt_chain.entangle_prompts(
        prompt_ids=[research_prompt_id, practical_prompt_id],
        entanglement_strength=0.8
    )

    # Create and execute prompt chain
    prompt_chain.create_prompt_chain(
        chain_name="quantum_explanation",
        prompt_ids=[system_prompt_id, "response_styles"],  # Can reference groups
        allow_quantum_selection=True
    )

    # Execute with context
    context = {
        'mode': 'research',  # This will influence quantum selection
        'user_expertise': 'intermediate',
        'preferred_depth': 'detailed'
    }

    variables = {
        'concept': 'quantum entanglement'
    }

    result = await prompt_chain.execute_prompt_chain(
        chain_name="quantum_explanation",
        context=context,
        variables=variables
    )

    print("Prompt Chain Result:")
    print(f"Selected prompts: {result.selected_prompts}")
    print(f"Final prompt: {result.final_prompt}")
    print(f"Quantum coherence: {result.quantum_coherence:.3f}")
    print(f"Selection confidence: {result.selection_confidence:.3f}")

asyncio.run(prompt_engineering_example())

Performance Optimization

Quantum Circuit Optimization

from quantumlangchain import QuantumConfig
from quantumlangchain.backends import QiskitBackend

async def optimization_example():
    # Optimized configuration for performance
    optimized_config = QuantumConfig(
        num_qubits=6,  # Balanced for complexity vs. noise
        circuit_depth=8,  # Reduced depth for lower decoherence
        decoherence_threshold=0.05,  # Strict coherence requirements
        backend_type="qiskit",
        shots=4096,  # Higher shots for better statistics
        optimization_level=3,  # Maximum optimization
        enable_error_correction=True
    )

    backend = QiskitBackend(config=optimized_config)

    # Monitor performance
    start_time = time.time()

    # Execute quantum operations
    circuit = await backend.create_entangling_circuit([0, 1, 2, 3])
    result = await backend.execute_circuit(circuit, shots=4096)

    execution_time = time.time() - start_time

    print(f"Execution time: {execution_time:.3f}s")
    print(f"Circuit depth: {circuit.depth()}")
    print(f"Gate count: {circuit.size()}")
    print(f"Quantum volume: {backend.get_quantum_volume()}")

asyncio.run(optimization_example())

Memory and Resource Management

async def resource_management_example():
    # Configure memory limits
    memory_config = {
        'max_classical_memory': 1024 * 1024 * 100,  # 100 MB
        'max_quantum_registers': 8,
        'cleanup_threshold': 0.8,
        'auto_snapshot_interval': 300,  # 5 minutes
        'decoherence_cleanup': True
    }

    memory = QuantumMemory(
        classical_dim=512,  # Reduced dimension for memory efficiency
        quantum_dim=4,
        backend=backend,
        config=memory_config
    )

    # Monitor memory usage
    stats = await memory.get_stats()
    print(f"Memory usage: {stats['memory_usage']:.2f}%")
    print(f"Quantum coherence: {stats['avg_coherence']:.3f}")
    print(f"Active entanglements: {stats['active_entanglements']}")

asyncio.run(resource_management_example())

Troubleshooting

Common Issues and Solutions

Decoherence Problems

# Monitor and handle decoherence
async def handle_decoherence():
    chain = QLChain(memory=memory, backend=backend)

    try:
        result = await chain.arun("Complex quantum query", quantum_enhanced=True)

        if result['quantum_coherence'] < 0.5:
            print("Warning: Low quantum coherence detected")

            # Reset quantum state
            await chain.reset_quantum_state()

            # Retry with reduced complexity
            result = await chain.arun(
                "Complex quantum query",
                quantum_enhanced=True,
                max_parallel_branches=2,  # Reduced complexity
                coherence_threshold=0.3   # Lower threshold
            )

    except QuantumDecoherenceError as e:
        print(f"Decoherence error: {e}")
        # Fallback to classical processing
        result = await chain.arun("Complex quantum query", quantum_enhanced=False)

Backend Connection Issues

# Handle backend failures gracefully
async def handle_backend_issues():
    try:
        backend = QiskitBackend()
        await backend.initialize()

    except QuantumBackendError as e:
        print(f"Primary backend failed: {e}")

        # Try alternative backend
        try:
            backend = PennyLaneBackend()
            await backend.initialize()
            print("Switched to PennyLane backend")

        except QuantumBackendError:
            print("All quantum backends failed, using classical fallback")
            backend = None

Memory Management

# Handle memory pressure
async def memory_cleanup():
    memory = QuantumMemory(classical_dim=768, quantum_dim=6)

    # Check memory stats
    stats = await memory.get_stats()

    if stats['memory_usage'] > 80:
        print("High memory usage detected, performing cleanup")

        # Clean up decoherent entries
        await memory.cleanup_decoherent_entries()

        # Compress memory
        await memory.compress_classical_storage()

        # Create checkpoint and reset if needed
        if stats['memory_usage'] > 90:
            snapshot_id = await memory.create_memory_snapshot()
            await memory.reset_to_coherent_state()
            print(f"Memory reset, snapshot saved: {snapshot_id}")

Performance Monitoring

import time
from dataclasses import dataclass

@dataclass
class PerformanceMetrics:
    execution_time: float
    quantum_coherence: float
    memory_usage: float
    success_rate: float
    decoherence_events: int

async def monitor_performance():
    metrics = PerformanceMetrics(0, 0, 0, 0, 0)

    # Run performance tests
    test_queries = [
        "Simple quantum query",
        "Complex multi-branch query", 
        "Entangled agent collaboration",
        "Large vector search"
    ]

    for query in test_queries:
        start_time = time.time()

        try:
            result = await chain.arun(query, quantum_enhanced=True)

            metrics.execution_time += time.time() - start_time
            metrics.quantum_coherence += result.get('quantum_coherence', 0)
            metrics.success_rate += 1

        except Exception as e:
            print(f"Query failed: {e}")
            metrics.decoherence_events += 1

    # Calculate averages
    num_queries = len(test_queries)
    metrics.execution_time /= num_queries
    metrics.quantum_coherence /= num_queries
    metrics.success_rate /= num_queries

    print("Performance Metrics:")
    print(f"Avg execution time: {metrics.execution_time:.3f}s")
    print(f"Avg quantum coherence: {metrics.quantum_coherence:.3f}")
    print(f"Success rate: {metrics.success_rate:.1%}")
    print(f"Decoherence events: {metrics.decoherence_events}")

asyncio.run(monitor_performance())

Best Practices

1. Quantum State Management

  • Monitor coherence levels regularly
  • Use appropriate decoherence thresholds
  • Reset quantum states when coherence drops
  • Implement graceful classical fallbacks

2. Resource Optimization

  • Balance quantum complexity with available resources
  • Use appropriate circuit depths for your hardware
  • Implement memory cleanup and monitoring
  • Cache frequently used quantum states

3. Error Handling

  • Always implement quantum error handling
  • Use try-catch blocks for quantum operations
  • Provide classical fallbacks for critical operations
  • Monitor and log quantum errors

4. Performance Tuning

  • Profile quantum operations regularly
  • Optimize circuit compilation
  • Use appropriate shot counts
  • Balance parallelism with decoherence

This comprehensive user guide provides practical examples and best practices for building sophisticated quantum-classical hybrid AI applications with QuantumLangChain.