Skip to content

21jritz/quantum-prompt-optimizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Quantum Prompt Optimizer

A quantum-accelerated LLM prompt optimization system that uses QAOA (Quantum Approximate Optimization Algorithm) to solve the Maximum Independent Set problem across candidate prompts, selecting the highest-quality, non-redundant prompt for any given topic.

Built and tested on NVIDIA NGC using CUDA-Q cu12-0.9.0.


Demo

Six candidate prompts are scored on quality metrics, QAOA identifies and resolves redundancies, and the winning prompt is selected automatically.

Topic: Primal Sequence anime character profile
Candidates: 6

[Prompt Scorer] Scoring 6 candidate prompts...
  Prompt 1: overall=39 | spec=14 clarity=75 rel=28
  Prompt 2: overall=75 | spec=54 clarity=75 rel=100
  Prompt 3: overall=24 | spec=10 clarity=60 rel=0
  Prompt 4: overall=70 | spec=54 clarity=75 rel=84
  Prompt 5: overall=42 | spec=22 clarity=75 rel=28
  Prompt 6: overall=75 | spec=54 clarity=75 rel=100

[Quantum Prompt Optimizer] Conflicts detected: 1
  - Prompt 1 vs Prompt 3: both low quality
[Quantum Prompt Optimizer] Optimal selection found at seed 0.

====================================================
  WINNING PROMPT
====================================================
Write a detailed description of the character appearance,
personality, and motivations in The Primal Sequence anime series.

Quality Score: 75/100
  Specificity : 54/100
  Clarity     : 75/100
  Relevance   : 100/100

What This Project Does

Choosing the best prompt from many candidates is a combinatorial optimization problem. Given N candidate prompts, finding the most diverse, highest-quality subset requires evaluating exponentially many combinations — an NP-hard problem.

This system encodes prompt similarity and quality relationships as a conflict graph and uses QAOA to solve the Maximum Independent Set problem: finding the largest group of prompts where none are redundant with each other. The classical scorer then selects the single best prompt from the quantum-selected set.

This is a genuinely novel application of quantum computing — using quantum optimization not for physics or chemistry, but for natural language prompt engineering.


Architecture

Input: Topic + N Candidate Prompts
        |
Prompt Scorer (prompt_scorer.py)
  - Specificity score (0-100)
  - Clarity score (0-100)
  - Relevance score (0-100)
        |
Conflict Graph Builder
  - Connects similar/redundant prompts
  - Connects low-quality prompt pairs
        |
QAOA Optimizer (quantum_optimizer.py)
  - Maximum Independent Set via CUDA-Q
  - Runs on NVIDIA NGC infrastructure
        |
Classical Selector
  - Picks highest scorer from quantum set
        |
Output: Winning Prompt + Quality Breakdown + Log

Why Quantum?

The Maximum Independent Set problem is NP-hard. For N prompts, classical brute-force must check 2^N subsets. QAOA encodes the conflict graph as a Hamiltonian and uses quantum superposition to explore many subsets simultaneously, with quantum interference amplifying conflict-free solutions.

This is the same mathematical structure used in:

  • Drug molecule optimization
  • Financial portfolio selection
  • Network design problems

Applied here to prompt engineering for the first time.


File Structure

quantum-prompt-optimizer/
├── main.py                -- full pipeline orchestrator
├── prompt_scorer.py       -- quality scoring on 3 metrics
├── quantum_optimizer.py   -- QAOA Maximum Independent Set solver
└── requirements.txt       -- dependencies

Tech Stack

Tool Role
NVIDIA CUDA-Q Quantum circuit simulation
QAOA Algorithm Maximum Independent Set optimization
Python 3.10 Primary language
NVIDIA NGC Cloud container infrastructure
Docker Container runtime

How to Run

Prerequisites

  • Docker Desktop installed
  • NVIDIA NGC account (free at ngc.nvidia.com)

Pull the CUDA-Q container

docker pull nvcr.io/nvidia/quantum/cuda-quantum:cu12-0.9.0

Run the optimizer

docker run --rm --entrypoint="" -v /your/local/path:/files \
  nvcr.io/nvidia/quantum/cuda-quantum:cu12-0.9.0 \
  python3 /files/main.py

Customize for your use case

Edit the candidates list and topic variable in main.py to optimize prompts for any domain.


Scoring Metrics

Specificity (35% weight): Measures how detailed and precise the prompt is. Longer prompts with domain-specific keywords score higher.

Clarity (35% weight): Measures how unambiguous the prompt is. Vague words like "maybe", "something", "stuff" reduce the score. Direct action verbs and question marks increase it.

Relevance (30% weight): Measures how on-topic the prompt is for the given subject. Topic word matches and domain-specific terminology boost the score.


Key Concepts

Maximum Independent Set: Given a graph where nodes are prompts and edges connect redundant or low-quality pairs, the Maximum Independent Set is the largest group of nodes with no edges between them — the most diverse, non-redundant subset of prompts.

Hamiltonian Encoding: Each conflict edge (prompt_a, prompt_b) becomes a term 0.5 * Z(a) * Z(b) in the cost Hamiltonian. Minimizing energy selects the maximum independent set.

Conflict Graph Construction: Two prompts are connected if their Jaccard similarity exceeds 0.3 (too redundant) or if both score below 40/100 (both too weak to include together).


Use Cases

  • AI Content Creation: Select the best prompt for character descriptions, world-building, dialogue generation
  • Research: Automatically select the most informative query from a set of candidates
  • Education: Optimize exam questions for clarity and specificity
  • Enterprise: Select the most precise prompt for customer service or document generation pipelines

Comparison to Previous Projects

Project Algorithm Problem Type New Concept
Quantum Scheduler QAOA Graph Coloring Baseline QAOA
Multi-Agent Scheduler QAOA + Classical Graph Coloring + 3-color Multi-agent coordination
Quantum Prompt Optimizer QAOA Maximum Independent Set Novel NLP application

Author

Joshua Ritz — github.com/21jritz

Built on NVIDIA NGC | CUDA-Q cu12-0.9.0 | Novel Quantum-NLP Application

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages