ket/c_api/objects.rs
1// SPDX-FileCopyrightText: 2020 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
2// SPDX-FileCopyrightText: 2020 Rafael de Santiago <r.santiago@ufsc.br>
3//
4// SPDX-License-Identifier: Apache-2.0
5
6//! C API for the objects module.
7
8use crate::prelude::*;
9
10/// Retrieves the measurement result from the `Process` instance.
11///
12/// # Arguments
13///
14/// * `process` - \[in\] A reference to the `Process` instance.
15/// * `index` - \[in\] The index of the measurement to query.
16/// * `available` - \[out\] A mutable pointer to a `bool` indicating if the result is available.
17/// * `result` - \[out\] A mutable pointer to a `u64` storing the measurement result.
18///
19/// # Returns
20///
21/// An integer representing the error code. `0` indicates success.
22#[no_mangle]
23pub extern "C" fn ket_process_get_measurement(
24 process: &Process,
25 index: usize,
26 available: &mut bool,
27 result: &mut u64,
28) -> i32 {
29 let measurement = process.get_measure(index);
30 if let Some(measurement) = measurement {
31 *result = measurement;
32 *available = true;
33 } else {
34 *available = false;
35 }
36
37 KetError::Success.error_code()
38}
39
40/// Retrieves the expected value from the `Process` instance.
41///
42/// # Arguments
43///
44/// * `process` - \[in\] A reference to the `Process` instance.
45/// * `index` - \[in\] The index of the expected value to query.
46/// * `available` - \[out\] A mutable pointer to a `bool` indicating if the result is available.
47/// * `result` - \[out\] A mutable pointer to a `f64` storing the expected value.
48///
49/// # Returns
50///
51/// An integer representing the error code. `0` indicates success.
52#[no_mangle]
53pub extern "C" fn ket_process_get_exp_value(
54 process: &Process,
55 index: usize,
56 available: &mut bool,
57 result: &mut f64,
58) -> i32 {
59 let exp_value = process.get_exp_value(index);
60 if let Some(exp_value) = exp_value {
61 *result = exp_value;
62 *available = true;
63 } else {
64 *available = false;
65 }
66
67 KetError::Success.error_code()
68}
69
70/// Retrieves the sample data from the `Process` instance.
71///
72/// # Arguments
73///
74/// * `process` - \[in\] A reference to the `Process` instance.
75/// * `index` - \[in\] The index of the sample to query.
76/// * `available` - \[out\] A mutable pointer to a `bool` indicating if the result is available.
77/// * `result` - \[out\] A mutable pointer to the array of `u64` storing the sample data.
78/// * `count` - \[out\] A mutable pointer to the array of `u64` storing the sample counts.
79/// * `size` - \[out\] A mutable pointer to the size of the sample data arrays.
80///
81/// # Returns
82///
83/// An integer representing the error code. `0` indicates success.
84#[no_mangle]
85pub extern "C" fn ket_process_get_sample(
86 process: &Process,
87 index: usize,
88 available: &mut bool,
89 result: &mut *const u64,
90 count: &mut *const u64,
91 size: &mut usize,
92) -> i32 {
93 let sample = process.get_sample(index);
94 if let Some(sample) = sample.as_ref() {
95 *result = sample.0.as_ptr();
96 *count = sample.1.as_ptr();
97 *size = sample.0.len();
98 *available = true;
99 } else {
100 *available = false;
101 }
102
103 KetError::Success.error_code()
104}
105
106/// Retrieves the size of the dump data from the `Process` instance.
107///
108/// # Arguments
109///
110/// * `process` - \[in\] A reference to the `Process` instance.
111/// * `index` - \[in\] The index of the dump to query.
112/// * `available` - \[out\] A mutable pointer to a `bool` indicating if the result is available.
113/// * `size` - \[out\] A mutable pointer to the size of the basis states in the dump.
114///
115/// # Returns
116///
117/// An integer representing the error code. `0` indicates success.
118#[no_mangle]
119pub extern "C" fn ket_process_get_dump_size(
120 process: &Process,
121 index: usize,
122 available: &mut bool,
123 size: &mut usize,
124) -> i32 {
125 let dump = process.get_dump(index);
126 if let Some(dump) = dump.as_ref() {
127 *size = dump.basis_states.len();
128 *available = true;
129 } else {
130 *available = false;
131 }
132
133 KetError::Success.error_code()
134}
135
136/// Retrieves the dump data from the `Process` instance.
137///
138/// # Arguments
139///
140/// * `process` - \[in\] A reference to the `Process` instance.
141/// * `index` - \[in\] The index of the dump to query.
142/// * `iterator` - \[in\] The iterator for accessing individual basis states in the dump.
143/// * `basis_state` - \[out\] A mutable pointer to the array of `u64` storing the basis state.
144/// * `basis_state_size` - \[out\] A mutable pointer to the size of the basis state array.
145/// * `amplitude_real` - \[out\] A mutable pointer to the real part of the amplitude.
146/// * `amplitude_imag` - \[out\] A mutable pointer to the imaginary part of the amplitude.
147///
148/// # Returns
149///
150/// An integer representing the error code. `0` indicates success.
151///
152/// # Safety
153///
154/// This function is marked as unsafe due to the use of raw pointers.
155#[no_mangle]
156pub unsafe extern "C" fn ket_process_get_dump(
157 process: &Process,
158 index: usize,
159 iterator: usize,
160 basis_state: &mut *const u64,
161 basis_state_size: &mut usize,
162 amplitude_real: &mut f64,
163 amplitude_imag: &mut f64,
164) -> i32 {
165 let dump = process.get_dump(index).unwrap();
166 let state = dump.basis_states[iterator].as_ptr();
167 let size = dump.basis_states[iterator].len();
168 *basis_state = state;
169 *basis_state_size = size;
170 *amplitude_real = dump.amplitudes_real[iterator];
171 *amplitude_imag = dump.amplitudes_imag[iterator];
172
173 KetError::Success.error_code()
174}