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
// Tests for `qxide fmt`, `qxide explain` and `qxide repl`.

use super::diag::Code;
use super::*;

const EXAMPLES: &[(&str, &str)] = &[
    ("bell.qx", include_str!("../../examples/qx/bell.qx")),
    ("ghz.qx", include_str!("../../examples/qx/ghz.qx")),
    ("teleport.qx", include_str!("../../examples/qx/teleport.qx")),
    (
        "functions.qx",
        include_str!("../../examples/qx/functions.qx"),
    ),
    ("rotation.qx", include_str!("../../examples/qx/rotation.qx")),
    (
        "teleport_fn.qx",
        include_str!("../../examples/qx/teleport_fn.qx"),
    ),
    (
        "conditional_fn.qx",
        include_str!("../../examples/qx/conditional_fn.qx"),
    ),
    ("template/main.qx", include_str!("../template/main.qx")),
];

const DESIGN_DOC: &str = include_str!("../../docs/design/qx-language.md");

fn lf(s: &str) -> String {
    s.replace("\r\n", "\n")
}

fn fmt_ok(src: &str) -> String {
    match format("t.qx", src) {
        Ok(s) => s,
        Err(Failure(d)) => panic!("fmt failed:\n{d}"),
    }
}

// ---- fmt ----

#[test]
fn fmt_leaves_every_example_unchanged() {
    for (name, src) in EXAMPLES {
        assert_eq!(fmt_ok(src), *src, "{name} is not in canonical form");
    }
}

// Strips all indentation, moves each block's `{` to its own line and doubles blank lines.
fn mangle(src: &str) -> String {
    let mut out = String::new();
    for line in lf(src).lines() {
        let line = line.trim_start();
        match line.strip_suffix(" {") {
            Some(head) if !line.starts_with("//") => {
                out.push_str(head);
                out.push_str("\n{\n");
            }
            _ => {
                out.push_str(line);
                out.push('\n');
            }
        }
        if line.is_empty() {
            out.push('\n');
        }
    }
    out
}

#[test]
fn fmt_restores_mangled_examples_without_changing_meaning() {
    for (name, src) in EXAMPLES {
        let src = lf(src);
        let messy = mangle(&src);
        assert_ne!(messy, src, "{name}: mangling changed nothing");
        let out = fmt_ok(&messy);
        assert_eq!(out, src, "{name}");
        assert_eq!(fmt::fingerprint(&out), fmt::fingerprint(&messy), "{name}");
        assert_eq!(qasm("t.qx", &out).ok(), qasm("t.qx", &src).ok(), "{name}");
    }
}

const MESSY: &str = "// header comment

int   sign( int n ){
  if n<0{return -1;}   // negative
  else if(n==0)
  {
    return 0 ;
  }
  return(int) -2.5*-1+n%2;
}



void   noop(){}
void main( ) {
    double d=( double )sign(-3) ;
    // own line, indented wrong
        QReg r=qreg( 3 ) ;
    for i in 0 .. len(r) { h( r[ i ] ) ; }



    BitReg m=measure( r );
    boolean ok = !(1 < 2) || d >= -1.0 && true;
    println( \"{} {} {}\" , m.to_binary() , ok, // trailing in a call
        m[0] );
    noop();
}
";

const TIDY: &str = "// header comment

int sign(int n) {
    if n < 0 {
        return -1;
    }   // negative
    else if (n == 0) {
        return 0;
    }
    return (int) -2.5 * -1 + n % 2;
}

void noop() {}
void main() {
    double d = (double) sign(-3);
    // own line, indented wrong
    QReg r = qreg(3);
    for i in 0..len(r) {
        h(r[i]);
    }

    BitReg m = measure(r);
    boolean ok = !(1 < 2) || d >= -1.0 && true;
    println(\"{} {} {}\", m.to_binary(), ok, // trailing in a call
        m[0]);
    noop();
}
";

#[test]
fn fmt_canonical_layout() {
    let out = fmt_ok(MESSY);
    assert_eq!(out, TIDY);
    assert_eq!(fmt_ok(&out), out, "formatting is idempotent");
    assert_eq!(fmt::fingerprint(&out), fmt::fingerprint(MESSY));
    assert_eq!(run("t.qx", &out, 1, 0).ok(), run("t.qx", MESSY, 1, 0).ok());
}

#[test]
fn fmt_keeps_literal_spelling_bom_and_crlf() {
    let src = "\u{feff}void main() {\r\n  int big=1_000;double e=1e3;\r\n  println(\"tab\\t{{}} {} {}\",big,e);\r\n}\r\n";
    let out = fmt_ok(src);
    assert_eq!(
        out,
        "\u{feff}void main() {\r\n    int big = 1_000;\r\n    double e = 1e3;\r\n    println(\"tab\\t{{}} {} {}\", big, e);\r\n}\r\n"
    );
}

