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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
# Qxide Error Codes

This catalog lists every .qx compiler error code; every diagnostic prints a code such as `Q0302`, and running `qxide explain Q0302` prints that code's entry from this file. Codes are never reused and are grouped by category: `Q01xx` for lexing and parsing, `Q02xx` for names and types, `Q03xx` for qubit ownership (linearity), `Q04xx` for errors found while running the program, and `Q05xx` for modules and imports. Each entry includes an explanation, an example that triggers the error, and a corrected version.

## Q0101: unexpected character in source code

The lexer encountered a character that is not allowed in the `.qx` language. Because the compiler cannot form a valid token from an unrecognized character, it rejects the file during lexing rather than guessing what was intended. This prevents typos or syntax borrowed from other languages from being silently misinterpreted.

Erroneous code example:

```qx
void main() {
    int x = 1 # 2;  // error[Q0101]
    println("{}", x);
}
```

To fix it, replace the unrecognized `#` character with the `+` operator so the expression forms valid `.qx` syntax.

```qx
void main() {
    int x = 1 + 2;
    println("{}", x);
}
```

## Q0102: unterminated string literal

A string literal starts with an opening double quote `"` but has no matching closing double quote `"`. Because the lexer cannot tell where the string ends and the rest of the program resumes, it cannot tokenize the surrounding code reliably. Requiring every string literal to be explicitly closed keeps token boundaries unambiguous.

Erroneous code example:

```qx
void main() {
    println("hello);
}
```

To fix it, add the missing closing `"` after `hello` so the string literal is properly terminated.

```qx
void main() {
    println("hello");
}
```

## Q0103: unknown escape sequence in a string literal

A string literal contains a backslash escape sequence that the `.qx` language does not recognize. Only `\n`, `\t`, `\\`, and `\"` are supported escape sequences. Rejecting unknown escapes during lexing prevents mistyped escapes from being silently turned into unintended characters.

Erroneous code example:

```qx
void main() {
    println("tab\q");  // error[Q0103]
}
```

To fix it, replace the unsupported `\q` escape sequence with the supported `\t` escape sequence.

```qx
void main() {
    println("tab\t");
}
```

## Q0104: integer literal out of range for `int`

An integer literal exceeds the range representable by a 64-bit signed integer (`i64`), which backs the `int` type. Because the value cannot fit without overflowing, the compiler rejects the literal instead of truncating or wrapping it silently. This ensures that integer constants always represent the exact value written in the source code.

Erroneous code example:

```qx
void main() {
    int big = 99999999999999999999;  // error[Q0104]
    println("{}", big);
}
```

To fix it, replace the out-of-range literal with `9223372036854775807`, which fits within the `i64` range of `int`.

```qx
void main() {
    int big = 9223372036854775807;
    println("{}", big);
}
```

## Q0105: unexpected token or chained comparison

The parser encountered an unexpected token that does not fit the grammar of the `.qx` language, including chained comparison operators. In `.qx`, comparison operators cannot be chained directly in a single expression. Rejecting unexpected tokens and chained comparisons prevents ambiguous syntax and ensures compound conditions are written explicitly.

Erroneous code example:

```qx
void main() {
    int a = 1;
    boolean ok = 0 < a < 2;  // error[Q0105]
    println("{}", ok);
}
```

To fix it, split the chained comparison `0 < a < 2` into two comparisons joined with `&&`, writing `0 < a && a < 2`.

```qx
void main() {
    int a = 1;
    boolean ok = 0 < a && a < 2;
    println("{}", ok);
}
```

## Q0106: nesting depth exceeds 128 levels

Error `Q0106` occurs when syntax nesting exceeds the compiler's maximum depth of 128 levels. Long left-associative chains also count toward this limit, because each binary operator in a chain like `a + b + c` and each index or method call in a chain like `r[0][0]` nests the syntax tree one level deeper. The parser enforces this limit to keep recursion bounded and prevent excessive stack usage and interpreter stack overflows on deeply nested input.

Erroneous code example:

```qx
void main() {
    int x = ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))));  // error[Q0106]
    println("{}", x);
}
```

To fix it, remove unnecessary nesting or split deeply nested expressions into smaller parts stored in local variables so the nesting depth stays within 128 levels.

```qx
void main() {
    int x = (1);
    println("{}", x);
}
```

## Q0107: use of an unsupported reserved keyword

Error `Q0107` occurs when a program uses a reserved keyword that the `.qx` compiler does not support yet: `while`, `loop`, `break`, `continue`, `struct`, `enum`, `impl`, `use`, `mod`, `const`, `match`, `with_ancilla`, or `apply`. These words are reserved in the grammar so they cannot be used as identifiers and remain available for future language features. Because the compiler does not yet implement them, the lexer rejects a reserved keyword as soon as it reads it.

Erroneous code example:

```qx
void main() {
    int i = 0;
    while i < 3 {  // error[Q0107]
        i = i + 1;
    }
}
```

To fix it, replace the unsupported `while` loop with a supported construct such as a `for` loop over a range.

```qx
void main() {
    for i in 0..3 {
        println("{}", i);
    }
}
```

## Q0108: unknown type name

Error `Q0108` occurs when a declaration or function signature names a type that does not exist in `.qx`, such as `long`. Every variable, parameter, and function return type must be one of the available built-in types: `int`, `double`, `boolean`, `Bit`, `BitReg`, `Qubit`, `QReg`, or `void` for functions that return nothing. Rejecting unrecognized type names ensures the compiler can statically check both classical and quantum values.

Erroneous code example:

```qx
void main() {
    long n = 1;  // error[Q0108]
    println("{}", n);
}
```

To fix it, replace the unknown type name with one of the available `.qx` types, such as `int` for integer values.

```qx
void main() {
    int n = 1;
    println("{}", n);
}
```

## Q0109: invalid format string in `println`

Error `Q0109` occurs when the format string passed to `println` uses unsupported or malformed brace syntax, such as named placeholders or unescaped braces. Format strings in `println` only allow `{}` to insert a value and `{{` or `}}` to print literal braces. Validating the format string at compile time catches formatting mistakes before the program runs.

Erroneous code example:

```qx
void main() {
    int x = 1;
    println("x = {x}", x);  // error[Q0109]
}
```

To fix it, replace any invalid placeholder in the `println` format string with `{}` for each value argument, and escape literal braces as `{{` or `}}`.

```qx
void main() {
    int x = 1;
    println("x = {}", x);
}
```

## Q0110: use of removed `let`, `fn`, or `mut` syntax

Error `Q0110` occurs when a program uses the removed Rust-style keywords `let`, `fn`, or `mut`. Since `qxide` 0.5.0, `.qx` uses Java/C-style declarations instead of these keywords, so the parser no longer accepts them. Recognizing these removed keywords allows the compiler to emit a targeted migration error rather than a generic syntax failure.

Erroneous code example:

```qx
fn main() {  // error[Q0110]
    let x = 1;
}
```

To fix it, declare functions with an explicit return type such as `void main()` and declare variables with their type such as `int x = 1;`, using `final` for constants instead of `let`, `fn`, or `mut`.

```qx
void main() {
    int x = 1;
    println("{}", x);
}
```

