qudit-expr 0.3.2

Accelerated and Extensible Quantum Library
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
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
use inkwell::AddressSpace;
use inkwell::builder::Builder;
use inkwell::values::FloatValue;
use inkwell::values::FunctionValue;

use coe::is_same;
use qudit_core::RealScalar;
use std::collections::HashMap;

use crate::ComplexExpression;
use crate::Expression;

use super::builtins::Builtins;
use super::module::Module;

#[derive(Debug)]
pub struct CodeGenError {
    pub message: String,
}

impl CodeGenError {
    pub fn new(message: &str) -> Self {
        CodeGenError {
            message: message.to_string(),
        }
    }
}

type CodeGenResult<T> = Result<T, CodeGenError>;

pub struct CodeGenerator<'ctx, R: RealScalar> {
    pub context: &'ctx Module<R>,
    pub builder: Builder<'ctx>,

    variables: HashMap<String, FloatValue<'ctx>>,
    expressions: HashMap<String, FloatValue<'ctx>>,
    functions: HashMap<String, FunctionValue<'ctx>>,
    fn_value_opt: Option<FunctionValue<'ctx>>,
    output_ptr_idx: Option<u32>,
}

impl<'ctx, R: RealScalar> CodeGenerator<'ctx, R> {
    pub fn new(context: &'ctx Module<R>) -> Self {
        let builder = context.context().create_builder();
        CodeGenerator {
            context,
            builder,
            variables: HashMap::new(),
            functions: HashMap::new(),
            expressions: HashMap::new(),
            fn_value_opt: None,
            output_ptr_idx: None,
        }
    }

