Skip to main content

Local, compiler-backed CUDA-Q optimization agent

Project description

Q
QBridge Agent

Natural language → QBridge DSL → local CUDA-Q solve.
No cloud compiler. No coding-agent detour.

QBridge-Agent is a lean optimization agent for quantum developers who already know they want QAOA on CUDA-Q, not a general-purpose LLM rewriting Python files until something runs. You describe a bounded binary optimization problem — knapsack, MaxCut, portfolio, scheduling — and the agent:

  1. Translates your request into validated QBridge DSL
  2. Compiles locally with the installed qbridge package (deterministic, auditable)
  3. Executes on your NVIDIA CUDA-Q target and prints the decoded assignment

No Aider loop. No hand-written @cudaq.kernel. No cloud compile endpoint.

Natural-language knapsack: NL → DSL → CUDA-Q solve

qbridge "Knapsack with 5 items, values [6,5,8,9,6], weights [2,3,4,5,3], capacity 8"


Why this beats a coding agent for QUBO problems

QBridge-Agent General coding agent (e.g. Aider)
Output Validated QBridge DSL → compiler-lowering Ad-hoc CUDA-Q scripts
Compile path Deterministic local qbridge LLM edits until syntax runs
Verification Independent constraint/objective check Usually none
Token cost (T1/T2 suite) ~1,241 mean tokens/trial ~3,331 (2.68×)
Wall time (same suite) ~3.8 s ~12.6 s (~3.36×)
Verified trials (18-run benchmark) 16 / 18 0 / 18

Benchmark: frozen six-task T1/T2 CUDA-Q suite, openai/gpt-4o-mini, NVIDIA cusvsim_fp32. Details in evaluations/README.md.


Table of contents

  1. Install
  2. CLI experience
  3. Environment setup (WSL + CUDA-Q)
  4. Mode A — DSL file (no API key)
  5. Mode B — natural language
  6. Workspace layout
  7. Programmatic API
  8. Worked examples
  9. CLI reference
  10. Troubleshooting
  11. Benchmarks & evaluations
  12. Scope & non-goals
  13. License & releases

Install

Requirements

Component Version / notes
Python 3.9+
qbridge[quantum] ≥ 0.3.0 — the CUDA-Q compiler
CUDA-Q 0.15.0, NVIDIA target available
OpenRouter API key First-run setup for NL mode (stored locally)

PyPI

pip install "qbridge[quantum]>=0.3.0" qbridge-agent

Source checkout

git clone https://github.com/ProfessionalQwerty/qbridge-agent.git
cd qbridge-agent
pip install -e ".[dev]"

Verify:

qbridge --help
qbridge-compile --help   # from qbridge package

CLI experience

QBridge-Agent is a single-shot CLI — one quoted problem or one DSL file per invocation. There is no persistent chat shell; output is structured, timed, and color-coded for terminals.

First launch

Running qbridge with no arguments (or your first NL prompt) opens setup:

──────────────────────────────────────────────────────────
  QBridge-Agent  local compiler-backed CUDA-Q optimization
──────────────────────────────────────────────────────────

 Welcome to QBridge.
 Describe an optimization problem in plain English.
 We translate to QBridge DSL, compile locally, and run CUDA-Q.

 OpenRouter setup  keys at https://openrouter.ai/keys
 Paste your key and press Enter (visible input — paste works reliably on Windows/WSL)
 OpenRouter API key: sk-or-v1-...
 Received  sk-or-v1-abc...xyz (73 chars)
 Model [openai/gpt-4o-mini]:
 ✓ Ready  model=tencent/hy3:free

Credentials are saved under ~/.qbridge-agent/ (credentials.json + config.json). Nothing is sent to a QBridge cloud endpoint — only OpenRouter for NL translation.

Everyday commands

Command What it does
qbridge Show configured model and usage hints
qbridge "…" NL → DSL → compile → CUDA-Q solve
qbridge problem.py DSL file → compile → solve (no API key)
qbridge --setup Re-enter OpenRouter key and default model
qbridge --doctor Test env vs saved key; show which is active
qbridge --no-solve problem.py Compile only, skip GPU solve

Terminal output (NL mode)

Each run prints a banner, timed steps, structured blocks, and token usage:

 ● Request  Knapsack with 5 items, values [6,5,8,9,6], weights …
 ● Model  tencent/hy3:free
 ● Translating DSL and compiling locally
 ✓ Translating DSL and compiling locally  12.69s

 Generated DSL
   path: qbridge-output/problem.py

 Solution
   Backend:    cudaq:nvidia
   Objective:  14.0
   Feasible:   True
   q0: 1  …

 Token usage
   prompt: 1078  completion: 158  total: 1236  wall time: 12.68s

 ✓ Done  workspace: …/qbridge-output

On failure, the CLI prints a red error line and actionable hints (e.g. qbridge --setup after a 401).

Credential precedence

Source Priority
OPENROUTER_API_KEY env Used only if it looks like a real sk-or-… key (≥ 20 chars)
~/.qbridge-agent/credentials.json Default after setup
Placeholder env (your-key, etc.) Ignored — saved file key is used instead