## Q0111: `var` used as a parameter or return type

`Q0111` occurs when `var` appears as a parameter type or return type in a function declaration. In `.qx`, `var` is allowed only in local variable declarations, not in function signatures. Parameter and return types must be written explicitly so each function signature clearly specifies the types it accepts and returns.

Erroneous code example:

```qx
var twice(int n) {  // error[Q0111]
    return n * 2;
}

void main() {
    println("{}", twice(2));
}
```

To fix it, replace `var` with the explicit return type `int` in the declaration of `twice`. In general, write an explicit type for every parameter and return type instead of `var`.

```qx
int twice(int n) {
    return n * 2;
}

void main() {
    println("{}", twice(2));
}
```

## Q0112: type name used as an identifier

`Q0112` occurs when a type name is used as the name of a variable, parameter, or function. Type names are reserved for naming types and cannot also serve as value or function identifiers in `.qx`. Keeping type names separate from ordinary identifiers prevents ambiguity when reading and parsing declarations.

Erroneous code example:

```qx
void main() {
    int Qubit = 1;  // error[Q0112]
    println("{}", Qubit);
}
```

To fix it, rename the variable from `Qubit` to an ordinary identifier such as `count` and update its references to use the new name.

```qx
void main() {
    int count = 1;
    println("{}", count);
}
```

## Q0201: use of an unknown variable

`Q0201` occurs when an expression references a variable name that has not been declared in the current scope. Every variable in `.qx` must be declared before it is used and be visible at the point where it is referenced. This check catches misspelled names, missing declarations, and out-of-scope references at compile time.

Erroneous code example:

```qx
void main() {
    println("{}", y);  // error[Q0201]
}
```

To fix it, declare `y` with `int y = 1;` before passing it to `println`. If you meant to use an existing variable, check its spelling and make sure it is in scope.

```qx
void main() {
    int y = 1;
    println("{}", y);
}
```

## Q0202: call to an unknown function

`Q0202` occurs when a call expression refers to a function name that cannot be found in the current scope. Every called function in `.qx` must be defined or available as a built-in in the scope where it is called. Checking function names at compile time catches misspelled names and calls to nonexistent functions before the circuit is built.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    hadamard(q);  // error[Q0202]
    discard(q);
}
```

To fix it, replace the unknown call `hadamard(q)` with `h(q)`. When calling your own functions, ensure the function is defined in scope and spelled consistently.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    discard(q);
}
```

## Q0203: wrong number of function arguments

`Q0203` occurs when a function call passes fewer or more arguments than the function's declaration expects. In `.qx`, every call must supply the exact number of arguments required by the target function. Enforcing argument counts at compile time ensures each parameter receives a matching value and prevents malformed operations in the resulting circuit.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    Qubit r = qubit();
    h(q, r);  // error[Q0203]
    discard(q);
    discard(r);
}
```

To fix it, call `h` once per qubit with a single argument, using `h(q)` and `h(r)` instead of passing both `q` and `r` in one call.

```qx
void main() {
    Qubit q = qubit();
    Qubit r = qubit();
    h(q);
    h(r);
    discard(q);
    discard(r);
}
```

## Q0204: type mismatch between expected and found types

This error occurs when an expression produces a type that does not match the type expected in that position, such as in a variable declaration. The `.qx` compiler checks types statically and requires the found type to match the expected type. Enforcing strict type compatibility catches type errors at compile time before the program is simulated or exported.

Erroneous code example:

```qx
void main() {
    int n = 1.5;  // error[Q0204]
    println("{}", n);
}
```

To fix it, change the declared type of `n` from `int` to `double` so that it matches the type of the value `1.5`.

```qx
void main() {
    double n = 1.5;
    println("{}", n);
}
```

## Q0205: duplicate function or parameter name

This error occurs when two functions or two parameters share the same name within the same scope. Each function and parameter name must be unique so the compiler can resolve every reference to a single definition without ambiguity. Disallowing duplicate names ensures that calls and references always refer to the intended declaration.

Erroneous code example:

```qx
int one() {
    return 1;
}

int one() {  // error[Q0205]
    return 2;
}

void main() {
    println("{}", one());
}
```

To fix it, rename the second function from `one` to `two` so each function has a unique name, and update `main` to call `one()` and `two()`.

```qx
int one() {
    return 1;
}

int two() {
    return 2;
}

void main() {
    println("{}", one() + two());
}
```

## Q0206: missing or invalid `void main()` entry point

This error occurs when a `.qx` program does not define a `main` function, or defines `main` with a signature other than `void main()`. Every `.qx` program builds a single quantum circuit starting from the `void main()` entry point. Without a valid `void main()` function, the compiler has no starting point for executing the program with `qxide run` or exporting it with `qxide qasm`.

Erroneous code example:

```qx
void start() {  // error[Q0206]
    println("hi");
}
```

To fix it, rename `start` to `main` so the program defines the required `void main()` entry point.

```qx
void main() {
    println("hi");
}
```

## Q0207: assignment to an immutable variable

This error occurs when a program attempts to assign a new value to an immutable variable, such as one declared with `final`. A `final` variable is bound to its initial value and cannot be reassigned later. Enforcing immutability at compile time prevents accidental modification of values that are intended to remain constant.

Erroneous code example:

```qx
void main() {
    final int n = 1;
    n = 2;  // error[Q0207]
    println("{}", n);
}
```

To fix it, remove the `final` modifier from the declaration of `n` so that `n` can be reassigned.

```qx
void main() {
    int n = 1;
    n = 2;
    println("{}", n);
}
```

## Q0209: indexing a value other than `QReg` or `BitReg`

This error occurs when the indexing operator `[]` is applied to a value whose type is neither `QReg` nor `BitReg`. In `.qx`, only quantum registers (`QReg`) and classical bit registers (`BitReg`) support element indexing. Rejecting indexing on non-register types such as `int` ensures that subscript expressions are only used on values that hold indexed elements.

Erroneous code example:

```qx
void main() {
    int n = 3;
    println("{}", n[0]);  // error[Q0209]
}
```

To fix it, replace the `int` with a quantum register created via `qreg(3)`, apply `h` to `r[0]`, and measure `r` into a `BitReg` `b` before indexing `b[0]`.

```qx
void main() {
    QReg r = qreg(3);
    h(r[0]);
    BitReg b = measure(r);
    println("{}", b[0]);
}
```

## Q0210: placeholder and argument count mismatch in `println`

A call to `println` passed a different number of arguments than the number of `{}` placeholders in its format string. Each `{}` placeholder requires one matching value argument after the format string, and every value argument must correspond to a placeholder. Checking these counts at compile time prevents missing values and silently ignored arguments.

Erroneous code example:

```qx
void main() {
    int a = 1;
    println("{} {}", a);  // error[Q0210]
}
```

To fix it, pass one argument for each `{}` placeholder in the format string, or adjust the number of placeholders to match the arguments. In the corrected program, passing `a` twice supplies both values required by the two `{}` placeholders.

```qx
void main() {
    int a = 1;
    println("{} {}", a, a);
}
```

## Q0211: first `println` argument is not a string literal

A call to `println` passed an expression that is not a string literal as its first argument. `println` uses its first argument as a format string containing `{}` placeholders for any values that follow, rather than printing a standalone value directly. Requiring a string literal allows the compiler to inspect the format string at compile time.

Erroneous code example:

```qx
void main() {
    int a = 1;
    println(a);  // error[Q0211]
}
```

To fix it, pass a format string literal as the first argument to `println`, followed by the value to print. In the corrected program, `"{}"` is passed as the format string and `a` is passed as the second argument.

```qx
void main() {
    int a = 1;
    println("{}", a);
}
```

## Q0212: unprintable value passed to `println`

A value whose type cannot be printed—such as `Qubit`—was passed as an argument to `println`. `println` can only format classical values, whereas a `Qubit` represents a quantum state inside the circuit rather than a directly readable classical value. Rejecting unprintable types at compile time makes it explicit that a quantum state cannot be printed directly.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    println("{}", q);  // error[Q0212]
    discard(q);
}
```