// A CRLF file may contain a raw line break inside a string literal; it must stay `\r\n`.
#[test]
fn fmt_keeps_crlf_line_breaks_inside_strings() {
    let src = "void main() {\r\n    println(\"a\r\nb\");\r\n}\r\n";
    assert_eq!(fmt_ok(src), src);
    let messy = "void main() {\r\n  println(\"a\r\nb\");\r\n}\r\n";
    assert_eq!(fmt_ok(messy), src);
}

#[test]
fn fmt_refuses_sources_that_do_not_parse() {
    let Err(Failure(d)) = format("t.qx", "void main() {\n    int x = ;\n}\n") else {
        panic!("expected a parse error");
    };
    assert!(d.starts_with("error[Q0105]"), "{d}");
    // Type errors do not stop formatting: only the layout is checked.
    assert_eq!(
        fmt_ok("void main(){int x=true;}"),
        "void main() {\n    int x = true;\n}\n"
    );
}

// ---- explain ----

fn table_codes() -> Vec<String> {
    let mut codes: Vec<String> = DESIGN_DOC
        .lines()
        .filter_map(|l| l.strip_prefix("| `Q"))
        .filter_map(|rest| rest.split_once("` |").map(|(d, _)| format!("Q{d}")))
        .filter(|c| explain::normalize(c).as_deref() == Some(c.as_str()))
        .collect();
    codes.sort();
    codes.dedup();
    codes
}

fn known_codes() -> Vec<String> {
    let mut all: Vec<String> = Code::ALL.iter().map(|c| c.as_str().to_string()).collect();
    all.extend(explain::RETIRED.iter().map(|c| c.to_string()));
    all.sort();
    all
}

#[test]
fn explain_catalog_covers_every_code_once_in_order() {
    let all: Vec<String> = explain::entries().into_iter().map(|(c, _)| c).collect();
    let mut sorted = all.clone();
    sorted.sort();
    sorted.dedup();
    assert_eq!(all, sorted, "catalog entries must be unique and in order");
    // Package-manager codes belong to the binary's `pkg` module; src/pkg/tests.rs checks them.
    let codes: Vec<String> = all.into_iter().filter(|c| !c.starts_with("Q06")).collect();
    assert_eq!(codes, known_codes());
}

#[test]
fn design_doc_tables_list_the_same_codes() {
    assert_eq!(table_codes(), known_codes());
}

/// The ```qx blocks of an entry: the example that triggers the code, then the corrected one.
fn qx_blocks(entry: &str) -> Vec<String> {
    let mut blocks = Vec::new();
    let mut rest = entry;
    while let Some(start) = rest.find("```qx\n") {
        let body = &rest[start + 6..];
        let end = body.find("```").expect("unterminated code block");
        blocks.push(body[..end].to_string());
        rest = &body[end + 3..];
    }
    blocks
}

#[test]
fn explain_examples_trigger_their_code_and_fixes_run() {
    // Safety-net and retired codes cannot be triggered by any program.
    let untriggerable = ["Q0219", "Q0407", "Q0412"];
    // Import errors need a project with several files; their examples are `java` blocks and the
    // same cases are tested in module_tests.rs. Q0508 (import in a lone file) stays a `qx` example.
    let needs_project = [
        "Q0501", "Q0502", "Q0503", "Q0504", "Q0505", "Q0506", "Q0507", "Q0509", "Q0510",
    ];
    for (code, entry) in explain::entries() {
        let blocks = qx_blocks(&entry);
        // Package-manager errors come from commands, not from a `.qx` program.
        if code.starts_with("Q06") {
            assert!(blocks.is_empty(), "{code}");
            continue;
        }
        if untriggerable.contains(&code.as_str()) {
            assert!(blocks.is_empty(), "{code}");
            continue;
        }
        if needs_project.contains(&code.as_str()) {
            assert!(blocks.is_empty(), "{code}");
            assert_eq!(
                entry.matches("```java\n").count(),
                2,
                "{code} needs an example and a fix"
            );
            continue;
        }
        assert_eq!(blocks.len(), 2, "{code} needs an example and a fix");
        match run("example.qx", &blocks[0], 1, 0) {
            Ok(_) => panic!("{code}: the example runs without errors"),
            Err(Failure(d)) => {
                let reported: Vec<&str> = d.lines().filter(|l| l.starts_with("error[")).collect();
                assert!(
                    reported
                        .iter()
                        .any(|l| l.starts_with(&format!("error[{code}]"))),
                    "{code}: example reports {reported:?}"
                );
            }
        }
        if let Err(Failure(d)) = run("fixed.qx", &blocks[1], 1, 0) {
            panic!("{code}: the corrected example fails:\n{d}");
        }
        assert_eq!(
            fmt_ok(&blocks[1]),
            blocks[1],
            "{code}: fixed example is not formatted"
        );
    }
}

