Skip to content

Commit

Permalink
Change dependency graph for qsc and qsc_qasm3 (#2134)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
idavis authored Jan 31, 2025
1 parent 515b087 commit 3b32013
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 64 deletions.
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/qsc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ qsc_passes = { path = "../qsc_passes" }
qsc_parse = { path = "../qsc_parse" }
qsc_partial_eval = { path = "../qsc_partial_eval" }
qsc_project = { path = "../qsc_project", features = ["fs"] }
qsc_qasm3 = { path = "../qsc_qasm3", features = ["fs"] }
qsc_rca = { path = "../qsc_rca" }
qsc_circuit = { path = "../qsc_circuit" }
rustc-hash = { workspace = true }
Expand Down
6 changes: 6 additions & 0 deletions compiler/qsc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ pub mod parse {
pub mod partial_eval {
pub use qsc_partial_eval::Error;
}

pub mod qasm3 {
pub use qsc_qasm3::io::*;
pub use qsc_qasm3::parse::*;
pub use qsc_qasm3::*;
}
6 changes: 5 additions & 1 deletion compiler/qsc_qasm3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ version.workspace = true
bitflags = { workspace = true }
num-bigint = { workspace = true }
miette = { workspace = true }
qsc = { path = "../qsc" }
qsc_ast = { path = "../qsc_ast" }
qsc_data_structures = { path = "../qsc_data_structures" }
qsc_frontend = { path = "../qsc_frontend" }
qsc_parse = { path = "../qsc_parse" }
rustc-hash = { workspace = true }
thiserror = { workspace = true }
oq3_source_file = { workspace = true }
Expand All @@ -27,6 +30,7 @@ indoc = { workspace = true }
miette = { workspace = true, features = ["fancy"] }
# Self import adding fs feature so that we can test
# loading qasm from file.
qsc = { path = "../qsc" }
qsc_qasm3 = { path = ".", features = ["fs"] }

[features]
Expand Down
62 changes: 31 additions & 31 deletions compiler/qsc_qasm3/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ use std::rc::Rc;

use num_bigint::BigInt;

use qsc::{
ast::{
self, Attr, Block, CallableBody, CallableDecl, CallableKind, Expr, ExprKind, Ident, Item,
Lit, Mutability, NodeId, Pat, PatKind, Path, PathKind, QubitInit, QubitInitKind,
QubitSource, Stmt, StmtKind, TopLevelNode, Ty, TyKind,
},
Span,
use qsc_ast::ast::{
self, Attr, Block, CallableBody, CallableDecl, CallableKind, Expr, ExprKind, Ident, Item, Lit,
Mutability, NodeId, Pat, PatKind, Path, PathKind, QubitInit, QubitInitKind, QubitSource, Stmt,
StmtKind, TopLevelNode, Ty, TyKind,
};
use qsc_data_structures::span::Span;

use crate::{
runtime::RuntimeFunctions,
Expand Down Expand Up @@ -203,7 +201,7 @@ where
}
}

pub(crate) fn build_lit_result_expr(value: qsc::ast::Result, span: Span) -> Expr {
pub(crate) fn build_lit_result_expr(value: qsc_ast::ast::Result, span: Span) -> Expr {
Expr {
id: NodeId::default(),
span,
Expand Down Expand Up @@ -231,15 +229,15 @@ pub(crate) fn build_lit_result_array_expr_from_bitstring<S: AsRef<str>>(
build_lit_result_array_expr(values, span)
}

pub(crate) fn build_lit_result_array_expr(values: Vec<qsc::ast::Result>, span: Span) -> Expr {
pub(crate) fn build_lit_result_array_expr(values: Vec<qsc_ast::ast::Result>, span: Span) -> Expr {
let exprs: Vec<_> = values
.into_iter()
.map(|v| build_lit_result_expr(v, Span::default()))
.collect();
build_expr_array_expr(exprs, span)
}

pub(crate) fn build_expr_array_expr(values: Vec<qsc::ast::Expr>, span: Span) -> Expr {
pub(crate) fn build_expr_array_expr(values: Vec<qsc_ast::ast::Expr>, span: Span) -> Expr {
let exprs: Vec<_> = values.into_iter().map(Box::new).collect();
Expr {
id: NodeId::default(),
Expand Down Expand Up @@ -314,7 +312,7 @@ pub(crate) fn build_binary_expr(
}
}

pub(crate) fn is_complex_binop_supported(op: qsc::ast::BinOp) -> bool {
pub(crate) fn is_complex_binop_supported(op: qsc_ast::ast::BinOp) -> bool {
matches!(
op,
ast::BinOp::Add | ast::BinOp::Sub | ast::BinOp::Mul | ast::BinOp::Div | ast::BinOp::Exp
Expand Down Expand Up @@ -1005,7 +1003,7 @@ pub(crate) fn build_top_level_ns_with_item<S: AsRef<str>>(
ns: S,
entry: ast::Item,
) -> TopLevelNode {
TopLevelNode::Namespace(qsc::ast::Namespace {
TopLevelNode::Namespace(qsc_ast::ast::Namespace {
id: NodeId::default(),
span: whole_span,
name: [Ident {
Expand All @@ -1031,10 +1029,10 @@ pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
// as an entry point. We will get a Q# compilation error if we
// attribute an operation with EntryPoint and it has input parameters.
if input_pats.is_empty() {
attrs.push(Box::new(qsc::ast::Attr {
attrs.push(Box::new(qsc_ast::ast::Attr {
id: NodeId::default(),
span: Span::default(),
name: Box::new(qsc::ast::Ident {
name: Box::new(qsc_ast::ast::Ident {
name: Rc::from("EntryPoint"),
..Default::default()
}),
Expand All @@ -1044,15 +1042,15 @@ pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
let input_pats = input_pats.into_iter().map(Box::new).collect::<Vec<_>>();
let input = match input_pats.len() {
0 => Box::new(Pat {
kind: Box::new(qsc::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
kind: Box::new(qsc_ast::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
..Default::default()
}),
1 => Box::new(Pat {
kind: Box::new(qsc::ast::PatKind::Paren(input_pats[0].clone())),
kind: Box::new(qsc_ast::ast::PatKind::Paren(input_pats[0].clone())),
..Default::default()
}),
_ => Box::new(qsc::ast::Pat {
kind: Box::new(qsc::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
_ => Box::new(qsc_ast::ast::Pat {
kind: Box::new(qsc_ast::ast::PatKind::Tuple(input_pats.into_boxed_slice())),
..Default::default()
}),
};
Expand All @@ -1062,38 +1060,40 @@ pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
.map(Box::new)
.collect::<Vec<_>>()
.into_boxed_slice();
qsc::ast::Item {
qsc_ast::ast::Item {
id: NodeId::default(),
span: whole_span,
doc: "".into(),
attrs: attrs.into_boxed_slice(),
kind: Box::new(qsc::ast::ItemKind::Callable(Box::new(
qsc::ast::CallableDecl {
kind: Box::new(qsc_ast::ast::ItemKind::Callable(Box::new(
qsc_ast::ast::CallableDecl {
id: NodeId::default(),
span: whole_span,
kind: qsc::ast::CallableKind::Operation,
name: Box::new(qsc::ast::Ident {
kind: qsc_ast::ast::CallableKind::Operation,
name: Box::new(qsc_ast::ast::Ident {
name: Rc::from(name.as_ref()),
..Default::default()
}),
generics: Box::new([]),
input,
output: Box::new(output_ty),
functors: None,
body: Box::new(qsc::ast::CallableBody::Block(Box::new(qsc::ast::Block {
id: NodeId::default(),
span: whole_span,
stmts,
}))),
body: Box::new(qsc_ast::ast::CallableBody::Block(Box::new(
qsc_ast::ast::Block {
id: NodeId::default(),
span: whole_span,
stmts,
},
))),
},
))),
}
}

pub(crate) fn build_arg_pat(name: String, span: Span, ty: Ty) -> Pat {
qsc::ast::Pat {
kind: Box::new(qsc::ast::PatKind::Bind(
Box::new(qsc::ast::Ident {
qsc_ast::ast::Pat {
kind: Box::new(qsc_ast::ast::PatKind::Bind(
Box::new(qsc_ast::ast::Ident {
name: Rc::from(name),
span,
..Default::default()
Expand Down
6 changes: 3 additions & 3 deletions compiler/qsc_qasm3/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ use oq3_syntax::ast::{
};
use oq3_syntax::SyntaxNode;
use oq3_syntax::{AstNode, HasTextName};
use qsc::ast;
use qsc::Span;
use qsc::{error::WithSource, SourceMap};
use qsc_ast::ast;
use qsc_data_structures::span::Span;
use qsc_frontend::{compile::SourceMap, error::WithSource};

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

Expand Down
6 changes: 3 additions & 3 deletions compiler/qsc_qasm3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub(crate) mod tests;
use std::{fmt::Write, sync::Arc};

use miette::Diagnostic;
use qsc::Span;
use qsc_ast::ast::Package;
use qsc_data_structures::span::Span;
use qsc_frontend::{compile::SourceMap, error::WithSource};
use thiserror::Error;

#[derive(Clone, Debug, Diagnostic, Eq, Error, PartialEq)]
Expand Down Expand Up @@ -458,8 +460,6 @@ pub enum ProgramType {
Fragments,
}

use qsc::{ast::Package, error::WithSource, SourceMap};

/// Represents the signature of an operation.
/// This is used to create a function signature for the
/// operation that is created from the QASM source code.
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/oqasm_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use oq3_semantics::types::Type;
use oq3_syntax::ast::{ArithOp, BinaryOp, Designator, Expr, Literal, LiteralKind};
use qsc::Span;
use qsc_data_structures::span::Span;

/// Extracts a Q# ```Span``` from the QASM3 syntax named element
pub(crate) fn span_for_named_item<T: oq3_syntax::ast::HasName>(value: &T) -> Span {
Expand Down
3 changes: 2 additions & 1 deletion compiler/qsc_qasm3/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::io::SourceResolver;
use crate::oqasm_helpers::text_range_to_span;
use oq3_syntax::SyntaxNode;
use oq3_syntax::{ast::Stmt, ParseOrErrors, SourceFile};
use qsc::{error::WithSource, SourceMap};
use qsc_frontend::compile::SourceMap;
use qsc_frontend::error::WithSource;
use std::path::{Path, PathBuf};
use std::sync::Arc;

Expand Down
8 changes: 3 additions & 5 deletions compiler/qsc_qasm3/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

use bitflags::bitflags;

use qsc::{
ast::{Stmt, TopLevelNode},
LanguageFeatures,
};
use qsc_ast::ast::{Stmt, TopLevelNode};
use qsc_data_structures::language_features::LanguageFeatures;

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

fn parse_stmt(name: &str) -> Stmt {
let (nodes, errors) = qsc::parse::top_level_nodes(name, LanguageFeatures::default());
let (nodes, errors) = qsc_parse::top_level_nodes(name, LanguageFeatures::default());
assert!(errors.is_empty(), "Failed to parse POW: {errors:?}");
assert!(
nodes.len() == 1,
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

use oq3_semantics::types::{IsConst, Type};
use qsc::Span;
use qsc_data_structures::span::Span;
use rustc_hash::FxHashMap;

/// We need a symbol table to keep track of the symbols in the program.
Expand Down
4 changes: 2 additions & 2 deletions compiler/qsc_qasm3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::fmt::{self, Display, Formatter};

use oq3_semantics::types::ArrayDims;
use qsc::Span;
use qsc_data_structures::span::Span;
use rustc_hash::FxHashMap;

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

#[derive(Clone, Debug, PartialEq)]
Expand Down
1 change: 0 additions & 1 deletion pip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ noisy_simulator = { path = "../noisy_simulator" }
num-bigint = { workspace = true }
num-complex = { workspace = true }
qsc = { path = "../compiler/qsc" }
qsc_qasm3 = { path = "../compiler/qsc_qasm3", features = ["fs"]}
resource_estimator = { path = "../resource_estimator" }
miette = { workspace = true, features = ["fancy-no-syscall"] }
rustc-hash = { workspace = true }
Expand Down
12 changes: 6 additions & 6 deletions pip/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};
use qsc::interpret::output::Receiver;
use qsc::interpret::{into_errors, Interpreter};
use qsc::qasm3::io::SourceResolver;
use qsc::qasm3::{
qasm_to_program, CompilerConfig, OperationSignature, QasmCompileUnit, QubitSemantics,
};
use qsc::target::Profile;
use qsc::{
ast::Package, error::WithSource, interpret, project::FileSystem, LanguageFeatures,
PackageStore, SourceMap,
};
use qsc::{Backend, PackageType, SparseSim};
use qsc_qasm3::io::SourceResolver;
use qsc_qasm3::{
qasm_to_program, CompilerConfig, OperationSignature, QasmCompileUnit, QubitSemantics,
};

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

/// Formats a list of QASM3 errors into a single string.
pub(crate) fn format_qasm_errors(errors: Vec<WithSource<qsc_qasm3::Error>>) -> String {
pub(crate) fn format_qasm_errors(errors: Vec<WithSource<qsc::qasm3::Error>>) -> String {
errors
.into_iter()
.map(|e| {
Expand Down
16 changes: 8 additions & 8 deletions pip/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ pub(crate) enum OutputSemantics {
ResourceEstimation,
}

impl From<OutputSemantics> for qsc_qasm3::OutputSemantics {
impl From<OutputSemantics> for qsc::qasm3::OutputSemantics {
fn from(output_semantics: OutputSemantics) -> Self {
match output_semantics {
OutputSemantics::Qiskit => qsc_qasm3::OutputSemantics::Qiskit,
OutputSemantics::OpenQasm => qsc_qasm3::OutputSemantics::OpenQasm,
OutputSemantics::ResourceEstimation => qsc_qasm3::OutputSemantics::ResourceEstimation,
OutputSemantics::Qiskit => qsc::qasm3::OutputSemantics::Qiskit,
OutputSemantics::OpenQasm => qsc::qasm3::OutputSemantics::OpenQasm,
OutputSemantics::ResourceEstimation => qsc::qasm3::OutputSemantics::ResourceEstimation,
}
}
}
Expand All @@ -215,12 +215,12 @@ pub enum ProgramType {
Fragments,
}

impl From<ProgramType> for qsc_qasm3::ProgramType {
impl From<ProgramType> for qsc::qasm3::ProgramType {
fn from(output_semantics: ProgramType) -> Self {
match output_semantics {
ProgramType::File => qsc_qasm3::ProgramType::File,
ProgramType::Operation => qsc_qasm3::ProgramType::Operation,
ProgramType::Fragments => qsc_qasm3::ProgramType::Fragments,
ProgramType::File => qsc::qasm3::ProgramType::File,
ProgramType::Operation => qsc::qasm3::ProgramType::Operation,
ProgramType::Fragments => qsc::qasm3::ProgramType::Fragments,
}
}
}
Expand Down

0 comments on commit 3b32013

Please sign in to comment.