Skip to content

Commit 451adda

Browse files
committed
Refactor test to use builder
1 parent 4e75296 commit 451adda

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

crates/pecos-engines/src/byte_message/message.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,17 @@ impl ByteMessage {
347347
builder.add_measurements(&[qubit], &[result_id]);
348348
}
349349
}
350+
Some(&"P") => {
351+
if parts.len() >= 2 {
352+
let qubit = parts[1].parse::<usize>().map_err(|_| {
353+
QueueError::OperationError(format!(
354+
"Invalid qubit in P command: {}",
355+
parts[1]
356+
))
357+
})?;
358+
builder.add_prep(&[qubit]);
359+
}
360+
}
350361
_ => {
351362
return Err(QueueError::OperationError(format!(
352363
"Unknown command type: {}",
@@ -1011,4 +1022,27 @@ mod tests {
10111022
.build();
10121023
assert!(!non_empty_message.is_empty().unwrap());
10131024
}
1025+
1026+
#[test]
1027+
fn test_parse_command_to_builder() {
1028+
// Test various commands including the new "P" command
1029+
let commands = [
1030+
"H 0", "CX 0 1", "RZ 0.5 2", "P 3", // Test the new P command
1031+
"M 4 0",
1032+
];
1033+
1034+
let message = ByteMessage::create_from_commands(&commands).unwrap();
1035+
1036+
// Parse the quantum operations from the message
1037+
let operations = message.parse_quantum_operations().unwrap();
1038+
1039+
// We should have 5 operations
1040+
assert_eq!(operations.len(), 5);
1041+
1042+
// Check the P command was correctly parsed
1043+
assert_eq!(operations[3].gate_type, GateType::Prep);
1044+
assert_eq!(operations[3].qubits, vec![3]);
1045+
assert!(operations[3].params.is_empty());
1046+
assert_eq!(operations[3].result_id, None);
1047+
}
10141048
}

crates/pecos-engines/tests/noise_determinism.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ fn test_prep_determinism() {
9595
// Apply noise to model1
9696
reset_model_with_seed(&mut model1, seed).unwrap();
9797

98-
// Create a message with multiple prep and H gates
99-
let msg =
100-
ByteMessage::create_from_commands(&["H 0", "H 0", "H 0", "H 0", "H 0", "H 0"]).unwrap();
98+
// Create a message with multiple prep gates
99+
let mut builder = ByteMessage::quantum_operations_builder();
100+
for _ in 0..6 {
101+
builder.add_prep(&[0]);
102+
}
103+
let msg = builder.build();
101104

102105
// Apply noise to the message
103106
let noisy1 = apply_noise(&mut model1, &msg);
@@ -141,16 +144,16 @@ fn test_single_qubit_gate_determinism() {
141144
reset_model_with_seed(&mut model1, seed).unwrap();
142145

143146
// Create a message with multiple single-qubit gates
144-
let mut commands = Vec::new();
147+
let mut builder = ByteMessage::quantum_operations_builder();
145148
for _ in 0..10 {
146149
// Repeat pattern to increase chance of errors
147-
commands.push("H 0");
148-
commands.push("RZ 0.5 0");
149-
commands.push("R1XY 0.5 0.5 0");
150-
commands.push("H 1");
151-
commands.push("RZ 0.5 1");
150+
builder.add_h(&[0]);
151+
builder.add_rz(0.5, &[0]);
152+
builder.add_r1xy(0.5, 0.5, &[0]);
153+
builder.add_h(&[1]);
154+
builder.add_rz(0.5, &[1]);
152155
}
153-
let msg = ByteMessage::create_from_commands(&commands).unwrap();
156+
let msg = builder.build();
154157

155158
// Apply noise the first time
156159
info!("Applying noise first time");
@@ -189,15 +192,15 @@ fn test_two_qubit_gate_determinism() {
189192
reset_model_with_seed(&mut model1, seed).unwrap();
190193

191194
// Create a message with many two-qubit gates to increase chance of errors
192-
let mut commands = Vec::new();
195+
let mut builder = ByteMessage::quantum_operations_builder();
193196
for _ in 0..20 {
194197
// Repeat pattern multiple times
195-
commands.push("CX 0 1");
196-
commands.push("CX 1 2");
197-
commands.push("CX 2 3");
198-
commands.push("CX 3 0");
198+
builder.add_cx(&[0], &[1]);
199+
builder.add_cx(&[1], &[2]);
200+
builder.add_cx(&[2], &[3]);
201+
builder.add_cx(&[3], &[0]);
199202
}
200-
let msg = ByteMessage::create_from_commands(&commands).unwrap();
203+
let msg = builder.build();
201204

202205
// Apply noise to the message
203206
let noisy1 = apply_noise(&mut model1, &msg);
@@ -233,8 +236,13 @@ fn test_measurement_determinism() {
233236
reset_model_with_seed(&mut model2, seed).unwrap();
234237

235238
// Create a message with measurements
236-
let msg =
237-
ByteMessage::create_from_commands(&["H 0", "H 1", "CX 0 1", "M 0 0", "M 1 1"]).unwrap();
239+
let mut builder = ByteMessage::quantum_operations_builder();
240+
builder.add_h(&[0]);
241+
builder.add_h(&[1]);
242+
builder.add_cx(&[0], &[1]);
243+
builder.add_measurements(&[0], &[0]);
244+
builder.add_measurements(&[1], &[1]);
245+
let msg = builder.build();
238246

239247
// Apply noise multiple times
240248
let noisy1 = apply_noise(&mut model1, &msg);
@@ -258,16 +266,16 @@ fn test_different_seeds_produce_different_results() {
258266
reset_model_with_seed(&mut model2, seed2).unwrap();
259267

260268
// Create a larger circuit to increase the chance of errors
261-
let mut commands = Vec::new();
269+
let mut builder = ByteMessage::quantum_operations_builder();
262270
for _ in 0..15 {
263271
// Repeat pattern to create a longer circuit
264-
commands.push("H 0");
265-
commands.push("CX 0 1");
266-
commands.push("H 1");
267-
commands.push("CX 1 2");
268-
commands.push("H 2");
272+
builder.add_h(&[0]);
273+
builder.add_cx(&[0], &[1]);
274+
builder.add_h(&[1]);
275+
builder.add_cx(&[1], &[2]);
276+
builder.add_h(&[2]);
269277
}
270-
let msg = ByteMessage::create_from_commands(&commands).unwrap();
278+
let msg = builder.build();
271279

272280
// Apply noise with different seeds
273281
let noisy1 = apply_noise(&mut model1, &msg);

0 commit comments

Comments
 (0)