quantrs2-circuit 0.1.3

Quantum circuit representation and DSL for the QuantRS2 framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Common types and result structures for verification

use super::config::VerifierConfig;
use scirs2_core::Complex64;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime};

/// Verification status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum VerificationStatus {
    /// Circuit is verified correct
    Verified,
    /// Circuit has verification failures
    Failed,
    /// Verification incomplete due to timeout or resource limits
    Incomplete,
    /// Verification couldn't be performed
    Unknown,
    /// Verification in progress
    InProgress,
}

/// Verification outcome
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum VerificationOutcome {
    /// Property holds
    Satisfied,
    /// Property violated
    Violated,
    /// Cannot determine (insufficient evidence)
    Unknown,
    /// Verification timeout
    Timeout,
    /// Verification error
    Error { message: String },
}

/// Types of numerical evidence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EvidenceType {
    /// Matrix norm measurement
    MatrixNorm,
    /// Eigenvalue analysis
    Eigenvalue,
    /// Fidelity measurement
    Fidelity,
    /// Entanglement measure
    Entanglement,
    /// Purity measurement
    Purity,
    /// Trace distance
    TraceDistance,
    /// Custom measurement
    Custom { name: String },
}

/// Numerical evidence for verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NumericalEvidence {
    /// Evidence type
    pub evidence_type: EvidenceType,
    /// Measured value
    pub measured_value: f64,
    /// Expected value
    pub expected_value: f64,
    /// Deviation from expected
    pub deviation: f64,
    /// Statistical p-value if applicable
    pub p_value: Option<f64>,
}

/// Error bounds for verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorBounds {
    /// Lower bound
    pub lower_bound: f64,
    /// Upper bound
    pub upper_bound: f64,
    /// Confidence interval
    pub confidence_interval: f64,
    /// Standard deviation
    pub standard_deviation: f64,
}

/// Violation severity levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ViolationSeverity {
    /// Minor violation (within acceptable bounds)
    Minor,
    /// Moderate violation
    Moderate,
    /// Major violation
    Major,
    /// High severity violation
    High,
    /// Critical violation (circuit likely incorrect)
    Critical,
}

/// Proof status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProofStatus {
    /// Theorem proved
    Proved,
    /// Theorem disproved
    Disproved,
    /// Proof incomplete
    Incomplete,
    /// Proof timeout
    Timeout,
    /// Proof error
    Error { message: String },
}

/// Proof steps
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofStep {
    /// Step description
    pub description: String,
    /// Rule or axiom used
    pub rule: String,
    /// Mathematical justification
    pub justification: String,
    /// Confidence in this step
    pub confidence: f64,
}

/// Formal proof representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormalProof {
    /// Proof tree
    pub proof_tree: ProofTree,
    /// Proof steps
    pub steps: Vec<ProofStep>,
    /// Axioms used
    pub axioms_used: Vec<String>,
    /// Proof confidence
    pub confidence: f64,
    /// Verification checksum
    pub checksum: String,
}

/// Proof tree structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofTree {
    /// Root goal
    pub root: ProofNode,
    /// Proof branches
    pub branches: Vec<Self>,
}

/// Proof tree node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofNode {
    /// Goal statement
    pub goal: String,
    /// Applied rule
    pub rule: Option<String>,
    /// Subgoals
    pub subgoals: Vec<String>,
    /// Proof status
    pub status: ProofStatus,
}

/// Counterexample for failed proofs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Counterexample {
    /// Input values that cause failure
    pub inputs: HashMap<String, f64>,
    /// Expected vs actual output
    pub expected_output: String,
    /// `actual_output`: String,
    pub actual_output: String,
    /// Minimal counterexample flag
    pub is_minimal: bool,
}

/// Proof complexity metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofComplexityMetrics {
    /// Number of proof steps
    pub step_count: usize,
    /// Proof depth
    pub proof_depth: usize,
    /// Number of axioms used
    pub axiom_count: usize,
    /// Memory usage for proof
    pub memory_usage: usize,
    /// Proof verification time
    pub verification_time: Duration,
}

/// Execution trace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionTrace {
    /// Sequence of states
    pub states: Vec<QuantumState>,
    /// Sequence of transitions
    pub transitions: Vec<StateTransition>,
    /// Trace length
    pub length: usize,
    /// Trace properties
    pub properties: HashMap<String, f64>,
}

/// Quantum state representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumState {
    /// State vector
    pub state_vector: Vec<Complex64>,
    /// State properties
    pub properties: HashMap<String, f64>,
    /// Time stamp
    pub timestamp: u64,
    /// State metadata
    pub metadata: HashMap<String, String>,
}

/// State transition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateTransition {
    /// Source state
    pub source: usize,
    /// Target state
    pub target: usize,
    /// Transition operation
    pub operation: String,
    /// Transition probability
    pub probability: f64,
    /// Transition time
    pub time: f64,
}

