Skip to content

Commit 3b32013

Browse files
authored
Change dependency graph for qsc and qsc_qasm3 (microsoft#2134)
This change reorganizes the deps for qsc, qsc_qasm3, and the pip package. The `qsc` crate will now depend on `qsc_qasm3` and reexport the appropriate API surface. The pip package now consumes that exported API from `qsc`.
1 parent 515b087 commit 3b32013

15 files changed

+77
-64
lines changed

Cargo.lock

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/qsc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ qsc_passes = { path = "../qsc_passes" }
3131
qsc_parse = { path = "../qsc_parse" }
3232
qsc_partial_eval = { path = "../qsc_partial_eval" }
3333
qsc_project = { path = "../qsc_project", features = ["fs"] }
34+
qsc_qasm3 = { path = "../qsc_qasm3", features = ["fs"] }
3435
qsc_rca = { path = "../qsc_rca" }
3536
qsc_circuit = { path = "../qsc_circuit" }
3637
rustc-hash = { workspace = true }

compiler/qsc/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ pub mod parse {
7474
pub mod partial_eval {
7575
pub use qsc_partial_eval::Error;
7676
}
77+
78+
pub mod qasm3 {
79+
pub use qsc_qasm3::io::*;
80+
pub use qsc_qasm3::parse::*;
81+
pub use qsc_qasm3::*;
82+
}

compiler/qsc_qasm3/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ version.workspace = true
1111
bitflags = { workspace = true }
1212
num-bigint = { workspace = true }
1313
miette = { workspace = true }
14-
qsc = { path = "../qsc" }
14+
qsc_ast = { path = "../qsc_ast" }
15+
qsc_data_structures = { path = "../qsc_data_structures" }
16+
qsc_frontend = { path = "../qsc_frontend" }
17+
qsc_parse = { path = "../qsc_parse" }
1518
rustc-hash = { workspace = true }
1619
thiserror = { workspace = true }
1720
oq3_source_file = { workspace = true }
@@ -27,6 +30,7 @@ indoc = { workspace = true }
2730
miette = { workspace = true, features = ["fancy"] }
2831
# Self import adding fs feature so that we can test
2932
# loading qasm from file.
33+
qsc = { path = "../qsc" }
3034
qsc_qasm3 = { path = ".", features = ["fs"] }
3135

3236
[features]

compiler/qsc_qasm3/src/ast_builder.rs

+31-31
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ use std::rc::Rc;
55

66
use num_bigint::BigInt;
77

8-
use qsc::{
9-
ast::{
10-
self, Attr, Block, CallableBody, CallableDecl, CallableKind, Expr, ExprKind, Ident, Item,
11-
Lit, Mutability, NodeId, Pat, PatKind, Path, PathKind, QubitInit, QubitInitKind,
12-
QubitSource, Stmt, StmtKind, TopLevelNode, Ty, TyKind,
13-
},
14-
Span,
8+
use qsc_ast::ast::{
9+
self, Attr, Block, CallableBody, CallableDecl, CallableKind, Expr, ExprKind, Ident, Item, Lit,
10+
Mutability, NodeId, Pat, PatKind, Path, PathKind, QubitInit, QubitInitKind, QubitSource, Stmt,
11+
StmtKind, TopLevelNode, Ty, TyKind,
1512
};
13+
use qsc_data_structures::span::Span;
1614

1715
use crate::{
1816
runtime::RuntimeFunctions,
@@ -203,7 +201,7 @@ where
203201
}
204202
}
205203

206-
pub(crate) fn build_lit_result_expr(value: qsc::ast::Result, span: Span) -> Expr {
204+
pub(crate) fn build_lit_result_expr(value: qsc_ast::ast::Result, span: Span) -> Expr {
207205
Expr {
208206
id: NodeId::default(),
209207
span,
@@ -231,15 +229,15 @@ pub(crate) fn build_lit_result_array_expr_from_bitstring<S: AsRef<str>>(
231229
build_lit_result_array_expr(values, span)
232230
}
233231

234-
pub(crate) fn build_lit_result_array_expr(values: Vec<qsc::ast::Result>, span: Span) -> Expr {
232+
pub(crate) fn build_lit_result_array_expr(values: Vec<qsc_ast::ast::Result>, span: Span) -> Expr {
235233
let exprs: Vec<_> = values
236234
.into_iter()
237235
.map(|v| build_lit_result_expr(v, Span::default()))
238236
.collect();
239237
build_expr_array_expr(exprs, span)
240238
}
241239

242-
pub(crate) fn build_expr_array_expr(values: Vec<qsc::ast::Expr>, span: Span) -> Expr {
240+
pub(crate) fn build_expr_array_expr(values: Vec<qsc_ast::ast::Expr>, span: Span) -> Expr {
243241
let exprs: Vec<_> = values.into_iter().map(Box::new).collect();
244242
Expr {
245243
id: NodeId::default(),
@@ -314,7 +312,7 @@ pub(crate) fn build_binary_expr(
314312
}
315313
}
316314

317-
pub(crate) fn is_complex_binop_supported(op: qsc::ast::BinOp) -> bool {
315+
pub(crate) fn is_complex_binop_supported(op: qsc_ast::ast::BinOp) -> bool {
318316
matches!(
319317
op,
320318
ast::BinOp::Add | ast::BinOp::Sub | ast::BinOp::Mul | ast::BinOp::Div | ast::BinOp::Exp
@@ -1005,7 +1003,7 @@ pub(crate) fn build_top_level_ns_with_item<S: AsRef<str>>(
10051003
ns: S,
10061004
entry: ast::Item,
10071005
) -> TopLevelNode {
1008-
TopLevelNode::Namespace(qsc::ast::Namespace {
1006+
TopLevelNode::Namespace(qsc_ast::ast::Namespace {
10091007
id: NodeId::default(),
10101008
span: whole_span,
10111009
name: [Ident {
@@ -1031,10 +1029,10 @@ pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
10311029
// as an entry point. We will get a Q# compilation error if we
10321030
// attribute an operation with EntryPoint and it has input parameters.
10331031
if input_pats.is_empty() {
1034-
attrs.push(Box::new(qsc::ast::Attr {
1032+
attrs.push(Box::new(qsc_ast::ast::Attr {
10351033
id: NodeId::default(),
10361034
span: Span::default(),
1037-
name: Box::new(qsc::ast::Ident {
1035+
name: Box::new(qsc_ast::ast::Ident {
10381036
name: Rc::from("EntryPoint"),
10391037
..Default::default()
10401038
}),
@@ -1044,15 +1042,15 @@ pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
10441042
let input_pats = input_pats.into_iter().map(Box::new).collect::<Vec<_>>();
10451043
let input = match input_pats.len() {
10461044
0 => Box::new(Pat {
1047-
kind: Box::new(qsc::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
1045+
kind: Box::new(qsc_ast::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
10481046
..Default::default()
10491047
}),
10501048
1 => Box::new(Pat {
1051-
kind: Box::new(qsc::ast::PatKind::Paren(input_pats[0].clone())),
1049+
kind: Box::new(qsc_ast::ast::PatKind::Paren(input_pats[0].clone())),
10521050
..Default::default()
10531051
}),
1054-
_ => Box::new(qsc::ast::Pat {
1055-
kind: Box::new(qsc::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
1052+
_ => Box::new(qsc_ast::ast::Pat {
1053+
kind: Box::new(qsc_ast::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
10561054
..Default::default()
10571055
}),
10581056
};
@@ -1062,38 +1060,40 @@ pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
10621060
.map(Box::new)
10631061
.collect::<Vec<_>>()
10641062
.into_boxed_slice();
1065-
qsc::ast::Item {
1063+
qsc_ast::ast::Item {
10661064
id: NodeId::default(),
10671065
span: whole_span,
10681066
doc: "".into(),
10691067
attrs: attrs.into_boxed_slice(),
1070-
kind: Box::new(qsc::ast::ItemKind::Callable(Box::new(
1071-
qsc::ast::CallableDecl {
1068+
kind: Box::new(qsc_ast::ast::ItemKind::Callable(Box::new(
1069+
qsc_ast::ast::CallableDecl {
10721070
id: NodeId::default(),
10731071
span: whole_span,
1074-
kind: qsc::ast::CallableKind::Operation,
1075-
name: Box::new(qsc::ast::Ident {
1072+
kind: qsc_ast::ast::CallableKind::Operation,
1073+
name: Box::new(qsc_ast::ast::Ident {
10761074
name: Rc::from(name.as_ref()),
10771075
..Default::default()
10781076
}),
10791077
generics: Box::new([]),
10801078
input,
10811079
output: Box::new(output_ty),
10821080
functors: None,
1083-
body: Box::new(qsc::ast::CallableBody::Block(Box::new(qsc::ast::Block {
1084-
id: NodeId::default(),
1085-
span: whole_span,
1086-
stmts,
1087-
}))),
1081+
body: Box::new(qsc_ast::ast::CallableBody::Block(Box::new(
1082+
qsc_ast::ast::Block {
1083+
id: NodeId::default(),
1084+
span: whole_span,
1085+
stmts,
1086+
},
1087+
))),
10881088
},
10891089
))),
10901090
}
10911091
}
10921092

10931093
pub(crate) fn build_arg_pat(name: String, span: Span, ty: Ty) -> Pat {
1094-
qsc::ast::Pat {
1095-
kind: Box::new(qsc::ast::PatKind::Bind(
1096-
Box::new(qsc::ast::Ident {
1094+
qsc_ast::ast::Pat {
1095+
kind: Box::new(qsc_ast::ast::PatKind::Bind(
1096+
Box::new(qsc_ast::ast::Ident {
10971097
name: Rc::from(name),
10981098
span,
10991099
..Default::default()

compiler/qsc_qasm3/src/compile.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ use oq3_syntax::ast::{
5252
};
5353
use oq3_syntax::SyntaxNode;
5454
use oq3_syntax::{AstNode, HasTextName};
55-
use qsc::ast;
56-
use qsc::Span;
57-
use qsc::{error::WithSource, SourceMap};
55+
use qsc_ast::ast;
56+
use qsc_data_structures::span::Span;
57+
use qsc_frontend::{compile::SourceMap, error::WithSource};
5858

5959
use crate::{parse::QasmSource, QasmCompileUnit};
6060

compiler/qsc_qasm3/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ pub(crate) mod tests;
1919
use std::{fmt::Write, sync::Arc};
2020

2121
use miette::Diagnostic;
22-
use qsc::Span;
22+
use qsc_ast::ast::Package;
23+
use qsc_data_structures::span::Span;
24+
use qsc_frontend::{compile::SourceMap, error::WithSource};
2325
use thiserror::Error;
2426

2527
#[derive(Clone, Debug, Diagnostic, Eq, Error, PartialEq)]
@@ -458,8 +460,6 @@ pub enum ProgramType {
458460
Fragments,
459461
}
460462

461-
use qsc::{ast::Package, error::WithSource, SourceMap};
462-
463463
/// Represents the signature of an operation.
464464
/// This is used to create a function signature for the
465465
/// operation that is created from the QASM source code.

compiler/qsc_qasm3/src/oqasm_helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use oq3_semantics::types::Type;
55
use oq3_syntax::ast::{ArithOp, BinaryOp, Designator, Expr, Literal, LiteralKind};
6-
use qsc::Span;
6+
use qsc_data_structures::span::Span;
77

88
/// Extracts a Q# ```Span``` from the QASM3 syntax named element
99
pub(crate) fn span_for_named_item<T: oq3_syntax::ast::HasName>(value: &T) -> Span {

compiler/qsc_qasm3/src/parse.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::io::SourceResolver;
55
use crate::oqasm_helpers::text_range_to_span;
66
use oq3_syntax::SyntaxNode;
77
use oq3_syntax::{ast::Stmt, ParseOrErrors, SourceFile};
8-
use qsc::{error::WithSource, SourceMap};
8+
use qsc_frontend::compile::SourceMap;
9+
use qsc_frontend::error::WithSource;
910
use std::path::{Path, PathBuf};
1011
use std::sync::Arc;
1112

compiler/qsc_qasm3/src/runtime.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
use bitflags::bitflags;
55

6-
use qsc::{
7-
ast::{Stmt, TopLevelNode},
8-
LanguageFeatures,
9-
};
6+
use qsc_ast::ast::{Stmt, TopLevelNode};
7+
use qsc_data_structures::language_features::LanguageFeatures;
108

119
/// Runtime functions that are used in the generated AST.
1210
/// These functions are not part of the QASM3 standard, but are used to implement
@@ -200,7 +198,7 @@ pub(crate) fn get_result_array_as_int_be_decl() -> Stmt {
200198
}
201199

202200
fn parse_stmt(name: &str) -> Stmt {
203-
let (nodes, errors) = qsc::parse::top_level_nodes(name, LanguageFeatures::default());
201+
let (nodes, errors) = qsc_parse::top_level_nodes(name, LanguageFeatures::default());
204202
assert!(errors.is_empty(), "Failed to parse POW: {errors:?}");
205203
assert!(
206204
nodes.len() == 1,

compiler/qsc_qasm3/src/symbols.rs

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

44
use oq3_semantics::types::{IsConst, Type};
5-
use qsc::Span;
5+
use qsc_data_structures::span::Span;
66
use rustc_hash::FxHashMap;
77

88
/// We need a symbol table to keep track of the symbols in the program.

compiler/qsc_qasm3/src/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::fmt::{self, Display, Formatter};
55

66
use oq3_semantics::types::ArrayDims;
7-
use qsc::Span;
7+
use qsc_data_structures::span::Span;
88
use rustc_hash::FxHashMap;
99

1010
thread_local! {
@@ -73,7 +73,7 @@ pub(crate) fn get_qsharp_gate_name<S: AsRef<str>>(gate_name: S) -> Option<&'stat
7373
#[derive(Debug, Clone, PartialEq)]
7474
pub struct QasmTypedExpr {
7575
pub ty: oq3_semantics::types::Type,
76-
pub expr: qsc::ast::Expr,
76+
pub expr: qsc_ast::ast::Expr,
7777
}
7878

7979
#[derive(Clone, Debug, PartialEq)]

pip/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ noisy_simulator = { path = "../noisy_simulator" }
1414
num-bigint = { workspace = true }
1515
num-complex = { workspace = true }
1616
qsc = { path = "../compiler/qsc" }
17-
qsc_qasm3 = { path = "../compiler/qsc_qasm3", features = ["fs"]}
1817
resource_estimator = { path = "../resource_estimator" }
1918
miette = { workspace = true, features = ["fancy-no-syscall"] }
2019
rustc-hash = { workspace = true }

pip/src/interop.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ use pyo3::prelude::*;
1010
use pyo3::types::{PyDict, PyList};
1111
use qsc::interpret::output::Receiver;
1212
use qsc::interpret::{into_errors, Interpreter};
13+
use qsc::qasm3::io::SourceResolver;
14+
use qsc::qasm3::{
15+
qasm_to_program, CompilerConfig, OperationSignature, QasmCompileUnit, QubitSemantics,
16+
};
1317
use qsc::target::Profile;
1418
use qsc::{
1519
ast::Package, error::WithSource, interpret, project::FileSystem, LanguageFeatures,
1620
PackageStore, SourceMap,
1721
};
1822
use qsc::{Backend, PackageType, SparseSim};
19-
use qsc_qasm3::io::SourceResolver;
20-
use qsc_qasm3::{
21-
qasm_to_program, CompilerConfig, OperationSignature, QasmCompileUnit, QubitSemantics,
22-
};
2323

2424
use crate::fs::file_system;
2525
use crate::interpreter::{
@@ -261,7 +261,7 @@ pub(crate) fn compile_qasm<S: AsRef<str>, R: SourceResolver>(
261261
program_ty: ProgramType,
262262
output_semantics: OutputSemantics,
263263
) -> PyResult<QasmCompileUnit> {
264-
let parse_result = qsc_qasm3::parse::parse_source(
264+
let parse_result = qsc::qasm3::parse::parse_source(
265265
source,
266266
format!("{}.qasm", operation_name.as_ref()),
267267
resolver,
@@ -494,7 +494,7 @@ fn into_estimation_errors(errors: Vec<interpret::Error>) -> Vec<resource_estimat
494494
}
495495

496496
/// Formats a list of QASM3 errors into a single string.
497-
pub(crate) fn format_qasm_errors(errors: Vec<WithSource<qsc_qasm3::Error>>) -> String {
497+
pub(crate) fn format_qasm_errors(errors: Vec<WithSource<qsc::qasm3::Error>>) -> String {
498498
errors
499499
.into_iter()
500500
.map(|e| {

pip/src/interpreter.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ pub(crate) enum OutputSemantics {
184184
ResourceEstimation,
185185
}
186186

187-
impl From<OutputSemantics> for qsc_qasm3::OutputSemantics {
187+
impl From<OutputSemantics> for qsc::qasm3::OutputSemantics {
188188
fn from(output_semantics: OutputSemantics) -> Self {
189189
match output_semantics {
190-
OutputSemantics::Qiskit => qsc_qasm3::OutputSemantics::Qiskit,
191-
OutputSemantics::OpenQasm => qsc_qasm3::OutputSemantics::OpenQasm,
192-
OutputSemantics::ResourceEstimation => qsc_qasm3::OutputSemantics::ResourceEstimation,
190+
OutputSemantics::Qiskit => qsc::qasm3::OutputSemantics::Qiskit,
191+
OutputSemantics::OpenQasm => qsc::qasm3::OutputSemantics::OpenQasm,
192+
OutputSemantics::ResourceEstimation => qsc::qasm3::OutputSemantics::ResourceEstimation,
193193
}
194194
}
195195
}
@@ -215,12 +215,12 @@ pub enum ProgramType {
215215
Fragments,
216216
}
217217

218-
impl From<ProgramType> for qsc_qasm3::ProgramType {
218+
impl From<ProgramType> for qsc::qasm3::ProgramType {
219219
fn from(output_semantics: ProgramType) -> Self {
220220
match output_semantics {
221-
ProgramType::File => qsc_qasm3::ProgramType::File,
222-
ProgramType::Operation => qsc_qasm3::ProgramType::Operation,
223-
ProgramType::Fragments => qsc_qasm3::ProgramType::Fragments,
221+
ProgramType::File => qsc::qasm3::ProgramType::File,
222+
ProgramType::Operation => qsc::qasm3::ProgramType::Operation,
223+
ProgramType::Fragments => qsc::qasm3::ProgramType::Fragments,
224224
}
225225
}
226226
}

0 commit comments

Comments
 (0)