To fix it, obtain a printable classical value before calling `println`. In the corrected program, `measure(q)` consumes `q` and produces a `Bit` value `b`, which `println` can print.

```qx
void main() {
    Qubit q = qubit();
    Bit b = measure(q);
    println("{}", b);
}
```

## Q0213: function does not return on all paths

A function declared with a non-`void` return type has at least one execution path that reaches the end of the function body without executing a `return` statement. Callers rely on receiving a value of the declared return type regardless of which branches execute. Requiring every control-flow path to return guarantees that the function always produces a value.

Erroneous code example:

```qx
int sign(int n) {
    if n < 0 {
        return -1;
    }
}  // error[Q0213]

void main() {
    println("{}", sign(-5));
}
```

To fix it, ensure that every control-flow path in the function ends with a `return` statement of the declared type. In the corrected program, adding `return 1;` after the `if` block handles the case where the condition is false so `sign` always returns an `int`.

```qx
int sign(int n) {
    if n < 0 {
        return -1;
    }
    return 1;
}

void main() {
    println("{}", sign(-5));
}
```

## Q0214: string literal used outside `println`

A string literal was used outside the first argument of a `println` call. In `.qx`, string literals are only allowed as the format argument to `println` and cannot be assigned to variables or used as general expression values. Restricting string literals to `println` ensures they are only used where the language supports them.

Erroneous code example:

```qx
void main() {
    int n = "three";  // error[Q0214]
    println("{}", n);
}
```

To fix it, replace the string literal with a value of the expected type and use string literals only as the format argument of `println`. In the corrected program, `n` is initialized with the `int` value `3` instead of a string literal.

```qx
void main() {
    int n = 3;
    println("{}", n);
}
```

## Q0215: `if` condition is not `boolean` or `Bit`

This error occurs when the condition expression of an `if` statement has a type other than `boolean` or `Bit`. The `.qx` compiler does not implicitly convert other types, such as `int`, into a truth value. Requiring an explicit `boolean` or `Bit` keeps branch conditions unambiguous and prevents accidental type mismatches.

Erroneous code example:

```qx
void main() {
    int n = 1;
    if n {  // error[Q0215]
        println("one");
    }
}
```

To fix it, replace the `int` condition with a comparison expression that evaluates to `boolean`, such as `n == 1`.

```qx
void main() {
    int n = 1;
    if n == 1 {
        println("one");
    }
}
```

## Q0217: redefinition of a built-in function

This error occurs when a user-defined function is declared with the same name as a built-in function. Built-in function names are reserved by `.qx` and cannot be shadowed or redefined by user code. Disallowing redefinitions prevents name conflicts and keeps calls to built-in operations unambiguous.

Erroneous code example:

```qx
void h() {  // error[Q0217]
    println("hi");
}

void main() {
    println("start");
}
```

To fix it, rename the user-defined function to a name that does not conflict with a built-in, such as `hello`, and call it by the new name.

```qx
void hello() {
    println("hi");
}

void main() {
    hello();
}
```

## Q0218: `Bit` or `BitReg` used for a build-time value

This error occurs when a `Bit` or `BitReg` measurement result is used in a position that requires a value known at circuit build time. A `.qx` program constructs one quantum circuit before execution, whereas measurement results are only known per shot when the circuit runs. Because the measured value does not exist while the circuit is being built, it cannot be used where a build-time value is required.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    for i in 0..b {  // error[Q0218]
        println("{}", i);
    }
}
```

To fix it, stop using the `Bit` value `b` as a loop bound and instead print `b` directly with `println`. A `Bit` can only be used in per-shot contexts such as an `if` condition or `println`.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    println("{}", b);
}
```

## Q0219: `Qubit` or `QReg` in function signature (retired)

Error code `Q0219` is retired and is no longer emitted by the compiler. Through version 0.5.0, the compiler reported this error for functions with `Qubit` or `QReg` parameters or return types because quantum values in function signatures were not yet supported. Since version 0.6.0, qubit parameters are borrowed by default or moved with `consume`, and functions can return `Qubit` and `QReg` values; the error number is reserved and not reused.

No action is needed on current compiler versions because `Q0219` is no longer produced. If you encounter `Q0219` with an older compiler, upgrade to version 0.6.0 or later to use `Qubit` and `QReg` in function signatures.

## Q0220: unknown method on a type

This error occurs when a method call names a method that does not exist on the receiver's type. In `.qx`, only `to_binary()` and `as_int()` are supported as methods on `BitReg` values. Checking method names at compile time ensures that every call refers to an operation actually defined for that type.

Erroneous code example:

```qx
void main() {
    QReg r = qreg(2);
    BitReg b = measure(r);
    println("{}", b.as_string());  // error[Q0220]
}
```

To fix it, replace the unknown `as_string()` call on `b` with `to_binary()`, one of the two methods available on `BitReg` values alongside `as_int()`.

```qx
void main() {
    QReg r = qreg(2);
    BitReg b = measure(r);
    println("{}", b.to_binary());
}
```

## Q0221: `consume` used on a classical parameter

A function parameter with a classical type, such as `int`, was declared with the `consume` modifier. In `.qx`, `consume` applies only to quantum parameter types (`Qubit` and `QReg`), because qubits have linear ownership that can be transferred into a function. Classical values are always copied when passed as arguments, so ownership transfer does not apply to them.

Erroneous code example:

```qx
int twice(consume int n) {  // error[Q0221]
    return n * 2;
}

void main() {
    println("{}", twice(2));
}
```

To fix it, remove `consume` from the `int` parameter `n` in `twice` so the classical argument is passed normally as a copied value.

```qx
int twice(int n) {
    return n * 2;
}

void main() {
    println("{}", twice(2));
}
```

## Q0222: `var` used with an unsupported type

The `var` keyword was used to declare a variable whose initializer does not have type `int`, `double`, or `boolean`. In `.qx`, `var` is allowed only for `int`, `double`, and `boolean` values. Restricting `var` keeps other types, such as `Qubit` and `Bit`, explicit in declarations rather than inferred.

Erroneous code example:

```qx
void main() {
    var q = qubit();  // error[Q0222]
    discard(q);
}
```

To fix it, replace `var` with the explicit type `Qubit` in the declaration of `q`.

