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
use std::collections::HashMap;
use std::fmt;

use serde::{Deserialize, Serialize};

use super::{format_qubits, MemoryReference, Qubit, WaveformInvocation};
use crate::expression::Expression;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum AttributeValue {
    String(String),
    Expression(Expression),
}

impl fmt::Display for AttributeValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use AttributeValue::*;
        match self {
            String(value) => write!(f, "\"{value}\""),
            Expression(value) => write!(f, "{value}"),
        }
    }
}

pub type FrameAttributes = HashMap<String, AttributeValue>;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FrameDefinition {
    pub identifier: FrameIdentifier,
    pub attributes: HashMap<String, AttributeValue>,
}

impl FrameDefinition {
    pub fn new(identifier: FrameIdentifier, attributes: FrameAttributes) -> Self {
        Self {
            identifier,
            attributes,
        }
    }
}

impl fmt::Display for FrameDefinition {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "DEFFRAME {}:{}",
            self.identifier,
            self.attributes
                .iter()
                .map(|(k, v)| format!("\n\t{k}: {v}"))
                .collect::<String>()
        )
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct FrameIdentifier {
    pub name: String,
    pub qubits: Vec<Qubit>,
}

impl FrameIdentifier {
    pub fn new(name: String, qubits: Vec<Qubit>) -> Self {
        Self { name, qubits }
    }
}

impl fmt::Display for FrameIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} \"{}\"", format_qubits(&self.qubits), self.name)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Capture {
    pub blocking: bool,
    pub frame: FrameIdentifier,
    pub memory_reference: MemoryReference,
    pub waveform: WaveformInvocation,
}

impl Capture {
    pub fn new(
        blocking: bool,
        frame: FrameIdentifier,
        memory_reference: MemoryReference,
        waveform: WaveformInvocation,
    ) -> Self {
        Self {
            blocking,
            frame,
            memory_reference,
            waveform,
        }
    }
}

impl fmt::Display for Capture {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.blocking {
            write!(f, "NONBLOCKING ")?;
        }
        write!(
            f,
            "CAPTURE {} {} {}",
            self.frame, self.waveform, self.memory_reference
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Pulse {
    pub blocking: bool,
    pub frame: FrameIdentifier,
    pub waveform: WaveformInvocation,
}

impl Pulse {
    pub fn new(blocking: bool, frame: FrameIdentifier, waveform: WaveformInvocation) -> Self {
        Self {
            blocking,
            frame,
            waveform,
        }
    }
}

impl fmt::Display for Pulse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.blocking {
            write!(f, "NONBLOCKING ")?
        }
        write!(f, "PULSE {} {}", self.frame, self.waveform)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RawCapture {
    pub blocking: bool,
    pub frame: FrameIdentifier,
    pub duration: Expression,
    pub memory_reference: MemoryReference,
}

impl RawCapture {
    pub fn new(
        blocking: bool,
        frame: FrameIdentifier,
        duration: Expression,
        memory_reference: MemoryReference,
    ) -> Self {
        Self {
            blocking,
            frame,
            duration,
            memory_reference,
        }
    }
}

impl fmt::Display for RawCapture {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.blocking {
            write!(f, "NONBLOCKING ")?
        }
        write!(
            f,
            "RAW-CAPTURE {} {} {}",
            self.frame, self.duration, self.memory_reference
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SetFrequency {
    pub frame: FrameIdentifier,
    pub frequency: Expression,
}

impl SetFrequency {
    pub fn new(frame: FrameIdentifier, frequency: Expression) -> Self {
        Self { frame, frequency }
    }
}

impl fmt::Display for SetFrequency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SET-FREQUENCY {} {}", self.frame, self.frequency)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SetPhase {
    pub frame: FrameIdentifier,
    pub phase: Expression,
}

impl SetPhase {
    pub fn new(frame: FrameIdentifier, phase: Expression) -> Self {
        Self { frame, phase }
    }
}

impl fmt::Display for SetPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SET-PHASE {} {}", self.frame, self.phase)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SetScale {
    pub frame: FrameIdentifier,
    pub scale: Expression,
}

impl SetScale {
    pub fn new(frame: FrameIdentifier, scale: Expression) -> Self {
        Self { frame, scale }
    }
}

impl fmt::Display for SetScale {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SET-SCALE {} {}", self.frame, self.scale)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ShiftFrequency {
    pub frame: FrameIdentifier,
    pub frequency: Expression,
}

impl ShiftFrequency {
    pub fn new(frame: FrameIdentifier, frequency: Expression) -> Self {
        Self { frame, frequency }
    }
}

impl fmt::Display for ShiftFrequency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHIFT-FREQUENCY {} {}", self.frame, self.frequency)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ShiftPhase {
    pub frame: FrameIdentifier,
    pub phase: Expression,
}

impl ShiftPhase {
    pub fn new(frame: FrameIdentifier, phase: Expression) -> Self {
        Self { frame, phase }
    }
}

impl fmt::Display for ShiftPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHIFT-PHASE {} {}", self.frame, self.phase)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SwapPhases {
    pub frame_1: FrameIdentifier,
    pub frame_2: FrameIdentifier,
}

impl SwapPhases {
    pub fn new(frame_1: FrameIdentifier, frame_2: FrameIdentifier) -> Self {
        Self { frame_1, frame_2 }
    }
}

impl fmt::Display for SwapPhases {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SWAP-PHASES {} {}", self.frame_1, self.frame_2)
    }
}