diff --git a/crates/pecos-qasm/src/engine.rs b/crates/pecos-qasm/src/engine.rs index 63f70f4e8..64c27289d 100644 --- a/crates/pecos-qasm/src/engine.rs +++ b/crates/pecos-qasm/src/engine.rs @@ -1238,15 +1238,16 @@ impl QASMEngine { match name { "RNGseed" => { if args.len() != 1 { - return Err(PecosError::ParseInvalidExpression( - "Expected a single seed for RNGseed. Received {args.len()}".to_string(), - )); + return Err(PecosError::ParseInvalidExpression(format!( + "RNGseed expects exactly 1 argument (seed), got {}", + args.len() + ))); } let seed: u64 = match &args[0] { Expression::Integer(bit_vec) => bit_vec.load(), _ => { return Err(PecosError::ParseInvalidExpression( - "Invalid seed for RNGseed. Expected u64".to_string(), + "RNGseed expects a u64 as its argument".to_string(), )); } }; @@ -1256,15 +1257,16 @@ impl QASMEngine { } "RNGindex" => { if args.len() != 1 { - return Err(PecosError::ParseInvalidExpression( - "Expected a single index for RNGseed. Received {args.len()}".to_string(), - )); + return Err(PecosError::ParseInvalidExpression(format!( + "RNGindex expects exactly 1 argument (index), got {}", + args.len() + ))); } let idx: u64 = match &args[0] { Expression::Integer(bit_vec) => bit_vec.load(), _ => { return Err(PecosError::ParseInvalidExpression( - "Invalid idx for RNGindex. Expected u64".to_string(), + "RNGindex expects a u64 as its argument".to_string(), )); } }; @@ -1273,15 +1275,16 @@ impl QASMEngine { } "RNGbound" => { if args.len() != 1 { - return Err(PecosError::ParseInvalidExpression( - "Expected a single bound for RNGbound. Received {args.len()}".to_string(), - )); + return Err(PecosError::ParseInvalidExpression(format!( + "RNGbound expects exactly 1 argument (bound), got {}", + args.len() + ))); } let ubound: u32 = match &args[0] { Expression::Integer(bit_vec) => bit_vec.load(), _ => { return Err(PecosError::ParseInvalidExpression( - "Invalid idx for RNGindex. Expected u64".to_string(), + "RNGbound expects a u32 as its argument".to_string(), )); } }; @@ -1290,10 +1293,12 @@ impl QASMEngine { } "RNGnum" => { if !args.is_empty() { - return Err(PecosError::ParseInvalidExpression( - "RNGnum receives no arguments. Received {args.len()}".to_string(), - )); + return Err(PecosError::ParseInvalidExpression(format!( + "RNGnum expects no arguments, got {}", + args.len() + ))); } + let rng_num = self.rng_model.rng_num(); // convert random number to bitvec @@ -1303,9 +1308,9 @@ impl QASMEngine { } Ok(ExpressionValue::BitVec(bitvec)) } - _ => Err(PecosError::ParseInvalidExpression( - "Invalid RNG function '{name}'".to_string(), - )), + _ => Err(PecosError::ParseInvalidExpression(format!( + "Unknown RNG function '{name}'" + ))), } } diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index 612056c15..5ce26db4d 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -31,7 +31,7 @@ def __init__( def __str__(self) -> str: """Returns the str representation of the model.""" - return f"RNG Model with bound {self.current_bound} with count {self.count}" + return f"RNG Model bounded by {self.current_bound} with current count {self.count}" def set_seed(self, seed: int) -> None: """Setting the seed for generating random numbers.""" @@ -54,8 +54,8 @@ def set_index(self, index: int) -> None: The number after from the stream will be the idx of interest. """ if self.count > index: - error_msg = "rngindex called after specified already generated" - raise BufferError(error_msg) + error_msg = f"RNGindex({index}) cannot move backward: current stream index is {self.count}" + raise ValueError(error_msg) while self.count < index: self.rng_random() @@ -97,5 +97,5 @@ def eval_func(self, params: dict, output: dict) -> None: binary_val = BitUInt(creg.size, rng) creg.set(binary_val) else: - error_msg = f"RNG function not supported {func_name}" + error_msg = f"Unknown RNG Function '{func_name}'" raise ValueError(error_msg) diff --git a/python/quantum-pecos/tests/pecos/unit/test_rng.py b/python/quantum-pecos/tests/pecos/unit/test_rng.py index 8fcca0ec8..7c49c0986 100644 --- a/python/quantum-pecos/tests/pecos/unit/test_rng.py +++ b/python/quantum-pecos/tests/pecos/unit/test_rng.py @@ -33,6 +33,20 @@ def test_bounded_random() -> None: assert 0 <= random_number < bound +def test_set_idx_raises_for_backwards_index() -> None: + """Verifies that an error is raised when specifying an index that was already consumed in the RNG stream.""" + rng = RNGModel(shot_id=0) + rng.set_seed(42) + + rng.set_index(4) + + try: + rng.set_index(3) + except ValueError as exc: + expected_error_msg = "RNGindex(3) cannot move backward: current stream index is 4" + assert str(exc) == expected_error_msg + + def test_set_idx() -> None: """Verifies that the idx is set properly for our model.""" rng = RNGModel(shot_id=0)