```qx
void main() {
    Qubit q = qubit();
    discard(q);
}
```

## Q0223: invalid cast between unsupported types

A cast expression attempted to convert between types other than `int` and `double`. In `.qx`, explicit casts are allowed only between `int` and `double`, converting in either direction. Disallowing other casts prevents conversions from unrelated types such as `boolean`.

Erroneous code example:

```qx
void main() {
    boolean ok = true;
    int n = (int) ok;  // error[Q0223]
    println("{}", n);
}
```

To fix it, cast a `double` value (`d`) to `int` instead of casting a `boolean` value.

```qx
void main() {
    double d = 2.7;
    int n = (int) d;
    println("{}", n);
}
```

## Q0301: use of a consumed qubit

A `Qubit` was used after it had already been consumed by being measured, discarded, or moved. In `.qx`, qubits have linear types checked at compile time, so once a qubit is consumed it is no longer available for further operations. This prevents a program from applying gates or other operations to a qubit whose quantum state has already been measured, discarded, or transferred.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    Bit b = measure(q);
    h(q);  // error[Q0301]
    println("{}", b);
}
```

To fix it, apply `h(q)` before calling `measure(q)` so that `q` is used only before it is consumed.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    println("{}", b);
}
```

## Q0302: unconsumed qubit at scope exit or `return`

A `Qubit` or `QReg` value, including a `consume` parameter that is not consumed on some path, reached the end of its scope or a `return` statement without being consumed. Because qubits in `.qx` have linear types checked at compile time, each value must be explicitly accounted for. While the Rust library would silently drop such a qubit, `.qx` requires an explicit `measure` or `discard` so that a forgotten measurement or an unintended leftover qubit is caught at compile time.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();  // error[Q0302]
    h(q);
}
```

To fix it, consume `q` before the end of `main` by calling `measure(q)` (or `discard(q)` if the measurement result is not needed). In the corrected program, `q` is measured into `Bit b` and printed before `main` ends.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    println("{}", b);
}
```

## Q0303: same qubit passed twice in one call

This error occurs when the same `Qubit` appears more than once in one call to `cx`, `ccx`, or a user function. A whole register counts as overlapping each of its elements, so passing `r` and `r[0]` together is also rejected. The check applies at compile time when possible, and duplicates that depend on runtime indices, such as `cx(r[i], r[j])` with `i == j`, are reported with the same code when the call runs.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    cx(q, q);  // error[Q0303]
    discard(q);
}
```

To fix it, allocate a second `Qubit` (`t`) with `qubit()` and pass distinct qubits to `cx(q, t)`. Make sure both `q` and `t` are consumed with `discard` before leaving scope.

```qx
void main() {
    Qubit q = qubit();
    Qubit t = qubit();
    cx(q, t);
    discard(q);
    discard(t);
}
```

## Q0304: qubit consumed in only one `if` branch

This error occurs when an `if` statement consumes a `Qubit` in one branch while leaving it unconsumed in the other, including when there is no `else` branch. Because `.qx` checks linear qubit types at compile time, the compiler must know statically whether a `Qubit` is live or consumed after the `if` statement finishes. When the two branches disagree, the qubit's state after the `if` would depend on the runtime condition.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    boolean done = true;
    if done {
        discard(q);  // error[Q0304]
    }
}
```

To fix it, add an `else` branch that also calls `discard(q)` so `q` is consumed in both branches of the `if` statement. Both paths then leave `q` in the same consumed state once control flow rejoins.

```qx
void main() {
    Qubit q = qubit();
    boolean done = true;
    if done {
        discard(q);
    } else {
        discard(q);
    }
}
```

## Q0305: outer qubit consumed inside a `for` loop

This error occurs when a `Qubit` declared outside a `for` loop is consumed inside the loop body, such as by calling `measure` or `discard`. A linear `Qubit` can only be consumed once, so consuming an outer qubit inside a loop would attempt to consume the same qubit again on later iterations. The compiler also reports `Q0302` for the outer qubit because a loop may run zero times, leaving the qubit unconsumed when it goes out of scope.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    for i in 0..2 {
        Bit b = measure(q);  // error[Q0305]
    }
}
```

To fix it, move the `qubit()` allocation inside the `for` loop body so each iteration allocates and consumes its own `Qubit` with `measure`. Because `q` is created and measured within the same iteration, it is neither consumed more than once nor left unconsumed after the loop.

```qx
void main() {
    for i in 0..2 {
        Qubit q = qubit();
        Bit b = measure(q);
        println("{}", b);
    }
}
```

## Q0306: move of a single qubit from a register

This error occurs when an individual qubit indexed from a `QReg` is moved out of the register, such as by passing it to `measure`. A `QReg` is tracked as a single linear value at compile time, so consuming one element would leave the register partially consumed while still in scope. Gates can still use individual indexed qubits without consuming them, whereas linear ownership applies to the register as a whole.

Erroneous code example:

```qx
void main() {
    QReg r = qreg(2);
    Bit b = measure(r[0]);  // error[Q0306]
    discard(r);
}
```

To fix it, keep gate calls such as `h(r[0])` on indexed elements, and call `measure(r)` on the entire `QReg` instead of on a single element. Measuring the whole register consumes `r` cleanly and returns a `BitReg`.

```qx
void main() {
    QReg r = qreg(2);
    h(r[0]);
    BitReg b = measure(r);
    println("{}", b);
}
```

## Q0307: reassignment of a quantum variable

This error occurs when a new value is assigned to an existing quantum variable after its declaration. Because `.qx` enforces linear qubit types at compile time, overwriting a quantum variable would lose track of the lifetime of the qubit previously bound to it. Disallowing reassignment ensures that each quantum variable tracks a single linear resource from declaration until consumption.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    q = qubit();  // error[Q0307]
    discard(q);
}
```

To fix it, consume the first `Qubit` with `discard(q)` and declare a new variable `p` for the second `qubit()` allocation instead of reassigning `q`. Then consume `p` with `discard(p)` before the end of the scope.

```qx
void main() {
    Qubit q = qubit();
    discard(q);
    Qubit p = qubit();
    discard(p);
}
```

## Q0308: disallowed operation inside an `if` on a `Bit`

An `if` statement whose condition is a `Bit` measurement result contains a disallowed operation such as `println`. Because a `.qx` program builds a single quantum circuit, the outcome of `measure` is not known until the circuit is simulated or executed. For that reason, only gates, `reset`, `for`, nested `if`, classical variable declarations, and calls to gate-only functions are allowed inside a measurement-conditioned `if`.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    if b {
        println("one");  // error[Q0308]
    }
}
```

To fix it, move the `println` call out of the `if` block so it runs after the measurement. In the corrected program, the measurement result `b` is printed directly instead of inside the `if`.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    println("{}", b);
}
```

## Q0309: outer variable assignment inside an `if` on a `Bit`

