Skip to main content

Q-Orca — Quantum Orchestrated State Machine Language

Project description

Q-Orca — Quantum Orchestrated State Machine Language

Q-Orca is a quantum-aware dialect of Orca, a state machine language written in Markdown. It extends Orca with Dirac ket notation for quantum states, unitary gate actions, entanglement verification, and simulation via Qiskit.


Setup

# Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate  # Linux/macOS
# .venv\Scripts\activate   # Windows

# Install Q-Orca in editable mode (with quantum libraries)
pip install -e ".[quantum]"

# Or install with MCP server support
pip install -e ".[all]"

# Or install without quantum deps first
pip install -e .
pip install qiskit
pip install qutip  # optional, for quantum verification

To exit the virtual environment: deactivate


Running

# Verify a quantum machine
q-orca verify examples/bell-entangler.q.orca.md
q-orca verify examples/bell-entangler.q.orca.md --json

# Compile to Mermaid diagram
q-orca compile mermaid examples/quantum-teleportation.q.orca.md

# Compile to OpenQASM 3.0
q-orca compile qasm examples/bell-entangler.q.orca.md

# Generate Qiskit simulation script
q-orca simulate examples/bell-entangler.q.orca.md

# Run simulation immediately
q-orca simulate examples/bell-entangler.q.orca.md --run

# Noisy simulation with 2048 shots
q-orca simulate examples/bell-entangler.q.orca.md --run --shots 2048

# With QuTiP verification
q-orca simulate examples/bell-entangler.q.orca.md --run --verbose

# MCP self-description (for Claude Code integration)
q-orca --tools --json

# Read source from stdin
cat examples/bell-entangler.q.orca.md | q-orca --stdin verify

Commands

q-orca verify

Parses and verifies a quantum machine definition. Runs 5 verification stages:

  1. Structural — reachability, deadlocks, orphan states
  2. Completeness — (state, event) coverage
  3. Determinism — guard mutual exclusion
  4. Quantum — unitarity, no-cloning, entanglement, collapse completeness
  5. Superposition leak — static analysis of superposition coherence

Options:

  • --json — output as JSON
  • --skip-completeness — skip event completeness checks
  • --skip-quantum — skip quantum-specific checks

q-orca compile

Compiles a machine to a target format.

q-orca compile mermaid examples/quantum-teleportation.q.orca.md
q-orca compile qasm examples/bell-entangler.q.orca.md

q-orca simulate

Generates and optionally runs a Qiskit Python script.

# Output the Qiskit script (no execution)
q-orca simulate examples/bell-entangler.q.orca.md

# Run the simulation immediately
q-orca simulate examples/bell-entangler.q.orca.md --run

# Noisy simulation with 2048 shots
q-orca simulate examples/bell-entangler.q.orca.md --run --shots 2048

# Skip QuTiP verification
q-orca simulate examples/bell-entangler.q.orca.md --run --skip-qutip

# JSON output
q-orca simulate examples/bell-entangler.q.orca.md --run --json

Examples

File Description
bell-entangler.q.orca.md Bell state via Hadamard + CNOT
quantum-teleportation.q.orca.md Teleports a qubit via Bell pair
deutsch-jozsa.q.orca.md Constant vs balanced oracle detection
ghz-state.q.orca.md 3-qubit GHZ state preparation
vqe-heisenberg.q.orca.md Variational quantum eigensolver for Heisenberg XXX Hamiltonian

Machine Format

The full source for every example is in examples/. Here is bell-entangler.q.orca.md:

# machine BellEntangler

## context
| Field      | Type          | Default          |
|------------|---------------|------------------|
| qubits     | list<qubit>   | [q0, q1]         |
| outcome    | int           | -1               |

## events
- prepare_H
- entangle
- measure_done

## state |00>
> Ground state, no entanglement yet

## state |+0> = (|0> + |1>)|00>/√2
> After Hadamard on qubit 0 — superposition

## state |ψ> = (|00> + |11>)/√2
> Bell state after Hadamard + CNOT

## state |00_collapsed> [final]
> Collapsed to |00> after measurement

## state |11_collapsed> [final]
> Collapsed to |11> after measurement

