qxide 0.7.0

Qxide: a language and Rust library for classical and quantum computers with C-style syntax, compile-time qubit safety like Rust's ownership, and OpenQASM 3 output
Documentation
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// `qxide repl`: the session is replayed as one program. Accepted inputs are kept as source text,
// and each new input is checked and run together with them through the same front end as
// `qxide run`, so every rule (including linearity) applies unchanged. The only difference is that
// qubits may stay live at the end of `main` until the session ends.

use std::io::{BufRead, Write};

use qxide::Backend;

use super::diag::{Code, Diagnostic, SourceMap, Span};
use super::{ast, front, interp, lexer, msg, parser, render_output, sim_error, Failure};

const NAME: &str = "<repl>";

#[derive(Clone)]
struct Entry {
    text: String,
    // Session line number of the entry's first line, counting accepted inputs as they were typed.
    line: usize,
    // Set when `text` wraps what was typed (`println("{}", code)` or `code;`): diagnostics show the
    // typed input instead.
    typed: Option<Typed>,
}

#[derive(Clone)]
struct Typed {
    input: String,
    // `text` is `prefix + code + suffix + rest`, where `input` is `code + rest`.
    prefix: usize,
    code: usize,
    suffix: usize,
}

// Where an entry's wrapped text sits in the assembled program.
struct Region {
    start: usize,
    len: usize,
    typed: Typed,
}

pub struct Session {
    fns: Vec<Entry>,
    stmts: Vec<Entry>,
    next_line: usize,
    seed: u64,
    // Output already shown; later runs reprint it first because the whole session is replayed.
    shown: String,
}

/// What one input produced: text for stdout, or rendered diagnostics for stderr.
pub enum Reply {
    Out(String),
    Err(String),
    Quit,
}