This error occurs when code inside an `if` whose condition is a `Bit` measurement result assigns to a variable declared outside the `if`. Because the condition comes from a measurement, its value depends on the shot when the quantum circuit runs, whereas an assignment to an outer classical variable would not depend on the shot. The compiler rejects the assignment so that classical variables outside the branch do not appear to change based on per-shot measurement outcomes.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    int n = 0;
    if b {
        n = 1;  // error[Q0309]
    }
    println("{}", n);
}
```

To fix it, avoid assigning to an outer variable inside the `if` and use the measurement result directly. The corrected program removes `n` and the `if` block, passing `b` straight to `println`.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    Bit b = measure(q);
    println("{}", b);
}
```

## Q0310: shadowing a qubit that is still alive

This error occurs when a declaration introduces a name that shadows an existing `Qubit` variable while that qubit is still alive. Because `Qubit` values follow linear ownership rules checked at compile time, every allocated qubit must eventually be consumed. Shadowing a live qubit would hide the original binding and make it impossible to consume.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    Qubit q = qubit();  // error[Q0310]
    discard(q);
}
```

To fix it, consume the first qubit before declaring another variable with the same name, or choose a different name. The corrected program calls `discard` on the first `q` before declaring the second `q` and discarding it as well.

```qx
void main() {
    Qubit q = qubit();
    discard(q);
    Qubit q = qubit();
    discard(q);
}
```

## Q0311: invalid expression used as a gate operand

This error occurs when an expression other than a `Qubit` variable or a register element such as `r[i]` is passed as a gate operand. Because `.qx` checks linear qubit ownership at compile time, a gate operates on a qubit that remains alive afterward and must later be consumed. Requiring a qubit variable or register element lets the compiler track the qubit's lifetime and prevents an unnamed temporary value from being lost.

Erroneous code example:

```qx
void main() {
    h(qubit());  // error[Q0311]
}
```

To fix it, bind the allocated qubit to a variable before passing it to the gate. The corrected program stores the result of `qubit()` in `q`, applies `h` to `q`, and then consumes `q` with `discard`.

```qx
void main() {
    Qubit q = qubit();
    h(q);
    discard(q);
}
```

## Q0312: newly allocated qubit or register never consumed

This error occurs when a newly allocated qubit or register is neither bound to a variable nor consumed. In `.qx`, qubits have linear types checked at compile time, so every allocated qubit must be explicitly consumed by `measure` or `discard`. Ignoring an allocation leaves a live qubit with no name and no way to consume it.

Erroneous code example:

```qx
void main() {
    qubit();  // error[Q0312]
}
```

To fix it, store the allocated qubit in a variable and consume it with `measure` or `discard`. The corrected program binds the result of `qubit()` to `q` and passes `q` to `discard`.

```qx
void main() {
    Qubit q = qubit();
    discard(q);
}
```

## Q0313: consuming a borrowed qubit parameter

This error occurs when a function attempts to measure, discard, move, or pass to a `consume` parameter a qubit parameter that was only borrowed. Without the `consume` modifier on the parameter type, the caller retains ownership of the qubit and remains responsible for measuring or discarding it after the call returns. Allowing the callee to consume a borrowed qubit would break linear ownership and could cause the same qubit to be consumed twice.

Erroneous code example:

```qx
Bit peek(Qubit q) {
    return measure(q);  // error[Q0313]
}

void main() {
    Qubit q = qubit();
    Bit b = peek(q);
    discard(q);
    println("{}", b);
}
```

To fix it, add `consume` before the parameter type (such as `consume Qubit q`) so the function takes ownership of the qubit and becomes responsible for measuring or discarding it. Then remove the caller's `discard` call, since the function now consumes the qubit.

```qx
Bit peek(consume Qubit q) {
    return measure(q);
}

void main() {
    Qubit q = qubit();
    Bit b = peek(q);
    println("{}", b);
}
```

## Q0314: disallowed function call inside a measurement `if`

This error occurs when an `if` statement whose condition is a measurement result calls a function that performs an operation disallowed inside a conditional block. A function called inside a measurement-conditioned `if` may only apply gates and `reset` to borrowed qubits. It cannot allocate, measure, discard, print, take a `consume` parameter, or return a qubit, because those operations cannot run conditionally on a measurement result.

Erroneous code example:

```qx
void reveal(Qubit q) {
    println("flipping");
    x(q);
}

void main() {
    Qubit coin = qubit();
    h(coin);
    Bit m = measure(coin);
    Qubit t = qubit();
    if m {
        reveal(t);  // error[Q0314]
    }
    discard(t);
}
```

To fix it, remove disallowed operations such as `println` from the function called inside the `if` block so it only applies gates or `reset` to borrowed qubits. If you still need those operations, call them before or after the `if` statement instead.

```qx
void flip(Qubit q) {
    x(q);
}

void main() {
    Qubit coin = qubit();
    h(coin);
    Bit m = measure(coin);
    Qubit t = qubit();
    if m {
        flip(t);
    }
    discard(t);
}
```

## Q0315: qubit consumed in short-circuit operand

Q0315 occurs at compile time when a qubit is consumed (by `measure`, `discard`, or a `consume` parameter) inside the right operand of `&&` or `||`. Because `&&` and `||` use short-circuit evaluation, the right operand is skipped at run time when the left operand already decides the result, leaving the qubit consumed on some runs and not others—like consuming a qubit in only one branch of an `if` (Q0304). Consuming a qubit in the left operand is fine because it always runs, and borrowing a qubit on the right (such as applying gates via a non-`consume` parameter) is also fine. After this error is reported, the qubit counts as consumed.

Erroneous code example:

```qx
int check(consume Qubit q) {
    Bit m = measure(q);
    return 1;
}

void main() {
    Qubit q = qubit();
    boolean ready = true;
    boolean go = ready || check(q) == 1;  // error[Q0315]
    println("{}", go);
}
```

To fix this error, consume the qubit in its own statement before the condition and use the stored result:

```qx
int check(consume Qubit q) {
    Bit m = measure(q);
    return 1;
}

void main() {
    Qubit q = qubit();
    boolean ready = true;
    int result = check(q);
    boolean go = ready || result == 1;
    println("{}", go);
}
```

## Q0401: integer arithmetic overflow on `int`

This error occurs when an arithmetic operation produces a result outside the representable range of `int`, which is a 64-bit signed integer (`i64`). Instead of silently wrapping around to an unintended value, `.qx` reports an overflow error when a calculation exceeds the type's bounds.

Erroneous code example:

```qx
void main() {
    int n = 9223372036854775807;
    int more = n + 1;  // error[Q0401]
    println("{}", more);
}
```

To fix it, switch the variables and literals to `double` so the computation does not overflow `int`, or keep the arithmetic within the `int` range.

```qx
void main() {
    double n = 9223372036854775807.0;
    double more = n + 1.0;
    println("{}", more);
}
```

## Q0402: division or remainder by zero

This error occurs when a division or remainder operation has a divisor of `0`. Because division and remainder by zero are mathematically undefined, the operation cannot produce a valid result and fails with an error.

Erroneous code example:

```qx
void main() {
    int zero = 0;
    println("{}", 10 / zero);  // error[Q0402]
}
```

To fix it, replace the `0` divisor with a non-zero value, or ensure the divisor is non-zero before performing division or remainder.

```qx
void main() {
    int two = 2;
    println("{}", 10 / two);
}
```

## Q0403: index out of bounds

This error occurs when an indexing operation uses an index outside the valid bounds of the value being indexed, such as a `QReg`. Indexing is zero-based, so valid indices run from `0` through one less than the length. Rejecting out-of-bounds indices prevents accessing a nonexistent element.

Erroneous code example:

```qx
void main() {
    QReg r = qreg(2);
    h(r[2]);  // error[Q0403]
    discard(r);
}
```

To fix it, allocate the `QReg` with enough elements to cover the target index—such as `qreg(3)` when indexing element `2`—or adjust the index so it stays within bounds.

```qx
void main() {
    QReg r = qreg(3);
    h(r[2]);
    discard(r);
}
```

## Q0405: function call depth limit exceeded

The runtime enforces a maximum function call depth of 200 and reports `Q0405` when nested calls go past that limit. This usually happens when a recursive function has no base case or never reaches one. Bounding the call depth stops runaway recursion from exhausting the stack and crashing execution.

Erroneous code example:

```qx
int count(int n) {
    return count(n + 1);  // error[Q0405]
}