## transitions
| Source          | Event        | Guard                  | Target              | Action                  |
|-----------------|--------------|------------------------|---------------------|-------------------------|
| |00>            | prepare_H    |                        | |+0>                | apply_H_on_q0           |
| |+0>            | entangle     |                        | |ψ>                 | apply_CNOT_q0_to_q1     |
| |ψ>             | measure_done | prob_collapse('00')=0.5| |00_collapsed>       | set_outcome_0           |
| |ψ>             | measure_done | prob_collapse('11')=0.5| |11_collapsed>       | set_outcome_1           |

## guards
| Name                | Expression                          |
|---------------------|-------------------------------------|
| prob_collapse('00') | fidelity(|ψ>, |00>) ** 2 ≈ 0.5     |
| prob_collapse('11') | fidelity(|ψ>, |11>) ** 2 ≈ 0.5     |

## actions
| Name                | Signature                          | Effect                     |
|---------------------|------------------------------------|----------------------------|
| apply_H_on_q0       | (qs) -> qs                         | Hadamard(qs[0])            |
| apply_CNOT_q0_to_q1 | (qs) -> qs                         | CNOT(qs[0], qs[1])         |
| set_outcome_0       | (ctx, val) -> Context              | ctx.outcome = 0            |
| set_outcome_1       | (ctx, val) -> Context              | ctx.outcome = 1            |

## effects
| Name          | Input                  | Output            |
|---------------|------------------------|-------------------|
| collapse      | state vector           | classical bit     |

## verification rules
- unitarity: all gates preserve norm
- entanglement: final state must have Schmidt rank >1 before measure
- completeness: all possible collapses covered (no missing branches)
- no-cloning: no copy ops allowed

Full source: examples/bell-entangler.q.orca.md — or view all examples in examples/


Verify output (5-stage pipeline)

$ q-orca verify examples/bell-entangler.q.orca.md --json
{
  "machine": "BellEntangler",
  "valid": true,
  "errors": []
}

All 5 stages pass silently. To see individual stage results, use the Python API:

from q_orca.skills import verify_skill

result = verify_skill({"file": "examples/bell-entangler.q.orca.md"})
# result = {
#   "status": "valid",    ← all 5 stages passed
#   "machine": "BellEntangler",
#   "states": 5,
#   "events": 3,
#   "transitions": 4,
#   "errors": []
# }

The 5 verification stages are:

Stage Module Checks
1 Structural structural.py Reachability, deadlocks, orphan states
2 Completeness completeness.py Every (state, event) pair has a transition
3 Determinism determinism.py Guards are mutually exclusive
4 Quantum quantum.py + dynamic.py Unitarity, no-cloning, entanglement (QuTiP), collapse completeness
5 Superposition superposition.py No superposition coherence leaks

Compile to Mermaid diagram

$ q-orca compile mermaid examples/bell-entangler.q.orca.md
stateDiagram-v2
  direction LR

  00 : |00>
  0 : |+0> = (|0> + |1>)|00>/√2
  unnamed : |ψ> = (|00> + |11>)/√2
  00_collapsed : |00_collapsed>
  11_collapsed : |11_collapsed>

  [*] --> 00
  00_collapsed --> [*]
  11_collapsed --> [*]

  00 --> 0 : prepare_H / apply_H_on_q0
  0 --> unnamed : entangle / apply_CNOT_q0_to_q1
  unnamed --> 00_collapsed : measure_done [prob_collapse('00')] / set_outcome_0
  unnamed --> 11_collapsed : measure_done [prob_collapse('11')] / set_outcome_1

  note right of 00
    Verification Rules:
    - unitarity: all gates preserve norm
    - entanglement: final state must have Schmidt rank >1 before measure
    - completeness: all possible collapses covered (no missing branches)
    - no_cloning: no copy ops allowed
  end note

Compile to OpenQASM 3.0

$ q-orca compile qasm examples/bell-entangler.q.orca.md
// Generated by Q-Orca compiler
// Machine: BellEntangler
OPENQASM 3.0;
include "stdgates.inc";

qubit[2] q;
bit[2] c;

int outcome = -1;

// Gate sequence derived from state machine transitions
// |00> -> |+0> via prepare_H
h q[0];
// |+0> -> |ψ> via entangle
cx q[0], q[1];
// |ψ> -> |00_collapsed> via measure_done
// |ψ> -> |11_collapsed> via measure_done

