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

Support further Python Interop for callables in Estimation and QIR Compilation #2091

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
68 changes: 67 additions & 1 deletion compiler/qsc/src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use qsc_circuit::{
operations::entry_expr_for_qubit_operation, Builder as CircuitBuilder, Circuit,
Config as CircuitConfig,
};
use qsc_codegen::qir::fir_to_qir;
use qsc_codegen::qir::{fir_to_qir, fir_to_qir_from_callable};
use qsc_data_structures::{
functors::FunctorApp,
language_features::LanguageFeatures,
Expand Down Expand Up @@ -639,6 +639,43 @@ impl Interpreter {
})
}

pub fn qirgen_from_callable(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a doc comment

&mut self,
callable: &Value,
args: Value,
) -> std::result::Result<String, Vec<Error>> {
if self.capabilities == TargetCapabilityFlags::all() {
return Err(vec![Error::UnsupportedRuntimeCapabilities]);
}

let Value::Global(store_item_id, _) = callable else {
panic!("value is not a global callable");
};

fir_to_qir_from_callable(
&self.fir_store,
self.capabilities,
None,
*store_item_id,
args,
)
.map_err(|e| {
let hir_package_id = match e.span() {
Some(span) => span.package,
None => map_fir_package_to_hir(self.package),
};
let source_package = self
.compiler
.package_store()
.get(hir_package_id)
.expect("package should exist in the package store");
vec![Error::PartialEvaluation(WithSource::from_map(
&source_package.sources,
e,
))]
})
}

/// Generates a circuit representation for the program.
///
/// `entry` can be the current entrypoint, an entry expression, or any operation
Expand Down Expand Up @@ -758,6 +795,35 @@ impl Interpreter {
)
}

/// Invokes the given callable with the given arguments on the given simulator with a new instance of the environment
/// but using the current compilation.
pub fn invoke_with_sim(
&mut self,
sim: &mut impl Backend<ResultType = impl Into<val::Result>>,
receiver: &mut impl Receiver,
callable: Value,
args: Value,
) -> InterpretResult {
qsc_eval::invoke(
self.package,
self.classical_seed,
&self.fir_store,
&mut Env::default(),
sim,
receiver,
callable,
args,
)
.map_err(|(error, call_stack)| {
eval_error(
self.compiler.package_store(),
&self.fir_store,
call_stack,
error,
)
})
}

fn compile_entry_expr(
&mut self,
expr: &str,
Expand Down
46 changes: 18 additions & 28 deletions compiler/qsc_codegen/src/qir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,48 @@ mod instruction_tests;
mod tests;

use qsc_data_structures::target::TargetCapabilityFlags;
use qsc_lowerer::map_hir_package_to_fir;
use qsc_partial_eval::{partially_evaluate, ProgramEntry};
use qsc_eval::val::Value;
use qsc_partial_eval::{partially_evaluate, partially_evaluate_call, ProgramEntry};
use qsc_rca::PackageStoreComputeProperties;
use qsc_rir::{
passes::check_and_transform,
rir::{self, ConditionCode},
utils::get_all_block_successors,
};

fn lower_store(package_store: &qsc_frontend::compile::PackageStore) -> qsc_fir::fir::PackageStore {
let mut fir_store = qsc_fir::fir::PackageStore::new();
for (id, unit) in package_store {
let package = qsc_lowerer::Lowerer::new().lower_package(&unit.package, &fir_store);
fir_store.insert(map_hir_package_to_fir(id), package);
}
fir_store
}

/// converts the given sources to QIR using the given language features.
pub fn hir_to_qir(
package_store: &qsc_frontend::compile::PackageStore,
capabilities: TargetCapabilityFlags,
compute_properties: Option<PackageStoreComputeProperties>,
entry: &ProgramEntry,
) -> Result<String, qsc_partial_eval::Error> {
let fir_store = lower_store(package_store);
fir_to_qir(&fir_store, capabilities, compute_properties, entry)
}

pub fn fir_to_qir(
fir_store: &qsc_fir::fir::PackageStore,
capabilities: TargetCapabilityFlags,
compute_properties: Option<PackageStoreComputeProperties>,
entry: &ProgramEntry,
) -> Result<String, qsc_partial_eval::Error> {
let mut program = get_rir_from_compilation(fir_store, compute_properties, entry, capabilities)?;
let compute_properties = compute_properties;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, this looks like it got left behind from an earlier experiment.

let compute_properties = compute_properties.unwrap_or_else(|| {
let analyzer = qsc_rca::Analyzer::init(fir_store);
analyzer.analyze_all()
});

let mut program = partially_evaluate(fir_store, &compute_properties, entry, capabilities)?;
check_and_transform(&mut program);
Ok(ToQir::<String>::to_qir(&program, &program))
}

fn get_rir_from_compilation(
pub fn fir_to_qir_from_callable(
fir_store: &qsc_fir::fir::PackageStore,
compute_properties: Option<PackageStoreComputeProperties>,
entry: &ProgramEntry,
capabilities: TargetCapabilityFlags,
) -> Result<rir::Program, qsc_partial_eval::Error> {
compute_properties: Option<PackageStoreComputeProperties>,
callable: qsc_fir::fir::StoreItemId,
args: Value,
) -> Result<String, qsc_partial_eval::Error> {
let compute_properties = compute_properties.unwrap_or_else(|| {
let analyzer = qsc_rca::Analyzer::init(fir_store);
analyzer.analyze_all()
});

partially_evaluate(fir_store, &compute_properties, entry, capabilities)
let mut program =
partially_evaluate_call(fir_store, &compute_properties, callable, args, capabilities)?;
check_and_transform(&mut program);
Ok(ToQir::<String>::to_qir(&program, &program))
}

/// A trait for converting a type into QIR of type `T`.
Expand Down
Loading
Loading