void main() {
    println("{}", count(0));
}
```

To fix it, ensure every recursive function has a reachable base case that stops further calls before the depth reaches 200. In the corrected program, `count` returns `n` once `n >= 10`, terminating the recursion.

```qx
int count(int n) {
    if n >= 10 {
        return n;
    }
    return count(n + 1);
}

void main() {
    println("{}", count(0));
}
```

## Q0406: quantum circuit simulation failed

When `qxide run` executes a circuit, the simulator reports `Q0406` if simulation fails with a `SimError`, such as exceeding the memory limit. State-vector simulation requires memory that grows rapidly with the number of qubits, so simulating too many qubits exceeds the simulator's memory limit. Stopping the run before allocation prevents the process from exhausting system memory.

Erroneous code example:

```qx
void main() {  // error[Q0406]
    QReg r = qreg(30);
    t(r[0]);
    BitReg b = measure(r);
    println("{}", b);
}
```

To fix it, allocate fewer qubits so the simulation fits within the memory limit, as the corrected program does by reducing the `qreg` size from `30` to `10`. If you do not need to simulate the circuit, you can also run `qxide qasm` to export it as OpenQASM 3 without running the simulator.

```qx
void main() {
    QReg r = qreg(10);
    t(r[0]);
    BitReg b = measure(r);
    println("{}", b);
}
```

## Q0407: function exited without returning a value

The interpreter raises `Q0407` as an internal safety net if a function with a return type finishes executing without returning a value. The checker already rejects missing return paths with `Q0213` before the program runs, so normal execution should never reach this error. It exists so the interpreter fails explicitly rather than continuing with a missing value if an unchecked path slips through.

Because `Q0213` should always catch missing returns before execution, seeing `Q0407` indicates a bug in `qxide`. Report the issue along with the program that triggered it.

## Q0408: negative register size for `qreg`

Passing a negative integer to `qreg` triggers `Q0408` at runtime. A quantum register (`QReg`) represents a count of qubits to allocate in the circuit, so a negative size is meaningless. Because the argument can come from a runtime `int` value, the interpreter verifies that it is non-negative before creating the register.

Erroneous code example:

```qx
void main() {
    int n = -1;
    QReg r = qreg(n);  // error[Q0408]
    discard(r);
}
```

To fix it, pass a non-negative `int` to `qreg`. In the corrected program, `n` is changed from `-1` to `1` so the register allocation succeeds.

```qx
void main() {
    int n = 1;
    QReg r = qreg(n);
    discard(r);
}
```

## Q0409: total qubit allocations exceeded the limit

The runtime reports `Q0409` when the cumulative number of qubits allocated across a program exceeds 1048576 (`2^20`). This limit counts all allocations over the entire run, even when earlier qubits or registers are discarded. Capping total qubit allocations keeps circuit construction bounded and prevents runaway allocation.

Erroneous code example:

```qx
void main() {
    QReg r = qreg(2000000);  // error[Q0409]
    discard(r);
}
```

To fix it, keep the total number of qubits allocated across the program at or below 1048576. In the corrected program, the `qreg` size is reduced from `2000000` to `1000`, staying within the limit.

```qx
void main() {
    QReg r = qreg(1000);
    discard(r);
}
```

## Q0410: calling `as_int()` on a register exceeding 64 bits

This error occurs when `as_int()` is called on a `BitReg` with more than 64 bits. The `as_int()` method supports at most 64 bits because a wider register cannot fit into a 64-bit integer. Any call to `as_int()` on a register exceeding 64 bits is rejected with `Q0410`.

Erroneous code example:

```qx
void main() {
    QReg r = qreg(65);
    BitReg b = measure(r);
    println("{}", b.as_int());  // error[Q0410]
}
```

To fix it, keep the register at 64 bits or fewer, as in `qreg(64)`, so `as_int()` can convert the resulting `BitReg`. If you need a register wider than 64 bits, print it with `to_binary()` or as an array using a plain placeholder instead.

```qx
void main() {
    QReg r = qreg(64);
    BitReg b = measure(r);
    println("{}", b.as_int());
}
```

## Q0411: non-finite rotation angle

A runtime error is reported while building the circuit when `rx`, `ry`, or `rz` receives an angle that is NaN or infinite. In `.qx`, `double` arithmetic follows IEEE 754 like Java, so `0.0 / 0.0` evaluates to NaN and `1.0 / 0.0` evaluates to infinity instead of raising an error. A non-finite rotation angle would make every amplitude of the simulated state NaN and cannot be written as OpenQASM 3. The library methods `Circuit::rx`, `Circuit::ry`, and `Circuit::rz` panic for the same input.

Erroneous code example:

```qx
void main() {
    Qubit q = qubit();
    double steps = 0.0;
    rx(q, PI / steps);  // error[Q0411]
    Bit m = measure(q);
    println("{}", m);
}
```

To fix this error, make sure the divisor is not zero or otherwise ensure the angle passed to `rx`, `ry`, or `rz` is finite.

```qx
void main() {
    Qubit q = qubit();
    double steps = 4.0;
    rx(q, PI / steps);
    Bit m = measure(q);
    println("{}", m);
}
```

## Q0412: internal error between the checker and the interpreter

The interpreter raises Q0412 as an internal safety net when a program that passed static checks cannot be executed because the checker and the interpreter disagree—for example, when an operand that the checker considered alive turns out to already be consumed at run time. It exists so the interpreter fails explicitly on such mismatches rather than continuing in an invalid state, and replaces the misleading Q0311 error that older versions printed in this situation.

Because static checking should catch invalid programs before execution, seeing Q0412 always indicates a bug in qxide. Report the issue along with the program that triggered it.

## Q0501: unknown package in an import

The first segment of an `import` path names a package that is neither the current package nor a dependency declared in `[dependencies]` in `qxide.toml`. Package names with hyphens, such as `bell-utils`, are imported using underscores (`bell_utils`). Requiring every external package to be declared in `qxide.toml` keeps builds reproducible and ensures dependencies are explicitly resolved before compilation.

Erroneous code example:

```java
// qxide.toml of package `app` has no [dependencies] yet
// src/main.qx
import bell_utils.bell_pair;  // error[Q0501]
```

To fix it, run `qxide add bell-utils`, which adds `bell-utils` to `[dependencies]` in `qxide.toml` and installs it so `bell_utils` can be imported.

```java
// after `qxide add bell-utils`
// src/main.qx
import bell_utils.bell_pair;
```

## Q0502: module file not found

An `import` statement refers to a module whose corresponding `.qx` file under `src/` does not exist or cannot be read. Each module path maps to a file relative to the package's `src/` directory (for example, `app.gates` maps to `src/gates.qx` and the package root `app` maps to `src/lib.qx`), and the diagnostic note shows the path the compiler tried to open. Checking that every imported module resolves to an existing source file catches typos and missing files before type checking.

Erroneous code example:

```java
// src/main.qx of package `app`; there is no src/gates.qx
import app.gates.entangle;  // error[Q0502]
```

To fix it, create the missing `src/gates.qx` module file (or correct the module path in the `import` statement) and define the `public` function you want to import.

```java
// src/gates.qx
public void entangle(Qubit control, Qubit target) {
    cx(control, target);
}
```

## Q0503: function not found in module

An `import` statement names a function that does not exist in the target module, usually because of a typo or because the function has not been defined there. The module file itself was found, but none of its function declarations match the requested name. Reporting missing imports upfront prevents unresolved calls from failing later during compilation.

Erroneous code example:

```java
// src/main.qx
import bell_utils.bellpair;  // error[Q0503]: the function is `bell_pair`
```

To fix it, check the spelling of the function in the target module and update the `import` statement to use the exact name `bell_pair`.

```java
// src/main.qx
import bell_utils.bell_pair;
```

## Q0504: imported function is not `public`

An `import` statement names a function that exists in the target module, but the function is not marked `public`. In `.qx`, functions are private to their defining module by default so that helper functions remain an implementation detail that can change without breaking callers. Only functions explicitly declared `public` are part of a module's interface and may be imported by other modules.

Erroneous code example:

```java
// src/gates.qx
void entangle(Qubit control, Qubit target) {
    cx(control, target);
}

