Skip to content

Commit

Permalink
merge similar functions into one
Browse files Browse the repository at this point in the history
  • Loading branch information
orpuente-MS committed Oct 25, 2024
1 parent feb28ce commit c0b8b0c
Showing 1 changed file with 10 additions and 60 deletions.
70 changes: 10 additions & 60 deletions compiler/qsc_rca/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,9 @@ impl<'a> Analyzer<'a> {
CallableKind::Function => {
derive_intrinsic_function_application_generator_set(callable_context)
}
CallableKind::Operation => {
CallableKind::Operation | CallableKind::Measurement => {
derive_instrinsic_operation_application_generator_set(callable_context)
}
CallableKind::Measurement => {
derive_instrinsic_measurement_application_generator_set(callable_context)
}
};

// Insert the generator set in the entry corresponding to the body specialization of the callable.
Expand Down Expand Up @@ -2241,61 +2238,10 @@ fn array_param_application_from_runtime_features(
fn derive_instrinsic_operation_application_generator_set(
callable_context: &CallableContext,
) -> ApplicationGeneratorSet {
assert!(matches!(callable_context.kind, CallableKind::Operation));

// The value kind of intrinsic operations is inherently dynamic if their output is not `Unit` or `Qubit`.
let value_kind = if callable_context.output_type == Ty::UNIT
|| callable_context.output_type == Ty::Prim(Prim::Qubit)
{
ValueKind::Element(RuntimeKind::Static)
} else {
ValueKind::new_dynamic_from_type(&callable_context.output_type)
};

// The compute kind of intrinsic operations is always quantum.
let inherent_compute_kind = ComputeKind::Quantum(QuantumProperties {
runtime_features: RuntimeFeatureFlags::empty(),
value_kind,
});

// Determine the compute kind of all dynamic parameter applications.
let mut dynamic_param_applications =
Vec::<ParamApplication>::with_capacity(callable_context.input_params.len());
for param in &callable_context.input_params {
// For intrinsic operations, we assume any parameter can contribute to the output, so if any parameter is
// dynamic the output of the operation is dynamic.
// When a parameter is bound to a dynamic value, its type contributes to the runtime features used by the
// operation application.
let runtime_features = derive_runtime_features_for_value_kind_associated_to_type(
ValueKind::new_dynamic_from_type(&param.ty),
&param.ty,
);
let value_kind = ValueKind::new_dynamic_from_type(&callable_context.output_type);
let param_compute_kind = ComputeKind::Quantum(QuantumProperties {
runtime_features,
value_kind,
});

// Create a parameter application depending on the parameter type.
let param_application = match &param.ty {
Ty::Array(_) => {
array_param_application_from_runtime_features(runtime_features, value_kind)
}
_ => ParamApplication::Element(param_compute_kind),
};
dynamic_param_applications.push(param_application);
}

ApplicationGeneratorSet {
inherent: inherent_compute_kind,
dynamic_param_applications,
}
}

fn derive_instrinsic_measurement_application_generator_set(
callable_context: &CallableContext,
) -> ApplicationGeneratorSet {
assert!(matches!(callable_context.kind, CallableKind::Measurement));
assert!(matches!(
callable_context.kind,
CallableKind::Operation | CallableKind::Measurement
));

// The value kind of intrinsic operations is inherently dynamic if their output is not `Unit` or `Qubit`.
let value_kind = if callable_context.output_type == Ty::UNIT
Expand All @@ -2308,7 +2254,11 @@ fn derive_instrinsic_measurement_application_generator_set(

// The compute kind of intrinsic operations is always quantum.
let inherent_compute_kind = ComputeKind::Quantum(QuantumProperties {
runtime_features: RuntimeFeatureFlags::CustomMeasurement,
runtime_features: if matches!(callable_context.kind, CallableKind::Operation) {
RuntimeFeatureFlags::empty()
} else {
RuntimeFeatureFlags::CustomMeasurement
},
value_kind,
});

Expand Down

0 comments on commit c0b8b0c

Please sign in to comment.