Skip to content

Commit

Permalink
add feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dberthault committed Dec 2, 2024
1 parent 0acd8a7 commit 463425a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
5 changes: 3 additions & 2 deletions roqoqo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ futures = { version = "0.3", optional = true }
petgraph = { version = "0.6.2", optional = true }
bincode = { version = "1.3", optional = true }
struqture = { version = "~1.9", features = ["json_schema"] }
rayon = { version = "~1.10"}
rayon = { version = "~1.10" }

[dev-dependencies]
serde_test = "1.0"
Expand Down Expand Up @@ -79,4 +79,5 @@ json_schema = [
circuitdag = ["petgraph"]
unstable_chain_with_environment = []
unstable_analog_operations = []
unstable_operation_definition = []
unstable_operation_definition = []
unstable_parallel_run = ["rayon"]
1 change: 1 addition & 0 deletions roqoqo/src/quantum_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl QuantumProgram {
}
}

#[cfg(feature = "unstable_parallel_run")]
/// Runs the QuantumProgram and returns expectation values.
///
/// Runs the quantum programm for multiple given sets of parameters in parallel and returns expectation values.
Expand Down
65 changes: 33 additions & 32 deletions roqoqo/tests/integration/quantum_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ fn test_cheated() {
}

#[test]
fn test_parallel() {
// setting ub BR measurement
let bri = CheatedInput::new(2);
fn test_registers() {
// setting up BR measurement
let mut circs: Vec<Circuit> = Vec::new();
let mut circ1 = Circuit::new();
let mut circ1_subs = Circuit::new();
Expand All @@ -177,45 +176,34 @@ fn test_parallel() {
circ2 += operations::RotateZ::new(0, "theta2".into());
circ2_subs += operations::RotateZ::new(0, 1.0.into());
circs.push(circ1);
let br = Cheated {
let br = ClassicalRegister {
constant_circuit: Some(circ2),
circuits: circs.clone(),
input: bri,
};

let input_parameter_names = vec!["theta".to_string(), "theta2".to_string()];
let program = QuantumProgram::Cheated {
let program = QuantumProgram::ClassicalRegister {
measurement: br,
input_parameter_names,
};

let backend = TestBackend;

let mut now = Instant::now();
let result = program.run_parallel(backend, &[vec![0.0, 1.0]]);
let one_parameter_set_time = now.elapsed();
assert!(result.is_ok());

let number_parameter_set = 1000;
now = Instant::now();
let result = program.run_parallel(backend, &vec![vec![0.0, 1.0]; number_parameter_set]);
let multiple_parameter_set_time = now.elapsed();
let result_fail = program.run(backend, &[0.0, 1.0]);
assert!(result_fail.is_err());
let result = program.run_registers(backend, &[0.0, 1.0]);
assert!(result.is_ok());
assert!(
multiple_parameter_set_time
< one_parameter_set_time.mul_f32(0.9) * number_parameter_set as u32
);

let result_fail = program.run_parallel(backend, &[vec![0.0]]);
let result_fail = program.run_registers(backend, &[0.0]);
assert!(result_fail.is_err());

let result_fail = program.run_parallel(backend, &[vec![0.0, 1.0, 3.0]]);
let result_fail = program.run_registers(backend, &[0.0, 1.0, 3.0]);
assert!(result_fail.is_err());
}

#[cfg(feature = "unstable_parallel_run")]
#[test]
fn test_registers() {
// setting up BR measurement
fn test_parallel() {
// setting ub BR measurement
let bri = CheatedInput::new(2);
let mut circs: Vec<Circuit> = Vec::new();
let mut circ1 = Circuit::new();
let mut circ1_subs = Circuit::new();
Expand All @@ -226,26 +214,39 @@ fn test_registers() {
circ2 += operations::RotateZ::new(0, "theta2".into());
circ2_subs += operations::RotateZ::new(0, 1.0.into());
circs.push(circ1);
let br = ClassicalRegister {
let br = Cheated {
constant_circuit: Some(circ2),
circuits: circs.clone(),
input: bri,
};

let input_parameter_names = vec!["theta".to_string(), "theta2".to_string()];
let program = QuantumProgram::ClassicalRegister {
let program = QuantumProgram::Cheated {
measurement: br,
input_parameter_names,
};

let backend = TestBackend;

let result_fail = program.run(backend, &[0.0, 1.0]);
assert!(result_fail.is_err());
let result = program.run_registers(backend, &[0.0, 1.0]);
let mut now = Instant::now();
let result = program.run_parallel(backend, &[vec![0.0, 1.0]]);
let one_parameter_set_time = now.elapsed();
assert!(result.is_ok());
let result_fail = program.run_registers(backend, &[0.0]);

let number_parameter_set = 1000;
now = Instant::now();
let result = program.run_parallel(backend, &vec![vec![0.0, 1.0]; number_parameter_set]);
let multiple_parameter_set_time = now.elapsed();
assert!(result.is_ok());
assert!(
multiple_parameter_set_time
< one_parameter_set_time.mul_f32(0.9) * number_parameter_set as u32
);

let result_fail = program.run_parallel(backend, &[vec![0.0]]);
assert!(result_fail.is_err());
let result_fail = program.run_registers(backend, &[0.0, 1.0, 3.0]);

let result_fail = program.run_parallel(backend, &[vec![0.0, 1.0, 3.0]]);
assert!(result_fail.is_err());
}

Expand Down

0 comments on commit 463425a

Please sign in to comment.