// Measurement
c[0] = measure q[0];
c[1] = measure q[1];

Simulate with Qiskit

Analytic (statevector) — fidelity + entanglement verification:

$ q-orca simulate examples/bell-entangler.q.orca.md --run
  Machine: BellEntangler
  Success: True
  Probabilities:
    00: 50.00%
    01: 0.00%
    10: 0.00%
    11: 50.00%
  QuTiP Verification:
    Unitarity: VERIFIED
    Entanglement: VERIFIED
    Schmidt Rank: 2

Probabilistic (shots) — observed counts:

$ q-orca simulate examples/bell-entangler.q.orca.md --run --shots 512
  Machine: BellEntangler
  Success: True
  Counts: {'11': 269, '00': 243}

JSON output (useful for tooling):

$ q-orca simulate examples/bell-entangler.q.orca.md --run --json
{
  "machine": "BellEntangler",
  "success": true,
  "probabilities": {
    "00": 0.5,
    "01": 0.0,
    "10": 0.0,
    "11": 0.5
  },
  "counts": null,
  "qutipVerification": {
    "unitarityVerified": true,
    "entanglementVerified": true,
    "schmidtRank": 2,
    "errors": []
  }
}

Generated Qiskit script snippet

# Generated by Q-Orca compiler
# Machine: BellEntangler

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector, Operator
from qiskit.providers.basic_provider import BasicSimulator

qubit_count = 2
qc = QuantumCircuit(2)

# Gate sequence from state machine
qc.h(0)        # |00> --prepare_H--> |+0>
qc.cx(0, 1)    # |+0> --entangle--> |ψ>

# Simulation (analytic)
sv = Statevector(qc)
probs = sv.probabilities()
# ...

# QuTiP Verification
unitary_matrix = Operator(qc).data.tolist()
U = np.array(unitary_matrix)
# Unitarity: U U† ≈ I
# Entanglement: Schmidt rank across Bell partition

MCP Server

Q-Orca includes an MCP (Model Context Protocol) server that exposes all skills as tools for AI clients like Claude Code.

Setup

# Install with MCP dependencies
pip install -e ".[mcp]"

# Or install with all dependencies (quantum + MCP)
pip install -e ".[all]"

Running the MCP Server

# Start the MCP server (uses stdio transport)
q-orca-mcp

# Or via Python module
python -m q_orca.mcp_server

Claude Code Configuration

Add to your Claude Code settings (~/.claude/settings.json or project .claude.json):

{
  "mcpServers": {
    "q-orca": {
      "command": "q-orca-mcp",
      "cwd": "/path/to/your/project"
    }
  }
}

Available MCP Tools

Tool Description
parse_machine Parse a Q-Orca machine and return structure as JSON
verify_machine Run 5-stage verification pipeline
compile_machine Compile to Mermaid, QASM, or Qiskit
generate_machine Generate quantum machine from natural language spec
refine_machine Fix verification errors using LLM
simulate_machine Run Qiskit simulation
server_status Get server version and LLM config

LLM Provider Configuration

ORCA_API_KEY is the universal key — it works for any provider:

# Universal API key (works for any provider)
export ORCA_API_KEY=your-api-key

# Optional overrides
export ORCA_PROVIDER=anthropic   # anthropic, openai, minimax, ollama, grok
export ORCA_MODEL=claude-sonnet-4-6
export ORCA_MAX_TOKENS=4096
export ORCA_TEMPERATURE=0.7

Or via a YAML config file (orca.yaml or .orca.yaml in your project):

# Anthropic (default)
provider: anthropic
model: claude-sonnet-4-6
api_key: ${ORCA_API_KEY}
# MiniMax
provider: minimax
model: MiniMax-M2.7
api_key: ${ORCA_API_KEY}

Provider-specific keys (ANTHROPIC_API_KEY, MINIMAX_API_KEY, OPENAI_API_KEY) are also supported as fallbacks.


Architecture

flowchart TD
    subgraph Input
        MD[".q.orca.md file"]
        NL[Natural Language]
    end

    MD --> Parser

    subgraph Parser
        MP[markdown_parser.py<br/>Two-phase parse]
    end

    Parser --> AST[AST: QMachineDef]

    subgraph "Verifier (5 stages)"
        V1[structural.py<br/>Reachability, deadlocks, orphans]
        V2[completeness.py<br/>(state, event) coverage]
        V3[determinism.py<br/>Guard mutual exclusion]
        V4[quantum.py<br/>Unitarity, no-cloning, entanglement]
        V4D[dynamic.py<br/>QuTiP: Schmidt rank, entropy]
        V5[superposition.py<br/>Superposition coherence leak]
        V1 --> V2 --> V3 --> V4 --> V4D --> V5
    end

    AST --> Verifier
    Verifier --> VResult{Valid?}

    VResult -->|Yes| Compiler
    VResult -->|No| Refine[refine_skill<br/>LLM fix loop]

    Refine -->|Fixed source| Parser
    NL --> Generate[generate_skill<br/>LLM generation]
    Generate -->|Raw .q.orca.md| Parser

    subgraph Compiler
        CM[Mermaid]
        CQ[QASM 3.0]
        CK[Qiskit script]
    end

    Compiler --> MermaidDiagram[Rendered state diagram]
    Compiler --> QASMCode[Quantum circuit code]
    Compiler --> QiskitScript[Python simulation]

    QiskitScript --> Runtime[Python runtime]
    Runtime --> SimResult[Counts, Probabilities, Fidelity]

    style Verifier fill:#1b4f72,color:#fff
    style Compiler fill:#27ae60,color:#fff
    style Runtime fill:#8e44ad,color:#fff
    style NL fill:#f39c12,color:#fff

Directory structure

q_orca/
├── __init__.py            # Package exports
├── ast.py                 # AST dataclasses
├── cli.py                 # CLI entrypoint
├── skills.py              # Skill functions (parse, verify, compile, generate, refine)
├── tools.py               # MCP tool JSON schemas
├── mcp_server.py          # MCP server (stdio JSON-RPC)
├── parser/
│   └── markdown_parser.py # Two-phase markdown parser
├── verifier/
│   ├── types.py           # Verification result types
│   ├── structural.py      # Reachability, deadlocks, orphans
│   ├── completeness.py    # (state, event) coverage
│   ├── determinism.py     # Guard mutual exclusion
│   ├── quantum.py         # Unitarity, no-cloning, entanglement
│   ├── superposition.py   # Superposition coherence leak
│   └── dynamic.py         # QuTiP circuit simulation
├── compiler/
│   ├── mermaid.py         # Mermaid state diagram
│   ├── qasm.py            # OpenQASM 3.0
│   └── qiskit.py          # Qiskit Python script
├── llm/
│   ├── provider.py        # Abstract LLM provider interface
│   ├── anthropic.py       # Anthropic provider
│   ├── openai.py          # OpenAI provider
│   ├── minimax.py         # MiniMax provider
│   ├── ollama.py          # Ollama provider
│   └── grok.py            # Grok provider
├── config/
│   ├── loader.py          # YAML/env config loader
│   └── types.py           # Config types
└── runtime/
    ├── types.py           # Simulation result types
    └── python.py          # Python subprocess runner + simulation

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

q_orca-0.2.0.tar.gz (59.2 kB view details)

Uploaded Source

Built Distribution

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

q_orca-0.2.0-py3-none-any.whl (59.4 kB view details)

Uploaded Python 3

File details

Details for the file q_orca-0.2.0.tar.gz.

File metadata

  • Download URL: q_orca-0.2.0.tar.gz
  • Upload date:
  • Size: 59.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for q_orca-0.2.0.tar.gz
Algorithm Hash digest
SHA256 00073a19a19b0375bdccf0da87e51eee2e678e91de13deb399ac6d70d942566c
MD5 7e6e1c73aa760bd266d86b21253a05b8
BLAKE2b-256 0d3edb79754e34f3f7675ddc1b94e4d40593462c5bad6991d70fb6245ceda1b5

See more details on using hashes here.

File details

Details for the file q_orca-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: q_orca-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 59.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for q_orca-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d6a98766e28ce74f12ae84f7bab2bb0507e37a80c25dc9755dc24f43337fa7
MD5 0b37fb9f1edcd9cf19f37c5c1b808586
BLAKE2b-256 4be2a16d67e7bb137dafc38c6c3b179dd3c4ca7f3f938b1ed143b71a837efa85

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