// src/main.qx
import app.gates.entangle;  // error[Q0504]
```

To fix it, mark `entangle` as `public` in `src/gates.qx` if it is intended to be imported by other modules.

```java
// src/gates.qx
public void entangle(Qubit control, Qubit target) {
    cx(control, target);
}
```

## Q0505: import cycle between modules

Two or more modules import each other, directly or transitively, forming an import cycle; the error message displays the chain of modules involved. The compiler rejects circular imports so that modules always load in a well-defined, acyclic order. Breaking cycles also keeps module boundaries clearer and easier to maintain.

Erroneous code example:

```java
// src/a.qx
import app.b.g;  // error[Q0505]: app.a -> app.b -> app.a
public void f(Qubit q) {
    g(q);
}

// src/b.qx
import app.a.f;
public void g(Qubit q) {
    h(q);
}
```

To fix it, remove the unneeded import from `src/b.qx`, or move any shared function into a separate module that both `app.a` and `app.b` can import.

```java
// src/a.qx
import app.b.g;
public void f(Qubit q) {
    g(q);
}

// src/b.qx (no longer imports app.a)
public void g(Qubit q) {
    h(q);
}
```

## Q0506: imported name conflicts with an existing function

An `import` statement introduces a function name that clashes either with a function defined in the same file or with a different function imported under the same name. Allowing two distinct definitions to claim the same name in a module's top-level scope would make it ambiguous which function a call refers to. Rejecting the conflict keeps every top-level function name in the file unambiguous.

Erroneous code example:

```java
// src/main.qx
import bell_utils.bell_pair;  // error[Q0506]

void bell_pair(Qubit a, Qubit b) {
    h(a);
}
```

To fix it, rename the local function so it does not clash with the imported name, or drop the `import` if you only need the local function.

```java
// src/main.qx
import bell_utils.bell_pair;

void my_pair(Qubit a, Qubit b) {
    h(a);
}
```

## Q0507: ambiguous call from wildcard imports

A function call matches `public` functions with the same name brought in by two or more wildcard imports (`import pkg.module.*;`). Overlapping wildcard imports do not trigger an error on their own; the diagnostic is reported at the call site only when the colliding name is actually used. Because the compiler cannot safely choose between the competing definitions, you must resolve the ambiguity explicitly.

Erroneous code example:

```java
// src/main.qx
import bell_utils.*;
import other.*;

void main() {
    Qubit q = qubit();
    flip(q);  // error[Q0507]: both modules have a public `flip`
    discard(q);
}
```

To fix it, import the intended function explicitly with a single-function import such as `import other.flip;`. Because single-function imports take priority over wildcard imports, the call to `flip` resolves unambiguously.

```java
// src/main.qx
import bell_utils.*;
import other.*;
import other.flip;

void main() {
    Qubit q = qubit();
    flip(q);
    discard(q);
}
```

## Q0508: `import` used outside a project

A lone `.qx` file contains an `import` statement, but there is no `qxide.toml` in its directory or any parent directory. Without a project manifest, the compiler has no package root or `[dependencies]` table to resolve package and module paths from. Restricting `import` to files inside a project ensures module resolution is always anchored to a known `qxide.toml`.

Erroneous code example:

```qx
import bell_utils.bell_pair;  // error[Q0508]

void main() {
    Qubit a = qubit();
    Qubit b = qubit();
    bell_pair(a, b);
    Bit ma = measure(a);
    Bit mb = measure(b);
    println("{} {}", ma, mb);
}
```

To fix it, either move the file into a project with a `qxide.toml` that depends on `bell-utils`, or, for a lone file, write the gates directly as below.

```qx
void main() {
    Qubit a = qubit();
    Qubit b = qubit();
    h(a);
    cx(a, b);
    Bit ma = measure(a);
    Bit mb = measure(b);
    println("{} {}", ma, mb);
}
```

## Q0509: import path is too short

An `import` statement contains only a bare package name instead of naming a function or wildcard. Every `import` in `.qx` must name a module and a function (`import pkg.function;` or `import pkg.module.function;`), or end with `.*` to import all `public` functions from a module. Requiring a function name or wildcard makes it explicit which symbols enter the file's scope.

Erroneous code example:

```java
// src/main.qx
import bell_utils;  // error[Q0509]
```

To fix it, name the specific function to import from the module (such as `bell_utils.bell_pair`), or end the path with `.*` to import all `public` functions from that module.

```java
// src/main.qx
import bell_utils.bell_pair;
```

## Q0510: `import` after a function declaration

An `import` statement appears after one or more function declarations in a `.qx` file. All `import` statements must come first in the file before any functions are declared. Keeping imports grouped at the top ensures a module's dependencies are immediately visible and parsed before any function bodies.

Erroneous code example:

```java
// src/main.qx
void main() {
}

import bell_utils.bell_pair;  // error[Q0510]
```

To fix it, move the `import` statement to the top of the file before all function declarations.

```java
// src/main.qx
import bell_utils.bell_pair;

