libket 0.7.0

Runtime library for the Ket programming language
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// SPDX-FileCopyrightText: 2026 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
//
// SPDX-License-Identifier: Apache-2.0

//! C API bindings for quantum execution backends.
//!
//! This module defines the FFI-compatible structs ([`CLiveExecution`],
//! [`CBatchExecution`], [`CNativeGateSet`]) that allow external C/C++ quantum
//! simulators or hardware drivers to plug into the Libket execution pipeline.
//!
//! ## JSON serialization contract
//!
//! All data exchanged between Rust and the C callbacks is serialized as JSON:
//!
//! - **Rust → C**: gate instructions, Hamiltonians, and gate matrices are
//!   serialized with [`serde_json`] into null-terminated C strings before
//!   being passed to the corresponding callback.
//! - **C → Rust**: JSON strings returned by C callbacks (e.g., sample counts,
//!   state dumps, native gate sequences) are parsed back with [`serde_json`]
//!   on the Rust side. If parsing fails, [`KetError::SerdeError`] is returned.
//!
//! Heap-allocated strings returned by C callbacks are **not** freed by Rust;
//! the C backend is responsible for their lifetime (typically they are freed
//! via [`super::ket_string_delete`] by the caller of the top-level C API).

use std::{
    ffi::{c_char, CStr, CString},
    ptr::null,
};

use crate::{
    c_api::c_ok,
    error::KetError,
    execution::{
        BatchExecution, DumpData, ExpValueStrategy, GradientStrategy, LiveExecution, NativeGate,
        NativeGateSet, QuantumExecution, SampleData,
    },
    ir::{gate::GateInstruction, hamiltonian::Hamiltonian},
    matrix::Matrix,
    process::QPUConfig,
};

/// FFI-compatible function-pointer table for a *live* (gate-at-a-time) execution backend.
///
/// In live mode each gate is dispatched to the hardware or simulator
/// immediately as it is applied to the process, and measurement results are
/// available without an explicit `ket_process_execute` call.
///
/// Construct a `CLiveExecution` value on the C side, fill in all function
/// pointers, then call [`ket_quantum_execution_live`] to create the
/// corresponding [`QuantumExecution`] variant.
///
/// All callbacks must return `0` on success or a non-zero libket error code on
/// failure.
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CLiveExecution {
    /// Dispatch a single quantum gate to the backend.
    ///
    /// `json` is a null-terminated JSON string encoding the gate instruction
    /// (type, parameters, target qubit, etc.).  Returns `0` on success.
    pub compute_gate: fn(json: *const c_char) -> i32,
    /// Dispatch a sequence of pre-translated native gate instructions to the backend.
    ///
    /// `json` is a null-terminated JSON string encoding the array of native
    /// gate instructions (each a `[name, angles, qubits]` triple). Returns `0`
    /// on success.
    pub compute_native_gates: fn(json: *const c_char) -> i32,

    /// Perform a single-shot measurement of `len` qubits.
    ///
    /// `qubits` points to an array of `len` qubit indices.  On success the
    /// measurement result is written to `*result` as a bitmask where bit `i`
    /// corresponds to `qubits[i]`.  Returns `0` on success.
    pub measure: fn(qubits: *const usize, len: usize, result: &mut u64) -> i32,

    /// Retrieve the full quantum state (probability amplitudes) of `len` qubits.
    ///
    /// `qubits` points to an array of `len` qubit indices.  On success
    /// `*result_json` is set to a heap-allocated null-terminated JSON string
    /// encoding the state; Rust will parse it and then it is the C side's
    /// responsibility to manage the lifetime.  Returns `0` on success.
    pub dump: fn(qubits: *const usize, len: usize, result_json: &mut *const c_char) -> i32,

    /// Perform a repeated measurement sample of `len` qubits over `shots` shots.
    ///
    /// `qubits` points to an array of `len` qubit indices.  On success
    /// `*result_json` is set to a heap-allocated null-terminated JSON string
    /// with the sample counts.  Returns `0` on success.
    pub sample:
        fn(qubits: *const usize, len: usize, shots: usize, result_json: &mut *const c_char) -> i32,

    /// Compute the expectation value of a Hamiltonian on the current quantum state.
    ///
    /// `hamiltonian_json` is a null-terminated JSON string encoding the
    /// Hamiltonian.  On success `*result` is set to the computed expectation
    /// value.  Returns `0` on success.
    pub exp_value: fn(hamiltonian_json: *const c_char, result: &mut f64) -> i32,
}