#[test]
fn explain_accepts_any_case_and_rejects_unknown_codes() {
    let entry = explain("q0302").expect("lowercase code");
    assert!(entry.starts_with("Q0302: "), "{entry}");
    assert_eq!(explain("Q0302"), Some(entry));
    assert!(explain("Q0219").is_some(), "retired codes keep their entry");
    for bad in ["Q9999", "Q0208", "Q302", "0302", "E0302", ""] {
        assert_eq!(explain(bad), None, "{bad}");
    }
}

// ---- repl ----

fn repl(input: &str) -> (String, String, u8) {
    repl_seed(input, 0)
}

fn repl_seed(input: &str, seed: u64) -> (String, String, u8) {
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let code = repl::run(input.as_bytes(), &mut out, &mut err, seed, false);
    (
        String::from_utf8(out).unwrap(),
        String::from_utf8(err).unwrap(),
        code,
    )
}

fn error_codes(err: &str) -> Vec<&str> {
    err.lines()
        .filter_map(|l| l.strip_prefix("error[")?.split(']').next())
        .collect()
}

#[test]
fn repl_keeps_variables_and_echoes_expressions() {
    let (out, err, code) = repl("int n = 2\nn = n + 1;\nn * 10\ndouble half = PI / 2;\n(int) half\nprintln(\"done {}\", n);\n");
    assert_eq!(
        (out.as_str(), err.as_str(), code),
        ("30\n1\ndone 3\n", "", 0)
    );
}

#[test]
fn repl_applies_linearity_and_keeps_going() {
    let (out, err, code) = repl(
        "Qubit q = qubit();\nx(q);\nBit m = measure(q);\nh(q);\nm\nQubit q = qubit();\ndiscard(q);\n",
    );
    assert_eq!(out, "1\n");
    assert_eq!(error_codes(&err), ["Q0301"], "{err}");
    // The rejected line is not counted: `h(q)` is reported on line 4, the line it was typed on.
    assert!(err.contains("<repl>:4:3"), "{err}");
    assert_eq!(code, 0);
}

#[test]
fn repl_reports_live_qubits_when_the_session_ends() {
    let (_, err, code) = repl("Qubit q = qubit();\nh(q);\nQReg r = qreg(2);\ndiscard(r);\n");
    assert_eq!(error_codes(&err), ["Q0302"], "{err}");
    assert!(err.contains("<repl>:1:7"), "{err}");
    assert!(
        !err.contains("| }"),
        "the hidden closing brace is not shown:\n{err}"
    );
    assert_eq!(code, 1);
    // `:quit` ends the session the same way.
    let (_, err, code) = repl("Qubit q = qubit();\n:quit\nh(q);\n");
    assert_eq!((error_codes(&err), code), (vec!["Q0302"], 1));
}

#[test]
fn repl_shown_bits_never_change_later_in_the_session() {
    // A later non-Clifford gate must not change the outcome of a measurement already printed.
    for seed in 0..16 {
        let (out, err, _) = repl_seed(
            "Qubit a = qubit();\nh(a);\nBit m = measure(a);\nm\nQubit b = qubit();\nh(b);\nt(b);\nBit n = measure(b);\nprintln(\"{} {}\", m, n);\n",
            seed,
        );
        assert_eq!(err, "");
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines.len(), 2, "{out}");
        assert!(lines[1].starts_with(lines[0]), "seed {seed}: {out}");
    }
}

#[test]
fn repl_defines_functions_across_lines_and_calls_them() {
    let src = "void flip(Qubit q) {\n    x(q);\n}\nint twice(int n) {\n    return n * 2;\n}\nQubit q = qubit();\nflip(q);\nmeasure(q)\ntwice(21)\n";
    let (out, err, code) = repl(src);
    assert_eq!((out.as_str(), err.as_str(), code), ("1\n42\n", "", 0));
}

#[test]
fn repl_commands() {
    let (out, err, code) = repl(":help\nQubit q = qubit();\nh(q);\n:qasm\n:reset\nq\n:nope\n");
    assert!(out.contains("h q[0];"), "{out}");
    assert!(out.contains("OPENQASM 3"), "{out}");
    // After `:reset`, `q` is gone (and no longer leaks).
    assert_eq!(error_codes(&err), ["Q0201"], "{err}");
    assert!(err.contains(":nope") || err.contains("nope"), "{err}");
    assert_eq!(code, 0);
}

