File size: 20,133 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# QATAAUM API Reference

**Version:** 1.0  
**Date:** 2026-07-22  
**Project:** QATAAUM Quantum Assembly Runtime

## Table of Contents

1. [Overview](#overview)
2. [Compiler API](#compiler-api)
3. [Simulator API](#simulator-api)
4. [Runtime API](#runtime-api)
5. [FFI API](#ffi-api)
6. [Error Handling](#error-handling)
7. [Examples](#examples)

---

## Overview

QATAAUM provides a comprehensive API for quantum circuit compilation, simulation, and execution. The API is organized into four main modules:

- **Compiler:** Parse, analyze, optimize, and compile quantum circuits
- **Simulator:** Execute circuits on classical simulators
- **Runtime:** Job management, queueing, and execution orchestration
- **FFI:** C-compatible interface for IBM i integration

### Design Principles

- **Type Safety:** Strong typing with Rust's type system
- **Error Handling:** Result types for all fallible operations
- **Zero-Copy:** Minimize allocations and copies
- **Thread Safety:** Safe concurrent access where appropriate
- **Clean Room:** Independent implementation from public specifications

---

## Compiler API

### Parser Module

#### `qataaum_parser::Lexer`



Tokenizes OpenQASM source code.



```rust

pub struct Lexer<'a> {

    source: &'a str,

    position: usize,

}



impl<'a> Lexer<'a> {

    /// Create a new lexer from source code

    pub fn new(source: &'a str) -> Self;

    

    /// Tokenize the entire source

    pub fn tokenize(&mut self) -> Result<Vec<Token>, LexerError>;

}

```



**Example:**

```rust

use qataaum_parser::Lexer;

let source = "OPENQASM 2.0; qreg q[2]; h q[0];";
let mut lexer = Lexer::new(source);
let tokens = lexer.tokenize()?;
```



#### `qataaum_parser::Parser`



Parses tokens into an Abstract Syntax Tree (AST).



```rust

pub struct Parser {

    tokens: Vec<Token>,

    position: usize,

}



impl Parser {

    /// Create a new parser from tokens

    pub fn new(tokens: Vec<Token>) -> Self;

    

    /// Parse tokens into an AST

    pub fn parse(&mut self) -> Result<Program, ParseError>;

}

```

**Example:**
```rust

use qataaum_parser::{Lexer, Parser};



let source = "OPENQASM 2.0; qreg q[2]; h q[0];";

let mut lexer = Lexer::new(source);

let tokens = lexer.tokenize()?;

let mut parser = Parser::new(tokens);

let ast = parser.parse()?;

```

### Semantic Analysis Module

#### `qataaum_semantic::SemanticAnalyzer`



Performs type checking and semantic validation.



```rust

pub struct SemanticAnalyzer {

    symbol_table: SymbolTable,
    errors: Vec<SemanticError>,

}


impl SemanticAnalyzer {
    /// Create a new semantic analyzer

    pub fn new() -> Self;

    

    /// Analyze an AST

    pub fn analyze(&mut self, program: &Program) -> Result<(), SemanticError>;

    

    /// Get the symbol table

    pub fn symbol_table(&self) -> &SymbolTable;

}

```


**Example:**
```rust

use qataaum_semantic::SemanticAnalyzer;



let mut analyzer = SemanticAnalyzer::new();

analyzer.analyze(&ast)?;

```

### IR Module

#### `qataaum_ir::IrBuilder`



Constructs typed intermediate representation.



```rust

pub struct IrBuilder {

    next_id: usize,
}

impl IrBuilder {
    /// Create a new IR builder

    pub fn new() -> Self;

    

    /// Build IR from AST

    pub fn build(&mut self, program: &Program) -> Result<TypedProgram, IrError>;

}

```


#### IR Levels

The QATAAUM compiler uses a 9-level IR pipeline:

1. **Level 0:** Source AST (lossless syntax representation)
2. **Level 1:** Typed AST (resolved names, types, effects)
3. **Level 2:** CFG (control-flow graph)
4. **Level 3:** SSA (static single assignment)
5. **Level 4:** GATE (hardware-independent gates)
6. **Level 5:** TOPO (topology-aware placement)
7. **Level 6:** SCHEDULE (time-aware scheduling)
8. **Level 7:** PULSE (pulse-level representation)
9. **Level 8:** EXEC (executable backend package)

**Example:**
```rust

use qataaum_ir::{IrBuilder, cfg::CfgBuilder, ssa::SsaBuilder};



// Build typed IR

let mut ir_builder = IrBuilder::new();

let typed_ir = ir_builder.build(&ast)?;



// Build CFG

let mut cfg_builder = CfgBuilder::new();

let cfg = cfg_builder.build(&typed_ir)?;



// Build SSA

let mut ssa_builder = SsaBuilder::new();

let ssa = ssa_builder.build(&cfg)?;

```

### Optimization Passes

#### `qataaum_passes::Pass`



Base trait for optimization passes.



```rust

pub trait Pass {

    type Input;

    type Output;

    

    /// Run the optimization pass

    fn run(&self, input: Self::Input) -> Result<Self::Output, PassError>;

}

```



#### `qataaum_passes::GateCancellationPass`

Eliminates inverse gate pairs.

```rust

pub struct GateCancellationPass;



impl GateCancellationPass {

    pub fn new() -> Self;

}



impl Pass for GateCancellationPass {

    type Input = GateIr;

    type Output = GateIr;

    

    fn run(&self, input: Self::Input) -> Result<Self::Output, PassError>;

}

```

**Example:**
```rust

use qataaum_passes::{GateCancellationPass, Pass};



let pass = GateCancellationPass::new();

let optimized = pass.run(gate_ir)?;

```

#### `qataaum_passes::RotationFoldingPass`



Combines consecutive rotation gates.



```rust

pub struct RotationFoldingPass;



impl RotationFoldingPass {

    pub fn new() -> Self;

}

```



**Example:**

```rust

use qataaum_passes::{RotationFoldingPass, Pass};

let pass = RotationFoldingPass::new();
let optimized = pass.run(gate_ir)?;

```



### Routing Module



#### `qataaum_routing::SabreRouter`

SABRE-inspired routing algorithm for qubit placement.

```rust

pub struct SabreRouter {

    topology: Topology,

    heuristic_weight: f64,

}



impl SabreRouter {

    /// Create router with linear topology

    pub fn new_linear(num_qubits: usize) -> Self;

    

    /// Create router with custom topology

    pub fn new(topology: Topology) -> Self;

    

    /// Route a circuit to the topology

    pub fn route(&mut self, circuit: &GateIr) -> Result<RoutedCircuit, RoutingError>;

}

```

**Example:**
```rust

use qataaum_routing::SabreRouter;



let mut router = SabreRouter::new_linear(5);

let routed = router.route(&gate_ir)?;

```

---

## Simulator API

### State Vector Simulator

#### `qataaum_simulator::StateVectorSimulator`



Exact state vector simulation.



```rust

pub struct StateVectorSimulator {

    num_qubits: usize,
    state: Vec<Complex64>,

}


impl StateVectorSimulator {
    /// Create a new simulator with n qubits (initialized to |0⟩)

    pub fn new(num_qubits: usize) -> Self;

    

    /// Apply Hadamard gate

    pub fn h(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    

    /// Apply Pauli-X gate

    pub fn x(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    

    /// Apply Pauli-Y gate

    pub fn y(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    

    /// Apply Pauli-Z gate

    pub fn z(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    

    /// Apply CNOT gate

    pub fn cx(&mut self, control: usize, target: usize) -> Result<(), SimulatorError>;

    

    /// Apply rotation-X gate

    pub fn rx(&mut self, qubit: usize, theta: f64) -> Result<(), SimulatorError>;

    

    /// Apply rotation-Y gate

    pub fn ry(&mut self, qubit: usize, theta: f64) -> Result<(), SimulatorError>;

    

    /// Apply rotation-Z gate

    pub fn rz(&mut self, qubit: usize, theta: f64) -> Result<(), SimulatorError>;

    

    /// Apply controlled-phase gate

    pub fn cp(&mut self, control: usize, target: usize, theta: f64) -> Result<(), SimulatorError>;

    

    /// Measure a qubit (collapses state)

    pub fn measure(&mut self, qubit: usize) -> Result<bool, SimulatorError>;

    

    /// Get probability of measuring |1⟩ on a qubit

    pub fn probability(&self, qubit: usize) -> Result<f64, SimulatorError>;

    

    /// Get the full state vector

    pub fn state(&self) -> &[Complex64];

    

    /// Reset to |0⟩ state

    pub fn reset(&mut self);

}

```


**Example:**
```rust

use qataaum_simulator::StateVectorSimulator;



let mut sim = StateVectorSimulator::new(2);

sim.h(0)?;

sim.cx(0, 1)?;

let result = sim.measure(0)?;

```

### Density Matrix Simulator

#### `qataaum_simulator::DensityMatrixSimulator`



Mixed-state simulation with noise support.



```rust

pub struct DensityMatrixSimulator {

    num_qubits: usize,
    density_matrix: Array2<Complex64>,

}


impl DensityMatrixSimulator {
    /// Create a new simulator with n qubits

    pub fn new(num_qubits: usize) -> Self;

    

    /// Apply single-qubit gates (same as StateVectorSimulator)

    pub fn h(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    pub fn x(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    pub fn y(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    pub fn z(&mut self, qubit: usize) -> Result<(), SimulatorError>;

    

    /// Apply two-qubit gates

    pub fn cx(&mut self, control: usize, target: usize) -> Result<(), SimulatorError>;

    pub fn cp(&mut self, control: usize, target: usize, theta: f64) -> Result<(), SimulatorError>;

    

    /// Apply noise channels

    pub fn apply_depolarizing_noise(&mut self, qubit: usize, p: f64) -> Result<(), SimulatorError>;

    pub fn apply_amplitude_damping(&mut self, qubit: usize, gamma: f64) -> Result<(), SimulatorError>;

    pub fn apply_phase_damping(&mut self, qubit: usize, gamma: f64) -> Result<(), SimulatorError>;

    

    /// Measure a qubit

    pub fn measure(&mut self, qubit: usize) -> Result<bool, SimulatorError>;

    

    /// Get the density matrix

    pub fn density_matrix(&self) -> &Array2<Complex64>;

    

    /// Calculate purity

    pub fn purity(&self) -> f64;

    

    /// Reset to |0⟩ state

    pub fn reset(&mut self);

}

```


**Example:**
```rust

use qataaum_simulator::DensityMatrixSimulator;



let mut sim = DensityMatrixSimulator::new(2);

sim.h(0)?;

sim.apply_depolarizing_noise(0, 0.01)?;

sim.cx(0, 1)?;

let purity = sim.purity();

```

---

## Runtime API

### Job Management

#### `shadow_rpg_q::Job`

Represents a quantum job.

```rust

pub struct Job {

    id: String,

    source: String,

    target: String,

    priority: u8,

    created_at: SystemTime,

}



impl Job {

    /// Create a new job

    pub fn new(id: &str, source: &str, target: &str, priority: u8) -> Self;

    

    /// Get job ID

    pub fn id(&self) -> &str;

    

    /// Get source code

    pub fn source(&self) -> &str;

    

    /// Get target backend

    pub fn target(&self) -> &str;

    

    /// Get priority (0-9, higher is more urgent)

    pub fn priority(&self) -> u8;

    

    /// Get creation timestamp

    pub fn created_at(&self) -> SystemTime;

}

```

**Example:**
```rust

use shadow_rpg_q::Job;



let job = Job::new(

    "job-001",

    "OPENQASM 2.0; qreg q[2]; h q[0]; cx q[0],q[1];",

    "simulator",

    5

);

```

### Queue Management

#### `shadow_rpg_q::JobQueue`

Priority queue for job scheduling.

```rust

pub struct JobQueue {

    jobs: BinaryHeap<PriorityJob>,

}



impl JobQueue {

    /// Create a new empty queue

    pub fn new() -> Self;

    

    /// Enqueue a job

    pub fn enqueue(&mut self, job: Job) -> Result<(), QueueError>;

    

    /// Dequeue the highest priority job

    pub fn dequeue(&mut self) -> Result<Job, QueueError>;

    

    /// Get queue length

    pub fn len(&self) -> usize;

    

    /// Check if queue is empty

    pub fn is_empty(&self) -> bool;

}

```

**Example:**
```rust

use shadow_rpg_q::{Job, JobQueue};



let mut queue = JobQueue::new();

let job = Job::new("job-001", source, "simulator", 5);

queue.enqueue(job)?;

let next_job = queue.dequeue()?;

```

### Journal

#### `shadow_rpg_q::Journal`

Append-only audit log for job state transitions.

```rust

pub struct Journal {

    entries: Vec<JournalEntry>,

}



impl Journal {

    /// Create a new journal

    pub fn new() -> Self;

    

    /// Write a journal entry

    pub fn write_entry(&mut self, state: &str, job: &Job, message: &str) -> Result<(), JournalError>;

    

    /// Read all entries for a job

    pub fn read_entries(&self, job_id: &str) -> Result<Vec<&JournalEntry>, JournalError>;

    

    /// Get total entry count

    pub fn len(&self) -> usize;

}

```

**Example:**
```rust

use shadow_rpg_q::Journal;



let mut journal = Journal::new();

journal.write_entry("RECEIVED", &job, "Job received")?;

journal.write_entry("COMPILED", &job, "Compilation complete")?;

let entries = journal.read_entries("job-001")?;

```

### Receipt

#### `shadow_rpg_q::Receipt`

Cryptographically sealed execution receipt.

```rust

pub struct Receipt {

    job_id: String,

    state: String,

    message: String,

    timestamp: SystemTime,

    hash: Option<String>,

}



impl Receipt {

    /// Create a new receipt

    pub fn new(job: &Job, state: &str, message: &str) -> Self;

    

    /// Seal the receipt with a cryptographic hash

    pub fn seal(&mut self) -> Result<(), ReceiptError>;

    

    /// Verify receipt integrity

    pub fn verify(&self) -> Result<bool, ReceiptError>;

    

    /// Get receipt hash

    pub fn hash(&self) -> Option<&str>;

}

```

**Example:**
```rust

use shadow_rpg_q::Receipt;



let mut receipt = Receipt::new(&job, "COMPLETED", "Success");

receipt.seal()?;

let is_valid = receipt.verify()?;

```

### Executor

#### `shadow_rpg_q::Executor`

Job execution orchestrator.

```rust

pub struct Executor {

    compiler: Compiler,

    simulator: Box<dyn Simulator>,

}



impl Executor {

    /// Create a new executor

    pub fn new() -> Self;

    

    /// Execute a job

    pub fn execute(&mut self, job: Job) -> Result<ExecutionResult, ExecutorError>;

}

```

**Example:**
```rust

use shadow_rpg_q::{Job, Executor};



let mut executor = Executor::new();

let job = Job::new("job-001", source, "simulator", 5);

let result = executor.execute(job)?;

```

---

## FFI API

### C-Compatible Interface

#### `qataaum_ibmi_ffi`

C-compatible FFI for IBM i integration.

```c

// Opaque handle types

typedef struct QataaumCompiler QataaumCompiler;

typedef struct QataaumSimulator QataaumSimulator;

typedef struct QataaumJob QataaumJob;



// Compiler functions

QataaumCompiler* qataaum_compiler_new(void);

void qataaum_compiler_free(QataaumCompiler* compiler);

int qataaum_compiler_compile(

    QataaumCompiler* compiler,

    const char* source,

    size_t source_len,

    char** output,

    size_t* output_len

);



// Simulator functions

QataaumSimulator* qataaum_simulator_new(size_t num_qubits);

void qataaum_simulator_free(QataaumSimulator* sim);

int qataaum_simulator_apply_h(QataaumSimulator* sim, size_t qubit);

int qataaum_simulator_apply_cx(QataaumSimulator* sim, size_t control, size_t target);

int qataaum_simulator_measure(QataaumSimulator* sim, size_t qubit, int* result);



// Job functions

QataaumJob* qataaum_job_new(

    const char* id,

    const char* source,

    const char* target,

    uint8_t priority

);

void qataaum_job_free(QataaumJob* job);

int qataaum_job_execute(QataaumJob* job, char** result, size_t* result_len);

```

**RPG Example:**
```rpg

DCL-PR qataaum_compiler_new POINTER EXTPROC(*CWIDEN:'qataaum_compiler_new');

END-PR;



DCL-PR qataaum_compiler_compile INT(10) EXTPROC(*CWIDEN:'qataaum_compiler_compile');

  compiler POINTER VALUE;

  source POINTER VALUE;

  source_len UNS(10) VALUE;

  output POINTER;

  output_len POINTER;

END-PR;



DCL-S compiler POINTER;

DCL-S source CHAR(1000);

DCL-S output POINTER;

DCL-S output_len UNS(10);



compiler = qataaum_compiler_new();

source = 'OPENQASM 2.0; qreg q[2]; h q[0];';

qataaum_compiler_compile(compiler: %ADDR(source): %LEN(source): %ADDR(output): %ADDR(output_len));

```

---

## Error Handling

All fallible operations return `Result<T, E>` types:

### Error Types

```rust

// Parser errors

pub enum LexerError {

    UnexpectedCharacter(char, usize),

    UnterminatedString(usize),

}



pub enum ParseError {

    UnexpectedToken(Token),

    UnexpectedEof,

    InvalidSyntax(String),

}



// Semantic errors

pub enum SemanticError {

    UndeclaredVariable(String),

    TypeMismatch { expected: Type, found: Type },

    InvalidOperation(String),

}



// IR errors

pub enum IrError {

    InvalidTransformation(String),

    UnsupportedFeature(String),

}



// Simulator errors

pub enum SimulatorError {

    InvalidQubit(usize),

    InvalidParameter(String),

    MeasurementError(String),

}



// Runtime errors

pub enum ExecutorError {

    CompilationFailed(String),

    SimulationFailed(String),

    InvalidJob(String),

}

```

### Error Handling Pattern

```rust

use qataaum_parser::{Lexer, Parser};



fn compile_circuit(source: &str) -> Result<Program, Box<dyn std::error::Error>> {

    let mut lexer = Lexer::new(source);

    let tokens = lexer.tokenize()?;

    let mut parser = Parser::new(tokens);

    let ast = parser.parse()?;

    Ok(ast)

}



match compile_circuit(source) {

    Ok(program) => println!("Success!"),

    Err(e) => eprintln!("Error: {}", e),

}

```

---

## Examples

### Complete Compilation Pipeline

```rust

use qataaum_parser::{Lexer, Parser};

use qataaum_semantic::SemanticAnalyzer;

use qataaum_ir::{IrBuilder, gate::GateIrBuilder};

use qataaum_passes::{GateCancellationPass, Pass};

use qataaum_routing::SabreRouter;



fn compile_and_route(source: &str) -> Result<RoutedCircuit, Box<dyn std::error::Error>> {

    // Parse

    let mut lexer = Lexer::new(source);

    let tokens = lexer.tokenize()?;

    let mut parser = Parser::new(tokens);

    let ast = parser.parse()?;

    

    // Semantic analysis

    let mut analyzer = SemanticAnalyzer::new();

    analyzer.analyze(&ast)?;

    

    // Build IR

    let mut ir_builder = IrBuilder::new();

    let ir = ir_builder.build(&ast)?;

    

    // Build Gate IR

    let mut gate_builder = GateIrBuilder::new();

    let mut gate_ir = gate_builder.build(&ir)?;

    

    // Optimize

    let pass = GateCancellationPass::new();

    gate_ir = pass.run(gate_ir)?;

    

    // Route

    let num_qubits = gate_ir.num_qubits();

    let mut router = SabreRouter::new_linear(num_qubits);

    let routed = router.route(&gate_ir)?;

    

    Ok(routed)

}

```

### Simulation with Noise

```rust

use qataaum_simulator::DensityMatrixSimulator;



fn simulate_with_noise() -> Result<f64, SimulatorError> {

    let mut sim = DensityMatrixSimulator::new(2);

    

    // Create Bell state with noise

    sim.h(0)?;

    sim.apply_depolarizing_noise(0, 0.01)?;

    sim.cx(0, 1)?;

    sim.apply_depolarizing_noise(1, 0.01)?;

    

    // Calculate purity

    let purity = sim.purity();

    Ok(purity)

}

```

### Job Execution Workflow

```rust

use shadow_rpg_q::{Job, JobQueue, Journal, Receipt, Executor};



fn execute_workflow(source: &str) -> Result<Receipt, Box<dyn std::error::Error>> {

    // Create job

    let job = Job::new("job-001", source, "simulator", 5);

    

    // Enqueue

    let mut queue = JobQueue::new();

    queue.enqueue(job.clone())?;

    

    // Journal

    let mut journal = Journal::new();

    journal.write_entry("RECEIVED", &job, "Job received")?;

    

    // Execute

    let mut executor = Executor::new();

    let result = executor.execute(job.clone())?;

    

    journal.write_entry("COMPLETED", &job, "Execution complete")?;

    

    // Create and seal receipt

    let mut receipt = Receipt::new(&job, "COMPLETED", "Success");

    receipt.seal()?;

    

    Ok(receipt)

}

```

---

## API Stability

- **Stable:** Parser, Semantic, IR, Simulator, Runtime core APIs
- **Experimental:** Pulse compiler, advanced optimization passes
- **Internal:** IR transformation details, routing heuristics

Breaking changes will be documented in release notes.

---

**Generated:** 2026-07-22  
**Version:** 1.0  
**License:** Apache-2.0