-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix RCA panic when compiling lambda (#2102)
Compiling a lambda that has more than one parameter can trigger a panic in RCA due to processing the fixed arguments for captures. This adds new tests for the lambda cases and confirms the dynamic runtime features for paramters and captures are detected as expected.
- Loading branch information
Showing
3 changed files
with
236 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use super::{check_last_statement_compute_properties, CompilationContext}; | ||
use expect_test::expect; | ||
|
||
#[test] | ||
fn check_rca_for_classical_lambda_one_parameter() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let myOp = a -> {}; | ||
myOp(1)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Classical | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_classical_lambda_two_parameters() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let myOp = (a, b) -> {}; | ||
myOp(1, 2)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Classical | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_classical_lambda_one_parameter_one_capture() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let x = 0.0; | ||
let myOp = a -> {x}; | ||
myOp(1)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Classical | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_classical_lambda_one_parameter_two_captures() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let x = 0.0; | ||
let y = 0.0; | ||
let myOp = a -> {x+y}; | ||
myOp(1)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Classical | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_classical_lambda_two_parameters_one_capture() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let x = 0.0; | ||
let myOp = (a, b) -> {x}; | ||
myOp(1, 2)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Classical | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_classical_lambda_two_parameters_two_captures() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let x = 0.0; | ||
let y = 0.0; | ||
let myOp = (a, b) -> {x+y}; | ||
myOp(1, 2)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Classical | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_dynamic_lambda_two_classical_parameters_one_dynamic_capture() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let x = { | ||
use q = Qubit(); | ||
if MResetZ(q) == One { | ||
1.0 | ||
} else { | ||
0.0 | ||
} | ||
}; | ||
let myOp = (a, b) -> {x}; | ||
myOp(1, 2)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Quantum: QuantumProperties: | ||
runtime_features: RuntimeFeatureFlags(UseOfDynamicDouble) | ||
value_kind: Element(Dynamic) | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_dynamic_lambda_two_dynamic_parameters_one_classical_capture() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
let x = 0.0; | ||
let myOp = (a, b) -> {a + x}; | ||
let a = { | ||
use q = Qubit(); | ||
if MResetZ(q) == One { | ||
1.0 | ||
} else { | ||
0.0 | ||
} | ||
}; | ||
myOp(a, 2)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Quantum: QuantumProperties: | ||
runtime_features: RuntimeFeatureFlags(UseOfDynamicBool | UseOfDynamicDouble) | ||
value_kind: Element(Dynamic) | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_operation_lambda_two_parameters() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
use (q0, q1) = (Qubit(), Qubit()); | ||
let myOp = (a, b) => MResetEachZ([a, b]); | ||
myOp(q0, q1)"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Quantum: QuantumProperties: | ||
runtime_features: RuntimeFeatureFlags(0x0) | ||
value_kind: Array(Content: Dynamic, Size: Static) | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn check_rca_for_operation_lambda_two_parameters_with_controls() { | ||
let mut compilation_context = CompilationContext::default(); | ||
compilation_context.update( | ||
r#" | ||
use (q0, q1) = (Qubit(), Qubit()); | ||
use (qs0, qs1) = (Qubit[2], Qubit[2]); | ||
let myOp = (a, b) => CNOT(a, b); | ||
Controlled Controlled myOp(qs0, (qs1, (q0, q1)))"#, | ||
); | ||
let package_store_compute_properties = compilation_context.get_compute_properties(); | ||
check_last_statement_compute_properties( | ||
package_store_compute_properties, | ||
&expect![[r#" | ||
ApplicationsGeneratorSet: | ||
inherent: Quantum: QuantumProperties: | ||
runtime_features: RuntimeFeatureFlags(UseOfDynamicBool) | ||
value_kind: Element(Static) | ||
dynamic_param_applications: <empty>"#]], | ||
); | ||
} |