#[test]
fn repl_rejects_top_level_return() {
    let (out, err, code) = repl("return;\nprintln(\"after\");\n");
    assert_eq!(out, "after\n");
    assert!(!err.is_empty());
    assert_eq!(code, 0);
}

// A `return` anywhere in `main` (not just at the top level) would end the replayed session.
#[test]
fn repl_rejects_nested_return() {
    for nested in [
        "if true { return; }",
        "if true {\n    if 1 < 2 {\n        return;\n    }\n}",
        "for i in 0..2 { return; }",
        "if false { } else { return; }",
    ] {
        let (out, err, code) = repl(&format!(
            "println(\"before\");\n{nested}\nprintln(\"after\");\nint x = 42;\nx\n"
        ));
        assert_eq!(out, "before\nafter\n42\n", "{nested}: {err}");
        assert!(!err.is_empty(), "{nested}");
        assert_eq!(code, 0);
    }
    // A `return` inside a function definition is fine.
    let (out, err, _) =
        repl("int f() {\n    if true {\n        return 1;\n    }\n    return 2;\n}\nf()\n");
    assert_eq!((out.as_str(), err.as_str()), ("1\n", ""));
}

// The REPL adds `);`/`;` after the input; a trailing `//` comment must not swallow it.
#[test]
fn repl_accepts_trailing_line_comments() {
    let (out, err, _) =
        repl("1 + 2 // sum\nint x = 1 // one\nx + 1 // two\nprintln(\"//\") // s\n");
    assert_eq!((out.as_str(), err.as_str()), ("3\n2\n//\n", ""));
}

// Input that closes `main` early could inject functions and silently break the session.
#[test]
fn repl_rejects_input_that_closes_main() {
    let (out, err, code) = repl("int a = 3\n} void evil() { int z = 1\nprintln(\"hi\");\na\n");
    assert_eq!(out, "hi\n3\n", "{err}");
    assert!(!err.is_empty());
    assert_eq!(code, 0);
}

#[test]
fn repl_strips_a_byte_order_mark() {
    let (out, err, _) = repl("\u{feff}int a = 3;\na\n");
    assert_eq!((out.as_str(), err.as_str()), ("3\n", ""));
}

// Diagnostics show what the user typed, not the `println("{}", …)` wrapper the REPL runs.
#[test]
fn repl_diagnostics_show_the_typed_input() {
    let (_, err, _) = repl("Qubit q = qubit();\nmeasure(q)\nh(q);\n");
    assert_eq!(error_codes(&err), ["Q0301"], "{err}");
    assert!(!err.contains("println"), "{err}");
    assert!(err.contains("2 | measure(q)"), "{err}");
    let (_, err, _) = repl("undefined_var + 1\n");
    assert_eq!(error_codes(&err), ["Q0201"], "{err}");
    assert!(err.contains("1 | undefined_var + 1\n"), "{err}");
    assert!(err.contains(" --> <repl>:1:1"), "{err}");
}

#[test]
fn repl_reports_runtime_errors_without_keeping_the_input() {
    let (out, err, code) = repl("int zero = 0;\n10 / zero\nzero + 1\n");
    assert_eq!(out, "1\n");
    assert_eq!(error_codes(&err), ["Q0402"], "{err}");
    assert_eq!(code, 0);
}

#[test]
fn repl_waits_for_closing_brackets() {
    assert_eq!(repl::open_brackets("if b {\n"), 1);
    assert_eq!(repl::open_brackets("println(\"{\"); // {\n"), 0);
    assert_eq!(repl::open_brackets("f(a,\n"), 1);
}

#[test]
fn repl_interactive_mode_shows_banner_and_prompts() {
    // The README transcript: a prompt goes to stdout before each line is read, diagnostics to stderr.
    let input = "Qubit q = qubit();\nh(q);\nBit m = measure(q);\nm\nh(q);\nvoid f() {\n}\n:quit\n";
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let code = repl::run(input.as_bytes(), &mut out, &mut err, 0, true);
    let out = String::from_utf8(out).unwrap();
    let banner = msg::repl_banner(env!("CARGO_PKG_VERSION"));
    assert_eq!(out, format!("{banner}qx> qx> qx> qx> 1\nqx> qx> ... qx> "));
    let err = String::from_utf8(err).unwrap();
    assert_eq!(error_codes(&err), ["Q0301"]);
    assert_eq!(code, 0);
}