Skip to content

Commit 7f6e302

Browse files
authored
Migrate the standard library to the project system (#1912)
This PR migrates the standard library into a Q# project structure. Previously, we provided the modern standard library API via re-export, while the "true" stdlib was defined in the old explicit-namespace style. Now, that is inverted: the stdlib's code is housed in a project with implicit namespaces, and the legacy (`Microsoft.Quantum.____`) API is provided via re-exports. Why do this? Well, for one, the standard library should look like a regular library and use the latest features we have. Secondarily, auto-imports and other language dev tools now refer to these items by their `Std.__` name, not their `Microsoft.Quantum.__` name. See the video below for an example of this in completions: https://github.com/user-attachments/assets/fa96e5b7-0cca-4a44-9224-7742d50f8370 Go-to def rendering the new paths: https://github.com/user-attachments/assets/5fea33ec-9478-4c16-9e78-b8f8b1420ed0 Note that `Unstable.*` has not been migrated to the modern API, as we want to explore stabilizing state preparation (#1910) Core is also not included, but for different reasons: #1911. Coming in a Follow Up PR™️ (`alex/1911` if you want to preview the work there)
1 parent 82e2a62 commit 7f6e302

File tree

151 files changed

+7221
-7218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+7221
-7218
lines changed

compiler/qsc/benches/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub fn large_nested_iteration(c: &mut Criterion) {
175175
[("none".into(), "".into())],
176176
Some(
177177
indoc! {"{
178-
open Microsoft.Quantum.Arrays;
178+
import Std.Arrays.*;
179179
mutable arr = [[0, size = 100], size = 1000];
180180
for i in IndexRange(arr) {
181181
mutable inner = arr[i];

compiler/qsc/benches/large.qs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the MIT License.
33

44
namespace Large {
5-
open Microsoft.Quantum.Intrinsic;
6-
open Microsoft.Quantum.Measurement;
5+
import Std.Intrinsic.*;
6+
import Std.Measurement.*;
77

88
operation ThousandQubitsParityX(qs : Qubit[]) : Result {
99
mutable res = Zero;

compiler/qsc/src/codegen/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ mod base_profile {
6969
#[test]
7070
fn simple() {
7171
let source = "namespace Test {
72-
open Microsoft.Quantum.Math;
72+
import Std.Math.*;
7373
open QIR.Intrinsic;
7474
@EntryPoint()
7575
operation Main() : Result {
@@ -393,7 +393,7 @@ mod adaptive_profile {
393393
#[test]
394394
fn simple() {
395395
let source = "namespace Test {
396-
open Microsoft.Quantum.Math;
396+
import Std.Math.*;
397397
open QIR.Intrinsic;
398398
@EntryPoint()
399399
operation Main() : Result {
@@ -608,7 +608,7 @@ mod adaptive_ri_profile {
608608
#[test]
609609
fn simple() {
610610
let source = "namespace Test {
611-
open Microsoft.Quantum.Math;
611+
import Std.Math.*;
612612
open QIR.Intrinsic;
613613
@EntryPoint()
614614
operation Main() : Result {

compiler/qsc/src/interpret/circuit_tests.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn m_base_profile() {
158158
let mut interpreter = interpreter(
159159
r"
160160
namespace Test {
161-
open Microsoft.Quantum.Measurement;
161+
import Std.Measurement.*;
162162
@EntryPoint()
163163
operation Main() : Result[] {
164164
use q = Qubit();
@@ -187,7 +187,7 @@ fn m_unrestricted_profile() {
187187
let mut interpreter = interpreter(
188188
r"
189189
namespace Test {
190-
open Microsoft.Quantum.Measurement;
190+
import Std.Measurement.*;
191191
@EntryPoint()
192192
operation Main() : Result[] {
193193
use q = Qubit();
@@ -215,7 +215,7 @@ fn mresetz_unrestricted_profile() {
215215
let mut interpreter = interpreter(
216216
r"
217217
namespace Test {
218-
open Microsoft.Quantum.Measurement;
218+
import Std.Measurement.*;
219219
@EntryPoint()
220220
operation Main() : Result[] {
221221
use q = Qubit();
@@ -243,7 +243,7 @@ fn mresetz_base_profile() {
243243
let mut interpreter = interpreter(
244244
r"
245245
namespace Test {
246-
open Microsoft.Quantum.Measurement;
246+
import Std.Measurement.*;
247247
@EntryPoint()
248248
operation Main() : Result[] {
249249
use q = Qubit();
@@ -271,7 +271,7 @@ fn unrestricted_profile_result_comparison() {
271271
let mut interpreter = interpreter(
272272
r"
273273
namespace Test {
274-
open Microsoft.Quantum.Measurement;
274+
import Std.Measurement.*;
275275
@EntryPoint()
276276
operation Main() : Result[] {
277277
use q1 = Qubit();
@@ -439,7 +439,7 @@ fn custom_intrinsic_mixed_args() {
439439
let mut interpreter = interpreter(
440440
r"
441441
namespace Test {
442-
open Microsoft.Quantum.ResourceEstimation;
442+
import Std.ResourceEstimation.*;
443443
444444
@EntryPoint()
445445
operation Main() : Unit {
@@ -555,7 +555,7 @@ fn operation_with_qubit_arrays() {
555555
@EntryPoint()
556556
operation Main() : Result[] { [] }
557557
558-
open Microsoft.Quantum.Measurement;
558+
import Std.Measurement.*;
559559
operation Test(q1: Qubit[], q2: Qubit[][], q3: Qubit[][][], q: Qubit) : Result[] {
560560
for q in q1 {
561561
H(q);
@@ -925,7 +925,7 @@ mod debugger_stepping {
925925
let circs = generate_circuit_steps(
926926
r"
927927
namespace Test {
928-
open Microsoft.Quantum.Measurement;
928+
import Std.Measurement.*;
929929
@EntryPoint()
930930
operation Main() : Result[] {
931931
use q = Qubit();
@@ -974,7 +974,7 @@ mod debugger_stepping {
974974
let circs = generate_circuit_steps(
975975
r"
976976
namespace Test {
977-
open Microsoft.Quantum.Measurement;
977+
import Std.Measurement.*;
978978
@EntryPoint()
979979
operation Main() : Result[] {
980980
use q = Qubit();
@@ -1012,7 +1012,7 @@ mod debugger_stepping {
10121012
let circs = generate_circuit_steps(
10131013
r"
10141014
namespace Test {
1015-
open Microsoft.Quantum.Measurement;
1015+
import Std.Measurement.*;
10161016
@EntryPoint()
10171017
operation Main() : Result[] {
10181018
use q = Qubit();

compiler/qsc/src/interpret/tests.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ mod given_interpreter {
263263
#[test]
264264
fn open_namespace() {
265265
let mut interpreter = get_interpreter();
266-
let (result, output) = line(&mut interpreter, "open Microsoft.Quantum.Diagnostics;");
266+
let (result, output) = line(&mut interpreter, "import Std.Diagnostics.*;");
267267
is_only_value(&result, &output, &Value::unit());
268268
let (result, output) = line(&mut interpreter, "DumpMachine()");
269269
is_unit_with_output(&result, &output, "STATE:\n|0⟩: 1+0i");
@@ -328,7 +328,7 @@ mod given_interpreter {
328328
#[test]
329329
fn global_qubits() {
330330
let mut interpreter = get_interpreter();
331-
let (result, output) = line(&mut interpreter, "open Microsoft.Quantum.Diagnostics;");
331+
let (result, output) = line(&mut interpreter, "import Std.Diagnostics.*;");
332332
is_only_value(&result, &output, &Value::unit());
333333
let (result, output) = line(&mut interpreter, "DumpMachine()");
334334
is_unit_with_output(&result, &output, "STATE:\n|0⟩: 1+0i");
@@ -453,7 +453,7 @@ mod given_interpreter {
453453
is_only_value(&result, &output, &Value::unit());
454454
let (result, output) = line(&mut interpreter, "open Other;");
455455
is_only_value(&result, &output, &Value::unit());
456-
let (result, output) = line(&mut interpreter, "open Microsoft.Quantum.Diagnostics;");
456+
let (result, output) = line(&mut interpreter, "import Std.Diagnostics.*;");
457457
is_only_value(&result, &output, &Value::unit());
458458
let (result, output) = line(&mut interpreter, "DumpMachine();");
459459
is_only_error(
@@ -463,7 +463,7 @@ mod given_interpreter {
463463
name error: `DumpMachine` could refer to the item in `Other` or `Microsoft.Quantum.Diagnostics`
464464
ambiguous name [line_3] [DumpMachine]
465465
found in this namespace [line_1] [Other]
466-
and also in this namespace [line_2] [Microsoft.Quantum.Diagnostics]
466+
and also in this namespace [line_2] [Std.Diagnostics]
467467
type error: insufficient type information to infer type
468468
[line_3] [DumpMachine()]
469469
"#]],
@@ -479,7 +479,7 @@ mod given_interpreter {
479479
&output,
480480
&expect![[r#"
481481
runtime error: qubits in invocation are not unique
482-
[qsharp-library-source:intrinsic.qs] [(control, target)]
482+
[qsharp-library-source:Std/Intrinsic.qs] [(control, target)]
483483
"#]],
484484
);
485485
}
@@ -749,7 +749,7 @@ mod given_interpreter {
749749
&mut interpreter,
750750
indoc! {r#"
751751
namespace Test {
752-
open Microsoft.Quantum.Math;
752+
import Std.Math.*;
753753
open QIR.Intrinsic;
754754
@EntryPoint()
755755
operation Main() : Result {
@@ -1552,7 +1552,7 @@ mod given_interpreter {
15521552
fn debugger_execution_with_call_to_library_succeeds() {
15531553
let source = indoc! { r#"
15541554
namespace Test {
1555-
open Microsoft.Quantum.Math;
1555+
import Std.Math.*;
15561556
@EntryPoint()
15571557
operation Main() : Int {
15581558
Binom(31, 7)
@@ -1579,7 +1579,7 @@ mod given_interpreter {
15791579
fn debugger_execution_with_early_return_succeeds() {
15801580
let source = indoc! { r#"
15811581
namespace Test {
1582-
open Microsoft.Quantum.Arrays;
1582+
import Std.Arrays.*;
15831583
15841584
operation Max20(i : Int) : Int {
15851585
if (i > 20) {

compiler/qsc/src/location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mod tests {
7272

7373
expect![[r#"
7474
Location {
75-
source: "qsharp-library-source:arrays.qs",
75+
source: "qsharp-library-source:Std/Arrays.qs",
7676
range: Range {
7777
start: Position {
7878
line: 0,

compiler/qsc_codegen/src/qsharp.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,26 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
162162
ImportOrExportItem {
163163
ref path,
164164
ref is_glob,
165-
..
165+
ref alias,
166166
},
167167
) in decl.items.iter().enumerate()
168168
{
169169
let is_last = ix == decl.items.len() - 1;
170170
self.visit_path(path);
171+
171172
if *is_glob {
172173
self.write(".*");
173174
}
175+
176+
if let Some(ref alias) = alias {
177+
self.write(&format!(" as {}", alias.name));
178+
}
179+
174180
if !is_last {
175181
self.write(", ");
176182
};
177183
}
184+
178185
self.write(";");
179186
}
180187
}

compiler/qsc_codegen/src/qsharp/tests.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ fn open() {
4040
check(
4141
indoc! {r#"
4242
namespace Sample {
43-
open Microsoft.Quantum.Intrinsic as sics;
43+
import Std.Intrinsic as sics;
4444
45-
open Microsoft.Quantum.Diagnostics;
46-
open Microsoft.Quantum.Intrinsic as intrin;
45+
import Std.Diagnostics.*;
46+
import Std.Intrinsic as intrin;
4747
@EntryPoint()
4848
operation Entry() : Unit {
4949
}
5050
}"#},
5151
None,
5252
&expect![[r#"
5353
namespace Sample {
54-
open Microsoft.Quantum.Intrinsic as sics;
55-
open Microsoft.Quantum.Diagnostics;
56-
open Microsoft.Quantum.Intrinsic as intrin;
54+
import Std.Intrinsic as sics;
55+
import Std.Diagnostics.*;
56+
import Std.Intrinsic as intrin;
5757
@EntryPoint()
5858
operation Entry() : Unit {}
5959
}"#]],
@@ -472,7 +472,7 @@ fn lambda_fns() {
472472
check(
473473
indoc! {r#"
474474
namespace A {
475-
open Microsoft.Quantum.Arrays;
475+
import Std.Arrays.*;
476476
operation B() : Unit {
477477
let add = (x, y) -> x + y;
478478
let intArray = [1, 2, 3, 4, 5];
@@ -501,7 +501,7 @@ fn lambda_fns() {
501501
None,
502502
&expect![[r#"
503503
namespace A {
504-
open Microsoft.Quantum.Arrays;
504+
import Std.Arrays.*;
505505
operation B() : Unit {
506506
let add = (x, y) -> x + y;
507507
let intArray = [1, 2, 3, 4, 5];
@@ -534,7 +534,7 @@ fn ranges() {
534534
check(
535535
indoc! {r#"
536536
namespace A {
537-
open Microsoft.Quantum.Arrays;
537+
import Std.Arrays.*;
538538
operation B() : Unit {
539539
let range = 1..3;
540540
let range = 2..2..5;
@@ -558,7 +558,7 @@ fn ranges() {
558558
None,
559559
&expect![[r#"
560560
namespace A {
561-
open Microsoft.Quantum.Arrays;
561+
import Std.Arrays.*;
562562
operation B() : Unit {
563563
let range = 1..3;
564564
let range = 2..2..5;
@@ -620,7 +620,7 @@ fn field_access_and_string_interning() {
620620
check(
621621
indoc! {r#"
622622
namespace A {
623-
open Microsoft.Quantum.Math;
623+
import Std.Math.*;
624624
function ComplexAsString(x : Complex) : String {
625625
if x.Imag < 0.0 {
626626
$"{x.Real} - {AbsD(x.Imag)}i"
@@ -632,7 +632,7 @@ fn field_access_and_string_interning() {
632632
None,
633633
&expect![[r#"
634634
namespace A {
635-
open Microsoft.Quantum.Math;
635+
import Std.Math.*;
636636
function ComplexAsString(x : Complex) : String {
637637
if x.Imag < 0. {
638638
$"{x.Real} - {AbsD(x.Imag)}i"

compiler/qsc_data_structures/src/namespaces.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ mod tests;
77
use rustc_hash::{FxHashMap, FxHashSet};
88
use std::{cell::RefCell, collections::BTreeMap, fmt::Display, iter::Peekable, ops::Deref, rc::Rc};
99

10-
pub const PRELUDE: [[&str; 3]; 4] = [
11-
["Microsoft", "Quantum", "Canon"],
12-
["Microsoft", "Quantum", "Core"],
13-
["Microsoft", "Quantum", "Intrinsic"],
14-
["Microsoft", "Quantum", "Measurement"],
10+
pub const PRELUDE: &[&[&str]; 4] = &[
11+
&["Std", "Canon"],
12+
&["Microsoft", "Quantum", "Core"],
13+
&["Std", "Intrinsic"],
14+
&["Std", "Measurement"],
1515
];
1616

1717
/// An ID that corresponds to a namespace in the global scope.
@@ -279,7 +279,7 @@ impl Default for NamespaceTreeRoot {
279279
memo: RefCell::new(FxHashMap::default()),
280280
};
281281
// insert the prelude namespaces using the `NamespaceTreeRoot` API
282-
for ns in &PRELUDE {
282+
for ns in PRELUDE {
283283
let iter = ns.iter().map(|s| Rc::from(*s)).peekable();
284284
let _ = tree.insert_or_find_namespace(iter);
285285
}

0 commit comments

Comments
 (0)