void main() {
}
```

## Q0601: `qxide.toml` not found

A package command such as `qxide install`, `qxide add`, `qxide remove`, or `qxide publish` requires a project, but no `qxide.toml` exists in the current directory or any parent directory.

Run the command from inside a project directory, or create a new project with `qxide new <name>`, which writes `qxide.toml` and `src/main.qx`.

## Q0602: invalid `qxide.toml`

The project manifest is not valid TOML or violates the manifest rules. `[package]` must define `name` and `version`, where `version` is a semver version such as `1.2.3` and `name` is at most 64 characters of lowercase ASCII letters, digits, and single hyphens, starting with a letter and not ending with a hyphen. `qxide` and dependency requirements must be valid semver requirements, each dependency must be a requirement string or an inline table with `version`, `path`, or both, and all values must have the expected types.

Correct the key reported in the error message so `qxide.toml` is valid TOML and follows the manifest schema.

```toml
[package]
name = "bell-utils"
version = "0.1.0"
qxide = ">=0.7"

[dependencies]
other-pkg = "0.3"
local-pkg = { version = "0.1", path = "../local-pkg" }
```

## Q0603: invalid `qxide.lock`

The lock file cannot be used because it is not valid TOML, has an unsupported format version, contains an invalid package or dependency name, has an invalid `source`, has a checksum that is not a lowercase 64-digit SHA-256 hexadecimal string, or lists the same package more than once. `qxide.lock` is generated by `qxide install` and `qxide add` and should not be edited by hand.

Delete `qxide.lock` and run `qxide install` to regenerate it. Regenerating re-resolves dependencies to the newest matching versions, so review the new lock file before committing it.

## Q0604: package not found

A dependency could not be found: the package name does not exist in the registry index, a path dependency's folder has no readable `qxide.toml`, the package name in that `qxide.toml` differs from the dependency name, or two path dependencies specify different folders for the same package name.

Check the package spelling (for example with `qxide search <word>`), verify the configured registry (`--registry` or `QXIDE_REGISTRY`), and check the `path` entries in `qxide.toml`.

## Q0605: no version matches the requirement

The package exists in the registry, but no published version that is not yanked satisfies the requirement. A `note:` line lists the available versions.

Update the requirement in `qxide.toml` to match one of the available versions, or add the dependency again with an explicit requirement using `qxide add <package>@<requirement>`.

## Q0606: version conflict

Dependency resolution failed because only one version of each package can be included in a build and resolution does not backtrack. This happens when two packages require incompatible versions of the same package (with `note:` lines showing which package requires what), when a package depends on itself, or when resolution does not converge.

Align the requirements (for example by updating a dependency that pins an older version), and remove any self-dependency from `[dependencies]`.

## Q0607: checksum mismatch

The SHA-256 checksum of a downloaded or cached archive differs from the checksum recorded in `qxide.lock`, or the registry index now lists a different checksum for a locked version. Registry versions are immutable, so this indicates that the archive or index was changed or tampered with; `note:` lines show the expected and actual checksums, and a corrupted cached archive is downloaded again automatically.

Do not bypass this error; investigate the registry or local cache. Only if the change is known to be legitimate, delete `qxide.lock` and run `qxide install` to lock the new archive.

## Q0608: registry unavailable or download failed

The registry index or a package archive could not be downloaded due to a network or TLS error, an HTTP status other than 200 or 404, a `curl` transfer that did not finish successfully (such as a connection cut off midway, in which case nothing is cached), `curl` not being installed, or an unsupported URL. HTTPS downloads use the system `curl` executable, and only `https://` URLs, `file://` URLs, and local paths are allowed; plain `http://` URLs are refused.

Check your network connection and verify that `curl` is installed, check `--registry` or `QXIDE_REGISTRY`, or pass `--offline` to work from the local cache when everything needed is already cached.

## Q0609: invalid registry index

The registry's JSON files cannot be used because `config.json` or `search.json` failed to parse, or a package index file has no usable entry. Unusable index lines (malformed JSON, a different package name, a version with build metadata, or a repeated version, where the first line wins) are skipped, and this error is reported only when no usable line remains. This is a problem in the registry, not in your project.

Report the issue to the registry maintainers, or use another registry with `--registry` or `QXIDE_REGISTRY`.

## Q0610: unsafe or invalid package archive

A package archive was rejected before anything was unpacked because it contains an entry outside the package's root folder, an absolute or drive path, a `..` component, a symbolic or hard link, a non-portable file name (such as a trailing dot or space, a reserved Windows device name like `CON` or `NUL`, or a character disallowed on Windows), two paths differing only in letter case, too many files, an archive larger than 10 MiB, or a corrupt gzip or tar stream. Nothing is written to disk when an archive is rejected.

This indicates a broken or malicious package; report it to the registry maintainers and do not use that version.

## Q0611: dependency not installed

The project has dependencies, but either there is no `qxide.lock` file or a locked dependency is not unpacked in the local cache (`$QXIDE_HOME`, defaulting to `~/.qxide`). `qxide run`, `qxide check`, and `qxide qasm` never download packages.

Run `qxide install` in the project directory to install the dependencies.

## Q0612: `qxide.lock` is out of date

The lock file no longer matches `qxide.toml` or the dependency graph: a direct or transitive requirement changed, a path dependency's `qxide.toml` changed, a package comes from a different registry or source, or `qxide.lock` lists packages that nothing depends on. `qxide run`, `qxide check`, `qxide qasm`, and `qxide install --locked` report this error instead of changing the lock file.

Run `qxide install` without `--locked` to update `qxide.lock`, then commit the updated lock file.

## Q0613: `qxide` version requirement not met

The `qxide` requirement in the project's or a dependency's `qxide.toml` (for example `qxide = ">=0.7"`) is not satisfied by the running `qxide` version.

Upgrade `qxide`, or use a dependency version whose requirement your installed `qxide` satisfies. When writing a requirement, prefer the `>=0.7` style, because a caret requirement such as `^0.7` rejects `0.8`.

## Q0614: publish rejected

`qxide publish` refused the package, for example because `description`, `license`, or `qxide` is missing from `qxide.toml`, a dependency is path-only (with no `version`), `src/lib.qx` is missing, the package does not pass `qxide check`, the version already exists in the registry, the version includes build metadata (such as `1.0.0+build`), or a file name is not portable.

Correct the reported problem before publishing again. Published versions are immutable, so release changes under a new version number.

## Q0615: not available offline

Offline mode (`--offline` or `QXIDE_OFFLINE=1`) was requested, but an index file or archive needed by the command is not in the local cache under `$QXIDE_HOME`.

Run the command once without offline mode while connected so the required data is cached.

## Q0616: filesystem error

Reading or writing a file or folder failed (for example due to missing permissions or a full disk, with the path and operating system error shown in the message), or the cache folder could not be determined because neither `HOME` nor `USERPROFILE` is set.

Check the reported path and its permissions, or set `QXIDE_HOME` to a writable folder.

## Q0617: not a declared dependency

`qxide remove <package>` was given a package name that is not listed in `[dependencies]` of `qxide.toml`.

Check the spelling against `qxide.toml`. Package names use hyphens, whereas `.qx` imports use underscores (for example, `bell-utils` is imported as `bell_utils`).