enum New<'a> {
    Nothing,
    Fn(&'a Entry),
    Stmt(&'a Entry),
}

struct Program {
    src: String,
    regions: Vec<Region>,
    // Session line number of every line of `src`; 0 for the lines the REPL adds around `main`.
    labels: Vec<usize>,
    // Offset of the new input inside `src`, and the closing brace of `main`.
    input_at: usize,
    main_close: Span,
}

impl Program {
    fn assemble(fns: &[Entry], stmts: &[Entry], new: New) -> Program {
        let mut p = Program {
            src: String::new(),
            regions: Vec::new(),
            labels: Vec::new(),
            input_at: 0,
            main_close: Span::default(),
        };
        for f in fns {
            p.push_entry(f);
            p.push("", 0);
        }
        if let New::Fn(f) = new {
            p.input_at = p.src.len();
            p.push_entry(f);
            p.push("", 0);
        }
        p.push("void main() {", 0);
        for s in stmts {
            p.push_entry(s);
        }
        if let New::Stmt(s) = new {
            p.input_at = p.src.len();
            p.push_entry(s);
        }
        p.main_close = Span::new(p.src.len(), p.src.len() + 1);
        p.src.push('}');
        p.labels.push(0);
        p
    }

    fn push_entry(&mut self, e: &Entry) {
        if let Some(typed) = &e.typed {
            self.regions.push(Region {
                start: self.src.len(),
                len: e.text.len(),
                typed: typed.clone(),
            });
        }
        self.push(&e.text, e.line);
    }

    // `src` with every wrapped entry replaced by what was typed, and the matching offset map.
    fn display(&self) -> (String, impl Fn(usize) -> usize + '_) {
        let mut shown = String::with_capacity(self.src.len());
        let mut at = 0;
        for r in &self.regions {
            shown.push_str(&self.src[at..r.start]);
            shown.push_str(&r.typed.input);
            at = r.start + r.len;
        }
        shown.push_str(&self.src[at..]);
        let map = move |off: usize| {
            let mut delta: isize = 0;
            for r in &self.regions {
                let t = &r.typed;
                if off < r.start {
                    break;
                }
                if off <= r.start + r.len {
                    let rel = off - r.start;
                    // Offsets inside the added prefix/suffix move to the nearest typed character.
                    let typed_rel = if rel <= t.prefix {
                        0
                    } else if rel - t.prefix <= t.code {
                        rel - t.prefix
                    } else {
                        (rel - t.prefix - t.suffix).max(t.code)
                    };
                    let typed_rel = typed_rel.min(t.input.len());
                    return (r.start as isize + delta) as usize + typed_rel;
                }
                delta += t.input.len() as isize - r.len as isize;
            }
            (off as isize + delta) as usize
        };
        (shown, map)
    }

    fn push(&mut self, text: &str, first_line: usize) {
        for (i, line) in text.split('\n').enumerate() {
            self.src.push_str(line);
            self.src.push('\n');
            self.labels
                .push(if first_line == 0 { 0 } else { first_line + i });
        }
    }

    fn render(&self, errors: &[Diagnostic]) -> Failure {
        let (shown, at) = self.display();
        let span = |s: Span| Span::new(at(s.start), at(s.end));
        let map = SourceMap::new(NAME, &shown).with_labels(self.labels.clone());
        let text: Vec<String> = errors
            .iter()
            .map(|d| {
                let mut d = d.clone();
                d.span = span(d.span);
                for n in &mut d.notes {
                    n.span = n.span.map(span);
                }
                map.render(&d)
            })
            .collect();
        Failure(text.join("\n"))
    }

    // Qubits still live when `main` ends are fine between inputs; this is the leak `Q0302` reports there.
    fn is_open_leak(&self, d: &Diagnostic) -> bool {
        d.code == Code::NeverConsumed
            && d.notes.first().and_then(|n| n.span) == Some(self.main_close)
    }
}

/// Brackets still open in `text`, ignoring string literals and `//` comments.
pub fn open_brackets(text: &str) -> i64 {
    scan(text).depth
}

struct Scan {
    depth: i64,
    // Lowest depth reached: below zero, the input closes a bracket it did not open.
    min_depth: i64,
    // Byte offset of a `//` comment that runs to the end of `text`, if any.
    trailing_comment: Option<usize>,
}

fn scan(text: &str) -> Scan {
    let mut s = Scan {
        depth: 0,
        min_depth: 0,
        trailing_comment: None,
    };
    let mut chars = text.char_indices().peekable();
    while let Some((i, c)) = chars.next() {
        match c {
            '"' => {
                while let Some((_, c)) = chars.next() {
                    match c {
                        '\\' => {
                            chars.next();
                        }
                        '"' | '\n' => break,
                        _ => {}
                    }
                }
            }
            '/' if chars.peek().map(|&(_, c)| c) == Some('/') => {
                s.trailing_comment = Some(i);
                for (_, c) in chars.by_ref() {
                    if c == '\n' {
                        s.trailing_comment = None;
                        break;
                    }
                }
            }
            '{' | '(' | '[' => s.depth += 1,
            '}' | ')' | ']' => {
                s.depth -= 1;
                s.min_depth = s.min_depth.min(s.depth);
            }
            _ => {}
        }
    }
    s
}

// A `return` anywhere in the new input would end the session's `main` and skip later inputs.
fn has_return(stmts: &[ast::Stmt], from: usize) -> bool {
    stmts.iter().any(|s| match s {
        ast::Stmt::Return { span, .. } => span.start >= from,
        ast::Stmt::If(i) => if_has_return(i, from),
        ast::Stmt::For { body, .. } => has_return(&body.stmts, from),
        _ => false,
    })
}

fn if_has_return(i: &ast::IfStmt, from: usize) -> bool {
    has_return(&i.then.stmts, from)
        || match &i.els {
            Some(ast::Else::Block(b)) => has_return(&b.stmts, from),
            Some(ast::Else::If(inner)) => if_has_return(inner, from),
            None => false,
        }
}

fn is_fn_decl(input: &str) -> bool {
    lexer::lex(input)
        .ok()
        .and_then(|toks| parser::parse(toks).ok())
        .is_some_and(|p| !p.fns.is_empty())
}

impl Session {
    pub fn new(seed: u64) -> Self {
        Self {
            fns: Vec::new(),
            stmts: Vec::new(),
            next_line: 1,
            seed,
            shown: String::new(),
        }
    }

    /// Handles one complete input: a `:command`, a function definition, statements, or an
    /// expression (printed like `println("{}", expr)`; a trailing `;` may be left out).
    pub fn input(&mut self, input: &str) -> Reply {
        // Piped files may start with a UTF-8 byte order mark, which `run` also ignores.
        let input = input.trim_start_matches('\u{feff}').trim();
        if input.is_empty() {
            return Reply::Out(String::new());
        }
        if let Some(cmd) = input.strip_prefix(':') {
            return self.command(cmd.trim());
        }
        let line = self.next_line;
        let entry = |text: String| Entry {
            text,
            line,
            typed: None,
        };
        let scanned = scan(input);
        // The code before a trailing `//` comment, which the added `);` or `;` must not follow.
        let code = input[..scanned.trailing_comment.unwrap_or(input.len())].trim_end();
        let rest = &input[code.len()..];
        // `prefix + code + suffix + rest`, shown in diagnostics as the typed `code + rest`.
        let wrap = |prefix: &str, suffix: &str| Entry {
            text: format!("{prefix}{code}{suffix}{rest}"),
            line,
            typed: Some(Typed {
                input: input.to_string(),
                prefix: prefix.len(),
                code: code.len(),
                suffix: suffix.len(),
            }),
        };
        let reply = if is_fn_decl(input) {
            let e = entry(input.to_string());
            self.try_run(New::Fn(&e)).map(|out| (e, true, out))
        } else if scanned.min_depth < 0 {
            // Closing `main` early would move every later input out of it.
            return Reply::Err(msg::repl_unbalanced());
        } else if code.ends_with(';') || code.ends_with('}') {
            let e = entry(input.to_string());
            self.try_run(New::Stmt(&e)).map(|out| (e, false, out))
        } else {
            // A bare expression is printed; anything else gets the `;` it was typed without.
            let echo = wrap("println(\"{}\", ", ");");
            match self.try_run(New::Stmt(&echo)) {
                Ok(out) => Ok((echo, false, out)),
                Err(_) => {
                    let e = wrap("", ";");
                    self.try_run(New::Stmt(&e)).map(|out| (e, false, out))
                }
            }
        };
        match reply {
            Ok((e, is_fn, out)) => {
                self.next_line += input.lines().count();
                if is_fn {
                    self.fns.push(e);
                } else {
                    self.stmts.push(e);
                }
                Reply::Out(out)
            }
            Err(Failure(d)) => Reply::Err(d),
        }
    }

    fn command(&mut self, cmd: &str) -> Reply {
        match cmd {
            "help" => Reply::Out(msg::repl_help()),
            "quit" | "q" => Reply::Quit,
            "reset" => {
                *self = Session::new(self.seed);
                Reply::Out(msg::repl_reset())
            }
            "qasm" => match self.build(New::Nothing) {
                Ok((_, e)) => Reply::Out(e.circuit.to_qasm()),
                Err(f) => Reply::Err(f.0),
            },
            other => Reply::Err(msg::repl_unknown_command(other)),
        }
    }

    /// Diagnostics for qubits that are still live, reported when the session ends.
    pub fn leaks(&self) -> Option<String> {
        let p = Program::assemble(&self.fns, &self.stmts, New::Nothing);
        let mut errors = front(&p.src).err()?;
        // The closing brace of `main` is not something the user typed; leave its note out.
        for d in &mut errors {
            d.notes.retain(|n| n.span != Some(p.main_close));
        }
        Some(p.render(&errors).0)
    }

    fn build(&self, new: New) -> Result<(Program, interp::Elaborated), Failure> {
        let is_stmt = matches!(new, New::Stmt(_));
        let p = Program::assemble(&self.fns, &self.stmts, new);
        let program = match front(&p.src) {
            Ok(program) => program,
            Err(errors) => {
                let errors: Vec<Diagnostic> =
                    errors.into_iter().filter(|d| !p.is_open_leak(d)).collect();
                if !errors.is_empty() {
                    return Err(p.render(&errors));
                }
                // Only open leaks: parse again, since `front` drops the AST on any error.
                let toks = lexer::lex(&p.src).map_err(|d| p.render(&[d]))?;
                parser::parse(toks).map_err(|d| p.render(&[d]))?
            }
        };
        if is_stmt {
            let main = program.fns.iter().find(|f| f.name.name == "main");
            let top_return = main.is_some_and(|f| has_return(&f.body.stmts, p.input_at));
            // A top-level `return` would end `main` and silently skip every later input.
            if top_return {
                return Err(Failure(msg::repl_return()));
            }
        }
        let e = interp::elaborate(&program).map_err(|d| p.render(&[d]))?;
        Ok((p, e))
    }

    // Replays the session plus the new input and returns only the output the input added.
    fn try_run(&mut self, new: New) -> Result<String, Failure> {
        let (p, e) = self.build(new)?;
        let prints_bits = e
            .output
            .iter()
            .any(|o| matches!(o, interp::OutPiece::Bit(_) | interp::OutPiece::Bits(..)));
        // Always the state-vector backend: `Auto` would switch backends once a non-Clifford gate
        // appears, and another backend may draw different outcomes for bits already shown.
        let out = if prints_bits {
            let at = Span::new(p.input_at, p.input_at);
            // Shot 0 of `--seed S`, the same seed derivation as `qxide run`.
            let shot = e
                .circuit
                .simulate_shots(Backend::StateVector, self.seed, 1)
                .next()
                .expect("one shot")
                .map_err(|err| p.render(&[sim_error(err, at)]))?;
            render_output(&e.output, Some(&shot))
        } else {
            render_output(&e.output, None)
        };
        let new = out
            .strip_prefix(self.shown.as_str())
            .unwrap_or(&out)
            .to_string();
        self.shown = out;
        Ok(new)
    }
}

/// Runs the read-eval-print loop until `:quit` or end of input. Returns the process exit code:
/// 0, or 1 when the session ends with qubits that were never measured or discarded.
pub fn run(
    input: impl BufRead,
    out: &mut impl Write,
    err: &mut impl Write,
    seed: u64,
    interactive: bool,
) -> u8 {
    let mut session = Session::new(seed);
    if interactive {
        let _ = write!(out, "{}", msg::repl_banner(env!("CARGO_PKG_VERSION")));
    }
    let mut lines = input.lines();
    let mut buf = String::new();
    loop {
        if interactive {
            let _ = write!(out, "{}", if buf.is_empty() { "qx> " } else { "... " });
            let _ = out.flush();
        }
        let Some(Ok(line)) = lines.next() else {
            if !buf.trim().is_empty() {
                report(session.input(&buf), out, err);
            }
            break;
        };
        buf.push_str(&line);
        buf.push('\n');
        if open_brackets(&buf) > 0 {
            continue;
        }
        let reply = session.input(&buf);
        buf.clear();
        if matches!(reply, Reply::Quit) {
            break;
        }
        report(reply, out, err);
    }
    match session.leaks() {
        Some(d) => {
            let _ = write!(err, "{d}");
            1
        }
        None => 0,
    }
}

fn report(reply: Reply, out: &mut impl Write, err: &mut impl Write) {
    match reply {
        Reply::Out(s) => {
            let _ = write!(out, "{s}");
            let _ = out.flush();
        }
        Reply::Err(s) => {
            let _ = write!(err, "{s}");
            let _ = err.flush();
        }
        Reply::Quit => {}
    }
}