Run qbridge --doctor to see which key is active and whether OpenRouter accepts it.

Styling

  • Cyan banner and bullets, magenta section headers, blue values, green success marks
  • Respects NO_COLOR and FORCE_COLOR
  • Falls back to ASCII glyphs (>, OK, X) when Unicode is unavailable

Environment setup (WSL + CUDA-Q)

CUDA-Q with NVIDIA targets is Linux-first. On Windows, use WSL2 — not bare python.exe, not an arbitrary WSL distro.

Verified example (Windows host → Ubuntu WSL)

wsl -d Ubuntu -- /home/dilip/qbridge-env/bin/python3 -c "import cudaq; print(cudaq.__version__)"
# CUDA-Q Version 0.15.0

Suggested virtualenv bootstrap (inside WSL Ubuntu)

python3 -m venv ~/qbridge-env
source ~/qbridge-env/bin/activate
pip install "qbridge[quantum]>=0.3.0" qbridge-agent
python -c "import cudaq; cudaq.set_target(\"nvidia\"); print(cudaq.get_target())"

Run the agent

qbridge "Knapsack with 5 items, values [6,5,8,9,6], weights [2,3,4,5,3], capacity 8"

If you previously exported a placeholder like OPENROUTER_API_KEY=your-key in your shell, remove it:

unset OPENROUTER_API_KEY
qbridge --doctor

Mode A — DSL file (no API key)

When you already have — or hand-write — QBridge DSL, the agent is a compile + solve CLI with structured output.

qbridge examples/knapsack_n5.py --workspace ./qbridge-output

Compile only (emit CUDA-Q kernel, skip GPU solve):

qbridge examples/knapsack_n5.py --workspace ./qbridge-output --no-solve

Expected terminal flow

──────────────────────────────────────────────────────────
  QBridge-Agent  local compiler-backed CUDA-Q optimization
──────────────────────────────────────────────────────────

 ● Loading QBridge DSL  qbridge-output
 ● Compiling to CUDA-Q
 ✓ Compiling to CUDA-Q  1.24s
 Artifacts
   CUDA-Q kernel: …/compiled_output/kernel_execution.py
   DSL source:    …/problem.py
 ● Running CUDA-Q on NVIDIA target
 ✓ Running CUDA-Q on NVIDIA target  2.81s
 Solution
   Backend:       cudaq:nvidia
   Objective:     …
   Feasible:      True
 ✓ Done  workspace: /…/qbridge-output

The DSL file must use import qbridge as qb and declare variables, objective, and constraints — not arbitrary Python. Full DSL rules: QBridge compiler README.


Mode B — natural language

After first-run setup, describe your problem in plain English:

qbridge "Knapsack: 5 items, values [6,5,8,9,6], weights [2,3,4,5,3], capacity 8"

Reconfigure anytime:

qbridge --setup

Model override for one run

qbridge "MaxCut on a 4-node cycle with edge weights 1,2,3,4" --model openai/gpt-4o-mini
When to use Mode
You have DSL already, or want zero API cost A — .py file
Exploring problems, demos, NL interface B — quoted prompt
CI / regression A (deterministic input)

Token usage is printed at the end of NL runs.


Workspace layout

Every run writes artifacts under --workspace (default: ./qbridge-output):

qbridge-output/
├── problem.py                    # DSL copy (Mode A) or generated DSL (Mode B)
├── prompt.txt                    # Mode B only — your NL request
├── compiled_output/
│   ├── kernel_execution.py       # Generated CUDA-Q kernel
│   ├── compile_meta.json         # Local compiler metadata
│   └── solution.json             # Assignment + objective + feasibility
├── _usage.json                   # Mode B — token counts + timings
└── response.txt                  # Mode B — raw LLM response (if pipeline saves it)

Reading solution.json

{
  "assignment": {"x0": 1, "x1": 0, "x2": 1},
  "objective_value": 19.0,
  "constraints_satisfied": true,
  "backend": "cudaq:nvidia",
  "cudaq_target": "Target nvidia ..."
}

If constraints_satisfied is false, the QAOA sample decoded to an infeasible assignment — tighten SOLVE_KWARGS in the generated DSL or re-run.


Programmatic API

Full NL pipeline

from pathlib import Path
from agent.cudaq_entry import run_cudaq_case

report = run_cudaq_case(
    prompt="Select at most 3 projects maximizing value under budget 100 ...",
    workspace=Path("./qbridge-output"),
    model="openai/gpt-4o-mini",
    api_key="...",  # or ~/.qbridge-agent/credentials.json
)

print(report["ok"], report["solution"], report["usage"])

DSL-only pipeline (no LLM)

from pathlib import Path
from agent.compiler_interface.pipeline import run_quantum_pipeline

dsl = Path("examples/knapsack_n5.py").read_text()
report = run_quantum_pipeline(
    "ignored",
    complete_fn=lambda _: dsl,
    workspace_root=Path("./qbridge-output"),
    include_solve=True,
)
print(report.ok, report.solution)

Worked examples