    fn int_type(&self) -> inkwell::types::IntType<'ctx> {
        if is_same::<R, f32>() {
            self.context.context().i32_type()
        } else if is_same::<R, f64>() {
            self.context.context().i64_type()
        } else {
            panic!("Unknown bit width");
        }
    }

    fn float_type(&self) -> inkwell::types::FloatType<'ctx> {
        if is_same::<R, f32>() {
            self.context.context().f32_type()
        } else if is_same::<R, f64>() {
            self.context.context().f64_type()
        } else {
            panic!("Unknown bit width");
        }
    }

    fn gen_write_func_proto(&self, name: &str) -> CodeGenResult<FunctionValue<'ctx>> {
        let ret_type = self.context.context().void_type();
        let ptr_type = self.context.context().ptr_type(AddressSpace::default());
        // Match Rust WriteFunc<R> signature: (*const R, *mut R, *const u64, *const u64, u64, *const bool)
        let param_types = vec![
            ptr_type.into(),                          // *const R
            ptr_type.into(),                          // *mut R
            ptr_type.into(),                          // *const u64
            ptr_type.into(),                          // *const u64
            self.context.context().i64_type().into(), // u64
            ptr_type.into(),                          // *const bool
        ];
        let func_type = ret_type.fn_type(&param_types, false);
        let func = self
            .context
            .with_module(|module| module.add_function(name, func_type, None));
        Ok(func)
    }

    fn build_expression(&mut self, expr: &Expression) -> CodeGenResult<FloatValue<'ctx>> {
        let expr_str = expr.to_string();
        let cached = self.expressions.get(&expr_str);
        if let Some(c) = cached {
            return Ok(*c);
        }

        let val = match expr {
            Expression::Pi => Ok(self.float_type().const_float(std::f64::consts::PI)),
            Expression::Constant(_) => Ok(self.float_type().const_float(expr.to_float())),
            Expression::Variable(name) => self
                .variables
                .get(name)
                .ok_or(CodeGenError::new(&format!("Variable {} not found", name)))
                .copied(),
            Expression::Neg(expr) => {
                let val = self.build_expression(expr)?;
                Ok(self.builder.build_float_neg(val, "tmp").unwrap())
            }
            Expression::Add(lhs, rhs) => {
                let lhs_val = self.build_expression(lhs)?;
                let rhs_val = self.build_expression(rhs)?;
                Ok(self
                    .builder
                    .build_float_add(lhs_val, rhs_val, "tmp")
                    .unwrap())
            }
            Expression::Sub(lhs, rhs) => {
                let lhs_val = self.build_expression(lhs)?;
                let rhs_val = self.build_expression(rhs)?;
                Ok(self
                    .builder
                    .build_float_sub(lhs_val, rhs_val, "tmp")
                    .unwrap())
            }
            Expression::Mul(lhs, rhs) => {
                let lhs_val = self.build_expression(lhs)?;
                let rhs_val = self.build_expression(rhs)?;
                Ok(self
                    .builder
                    .build_float_mul(lhs_val, rhs_val, "tmp")
                    .unwrap())
            }
            Expression::Div(lhs, rhs) => {
                let lhs_val = self.build_expression(lhs)?;
                let rhs_val = self.build_expression(rhs)?;
                Ok(self
                    .builder
                    .build_float_div(lhs_val, rhs_val, "tmp")
                    .unwrap())
            }
            Expression::Pow(base, exponent) => {
                let base_val = self.build_expression(base)?;
                let exponent_val = self.build_expression(exponent)?;
                let pow = self.get_builtin("pow");
                let args = [base_val.into(), exponent_val.into()];
                let val = self
                    .builder
                    .build_call(pow, &args, "tmp")
                    .unwrap()
                    .try_as_basic_value()
                    .basic()
                    .unwrap()
                    .into_float_value();
                Ok(val)
            }
            Expression::Sqrt(expr) => {
                let arg = self.build_expression(expr)?;
                let sqrt = self.get_builtin("sqrt");
                let val = self
                    .builder
                    .build_call(sqrt, &[arg.into()], "tmp")
                    .unwrap()
                    .try_as_basic_value()
                    .basic()
                    .unwrap()
                    .into_float_value();
                Ok(val)
            }
            Expression::Sin(expr) => {
                let arg = self.build_expression(expr)?;
                let sin = self.get_builtin("sin");
                let val = self
                    .builder
                    .build_call(sin, &[arg.into()], "tmp")
                    .unwrap()
                    .try_as_basic_value()
                    .basic()
                    .unwrap()
                    .into_float_value();
                Ok(val)
            }
            Expression::Cos(expr) => {
                let arg = self.build_expression(expr)?;
                let cos = self.get_builtin("cos");
                let val = self
                    .builder
                    .build_call(cos, &[arg.into()], "tmp")
                    .unwrap()
                    .try_as_basic_value()
                    .basic()
                    .unwrap()
                    .into_float_value();
                Ok(val)
            }
        };

        if let Ok(val) = val {
            self.expressions.insert(expr_str, val);
        }
        val
    }

    pub fn compile_expr(
        &mut self,
        expr: &ComplexExpression,
        re_offset: usize,
        need_to_write_real_zero: bool,
    ) -> CodeGenResult<()> {
        let re_offset: u64 = re_offset as u64;
        let ptr_idx = self
            .output_ptr_idx
            .to_owned()
            .expect("Output pointer index not set");
        let ptr = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(ptr_idx)
            .unwrap()
            .into_pointer_value();

        if need_to_write_real_zero || !expr.real.is_zero_fast() {
            let val = self.build_expression(&expr.real)?;
            let offset = self.int_type().const_int(re_offset, false);
            let offset_ptr = unsafe {
                self.builder
                    .build_gep(self.float_type(), ptr, &[offset], "offset_ptr")
                    .unwrap()
            };

            match self.builder.build_store(offset_ptr, val) {
                Ok(_) => {}
                Err(e) => {
                    return Err(CodeGenError::new(&format!("Error storing value: {}", e)));
                }
            };
        }

        if !expr.imag.is_zero_fast() {
            let val = self.build_expression(&expr.imag)?;
            let offset = self.int_type().const_int(re_offset + 1, false);
            let offset_ptr = unsafe {
                self.builder
                    .build_gep(self.float_type(), ptr, &[offset], "offset_ptr")
                    .unwrap()
            };

            match self.builder.build_store(offset_ptr, val) {
                Ok(_) => {}
                Err(e) => {
                    return Err(CodeGenError::new(&format!("Error storing value: {}", e)));
                }
            };
        }

        Ok(())
    }

    fn build_var_table(&mut self, variables: &[String]) {
        self.variables.clear();
        let params_ptr = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(0)
            .unwrap()
            .into_pointer_value();
        let param_offset_ptr = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(2)
            .unwrap()
            .into_pointer_value();

        for (map_idx, var_name) in variables.iter().enumerate() {
            // Load the actual offset from param_offset_ptr using map_idx
            // param_offset_ptr points to u64 values according to Rust signature
            let map_idx_val = self
                .context
                .context()
                .i64_type()
                .const_int(map_idx as u64, false);
            let actual_offset_ptr = unsafe {
                self.builder
                    .build_gep(
                        self.context.context().i64_type(),
                        param_offset_ptr,
                        &[map_idx_val],
                        "actual_offset_ptr_gep",
                    )
                    .unwrap()
            };
            let actual_offset_val = self
                .builder
                .build_load(
                    self.context.context().i64_type(),
                    actual_offset_ptr,
                    "actual_offset_val",
                )
                .unwrap()
                .into_int_value();

            // Use the actual_offset_val to index into params_ptr
            let var_ptr = unsafe {
                self.builder
                    .build_gep(
                        self.float_type(),
                        params_ptr,
                        &[actual_offset_val],
                        "var_ptr_gep",
                    )
                    .unwrap()
            };

            let val = self
                .builder
                .build_load(self.float_type(), var_ptr, var_name)
                .unwrap()
                .into_float_value();
            self.variables.insert(var_name.to_owned(), val);
        }
    }

    fn get_builtin(&mut self, name: &str) -> FunctionValue<'ctx> {
        if let Some(f) = self.functions.get(name) {
            return *f;
        }

        let b = match Builtins::from_str(name) {
            Some(b) => b,
            None => {
                panic!("Unsupported builtin function: {}", name);
            }
        };

        let intr = match b.intrinsic() {
            Some(i) => i,
            None => {
                panic!("Unsupported builtin function: {}", name);
            }
        };

        let decl = self
            .context
            .with_module(|module| intr.get_declaration(&module, &[self.float_type().into()]));

        let fn_value = match decl {
            Some(f) => f,
            None => {
                panic!("Unsupported builtin function: {}", name);
            }
        };

        self.functions.insert(name.to_string(), fn_value);
        fn_value
    }

    // fn get_expression(&mut self, name: &str) -> Option<FloatValue<'ctx>> {
    //     if let Some(c) = self.expressions.get(name) {
    //         return Some(c.clone());
    //     }

    //     let c = match name {
    //         "pi" => Some(self.float_type().const_float(std::f64::consts::PI)),
    //         "Ï€" => Some(self.float_type().const_float(std::f64::consts::PI)),
    //         "e" => Some(self.float_type().const_float(std::f64::consts::E)),
    //         _ => None
    //     };

    //     if let Some(c) = c {
    //         self.expressions.insert(name.to_string(), c);
    //         return Some(c);
    //     }

    //     None
    // }

    pub fn gen_func(
        &mut self,
        fn_name: &str,
        fn_expr: &[Expression],
        var_table: &[String],
        fn_len: usize,
    ) -> CodeGenResult<()> {
        self.expressions.clear();
        let func = self.gen_write_func_proto(fn_name)?;
        let entry = self.context.context().append_basic_block(func, "entry");
        self.builder.position_at_end(entry);
        self.fn_value_opt = Some(func);
        // println!("name: {:?}, var_table: {:?}", fn_name, var_table);
        self.build_var_table(var_table);

        let output_ptr = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(1)
            .unwrap()
            .into_pointer_value();
        let output_map_ptr = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(3)
            .unwrap()
            .into_pointer_value();
        let fn_unit_offset = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(4)
            .unwrap()
            .into_int_value();
        let const_param_ptr = self
            .fn_value_opt
            .unwrap()
            .get_nth_param(5)
            .unwrap()
            .into_pointer_value();
        let int_increment = self.context.context().i64_type().const_int(1u64, false);
        let mut dyn_fn_unit_idx = self.context.context().i64_type().const_int(0u64, false);

        for (fn_unit_idx, fn_unit_exprs) in fn_expr.chunks(fn_len).enumerate() {
            let current_fn = self.fn_value_opt.unwrap();
            let compute_block = self
                .context
                .context()
                .append_basic_block(current_fn, &format!("compute_unit_{}", fn_unit_idx));
            let skip_block = self
                .context
                .context()
                .append_basic_block(current_fn, &format!("skip_unit_{}", fn_unit_idx));

            if fn_unit_idx == 0 {
                // First function unit is the function, always compute.
                self.builder
                    .build_unconditional_branch(compute_block)
                    .unwrap();
            } else if fn_unit_idx <= var_table.len() {
                // Next var_table.len() function units are the partials in the gradient.
                // Check if the corresponding parameter is constant.
                let param_idx_for_const_check = self
                    .context
                    .context()
                    .i64_type()
                    .const_int((fn_unit_idx - 1) as u64, false);
                let const_param_elem_ptr = unsafe {
                    self.builder
                        .build_gep(
                            self.context.context().i8_type(),
                            const_param_ptr,
                            &[param_idx_for_const_check],
                            "const_param_elem_ptr",
                        )
                        .unwrap()
                };
                let is_constant_val = self
                    .builder
                    .build_load(
                        self.context.context().i8_type(),
                        const_param_elem_ptr,
                        "is_constant",
                    )
                    .unwrap()
                    .into_int_value();

                let is_constant_true = self
                    .builder
                    .build_int_compare(
                        inkwell::IntPredicate::EQ,
                        is_constant_val,
                        self.context.context().i8_type().const_int(1, false),
                        "is_constant_true",
                    )
                    .unwrap();

                // If is_constant_true, branch to skip_block. Else, branch to compute_block.
                self.builder
                    .build_conditional_branch(is_constant_true, skip_block, compute_block)
                    .unwrap();
            } else {
                // Last are the hessian units

                let index = fn_unit_idx - var_table.len() - 1;
                let param_j = ((((8 * index + 1) as f64).sqrt().floor() as usize) - 1) / 2;
                let param_i = index - param_j * (param_j + 1) / 2;

                // Check if the corresponding parameter is constant.
                let param_idx_i_for_const_check = self
                    .context
                    .context()
                    .i64_type()
                    .const_int((param_i - 1) as u64, false);
                let const_param_elem_ptr = unsafe {
                    self.builder
                        .build_gep(
                            self.context.context().i8_type(),
                            const_param_ptr,
                            &[param_idx_i_for_const_check],
                            "const_param_i_elem_ptr",
                        )
                        .unwrap()
                };
                let is_i_constant_val = self
                    .builder
                    .build_load(
                        self.context.context().i8_type(),
                        const_param_elem_ptr,
                        "is_i_constant",
                    )
                    .unwrap()
                    .into_int_value();

                let is_i_constant_true = self
                    .builder
                    .build_int_compare(
                        inkwell::IntPredicate::EQ,
                        is_i_constant_val,
                        self.context.context().i8_type().const_int(1, false),
                        "is_i_constant_true",
                    )
                    .unwrap();

                // Check if the corresponding parameter is constant.
                let param_idx_j_for_const_check = self
                    .context
                    .context()
                    .i64_type()
                    .const_int((param_j - 1) as u64, false);
                let const_param_elem_ptr = unsafe {
                    self.builder
                        .build_gep(
                            self.context.context().i8_type(),
                            const_param_ptr,
                            &[param_idx_j_for_const_check],
                            "const_param_j_elem_ptr",
                        )
                        .unwrap()
                };
                let is_j_constant_val = self
                    .builder
                    .build_load(
                        self.context.context().i8_type(),
                        const_param_elem_ptr,
                        "is_j_constant",
                    )
                    .unwrap()
                    .into_int_value();

                let is_j_constant_true = self
                    .builder
                    .build_int_compare(
                        inkwell::IntPredicate::EQ,
                        is_j_constant_val,
                        self.context.context().i8_type().const_int(1, false),
                        "is_j_constant_true",
                    )
                    .unwrap();

                let is_either_constant = self
                    .builder
                    .build_or(is_i_constant_true, is_j_constant_true, "is_either_constant")
                    .unwrap();

                // If is_constant_true, branch to skip_block. Else, branch to compute_block.
                self.builder
                    .build_conditional_branch(is_either_constant, skip_block, compute_block)
                    .unwrap();
            }

            // Position builder at the compute block to emit the actual computation logic
            self.builder.position_at_end(compute_block);

            let this_unit_offset = self
                .builder
                .build_int_mul(fn_unit_offset, dyn_fn_unit_idx, "this_unit_offset")
                .unwrap();
            dyn_fn_unit_idx = self
                .builder
                .build_int_add(dyn_fn_unit_idx, int_increment, "dyn_fn_unit_idx")
                .unwrap();

            for (i, expr) in fn_unit_exprs.iter().enumerate() {
                if expr.is_zero_fast() {
                    continue;
                }

                let val = self.build_expression(expr)?;
                let offset_ptr = unsafe {
                    let output_idx = self.context.context().i64_type().const_int(i as u64, false);
                    let output_map_elem_ptr = self
                        .builder
                        .build_gep(
                            self.context.context().i64_type(),
                            output_map_ptr,
                            &[output_idx],
                            "output_map_elem_ptr",
                        )
                        .unwrap();
                    let output_map_offset = self
                        .builder
                        .build_load(
                            self.context.context().i64_type(),
                            output_map_elem_ptr,
                            "output_map_offset",
                        )
                        .unwrap()
                        .into_int_value();
                    let combined_offset = self
                        .builder
                        .build_int_add(output_map_offset, this_unit_offset, "combined_offset")
                        .unwrap();
                    self.builder
                        .build_gep(
                            self.float_type(),
                            output_ptr,
                            &[combined_offset],
                            "offset_ptr",
                        )
                        .unwrap()
                };

                match self.builder.build_store(offset_ptr, val) {
                    Ok(_) => {}
                    Err(e) => {
                        return Err(CodeGenError::new(&format!("Error storing value: {}", e)));
                    }
                };
            }

            // After computation (if compute_block was taken), branch to the skip_block
            // to ensure control flow continues to the next iteration of the loop.
            self.builder.build_unconditional_branch(skip_block).unwrap();

            // Position builder at the skip block for the next iteration.
            self.builder.position_at_end(skip_block);
        }

        match self.builder.build_return(None) {
            Ok(_) => Ok(()),
            Err(e) => Err(CodeGenError::new(&format!("Error building return: {}", e))),
        }
    }
}