tket_json_rs/
pass.rs

1//! Serialized definition for TKET passes.
2//!
3//! Based on the `compiler_pass_v1` schema.
4//! <https://github.com/CQCL/tket/blob/main/schemas/compiler_pass_v1.json>
5
6pub mod standard;
7
8#[cfg(feature = "schemars")]
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11
12use standard::StandardPass;
13
14/// Stub for a serialized architecture blob following `architecture_v1.json`.
15//
16// TODO: Replace with the actual schema.
17// <https://github.com/CQCL/tket/blob/main/schemas/architecture_v1.json>
18pub type Architecture = serde_json::Value;
19
20/// Stub for a serialized placement blob following `placement_v1.json`.
21//
22// TODO: Replace with the actual schema.
23// <https://github.com/CQCL/tket/blob/main/schemas/placement_v1.json>
24pub type Placement = serde_json::Value;
25
26/// Stub for a serialized predicate blob following `predicate_v1.json`.
27//
28// TODO: Replace with the actual schema.
29// <https://github.com/CQCL/tket/blob/main/schemas/predicate_v1.json>
30pub type Predicate = serde_json::Value;
31
32/// A pass in a TKET circuit.
33//
34// This struct is both tagged adjacently (with a `pass_class` string field) and
35// externally (the pass definition itself is contained inside a field with the
36// same name as the pass class).
37//
38// Hence the non-standard structure of the enum.
39//
40// NOTE: The pytket schema defines serializations for `RepeatWithMetricPass`,
41// but it is not actually supported by pytket so we have removed it here.
42#[cfg_attr(feature = "schemars", derive(JsonSchema))]
43#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, derive_more::From)]
44#[serde(tag = "pass_class")]
45pub enum BasePass {
46    /// A standard pass.
47    StandardPass {
48        /// The pass data.
49        #[serde(rename = "StandardPass")]
50        pass: StandardPass,
51    },
52    /// A sequence of passes.
53    SequencePass {
54        /// The pass data.
55        #[serde(rename = "SequencePass")]
56        pass: SequencePass,
57    },
58    /// A pass that iterates an internal pass until no further change.
59    RepeatPass {
60        /// The pass data.
61        #[serde(rename = "RepeatPass")]
62        pass: RepeatPass,
63    },
64    /// A pass that iterates an internal pass until some predicate is satisfied.
65    RepeatUntilSatisfiedPass {
66        /// The pass data.
67        #[serde(rename = "RepeatUntilSatisfiedPass")]
68        pass: RepeatUntilSatisfiedPass,
69    },
70}
71
72/// A pass that executes a sequence of passes in order.
73#[cfg_attr(feature = "schemars", derive(JsonSchema))]
74#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
75pub struct SequencePass {
76    /// The passes to be executed in order.
77    pub sequence: Vec<BasePass>,
78}
79
80/// A pass that iterates an internal pass until no further change.
81#[cfg_attr(feature = "schemars", derive(JsonSchema))]
82#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
83pub struct RepeatPass {
84    /// The body of the loop, to be iterated until no further change.
85    pub body: Box<BasePass>,
86}
87
88/// A pass that iterates an internal pass until some predicate is satisfied.
89#[cfg_attr(feature = "schemars", derive(JsonSchema))]
90#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
91pub struct RepeatUntilSatisfiedPass {
92    /// The body of the loop.
93    pub body: Box<BasePass>,
94    /// The predicate that conditions the loop.
95    /// The loop is terminated when this predicate returns True.
96    pub predicate: Predicate,
97}