Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consideration: ExternedCall trait #426

Open
erichulburd opened this issue Jan 2, 2025 · 0 comments
Open

Consideration: ExternedCall trait #426

erichulburd opened this issue Jan 2, 2025 · 0 comments

Comments

@erichulburd
Copy link
Collaborator

When implementing an extern function in rigetti/qcs-sdk-rust#515, it occurred to me that an ExternedCall trait is natural in supporting backend functions. That generalization probably belongs in this crate, rather than the respective backend's client SDK.

Implementation may look like the following.

//! This module contains a definition for the [`ExternedCall`] trait.
//! Implementations of this trait represent externed functions that
//! the a Quil backend supports.
//!
//! Trait implementations support semantically meaningful interfaces
//! that may be converted into Quil [`quil_rs::instruction::Call`]
//! instructions as well as their associated `PRAGMA EXTERN`
//! instructions.
use std::convert::TryInto;

use quil_rs::quil::ToQuilError;

/// A trait for supporting `PRAGMA EXTERN` and [`quil_rs::instruction::Call`] instructions.
pub trait ExternedCall: Sized + TryInto<quil_rs::instruction::Call> {
    /// An error that may occur when building the signature.
    type Error: From<ToQuilError>;

    /// The name of the externed function.
    const NAME: &'static str;

    /// Build the signature for the externed function. The translation service
    /// may use this function to check whether user submitted signatures match
    /// the expected signature.
    fn build_signature(
    ) -> Result<quil_rs::instruction::ExternSignature, <Self as ExternedCall>::Error>;

    /// Build a `PRAGMA EXTERN` instruction for the externed function.
    fn pragma_extern() -> Result<quil_rs::instruction::Pragma, <Self as ExternedCall>::Error> {
        use quil_rs::quil::Quil;

        Ok(quil_rs::instruction::Pragma::new(
            quil_rs::instruction::RESERVED_PRAGMA_EXTERN.to_string(),
            vec![quil_rs::instruction::PragmaArgument::Identifier(
                Self::NAME.to_string(),
            )],
            Some(
                Self::build_signature()?
                    .to_quil()
                    .map_err(<Self as ExternedCall>::Error::from)?,
            ),
        ))
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant