Skip to main content

qualia_core_db/solvers/qpu/
mod.rs

1//! QPU (Quantum Processing Unit) solver integration.
2//!
3//! Moved from the standalone `qpu/` crate.  Config-file loading and the old
4//! single-endpoint `QpuClient` have been removed; authentication and HTTP
5//! egress are now handled by `qualia-client-core::qpu_oracle` and
6//! `qualia-client-core::qpu_dispatcher`.
7
8pub mod dispatcher;
9pub mod pre_solver;
10
11use serde::{Deserialize, Serialize};
12
13// ── Problem type ──────────────────────────────────────────────────────────────
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
16pub enum ProblemType {
17    /// Quantum annealing (QUBO)
18    Annealing,
19    /// Gate-model quantum circuit
20    GateModel,
21    /// Variational Quantum Eigensolver
22    Vqe,
23    /// Quantum Approximate Optimisation Algorithm
24    Qaoa,
25}
26
27// ── Job types ─────────────────────────────────────────────────────────────────
28
29/// Parameters for a QPU job submission.
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct JobParameters {
32    pub num_qubits: u32,
33    /// Hamiltonian JSON (annealing problems)
34    pub hamiltonian: Option<String>,
35    /// Circuit JSON (gate-model problems)
36    pub circuit: Option<String>,
37    pub circuit_depth: u32,
38    pub shots: u32,
39    pub extra: serde_json::Value,
40}
41
42impl Default for JobParameters {
43    fn default() -> Self {
44        Self {
45            num_qubits: 1,
46            hamiltonian: None,
47            circuit: None,
48            circuit_depth: 1,
49            shots: 1000,
50            extra: serde_json::Value::Null,
51        }
52    }
53}
54
55/// A QPU job ready for dispatch.
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct QpuJob {
58    pub job_id: String,
59    pub problem_type: ProblemType,
60    pub parameters: JobParameters,
61    /// Unix timestamp ms (wall-clock submission time)
62    pub created_at_ms: u64,
63}
64
65impl QpuJob {
66    /// Creates `QpuJob` with the given state.
67    pub fn new(job_id: String, problem_type: ProblemType, parameters: JobParameters) -> Self {
68        Self {
69            job_id,
70            problem_type,
71            parameters,
72            created_at_ms: current_time_ms(),
73        }
74    }
75}
76
77impl Default for QpuJob {
78    fn default() -> Self {
79        Self {
80            job_id: "default_job".into(),
81            problem_type: ProblemType::Annealing,
82            parameters: JobParameters::default(),
83            created_at_ms: 0,
84        }
85    }
86}
87
88// ── Result types ──────────────────────────────────────────────────────────────
89
90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
91pub enum JobStatus {
92    Pending,
93    Running,
94    Completed,
95    Failed,
96    Timeout,
97}
98
99/// Measurement result from a QPU run.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct Measurement {
102    pub bitstring: String,
103    pub count: u32,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct JobResultData {
108    pub measurements: Vec<Measurement>,
109    pub energies: Option<Vec<f64>>,
110    pub metadata: serde_json::Value,
111}
112
113/// Completed or failed QPU job result.
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct QpuResult {
116    pub job_id: String,
117    pub status: JobStatus,
118    pub result: Option<JobResultData>,
119    pub completed_at_ms: Option<u64>,
120    pub error: Option<String>,
121}
122
123impl QpuResult {
124    /// `true` when the job failed.
125    pub fn failed(job_id: String, msg: String) -> Self {
126        Self {
127            job_id,
128            status: JobStatus::Failed,
129            result: None,
130            completed_at_ms: Some(current_time_ms()),
131            error: Some(msg),
132        }
133    }
134}
135
136// ── Error ─────────────────────────────────────────────────────────────────────
137
138#[derive(Debug)]
139pub enum QpuError {
140    Api(String),
141    Network(String),
142    JobFailed(String),
143    Timeout,
144    NotUnlocked,
145}
146
147impl std::fmt::Display for QpuError {
148    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149        match self {
150            Self::Api(s) => write!(f, "QPU API error: {}", s),
151            Self::Network(s) => write!(f, "QPU network error: {}", s),
152            Self::JobFailed(s) => write!(f, "QPU job failed: {}", s),
153            Self::Timeout => write!(f, "QPU job timed out"),
154            Self::NotUnlocked => {
155                write!(f, "QPU Oracle not unlocked — affirm commitment in Settings")
156            }
157        }
158    }
159}
160
161impl std::error::Error for QpuError {}
162
163// ── Utilities ─────────────────────────────────────────────────────────────────
164
165fn current_time_ms() -> u64 {
166    std::time::SystemTime::now()
167        .duration_since(std::time::UNIX_EPOCH)
168        .map(|d| d.as_millis() as u64)
169        .unwrap_or(0)
170}