Example File Command
5-item knapsack examples/knapsack_n5.py qbridge examples/knapsack_n5.py --workspace ./out
MaxCut (NL) examples/maxcut_agent_run.md See file for quoted prompt
Portfolio (NL) examples/optimize_portfolio.md Budget + group cover constraints
Scheduling (NL) examples/scheduling_agent_run.md Resource + time windows

MaxCut prompt example

qbridge "MaxCut on triangle graph nodes 0-1-2 with edge weights 1,2,3. Binary vars x0,x1,x2." \
  --workspace ./maxcut-out

CLI reference

qbridge [request] [--workspace WORKSPACE] [--no-solve] [--setup] [--doctor] [--model MODEL]
qbridge-agent   # alias
Argument Description
request Quoted NL problem or path to .py DSL file
--setup Re-enter OpenRouter key and model
--doctor Diagnose credential sources and OpenRouter connectivity
--workspace PATH Output directory (default: ./qbridge-output)
--no-solve Compile only (DSL mode)
--model ID Override saved model for one run

Credentials: ~/.qbridge-agent/credentials.json (key) and config.json (model). A valid OPENROUTER_API_KEY env var overrides the saved file; placeholder values are ignored.

Exit codes

Code Meaning
0 Success
1 Compile/solve/agent failure
2 Setup cancelled / missing credentials

Environment variables

Variable Used by
OPENROUTER_API_KEY NL mode (optional if setup completed)
QBRIDGE_BENCH_MODEL / QBRIDGE_MODEL Default model override
NO_COLOR / FORCE_COLOR Terminal styling

Troubleshooting

Symptom Cause Fix
401 Unauthorized from OpenRouter Bad/stale key or placeholder env unset OPENROUTER_API_KEY; qbridge --doctor; qbridge --setup
--doctor shows env your-key Doc placeholder left in shell unset OPENROUTER_API_KEY
Setup rejects pasted key Hidden input mangled paste (older versions) Upgrade to ≥ 0.3.1; use visible paste prompt
No module named 'cudaq' Wrong Python / no CUDA-Q Use WSL Ubuntu + qbridge[quantum] env
No module named 'qbridge.dsl_build' Wrong qbridge package installed pip install "qbridge[quantum]>=0.3.0" from PyPI
constraints_satisfied: false Weak QAOA / penalty settings Edit generated DSL SOLVE_KWARGS (↑ alpha, p, n_seeds)
Garbled Unicode on Windows terminal Legacy code page Use Windows Terminal; agent falls back to ASCII glyphs
Agent generates invalid DSL LLM drift Retry; or write DSL manually (Mode A)

Benchmarks & evaluations

Reproduce the V2 vs Aider comparison (maintainers):

wsl -d Ubuntu -- bash -c '
  cd /mnt/c/Users/you/qbridge-agent &&
  export PYTHONPATH=$(pwd) &&
  export OPENROUTER_API_KEY=$OPENROUTER_API_KEY &&
  /home/dilip/qbridge-env/bin/python3 evaluations/run_benchmark.py \
    --trials 3 --results evaluations/results/my_run
'

See evaluations/README.md for methodology, token capture (AIDER_USAGE_LOG), and timing breakdowns.


Scope & non-goals

Supported

  • Small constrained binary/integer optimization expressible in current QBridge DSL
  • Local compile + NVIDIA CUDA-Q execution
  • NL → DSL translation via OpenRouter

Not supported (by design)

  • Arbitrary Python optimization scripts
  • General quantum algorithms (VQE chemistry, Shor, custom ansätze)
  • Cloud QBridge compile/solve APIs
  • Interactive multi-turn chat UI (one qbridge "..." per problem)

Unsupported inputs are errors, not best-effort guesses.


License & releases

Current release: qbridge-agent 0.3.1 with qbridge[quantum]>=0.3.0.

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

qbridge_agent-0.3.1.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

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

qbridge_agent-0.3.1-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

Details for the file qbridge_agent-0.3.1.tar.gz.

File metadata

  • Download URL: qbridge_agent-0.3.1.tar.gz
  • Upload date:
  • Size: 30.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for qbridge_agent-0.3.1.tar.gz
Algorithm Hash digest
SHA256 19515598fdbfe8ba5a986c293ed29be6c9a89b2d5977c26e44245a217bf9f2a1
MD5 0999fd3cc06851080587a7368111bf98
BLAKE2b-256 131ffa837bb413b783230965c009cc6681f2472f1cbae5bf266e4a63e8abc0bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for qbridge_agent-0.3.1.tar.gz:

Publisher: publish.yml on ProfessionalQwerty/qbridge-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qbridge_agent-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: qbridge_agent-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 29.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for qbridge_agent-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5087349bd5f844fe61a0838458048ad4e505062cb4ef23dc78b4563ecf1f4e7a
MD5 c6c57af754dad90a0a5175c4156d108c
BLAKE2b-256 7d6aa3773101e98989c491cecb861519b5f023e34dd899881ed9369651742b5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for qbridge_agent-0.3.1-py3-none-any.whl:

Publisher: publish.yml on ProfessionalQwerty/qbridge-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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