mod openqasm;
mod profile;
mod qir;
use crate::VerifiedProgram;
use crate::frontend::ast::Span;
use std::fmt;
pub use openqasm::import_openqasm3;
pub const MAX_OPENQASM_BYTES: usize = 1 << 20;
pub(crate) const MAX_QUBITS: usize = 12;
pub(crate) const MAX_GATES: usize = 4096;
pub(crate) const MAX_TOKENS: usize = 65_536;
pub(crate) const MAX_OPERATIONS: usize = 65_536;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InteropErrorKind {
Parse,
Unsupported,
Limit,
InvalidIr,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InteropError {
pub kind: InteropErrorKind,
pub message: String,
pub span: Option<Span>,
pub operation: Option<usize>,
}
impl InteropError {
fn new(kind: InteropErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
span: None,
operation: None,
}
}
fn unsupported(message: impl Into<String>) -> Self {
Self::new(InteropErrorKind::Unsupported, message)
}
fn limit(message: impl Into<String>) -> Self {
Self::new(InteropErrorKind::Limit, message)
}
fn at(mut self, span: Span) -> Self {
self.span = Some(span);
self
}
}
impl fmt::Display for InteropError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.kind, self.message)?;
if let Some(span) = self.span {
write!(f, " at bytes {}..{}", span.start, span.end)?;
}
if let Some(index) = self.operation {
write!(f, " at raw operation {index}")?;
}
Ok(())
}
}
impl std::error::Error for InteropError {}
pub fn export_openqasm3(program: &VerifiedProgram) -> Result<String, InteropError> {
Ok(openqasm::write(&profile::extract(program)?))
}
pub fn export_qir_base(program: &VerifiedProgram) -> Result<String, InteropError> {
Ok(qir::write(&profile::extract(program)?))
}