Skip to main content

_native/
lib.rs

1//! QIR labeled formatting, with an optional Python compatibility adapter.
2//!
3//! The Rust API accepts typed shot data and returns QIR 2.1 labeled output:
4//!
5//! ```
6//! use _native::{
7//!     QShotValType, QirLabeledFormatter, QirMetadata, QsysShotItemValue, QsysShots,
8//! };
9//!
10//! let results: QsysShots = vec![vec![(
11//!     "USER:INT:answer".into(),
12//!     QsysShotItemValue::Scalar(QShotValType::Int(42)),
13//! )]];
14//! let output = QirLabeledFormatter::new().qir_labeled_output(&results, &QirMetadata::new());
15//!
16//! assert!(output.contains("OUTPUT\tINT\t42\tanswer\n"));
17//! ```
18
19#![warn(missing_docs)]
20
21mod labeled_formatter;
22
23pub use labeled_formatter::{
24    FormattedValue, QShotValType, QirLabeledFormatter, QirMetadata, QirOutput, QsysShot,
25    QsysShotItem, QsysShotItemValue, QsysShots,
26};
27
28#[cfg(feature = "python")]
29mod python;
30
31#[cfg(feature = "python")]
32#[pyo3::pymodule]
33mod _native {
34    #[pymodule_export]
35    use crate::python::PythonQirLabeledFormatter;
36}
37
38#[cfg(test)]
39mod tests {
40    //! Public package surface tests.
41
42    use super::*;
43
44    /// The package should expose a stable top-level import surface.
45    #[test]
46    fn test_top_level_exports_support_basic_usage() {
47        let value: QShotValType = QShotValType::Int(42);
48        let item: QsysShotItem = ("USER:INT:answer".into(), QsysShotItemValue::Scalar(value));
49        let shot: QsysShot = vec![item];
50        let results: QsysShots = vec![shot];
51
52        let output = QirLabeledFormatter::new().qir_labeled_output(&results, &QirMetadata::new());
53
54        assert!(output.contains("OUTPUT\tINT\t42\tanswer\n"));
55    }
56
57    /// The main formatter should be directly importable from the package root.
58    #[test]
59    fn test_top_level_formatter_can_emit_values() {
60        let mut output = QirOutput::default();
61        QirLabeledFormatter::new().emit(
62            &mut output,
63            "RESULT_ARRAY",
64            Some("bits"),
65            &QsysShotItemValue::List(vec![
66                QShotValType::Int(1),
67                QShotValType::Int(0),
68                QShotValType::Int(1),
69            ]),
70        );
71
72        assert_eq!(output.text, "OUTPUT\tRESULT_ARRAY\t101\tbits\n");
73    }
74
75    /// Python package metadata must derive its version from Cargo.
76    #[test]
77    fn test_python_version_is_dynamic() {
78        let pyproject = include_str!("../pyproject.toml");
79        let project = pyproject
80            .split_once("[project]")
81            .expect("pyproject.toml must contain a [project] table")
82            .1
83            .split("\n[")
84            .next()
85            .unwrap();
86
87        assert!(
88            project
89                .lines()
90                .any(|line| line.trim() == r#"dynamic = ["version"]"#)
91        );
92        assert!(
93            !project
94                .lines()
95                .any(|line| line.trim().starts_with("version ="))
96        );
97    }
98}