/// State space statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateSpaceStatistics {
    /// Total number of states
    pub total_states: usize,
    /// Number of transitions
    pub total_transitions: usize,
    /// Maximum path length
    pub max_path_length: usize,
    /// Average path length
    pub avg_path_length: f64,
    /// State space diameter
    pub diameter: usize,
    /// Memory usage
    pub memory_usage: usize,
}

/// Test outcome
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TestOutcome {
    /// Test passed
    Pass,
    /// Test failed
    Fail,
    /// Test skipped
    Skip,
    /// Test error
    Error { message: String },
}

/// Verification statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationStatistics {
    /// Total verification time
    pub total_time: Duration,
    /// Number of properties verified
    pub properties_verified: usize,
    /// Number of invariants checked
    pub invariants_checked: usize,
    /// Number of theorems proved
    pub theorems_proved: usize,
    /// Success rate
    pub success_rate: f64,
    /// Memory usage
    pub memory_usage: usize,
    /// Confidence statistics
    pub confidence_stats: ConfidenceStatistics,
}

/// Confidence statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfidenceStatistics {
    /// Average confidence
    pub average_confidence: f64,
    /// Minimum confidence
    pub min_confidence: f64,
    /// Maximum confidence
    pub max_confidence: f64,
    /// Confidence standard deviation
    pub confidence_std_dev: f64,
}

/// Verification issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationIssue {
    /// Issue type
    pub issue_type: IssueType,
    /// Issue severity
    pub severity: IssueSeverity,
    /// Issue description
    pub description: String,
    /// Location in circuit
    pub location: Option<CircuitLocation>,
    /// Suggested fix
    pub suggested_fix: Option<String>,
    /// Related evidence
    pub evidence: Vec<NumericalEvidence>,
}

/// Issue types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IssueType {
    /// Property violation
    PropertyViolation,
    /// Invariant violation
    InvariantViolation,
    /// Theorem proof failure
    TheoremFailure,
    /// Model checking failure
    ModelCheckFailure,
    /// Symbolic execution error
    SymbolicExecutionError,
    /// Numerical instability
    NumericalInstability,
    /// Performance issue
    PerformanceIssue,
}

/// Issue severity
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum IssueSeverity {
    /// Low severity - informational
    Low,
    /// Medium severity - potential problem
    Medium,
    /// High severity - likely problem
    High,
    /// Critical severity - definite problem
    Critical,
}

/// Circuit location
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CircuitLocation {
    /// Gate index
    pub gate_index: usize,
    /// Qubit indices
    pub qubit_indices: Vec<usize>,
    /// Circuit depth
    pub depth: usize,
}

/// Verification metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationMetadata {
    /// Verification timestamp
    pub timestamp: SystemTime,
    /// Verifier version
    pub verifier_version: String,
    /// `SciRS2` version
    pub scirs2_version: String,
    /// Verification configuration
    pub config: VerifierConfig,
    /// Hardware information
    pub hardware_info: HashMap<String, String>,
}

/// Proof strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProofStrategy {
    /// Direct proof
    Direct,
    /// Proof by contradiction
    Contradiction,
    /// Proof by induction
    Induction,
    /// Case analysis
    CaseAnalysis,
    /// Symbolic computation
    SymbolicComputation,
    /// Numerical verification
    NumericalVerification,
    /// Statistical testing
    StatisticalTesting,
}

/// Error models for quantum error correction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ErrorModel {
    /// Depolarizing noise
    Depolarizing { probability: f64 },
    /// Bit flip errors
    BitFlip { probability: f64 },
    /// Phase flip errors
    PhaseFlip { probability: f64 },
    /// Amplitude damping
    AmplitudeDamping { gamma: f64 },
    /// Custom error model
    Custom {
        description: String,
        parameters: HashMap<String, f64>,
    },
}

/// Expected output for algorithm verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExpectedOutput {
    /// Classical bit string
    ClassicalBits { bits: Vec<bool> },
    /// Quantum state
    QuantumState { state: Vec<Complex64> },
    /// Probability distribution
    ProbabilityDistribution { probabilities: Vec<f64> },
    /// Measurement statistics
    MeasurementStats { mean: f64, variance: f64 },
}

/// Proof obligations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofObligation {
    /// Obligation name
    pub name: String,
    /// Preconditions
    pub preconditions: Vec<String>,
    /// Postconditions
    pub postconditions: Vec<String>,
    /// Proof steps
    pub proof_steps: Vec<ProofStep>,
}

/// Test case for functional correctness
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestCase {
    /// Input state
    pub input: Vec<Complex64>,
    /// Expected output
    pub expected_output: Vec<Complex64>,
    /// Test description
    pub description: String,
    /// Test weight
    pub weight: f64,
}

/// Complexity class for scalability analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ComplexityClass {
    /// Constant O(1)
    Constant,
    /// Logarithmic O(log n)
    Logarithmic,
    /// Linear O(n)
    Linear,
    /// Polynomial O(n^k)
    Polynomial { degree: f64 },
    /// Exponential O(2^n)
    Exponential,
}