qualia_core_db/solvers/qpu/
mod.rs1pub mod dispatcher;
9pub mod pre_solver;
10
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
16pub enum ProblemType {
17 Annealing,
19 GateModel,
21 Vqe,
23 Qaoa,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct JobParameters {
32 pub num_qubits: u32,
33 pub hamiltonian: Option<String>,
35 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#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct QpuJob {
58 pub job_id: String,
59 pub problem_type: ProblemType,
60 pub parameters: JobParameters,
61 pub created_at_ms: u64,
63}
64
65impl QpuJob {
66 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
91pub enum JobStatus {
92 Pending,
93 Running,
94 Completed,
95 Failed,
96 Timeout,
97}
98
99#[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#[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 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#[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
163fn 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}