Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions source/compiler/qsc_openqasm_compiler/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,25 +1182,25 @@ pub(crate) fn build_unary_op_expr(op: ast::UnOp, expr: ast::Expr, prefix_span: S

pub(crate) fn map_qsharp_type_to_ast_ty(output_ty: &crate::types::Type, span: Span) -> Ty {
let mut ty = match output_ty {
crate::types::Type::Angle(_) => build_angle_ty_ident(),
crate::types::Type::Result(_) => build_path_ident_ty("Result"),
crate::types::Type::Angle => build_angle_ty_ident(),
crate::types::Type::Result => build_path_ident_ty("Result"),
crate::types::Type::Qubit => build_path_ident_ty("Qubit"),
crate::types::Type::BigInt(_) => build_path_ident_ty("BigInt"),
crate::types::Type::Int(_) => build_path_ident_ty("Int"),
crate::types::Type::Double(_) => build_path_ident_ty("Double"),
crate::types::Type::Complex(_) => build_complex_ty_ident(),
crate::types::Type::Bool(_) => build_path_ident_ty("Bool"),
crate::types::Type::ResultArray(dims, _) => build_array_type_name("Result", *dims),
crate::types::Type::BigInt => build_path_ident_ty("BigInt"),
crate::types::Type::Int => build_path_ident_ty("Int"),
crate::types::Type::Double => build_path_ident_ty("Double"),
crate::types::Type::Complex => build_complex_ty_ident(),
crate::types::Type::Bool => build_path_ident_ty("Bool"),
crate::types::Type::ResultArray(dims) => build_array_type_name("Result", *dims),
crate::types::Type::QubitArray(dims) => build_array_type_name("Qubit", *dims),
crate::types::Type::BigIntArray(dims, _) => build_array_type_name("BigInt", *dims),
crate::types::Type::IntArray(dims, _) => build_array_type_name("Int", *dims),
crate::types::Type::BigIntArray(dims) => build_array_type_name("BigInt", *dims),
crate::types::Type::IntArray(dims) => build_array_type_name("Int", *dims),
crate::types::Type::DoubleArray(dims) => build_array_type_name("Double", *dims),
crate::types::Type::BoolArray(dims, _) => build_array_type_name("Bool", *dims),
crate::types::Type::ComplexArray(dims, _) => {
crate::types::Type::BoolArray(dims) => build_array_type_name("Bool", *dims),
crate::types::Type::ComplexArray(dims) => {
let ty = build_complex_ty_ident();
wrap_array_ty_by_dims(*dims, ty)
}
crate::types::Type::AngleArray(dims, _) => {
crate::types::Type::AngleArray(dims) => {
let ty = build_angle_ty_ident();
wrap_array_ty_by_dims(*dims, ty)
}
Expand Down
42 changes: 20 additions & 22 deletions source/compiler/qsc_openqasm_compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl QasmCompiler {
if let Some(inputs) = &input {
for input in inputs {
let qsharp_ty = self.map_semantic_type_to_qsharp_type(&input.ty, input.ty_span);
if matches!(qsharp_ty, crate::types::Type::Angle(..)) {
if matches!(qsharp_ty, crate::types::Type::Angle) {
let message =
"use `float` types for passing input, using `angle` types".to_string();
let kind = CompilerErrorKind::NotSupported(message, input.span);
Expand Down Expand Up @@ -504,8 +504,8 @@ impl QasmCompiler {
.map(|symbol| {
let qsharp_ty =
self.map_semantic_type_to_qsharp_type(&symbol.ty, symbol.ty_span);
if matches!(qsharp_ty, crate::types::Type::Angle(..)) {
crate::types::Type::Double(symbol.ty.is_const())
if matches!(qsharp_ty, crate::types::Type::Angle) {
crate::types::Type::Double
} else {
qsharp_ty
}
Expand Down Expand Up @@ -751,7 +751,7 @@ impl QasmCompiler {
rhs_span,
rhs_span,
rhs_span,
&crate::types::Type::ResultArray(crate::types::ArrayDimensions::One, false),
&crate::types::Type::ResultArray(crate::types::ArrayDimensions::One),
temp_var_stmt_init_expr,
);
let temp_var_expr = build_path_ident_expr("bitarray", rhs_span, rhs_span);
Expand Down Expand Up @@ -2516,9 +2516,8 @@ impl QasmCompiler {
return crate::types::Type::Err;
}

let is_const = ty.is_const();
match ty {
Type::Bit(_) => crate::types::Type::Result(is_const),
Type::Bit(_) => crate::types::Type::Result,
Type::Qubit => crate::types::Type::Qubit,
Type::HardwareQubit => {
errs.push(unsupported_err("hardware qubits", span));
Expand All @@ -2530,18 +2529,18 @@ impl QasmCompiler {
Type::Int(width, _) | Type::UInt(width, _) => {
if let Some(width) = width {
if *width > 64 {
crate::types::Type::BigInt(is_const)
crate::types::Type::BigInt
} else {
crate::types::Type::Int(is_const)
crate::types::Type::Int
}
} else {
crate::types::Type::Int(is_const)
crate::types::Type::Int
}
}
Type::Float(_, _) => crate::types::Type::Double(is_const),
Type::Angle(_, _) => crate::types::Type::Angle(is_const),
Type::Complex(_, _) => crate::types::Type::Complex(is_const),
Type::Bool(_) => crate::types::Type::Bool(is_const),
Type::Float(_, _) => crate::types::Type::Double,
Type::Angle(_, _) => crate::types::Type::Angle,
Type::Complex(_, _) => crate::types::Type::Complex,
Type::Bool(_) => crate::types::Type::Bool,
Type::Duration(_) => {
errs.push(unsupported_err("duration type values", span));
crate::types::Type::Err
Expand All @@ -2551,7 +2550,7 @@ impl QasmCompiler {
crate::types::Type::Err
}
Type::BitArray(_, _) => {
crate::types::Type::ResultArray(crate::types::ArrayDimensions::One, is_const)
crate::types::Type::ResultArray(crate::types::ArrayDimensions::One)
}
Type::Array(array)
if !matches!(
Expand Down Expand Up @@ -2619,13 +2618,13 @@ impl QasmCompiler {
match base_ty {
qsc_openqasm_parser::semantic::types::ArrayBaseType::Duration => unreachable!(),
qsc_openqasm_parser::semantic::types::ArrayBaseType::Bool => {
crate::types::Type::BoolArray(dims, false)
crate::types::Type::BoolArray(dims)
}
qsc_openqasm_parser::semantic::types::ArrayBaseType::Angle(_) => {
crate::types::Type::AngleArray(dims, false)
crate::types::Type::AngleArray(dims)
}
qsc_openqasm_parser::semantic::types::ArrayBaseType::Complex(_) => {
crate::types::Type::ComplexArray(dims, false)
crate::types::Type::ComplexArray(dims)
}
qsc_openqasm_parser::semantic::types::ArrayBaseType::Float(_) => {
crate::types::Type::DoubleArray(dims)
Expand All @@ -2634,27 +2633,26 @@ impl QasmCompiler {
| qsc_openqasm_parser::semantic::types::ArrayBaseType::UInt(width) => {
if let Some(width) = width {
if *width > 64 {
crate::types::Type::BigIntArray(dims, false)
crate::types::Type::BigIntArray(dims)
} else {
crate::types::Type::IntArray(dims, false)
crate::types::Type::IntArray(dims)
}
} else {
crate::types::Type::IntArray(dims, false)
crate::types::Type::IntArray(dims)
}
}
}
}

/// Returns `true` if both `OpenQASM` types map to the same Q# type without errors.
/// Ignores const qualifiers, since constness does not affect the Q# representation.
fn maps_to_same_qsharp_type(
a: &qsc_openqasm_parser::semantic::types::Type,
b: &qsc_openqasm_parser::semantic::types::Type,
) -> bool {
let mut errs = Vec::new();
let ty_a = Self::semantic_type_for_qsharp_type(a, Span::default(), &mut errs);
let ty_b = Self::semantic_type_for_qsharp_type(b, Span::default(), &mut errs);
errs.is_empty() && ty_a.without_const() == ty_b.without_const()
errs.is_empty() && ty_a == ty_b
}

fn get_argument_validation_stmts(
Expand Down
74 changes: 26 additions & 48 deletions source/compiler/qsc_openqasm_compiler/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ impl Complex {

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum Type {
Angle(bool),
Bool(bool),
BigInt(bool),
Complex(bool),
Int(bool),
Double(bool),
Angle,
Bool,
BigInt,
Complex,
Int,
Double,
Qubit,
Result(bool),
Result,
Tuple(Vec<Type>),
Range,
BoolArray(ArrayDimensions, bool),
BigIntArray(ArrayDimensions, bool),
IntArray(ArrayDimensions, bool),
BoolArray(ArrayDimensions),
BigIntArray(ArrayDimensions),
IntArray(ArrayDimensions),
DoubleArray(ArrayDimensions),
ComplexArray(ArrayDimensions, bool),
AngleArray(ArrayDimensions, bool),
ComplexArray(ArrayDimensions),
AngleArray(ArrayDimensions),
QubitArray(ArrayDimensions),
ResultArray(ArrayDimensions, bool),
ResultArray(ArrayDimensions),
/// # cargs, # qargs
Gate(u32, u32),
/// kind, args, return ty
Expand All @@ -46,28 +46,6 @@ pub enum Type {
Err,
}

impl Type {
/// Returns the same type with the const qualifier set to `false`.
pub fn without_const(self) -> Self {
match self {
Self::Angle(_) => Self::Angle(false),
Self::Bool(_) => Self::Bool(false),
Self::BigInt(_) => Self::BigInt(false),
Self::Complex(_) => Self::Complex(false),
Self::Int(_) => Self::Int(false),
Self::Double(_) => Self::Double(false),
Self::Result(_) => Self::Result(false),
Self::BoolArray(d, _) => Self::BoolArray(d, false),
Self::BigIntArray(d, _) => Self::BigIntArray(d, false),
Self::IntArray(d, _) => Self::IntArray(d, false),
Self::ComplexArray(d, _) => Self::ComplexArray(d, false),
Self::AngleArray(d, _) => Self::AngleArray(d, false),
Self::ResultArray(d, _) => Self::ResultArray(d, false),
other => other,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallableKind {
/// A function.
Expand Down Expand Up @@ -155,15 +133,15 @@ impl From<qsc_openqasm_parser::semantic::types::Dims> for ArrayDimensions {
impl Display for Type {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Type::Angle(_) => write!(f, "Angle"),
Type::Bool(_) => write!(f, "bool"),
Type::BigInt(_) => write!(f, "BigInt"),
Type::Complex(_) => write!(f, "Complex"),
Type::Int(_) => write!(f, "Int"),
Type::Double(_) => write!(f, "Double"),
Type::Angle => write!(f, "Angle"),
Type::Bool => write!(f, "bool"),
Type::BigInt => write!(f, "BigInt"),
Type::Complex => write!(f, "Complex"),
Type::Int => write!(f, "Int"),
Type::Double => write!(f, "Double"),
Type::Qubit => write!(f, "Qubit"),
Type::Range => write!(f, "Range"),
Type::Result(_) => write!(f, "Result"),
Type::Result => write!(f, "Result"),
Type::Tuple(types) => {
write!(f, "(")?;
for (i, ty) in types.iter().enumerate() {
Expand All @@ -174,14 +152,14 @@ impl Display for Type {
}
write!(f, ")")
}
Type::BoolArray(dim, _) => write!(f, "bool{dim}"),
Type::BigIntArray(dim, _) => write!(f, "BigInt{dim}"),
Type::IntArray(dim, _) => write!(f, "Int{dim}"),
Type::BoolArray(dim) => write!(f, "bool{dim}"),
Type::BigIntArray(dim) => write!(f, "BigInt{dim}"),
Type::IntArray(dim) => write!(f, "Int{dim}"),
Type::DoubleArray(dim) => write!(f, "Double{dim}"),
Type::ComplexArray(dim, _) => write!(f, "Complex{dim}"),
Type::AngleArray(dim, _) => write!(f, "Angle{dim}"),
Type::ComplexArray(dim) => write!(f, "Complex{dim}"),
Type::AngleArray(dim) => write!(f, "Angle{dim}"),
Type::QubitArray(dim) => write!(f, "Qubit{dim}"),
Type::ResultArray(dim, _) => write!(f, "Result{dim}"),
Type::ResultArray(dim) => write!(f, "Result{dim}"),
Type::Callable(kind, args, return_type) => {
write!(f, "Callable({kind}, {args:?}, {return_type})")
}
Expand Down