tket_json_rs/clexpr/
operator.rs

1//! A tree of operators forming a classical expression.
2
3#[cfg(feature = "schemars")]
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use super::op::ClOp;
8
9/// A node in a classical expression tree.
10#[cfg_attr(feature = "schemars", derive(JsonSchema))]
11#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
12#[non_exhaustive]
13pub struct ClOperator {
14    /// The operation to be performed.
15    pub op: ClOp,
16    /// The arguments to the operation.
17    pub args: Vec<ClArgument>,
18}
19
20/// An argument to a classical expression operation.
21#[cfg_attr(feature = "schemars", derive(JsonSchema))]
22#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
23#[non_exhaustive]
24#[serde(tag = "type", content = "input")]
25pub enum ClArgument {
26    /// A terminal argument.
27    #[serde(rename = "term")]
28    Terminal(ClTerminal),
29    /// A sub-expression.
30    #[serde(rename = "expr")]
31    Expression(Box<ClOperator>),
32}
33
34/// A terminal argument in a classical expression operation.
35#[cfg_attr(feature = "schemars", derive(JsonSchema))]
36#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
37#[non_exhaustive]
38#[serde(tag = "type", content = "term")]
39pub enum ClTerminal {
40    /// A terminal argument.
41    #[serde(rename = "var")]
42    Variable(ClVariable),
43    /// A constant integer.
44    #[serde(rename = "int")]
45    Int(u64),
46}
47
48/// A variable terminal argument in a classical expression operation.
49///
50/// The indices refer to the local identifiers in the [`super::ClExpr`] structure.
51#[cfg_attr(feature = "schemars", derive(JsonSchema))]
52#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Hash)]
53#[non_exhaustive]
54#[serde(tag = "type", content = "var")]
55pub enum ClVariable {
56    /// A register variable.
57    #[serde(rename = "reg")]
58    Register {
59        /// The register identifier in [`super::ClExpr::reg_posn`].
60        index: u32,
61    },
62    /// A constant bit.
63    #[serde(rename = "bit")]
64    Bit {
65        /// The bit identifier in [`super::ClExpr::bit_posn`].
66        index: u32,
67    },
68}
69
70impl Default for ClArgument {
71    fn default() -> Self {
72        ClArgument::Terminal(ClTerminal::default())
73    }
74}
75
76impl Default for ClTerminal {
77    fn default() -> Self {
78        ClTerminal::Int(0)
79    }
80}
81
82impl Default for ClVariable {
83    fn default() -> Self {
84        ClVariable::Register { index: 0 }
85    }
86}