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
/*
* SPDX-FileCopyrightText: 2026 Evandro Chagas Ribeiro da Rosa<evandro@quantuloop.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* A straight-line sequence of quantum gate instructions.
*
* A [`BasicBlock`] is created empty and gates are appended one at a time via
* [`BasicBlock::append_gate`] or in bulk via [`BasicBlock::append_block`].
* Each append operation attempts lightweight algebraic simplifications:
* adjacent inverse gate pairs are cancelled and consecutive same-axis
* rotation gates are merged into one.
*
* The block also maintains a dependency index (`qubits_op`) consumed by the
* qubit-allocation and circuit-mapping passes.
*/
typedef struct BasicBlock BasicBlock;
/**
* A quantum process: the top-level compilation and execution unit.
*
* Create a [`Process`] with [`Process::new`], allocate qubits with
* [`Process::alloc`], append gate sequences with [`Process::append_block`],
* and trigger execution with [`Process::execute`] (batch mode) or read
* results immediately after each gate/measurement in live mode.
*
* Each process follows a linear [`ProcessState`] machine and can be executed
* at most once. Results are cached and retrieved via [`Process::read_sample`],
* [`Process::read_exp_value`], and [`Process::read_gradient`].
*/
typedef struct Process Process;
/**
* Configuration of the quantum processing unit (QPU) available to this process.
*/
typedef struct QPUConfig 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.
*/
typedef struct CLiveExecution CLiveExecution;
/**
* 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.
*/
typedef struct CNativeGateSet CNativeGateSet;
/**
* 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.
*/
typedef struct CBatchExecution CBatchExecution;
/**
* 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).
*/
int32_t ;
/**
* 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`.
*/
int32_t ;
/**
* 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`.
*/
int32_t ;
/**
* Allocates a new, empty [`BasicBlock`] and writes its pointer to `block`.
*/
int32_t ;
/**
* Deallocates the memory associated with a [`BasicBlock`].
*/
int32_t ;
/**
* Appends a new quantum gate (provided as a JSON payload) to a [`BasicBlock`].
*/
int32_t ;
/**
* Creates a new [`BasicBlock`] that is the adjoint (inverse) of the provided block.
*/
int32_t ;
/**
* Creates a new [`BasicBlock`] resulting from adding control qubits to all gates in the block.
*/
int32_t ;
/**
* Enables approximate decomposition strategies for all gates in the given [`BasicBlock`].
*/
int32_t ;
/**
* Locks the control qubits of all gates in the given [`BasicBlock`], preventing further additions.
*/
int32_t ;
/**
* Appends the contents of `other` block to `block` and consumes `other`.
*/
int32_t ;
/**
* Adds a global phase to the [`BasicBlock`].
*/
int32_t ;
/**
* Serializes the properties of the [`BasicBlock`] into a JSON payload and writes it to the buffer.
*/
int32_t ;
/**
* Marks the [`BasicBlock`] as implementing a diagonal gate.
*/
int32_t ;
/**
* Marks the [`BasicBlock`] as implementing a permutation gate.
*/
int32_t ;
/**
* 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.
*/
int32_t ;
/**
* 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.
*/
int32_t ;
/**
* Allocates a new [`Process`] using the provided QPU configuration.
*
* `qpu_config` is consumed by this call: ownership is transferred to the
* newly created `Process` and the pointer must not be used afterwards.
*
* # Parameters
* - `qpu_config`: Pointer to a [`QPUConfig`].
* - `process`: Output: receives the pointer to the newly allocated
* [`Process`]. Must eventually be freed with [`ket_process_delete`].
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Allocates a new [`QPUConfig`] for a QPU with the specified number of physical qubits.
*
* The newly created configuration has **no execution backend** attached. A
* `QuantumExecution` backend must be set (via `ket_quantum_execution_live`
* or `ket_quantum_execution_batch`) before passing the config to
* [`ket_process_new`].
*
* # Parameters
* - `num_qubits`: The number of physical qubits available on the QPU.
* - `qpu_config`: Output: receives the pointer to the newly allocated
* [`QPUConfig`]. Ownership is transferred to the caller until it is
* consumed by [`ket_process_new`].
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Deallocates the memory associated with a [`Process`].
*
* # Parameters
* - `process`: Pointer to the [`Process`] to free.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Allocates a new logical qubit in the process.
*
* Returns an error if the process is in a state that does not accept new
* qubits (e.g., `ProcessTerminated`) or if the QPU qubit limit has been
* reached (`QubitLimitExceeded`).
*
* # Parameters
* - `process`: The process in which the qubit is allocated.
* - `qubit`: Output: receives the index of the newly allocated logical qubit.
*
* # Returns
* `0` on success, non-zero error code on failure (including
* `ProcessTerminated` and `QubitLimitExceeded`).
*/
int32_t ;
/**
* Appends a [`BasicBlock`] of gates to the process.
*
* The `block` is **consumed** by this call and must not be used afterwards.
* Returns an error if the process state does not allow gate appending
* (`GateAppendForbidden`) or if a qubit index referenced in the block is
* out of range (`QubitIndexOutOfRange`).
*
* # Parameters
* - `process`: The process to which the block is appended.
* - `block`: The block to consume and append. Ownership is transferred.
*
* # Returns
* `0` on success, non-zero error code on failure (including
* `GateAppendForbidden` and `QubitIndexOutOfRange`).
*/
int32_t ;
/**
* Returns the accumulated gate instructions of the process as a JSON string.
*
* The returned string is a JSON array of gate instruction objects representing
* the gates in the process's main block. The string is heap-allocated and the
* caller **must** free it with [`super::ket_string_delete`].
*
* # Parameters
* - `process`: The process whose gate list is serialized.
* - `block`: Output: set to a pointer to a heap-allocated null-terminated
* JSON string.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Performs a single-shot mid-circuit measurement of `qubits`.
*
* This function is only valid in **live mode**. Any gates accumulated since
* the last measurement are dispatched to the backend before the measurement
* is performed.
*
* # Parameters
* - `process`: The process on which the measurement is performed.
* - `qubits`: Pointer to an array of qubit indices to measure.
* - `qubits_len`: Number of elements in `qubits`.
* - `result`: Output: the measurement result encoded as a bitmask
* (bit `i` corresponds to `qubits[i]`).
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Dumps the quantum state of `qubits` (live mode only).
*
* Returns the full probability amplitude vector for the specified qubits as a
* heap-allocated JSON string. The caller **must** free it with
* [`super::ket_string_delete`].
*
* # Parameters
* - `process`: The process whose state is dumped.
* - `qubits`: Pointer to an array of qubit indices to dump.
* - `qubits_len`: Number of elements in `qubits`.
* - `dump_json`: Output: set to a pointer to a heap-allocated
* null-terminated JSON string encoding the quantum state.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Requests a measurement sample of `qubits` over `shots` repetitions.
*
* In **live mode**, the circuit is executed immediately and the result is
* returned as a heap-allocated JSON string (caller must free with
* [`super::ket_string_delete`]). In **batch mode**, the request is recorded for the
* next [`ket_process_execute`] call and `*sample_json` is set to a JSON
* `null` string; the actual data becomes available after execution and can be
* retrieved with [`ket_process_read_sample`].
*
* # Parameters
* - `process`: The process on which the sample is requested.
* - `qubits`: Pointer to an array of qubit indices to sample.
* - `qubits_len`: Number of elements in `qubits`.
* - `shots`: Number of repetitions.
* - `sample_json`: Output: set to a pointer to a heap-allocated
* null-terminated JSON string (caller must free with [`super::ket_string_delete`]).
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Returns a serialized JSON array of expectation values from the most recent execution.
*
* If no execution has been performed yet, the returned JSON string contains
* `null`. The string is heap-allocated; the caller **must** free it with
* [`super::ket_string_delete`].
*
* # Parameters
* - `process`: The process from which expectation values are read.
* - `result_json`: Output: set to a pointer to a heap-allocated
* null-terminated JSON string.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Returns a serialized JSON array of parameter-shift gradients from the most recent execution.
*
* If no execution has been performed yet, the returned JSON string contains
* `null`. The string is heap-allocated; the caller **must** free it with
* [`super::ket_string_delete`].
*
* # Parameters
* - `process`: The process from which gradients are read.
* - `result_json`: Output: set to a pointer to a heap-allocated
* null-terminated JSON string.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Registers a new differentiable parameter with the given initial value.
*
* The returned `param_index` uniquely identifies this parameter within the
* process. Use it to construct `Param::Ref` values when building
* parameterised gate instructions. When the parameter value changes between
* executions, gates that reference the same index are automatically updated
* during the next compilation pass.
*
* # Parameters
* - `process`: The process in which the parameter is registered.
* - `param`: The initial numeric value of the parameter.
* - `param_index`: Output: receives the index that identifies this parameter
* within the process.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Compiles and executes the accumulated circuit.
*
* This is the primary entry point for **batch mode** execution. Calling it in
* live mode returns an `ExplicitExecuteInLiveMode` error. If no pending
* measurement or expectation-value requests have been recorded,
* `NoPendingMeasurement` is returned. If the process is already in the
* `Terminated` state, this call is idempotent and returns success.
*
* # Parameters
* - `process`: The process to compile and execute.
*
* # Returns
* `0` on success, non-zero error code on failure (including
* `NoPendingMeasurement` and `ExplicitExecuteInLiveMode`).
*/
int32_t ;
/**
* Reads the cached sample data from a [`Process`].
*
* Returns the sample counts recorded during the most recent execution as a
* heap-allocated JSON string. If no execution has been performed yet, the
* returned JSON string contains `null`. The caller **must** free the string
* with [`super::ket_string_delete`].
*
* # Parameters
* - `process`: The process from which sample data is read.
* - `sample_json`: Output: set to a pointer to a heap-allocated
* null-terminated JSON string.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Requests the expectation value computation for a given [`Hamiltonian`] (passed as JSON) on a [`Process`].
*
* In **live mode**, the expectation value is computed immediately:
* `*some_result` is set to `true` and the value is written to `*result`.
* In **batch mode**, the request is recorded for the next
* [`ket_process_execute`] call: `*some_result` is set to `false` and
* `*result` is left unmodified until execution completes and
* [`ket_process_read_exp_value`] is called.
*
* # Parameters
* - `process`: The process on which the expectation value is computed.
* - `hamiltonian_json`: A null-terminated JSON string encoding the
* Hamiltonian.
* - `result`: Output: the computed expectation value (valid only when
* `*some_result` is `true`).
* - `some_result`: Output: set to `true` if the expectation value was
* computed immediately (live mode), or `false` if the request was
* deferred (batch mode).
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Returns the current status of the process as a static C string.
*
* The string is one of:
* - `"AcceptingGate"`: the process is ready to accept gate blocks.
* - `"AcceptingHamiltonian"`: all gates have been appended; expectation-value
* requests can now be submitted.
* - `"ReadyToExecute"`: execution has been requested and the circuit is ready.
* - `"Terminated"`: execution has completed and results are available.
*
* The returned pointer points to a static string and must **not** be freed.
*
* # Parameters
* - `process`: The process whose status is queried.
* - `status`: Output: set to a pointer to a static null-terminated string.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;
/**
* Registers a new parameter with the given initial value in the process.
*
* The parameter's index within the process parameter list is written to `index`.
*
* # Parameters
* - `process`: The process to register the parameter in.
* - `value`: The initial value of the parameter.
* - `index`: Output: set to the registered parameter's index.
*
* # Returns
* `0` on success, non-zero error code on failure.
*/
int32_t ;