impl LiveExecution for CLiveExecution {
    fn compute_gate(&mut self, gate: &GateInstruction) -> Result<(), KetError> {
        let Ok(json) = serde_json::to_string(gate) else {
            return Err(KetError::SerdeError);
        };

        let Ok(json) = CString::new(json) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.compute_gate)(json.as_ptr()));

        match err {
            KetError::Success => Ok(()),
            err => Err(err),
        }
    }

    fn compute_native_gates(&mut self, gates: &[NativeGate]) -> Result<(), KetError> {
        let Ok(gates) = serde_json::to_string(gates) else {
            return Err(KetError::SerdeError);
        };

        let Ok(gates) = CString::new(gates) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.compute_native_gates)(gates.as_ptr()));
        match err {
            KetError::Success => Ok(()),
            err => Err(err),
        }
    }

    fn measure(&mut self, qubits: &[usize]) -> Result<u64, KetError> {
        let mut result = 42;

        let err =
            KetError::from_error_code((self.measure)(qubits.as_ptr(), qubits.len(), &mut result));

        match err {
            KetError::Success => Ok(result),
            err => Err(err),
        }
    }

    fn sample(&mut self, qubits: &[usize], shots: usize) -> Result<SampleData, KetError> {
        let mut result_json = null();

        let err = KetError::from_error_code((self.sample)(
            qubits.as_ptr(),
            qubits.len(),
            shots,
            &mut result_json,
        ));

        match err {
            KetError::Success => {
                let Ok(result_json) = { unsafe { CStr::from_ptr(result_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };

                match serde_json::from_str::<SampleData>(result_json) {
                    Ok(sample_date) => Ok(sample_date),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }

    fn exp_value(&mut self, hamiltonian: Hamiltonian) -> Result<f64, KetError> {
        let mut result = f64::NAN;

        let Ok(json) = serde_json::to_string(&hamiltonian) else {
            return Err(KetError::SerdeError);
        };

        let Ok(json) = CString::new(json) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.exp_value)(json.as_ptr(), &mut result));

        match err {
            KetError::Success => Ok(result),
            err => Err(err),
        }
    }

    fn dump(&mut self, qubits: &[usize]) -> Result<DumpData, KetError> {
        let mut result_json = null();

        let err =
            KetError::from_error_code((self.dump)(qubits.as_ptr(), qubits.len(), &mut result_json));

        match err {
            KetError::Success => {
                let Ok(result_json) = { unsafe { CStr::from_ptr(result_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };

                match serde_json::from_str::<DumpData>(result_json) {
                    Ok(dump_data) => Ok(dump_data),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }
}

/// FFI-compatible function-pointer table for a *batch* execution backend.
///
/// In batch mode the entire compiled circuit is serialized and handed to the
/// backend in a single call when `ket_process_execute` is invoked, rather than
/// dispatching gate-by-gate.
///
/// Construct a `CBatchExecution` value on the C side, fill in all function
/// pointers, then call [`ket_quantum_execution_batch`] to create the
/// corresponding [`QuantumExecution`] variant.
///
/// All callbacks must return `0` on success or a non-zero libket error code on
/// failure.
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CBatchExecution {
    pub sample: fn(
        gates_json: *const c_char,
        qubits_to_sample: *const usize,
        qubits_to_sample_len: usize,
        shots: usize,
        sample_json: &mut *const c_char,
    ) -> i32,

    pub exp_value: fn(
        gates_json: *const c_char,
        hamiltonian_list_json: *const c_char,
        result: *mut f64,
    ) -> i32,

    pub sample_native: fn(
        gates_json: *const c_char,
        qubits_to_sample: *const usize,
        qubits_to_sample_len: usize,
        shots: usize,
        sample_json: &mut *const c_char,
    ) -> i32,

    pub exp_value_native: fn(
        gates_json: *const c_char,
        hamiltonian_list_json: *const c_char,
        result: *mut f64,
    ) -> i32,

    pub gradient: fn(
        gates_json: *const c_char,
        hamiltonian_list_json: *const c_char,
        exp_result: &mut f64,
        grad_json: &mut *const c_char,
    ) -> i32,
}

impl BatchExecution for CBatchExecution {
    fn sample(
        &self,
        gates: &[GateInstruction],
        qubits_to_sample: &[usize],
        shots: usize,
    ) -> Result<SampleData, KetError> {
        let mut sample_json = std::ptr::null();

        let Ok(gates_json) = serde_json::to_string(gates) else {
            return Err(KetError::SerdeError);
        };
        let Ok(gates_json) = CString::new(gates_json) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.sample)(
            gates_json.as_ptr(),
            qubits_to_sample.as_ptr(),
            qubits_to_sample.len(),
            shots,
            &mut sample_json,
        ));

        match err {
            KetError::Success => {
                let Ok(result_json) = { unsafe { CStr::from_ptr(sample_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };

                match serde_json::from_str::<SampleData>(result_json) {
                    Ok(sample_data) => Ok(sample_data),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }

    fn exp_value(
        &self,
        gates: &[GateInstruction],
        hamiltonian_list: &[Hamiltonian],
    ) -> Result<Vec<f64>, KetError> {
        let mut result = vec![f64::NAN; hamiltonian_list.len()];

        let Ok(gates_json) = serde_json::to_string(gates) else {
            return Err(KetError::SerdeError);
        };
        let Ok(gates_json) = CString::new(gates_json) else {
            return Err(KetError::SerdeError);
        };

        let Ok(ham_json) = serde_json::to_string(&hamiltonian_list) else {
            return Err(KetError::SerdeError);
        };
        let Ok(ham_json) = CString::new(ham_json) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.exp_value)(
            gates_json.as_ptr(),
            ham_json.as_ptr(),
            result.as_mut_ptr(),
        ));

        match err {
            KetError::Success => Ok(result),
            err => Err(err),
        }
    }

    fn sample_native(
        &self,
        gates: &[NativeGate],
        qubits_to_sample: &[usize],
        shots: usize,
    ) -> Result<SampleData, KetError> {
        let mut sample_json = std::ptr::null();

        let Ok(gates_json) = serde_json::to_string(gates) else {
            return Err(KetError::SerdeError);
        };
        let Ok(gates_json) = CString::new(gates_json) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.sample_native)(
            gates_json.as_ptr(),
            qubits_to_sample.as_ptr(),
            qubits_to_sample.len(),
            shots,
            &mut sample_json,
        ));

        match err {
            KetError::Success => {
                let Ok(result_json) = { unsafe { CStr::from_ptr(sample_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };

                match serde_json::from_str::<SampleData>(result_json) {
                    Ok(sample_data) => Ok(sample_data),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }

    fn exp_value_native(
        &self,
        gates: &[NativeGate],
        hamiltonian_list: &[Hamiltonian],
    ) -> Result<Vec<f64>, KetError> {
        let mut result = vec![f64::NAN; hamiltonian_list.len()];

        let Ok(gates_json) = serde_json::to_string(gates) else {
            return Err(KetError::SerdeError);
        };
        let Ok(gates_json) = CString::new(gates_json) else {
            return Err(KetError::SerdeError);
        };

        let Ok(ham_json) = serde_json::to_string(&hamiltonian_list) else {
            return Err(KetError::SerdeError);
        };
        let Ok(ham_json) = CString::new(ham_json) else {
            return Err(KetError::SerdeError);
        };

        let err = KetError::from_error_code((self.exp_value_native)(
            gates_json.as_ptr(),
            ham_json.as_ptr(),
            result.as_mut_ptr(),
        ));

        match err {
            KetError::Success => Ok(result),
            err => Err(err),
        }
    }

    fn gradient(
        &self,
        gates: &[GateInstruction],
        hamiltonian: &Hamiltonian,
    ) -> Result<(f64, Vec<f64>), KetError> {
        let mut exp_result = 0.0;

        let Ok(gates_json) = serde_json::to_string(gates) else {
            return Err(KetError::SerdeError);
        };
        let Ok(gates_json) = CString::new(gates_json) else {
            return Err(KetError::SerdeError);
        };

        let Ok(ham_json) = serde_json::to_string(hamiltonian) else {
            return Err(KetError::SerdeError);
        };
        let Ok(ham_json) = CString::new(ham_json) else {
            return Err(KetError::SerdeError);
        };

        let mut grad_json = std::ptr::null();

        let err = KetError::from_error_code((self.gradient)(
            gates_json.as_ptr(),
            ham_json.as_ptr(),
            &mut exp_result,
            &mut grad_json,
        ));

        match err {
            KetError::Success => {
                let Ok(grad_json) = { unsafe { CStr::from_ptr(grad_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };

                match serde_json::from_str::<Vec<f64>>(grad_json) {
                    Ok(grad_json) => Ok((exp_result, grad_json)),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }
}

/// FFI-compatible function-pointer table for translating abstract gates into
/// backend-native instructions.
///
/// The compiler calls these callbacks during the decomposition and routing
/// passes to convert high-level gate representations into the instruction set
/// understood by the target hardware or simulator.
///
/// Construct a `CNativeGateSet` value on the C side, fill in all function
/// pointers, then pass it to [`ket_quantum_execution_batch`].  Passing `NULL`
/// instead uses identity translation (no decomposition).
///
/// All callbacks must return `0` on success or a non-zero libket error code on
/// failure.
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CNativeGateSet {
    /// Translate a single-qubit gate (given as a 2×2 unitary matrix) into
    /// native gate instructions.
    ///
    /// `gate_json` is a null-terminated JSON string encoding the gate as a
    /// flat 2×2 complex matrix `[[re,im,re,im],[re,im,re,im]]`.  `target` is
    /// the target qubit index.  On success `*native_gate_json` is set to a
    /// heap-allocated null-terminated JSON string encoding the native gate
    /// sequence.  Returns `0` on success.
    pub translate:
        fn(gate_json: *const c_char, target: usize, native_gate_json: &mut *const c_char) -> i32,

    /// Produce the native gate sequence that implements a CNOT between two qubits.
    ///
    /// The first argument is the control qubit index and the second is the
    /// target qubit index.  On success `*native_gate_json` is set to a
    /// heap-allocated null-terminated JSON string encoding the native CNOT
    /// decomposition.  Returns `0` on success.
    pub cnot: fn(control: usize, target: usize, native_gate_json: &mut *const c_char) -> i32,
}

impl NativeGateSet for CNativeGateSet {
    fn translate(&self, matrix: &Matrix, target: usize) -> Result<Vec<NativeGate>, KetError> {
        let matrix_json = [
            [
                matrix[0][0].re,
                matrix[0][0].im,
                matrix[0][1].re,
                matrix[0][1].im,
            ],
            [
                matrix[1][0].re,
                matrix[1][0].im,
                matrix[1][1].re,
                matrix[1][1].im,
            ],
        ];
        let Ok(gate) = serde_json::to_string(&matrix_json) else {
            return Err(KetError::SerdeError);
        };
        let Ok(gate) = CString::new(gate) else {
            return Err(KetError::SerdeError);
        };

        let mut result_json = null();

        let err =
            KetError::from_error_code((self.translate)(gate.as_ptr(), target, &mut result_json));

        match err {
            KetError::Success => {
                let Ok(result_json) = { unsafe { CStr::from_ptr(result_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };
                match serde_json::from_str(result_json) {
                    Ok(result) => Ok(result),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }

    fn cnot(&self, control: usize, target: usize) -> Result<Vec<NativeGate>, KetError> {
        let mut result_json = null();

        let err = KetError::from_error_code((self.cnot)(control, target, &mut result_json));

        match err {
            KetError::Success => {
                let Ok(result_json) = { unsafe { CStr::from_ptr(result_json) } }.to_str() else {
                    return Err(KetError::SerdeError);
                };
                match serde_json::from_str(result_json) {
                    Ok(result) => Ok(result),
                    Err(_) => Err(KetError::SerdeError),
                }
            }
            err => Err(err),
        }
    }
}

/// Creates a live-mode [`QuantumExecution`] backed by the provided callback table.
///
/// In live mode each gate is dispatched to the backend immediately as it is
/// applied to the process. The `decompose` flag controls whether multi-qubit
/// gates are decomposed into single-qubit and two-qubit native gates before
/// being dispatched.
///
/// # Parameters
/// - `live`: Pointer to a caller-owned [`CLiveExecution`] struct whose
///   function pointers implement the backend. The struct is cloned; the caller
///   retains ownership of the original.
/// - `decompose`: If `true`, enables multi-qubit gate decomposition before
///   dispatching each gate to the backend.
/// - `quantum_execution`: Output: receives the pointer to the newly allocated
///   [`QuantumExecution`] object. Ownership is transferred to the caller.
///
/// # Returns
/// `0` on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn ket_quantum_execution_live(
    num_qubits: usize,
    live: &CLiveExecution,
    decompose: bool,
    native_gate_set: *const CNativeGateSet,
    qpu_config: &mut *mut QPUConfig,
) -> i32 {
    let native_gate_set = if native_gate_set.is_null() {
        None
    } else {
        Some(Box::new(unsafe { &*native_gate_set }.clone()) as Box<dyn NativeGateSet>)
    };

    let execution = QuantumExecution::Live {
        qpu: Box::new(live.clone()),
        decompose,
        native_gate_set,
    };

    *qpu_config = Box::into_raw(Box::new(QPUConfig {
        num_qubits,
        quantum_execution: Some(execution),
    }));

    c_ok()
}

/// Creates a batch-mode [`QuantumExecution`] backed by the provided callback tables.
///
/// In batch mode the full compiled circuit is handed to the backend in a
/// single call when `ket_process_execute` is invoked. The `native_gate_set`
/// callback table is used during the decomposition and routing passes to
/// translate abstract gates into hardware-native instructions.
///
/// # Parameters
/// - `batch`: Pointer to a caller-owned [`CBatchExecution`] struct. The struct
///   is cloned; the caller retains ownership of the original.
/// - `native_gate_set`: Pointer to a caller-owned [`CNativeGateSet`] struct,
///   or `NULL` to use identity translation (no decomposition into native gates).
/// - `gradient`: If `true`, enables parameter-shift rule gradient computation
///   during execution.
/// - `coupling_graph_json`: A null-terminated JSON string that is either
///   `null` (no connectivity constraint) or a JSON array of `[usize, usize]`
///   edge pairs describing the QPU coupling graph used for qubit routing.
/// - `quantum_execution`: Output: receives the pointer to the newly allocated
///   [`QuantumExecution`] object. Ownership is transferred to the caller.
///
/// # Returns
/// `0` on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn ket_quantum_execution_batch(
    num_qubits: usize,
    batch: &CBatchExecution,
    native_gate_set: *const CNativeGateSet,
    decompose: bool,
    gradient: bool,
    coupling_graph_json: *const c_char,
    exp_value_strategy_json: *const c_char,
    qpu_config: &mut *mut QPUConfig,
) -> i32 {
    let native_gate_set: Option<Box<dyn NativeGateSet>> = unsafe {
        if native_gate_set.is_null() {
            None
        } else {
            Some(Box::new((*native_gate_set).clone()))
        }
    };

    let coupling_graph = from_json!(coupling_graph_json, Option<Vec<(usize, usize)>>);
    let exp_value = from_json!(exp_value_strategy_json, ExpValueStrategy);

    let execution = QuantumExecution::Batch {
        qpu: Box::new(batch.clone()),
        native_gate_set,
        gradient: if gradient {
            GradientStrategy::ParameterShiftRule
        } else {
            GradientStrategy::None
        },
        exp_value,
        coupling_graph,
        decompose,
    };

    *qpu_config = Box::into_raw(Box::new(QPUConfig {
        num_qubits,
        quantum_execution: Some(execution),
    }));

    c_ok()
}