From 5b8a9aed206de819777b5afb3201f3b5893f3211 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 18 Mar 2026 02:10:23 -0400 Subject: [PATCH 1/3] fixed some more error codes for rng --- crates/pecos-qasm/src/engine.rs | 42 +++++++++++-------- .../src/pecos/engines/cvm/rng_model.py | 7 ++-- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/crates/pecos-qasm/src/engine.rs b/crates/pecos-qasm/src/engine.rs index 63f70f4e8..d60aa6c6f 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 argument 1 to be a u64 integer seed".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 argument 1 to be a u64 integer index".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 argument 1 to be a u32 integer bound".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,10 @@ 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..c35f0d993 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,7 @@ 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) + raise ValueError(f"Invalid start index: index {index} is before the current stream index: {self.count}") while self.count < index: self.rng_random() @@ -97,5 +96,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"Invalid RNG Function Encountered {func_name}" raise ValueError(error_msg) From 06ac49b81886a8b3361c0b3d8ccb07372e55405e Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Fri, 20 Mar 2026 03:49:26 -0400 Subject: [PATCH 2/3] called just lint-fix and responded to feedback --- crates/pecos-qasm/src/engine.rs | 9 ++++----- .../src/pecos/engines/cvm/rng_model.py | 5 +++-- python/quantum-pecos/tests/pecos/unit/test_rng.py | 13 +++++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/pecos-qasm/src/engine.rs b/crates/pecos-qasm/src/engine.rs index d60aa6c6f..64c27289d 100644 --- a/crates/pecos-qasm/src/engine.rs +++ b/crates/pecos-qasm/src/engine.rs @@ -1247,7 +1247,7 @@ impl QASMEngine { Expression::Integer(bit_vec) => bit_vec.load(), _ => { return Err(PecosError::ParseInvalidExpression( - "RNGseed expects argument 1 to be a u64 integer seed".to_string(), + "RNGseed expects a u64 as its argument".to_string(), )); } }; @@ -1266,7 +1266,7 @@ impl QASMEngine { Expression::Integer(bit_vec) => bit_vec.load(), _ => { return Err(PecosError::ParseInvalidExpression( - "RNGindex expects argument 1 to be a u64 integer index".to_string(), + "RNGindex expects a u64 as its argument".to_string(), )); } }; @@ -1284,7 +1284,7 @@ impl QASMEngine { Expression::Integer(bit_vec) => bit_vec.load(), _ => { return Err(PecosError::ParseInvalidExpression( - "RNGbound expects argument 1 to be a u32 integer bound".to_string(), + "RNGbound expects a u32 as its argument".to_string(), )); } }; @@ -1309,8 +1309,7 @@ impl QASMEngine { Ok(ExpressionValue::BitVec(bitvec)) } _ => Err(PecosError::ParseInvalidExpression(format!( - "Unknown RNG function '{}'", - name + "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 c35f0d993..5ce26db4d 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -54,7 +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: - raise ValueError(f"Invalid start index: index {index} is before the current stream index: {self.count}") + 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() @@ -96,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"Invalid RNG Function Encountered {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..17ced2817 100644 --- a/python/quantum-pecos/tests/pecos/unit/test_rng.py +++ b/python/quantum-pecos/tests/pecos/unit/test_rng.py @@ -33,6 +33,19 @@ def test_bounded_random() -> None: assert 0 <= random_number < bound +def test_set_idx_raises_for_backwards_index() -> None: + 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) From f51bbae60f7d318d9892a9692aab33f99d35977e Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Fri, 20 Mar 2026 17:01:53 -0400 Subject: [PATCH 3/3] added docstring to new test case --- python/quantum-pecos/tests/pecos/unit/test_rng.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/quantum-pecos/tests/pecos/unit/test_rng.py b/python/quantum-pecos/tests/pecos/unit/test_rng.py index 17ced2817..7c49c0986 100644 --- a/python/quantum-pecos/tests/pecos/unit/test_rng.py +++ b/python/quantum-pecos/tests/pecos/unit/test_rng.py @@ -34,6 +34,7 @@ def test_bounded_random() -> None: 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)