Skip to main content

tk_pg_core/
gates.rs

1use serde::{Deserialize, Serialize};
2
3/// The supported gate types that can appear in a Pauli graph.
4#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
5pub enum GateType {
6    // TQE gates
7    /// An $X$-controlled $X$ gate, i.e. H(0);CX(0,1);H(0)
8    XX,
9    /// An $X$-controlled $Y$ gate, i.e. H(0);CY(0,1);H(0)
10    XY,
11    /// An $X$-controlled $Z$ gate, i.e. CX(1,0)
12    XZ,
13    /// A $Y$-controlled $X$ gate, i.e. V(0);CX(0,1);Vdg(0)
14    YX,
15    /// A $Y$-controlled $Y$ gate, i.e. V(0);CY(0,1);Vdg(0)
16    YY,
17    /// A $Y$-controlled $Z$ gate, i.e. CY(1,0)
18    YZ,
19    /// A $Z$-controlled $X$ gate, i.e. CX(0,1)
20    ZX,
21    /// A $Z$-controlled $Y$ gate, i.e. CY(0,1)
22    ZY,
23    /// A $Z$-controlled $Z$ gate, i.e. CZ(0,1)
24    ZZ,
25    // Single-qubit Clifford gates
26    /// The Hadamard gate.
27    H,
28    /// The phase gate.
29    S,
30    /// The inverse phase gate.
31    Sdg,
32    /// The square-root-of-$X$ gate.
33    V,
34    /// The inverse square-root-of-$X$ gate.
35    Vdg,
36    /// The Pauli-$X$ gate.
37    X,
38    /// The Pauli-$Y$ gate.
39    Y,
40    /// The Pauli-$Z$ gate.
41    Z,
42    // Special gates
43    /// A computational-basis measurement gate.
44    /// args: [qubit, cbit]
45    Measure,
46    /// A qubit reset gate.
47    Reset,
48    /// A swap gate.
49    SWAP,
50    /// An opaque gate with implementation-defined behaviour.
51    BlackBox,
52    // Rotation gates
53    /// A rotation around the $X$ axis.
54    RX,
55    /// A rotation around the $Y$ axis.
56    RY,
57    /// A rotation around the $Z$ axis.
58    RZ,
59    /// A $ZZ$ phase gate.
60    ZZPHASE,
61    /// A phased-$X$ gate.
62    PHASEDX,
63}
64
65/// Returns the number of arguments for a given gate type.
66///
67/// # Arguments
68///
69/// - `gate_type` (`&GateType`) - The gate type for which to determine the number of arguments.
70///
71/// # Returns
72///
73/// - `Option<usize>` - The number of arguments for the given gate type. Returns `None` if the gate type has an undefined number of arguments.
74///
75pub(crate) fn gate_type_n_args(gate_type: &GateType) -> Option<usize> {
76    match gate_type {
77        GateType::XX
78        | GateType::XY
79        | GateType::XZ
80        | GateType::YX
81        | GateType::YY
82        | GateType::YZ
83        | GateType::ZX
84        | GateType::ZY
85        | GateType::ZZ
86        | GateType::SWAP
87        | GateType::ZZPHASE => Some(2),
88        GateType::H
89        | GateType::S
90        | GateType::Sdg
91        | GateType::V
92        | GateType::Vdg
93        | GateType::X
94        | GateType::Y
95        | GateType::Z
96        | GateType::RX
97        | GateType::RY
98        | GateType::RZ
99        | GateType::PHASEDX => Some(1),
100        GateType::Measure => Some(2),
101        GateType::Reset => Some(1),
102        GateType::BlackBox => None,
103    }
104}
105
106/// Describes the number of parameters for a given gate type.
107///
108/// # Arguments
109///
110/// - `gate_type` (`&GateType`) - The gate type for which to determine the number of parameters.
111///
112/// # Returns
113///
114/// - `usize` - The number of parameters for the given gate type.
115///
116pub(crate) fn gate_type_n_params(gate_type: &GateType) -> usize {
117    match gate_type {
118        GateType::XX
119        | GateType::XY
120        | GateType::XZ
121        | GateType::YX
122        | GateType::YY
123        | GateType::YZ
124        | GateType::ZX
125        | GateType::ZY
126        | GateType::ZZ
127        | GateType::SWAP
128        | GateType::H
129        | GateType::S
130        | GateType::Sdg
131        | GateType::V
132        | GateType::Vdg
133        | GateType::X
134        | GateType::Y
135        | GateType::Z => 0,
136        GateType::RX | GateType::RY | GateType::RZ | GateType::ZZPHASE => 1,
137        GateType::PHASEDX => 2,
138        GateType::Measure => 0,
139        GateType::Reset => 0,
140        GateType::BlackBox => 0,
141    }
142}