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
// SPDX-FileCopyrightText: 2026 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
//
// SPDX-License-Identifier: Apache-2.0
//! Foreign Function Interface (FFI) for the Libket library.
//!
//! This module provides a C-compatible API for the core data structures and
//! execution mechanisms in Libket, enabling it to be consumed by other languages
//! such as Python, C, and C++.
//!
//! # Design overview
//!
//! All opaque Rust types ([`crate::process::Process`],
//! [`crate::ir::block::BasicBlock`], [`crate::process::QPUConfig`],
//! [`crate::execution::QuantumExecution`]) are heap-allocated behind raw pointers.
//! Callers are responsible for pairing every `*_new` call with the corresponding
//! `*_delete` call to avoid memory leaks.
//!
//! Gate instructions and measurement results are exchanged as **JSON strings**
//! (`const char *`). The caller owns any `char *` that Libket writes into an
//! output pointer and **must** free it by calling [`ket_string_delete`] when
//! done. Static C strings (e.g., the status returned by
//! `ket_process_status`) are not heap-allocated and must **not** be freed.
//!
//! Every exported function returns a signed 32-bit integer error code. A
//! return value of `0` indicates success. Non-zero values map to the
//! [`KetError`] variants via [`KetError::from_error_code`]; call
//! [`ket_error_message`] to retrieve a human-readable description.
use ;
use crateKetError;
/// Retrieves the human-readable error message for a given integer error code.
///
/// On success the function heap-allocates a null-terminated C string and
/// writes its address into `*error_message`. The caller must free this string
/// with [`ket_string_delete`] when done.
///
/// # Returns
/// `0` on success; a non-zero error code if the C string could not be
/// allocated (e.g., if the message contains interior null bytes).
pub extern "C"
/// Frees a heap-allocated C string that was previously written by Libket.
///
/// Every `const char **` output parameter written by Libket functions
/// (`ket_error_message`, `ket_process_gates_json`, `ket_process_dump`, etc.)
/// points to memory that must be released through this function. Passing a
/// pointer that was not produced by Libket, or freeing the same pointer twice,
/// is undefined behaviour.
///
/// # Returns
/// Always returns `0`.
pub extern "C"
/// Returns the success error code (`0`).
pub
/// Converts a `Result<(), KetError>` to a C-compatible integer error code.
///
/// Returns `0` on `Ok(())`, or the discriminant of the `KetError` variant
/// on `Err`. Suitable for wrapping infallible-looking calls that propagate
/// errors through the return value.
pub
const BUILD_INFO: &str = format!;
/// Returns a pointer and byte-length for a static build-information string.
///
/// The string is embedded at compile time and contains the crate name,
/// version, Rust compiler version, and compilation target triple. It is
/// **not** null-terminated; use `size` to bound reads. The pointer is valid
/// for the lifetime of the program and must **not** be freed.
///
/// # Parameters
/// - `msg` : Set to the address of the first byte of the build-info string.
/// - `size`: Set to the byte length of the build-info string.
///
/// # Returns
/// Always returns `0`.
pub const extern "C"