Skip to content

Commit 8736c84

Browse files
authored
Some more RNG Error fixes (#270)
* fixed some more error codes for rng * called just lint-fix and responded to feedback * added docstring to new test case
1 parent e23cecd commit 8736c84

3 files changed

Lines changed: 41 additions & 22 deletions

File tree

crates/pecos-qasm/src/engine.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,15 +1240,16 @@ impl QASMEngine {
12401240
match name {
12411241
"RNGseed" => {
12421242
if args.len() != 1 {
1243-
return Err(PecosError::ParseInvalidExpression(
1244-
"Expected a single seed for RNGseed. Received {args.len()}".to_string(),
1245-
));
1243+
return Err(PecosError::ParseInvalidExpression(format!(
1244+
"RNGseed expects exactly 1 argument (seed), got {}",
1245+
args.len()
1246+
)));
12461247
}
12471248
let seed: u64 = match &args[0] {
12481249
Expression::Integer(bit_vec) => bit_vec.load(),
12491250
_ => {
12501251
return Err(PecosError::ParseInvalidExpression(
1251-
"Invalid seed for RNGseed. Expected u64".to_string(),
1252+
"RNGseed expects a u64 as its argument".to_string(),
12521253
));
12531254
}
12541255
};
@@ -1258,15 +1259,16 @@ impl QASMEngine {
12581259
}
12591260
"RNGindex" => {
12601261
if args.len() != 1 {
1261-
return Err(PecosError::ParseInvalidExpression(
1262-
"Expected a single index for RNGseed. Received {args.len()}".to_string(),
1263-
));
1262+
return Err(PecosError::ParseInvalidExpression(format!(
1263+
"RNGindex expects exactly 1 argument (index), got {}",
1264+
args.len()
1265+
)));
12641266
}
12651267
let idx: u64 = match &args[0] {
12661268
Expression::Integer(bit_vec) => bit_vec.load(),
12671269
_ => {
12681270
return Err(PecosError::ParseInvalidExpression(
1269-
"Invalid idx for RNGindex. Expected u64".to_string(),
1271+
"RNGindex expects a u64 as its argument".to_string(),
12701272
));
12711273
}
12721274
};
@@ -1275,15 +1277,16 @@ impl QASMEngine {
12751277
}
12761278
"RNGbound" => {
12771279
if args.len() != 1 {
1278-
return Err(PecosError::ParseInvalidExpression(
1279-
"Expected a single bound for RNGbound. Received {args.len()}".to_string(),
1280-
));
1280+
return Err(PecosError::ParseInvalidExpression(format!(
1281+
"RNGbound expects exactly 1 argument (bound), got {}",
1282+
args.len()
1283+
)));
12811284
}
12821285
let ubound: u32 = match &args[0] {
12831286
Expression::Integer(bit_vec) => bit_vec.load(),
12841287
_ => {
12851288
return Err(PecosError::ParseInvalidExpression(
1286-
"Invalid idx for RNGindex. Expected u64".to_string(),
1289+
"RNGbound expects a u32 as its argument".to_string(),
12871290
));
12881291
}
12891292
};
@@ -1292,10 +1295,12 @@ impl QASMEngine {
12921295
}
12931296
"RNGnum" => {
12941297
if !args.is_empty() {
1295-
return Err(PecosError::ParseInvalidExpression(
1296-
"RNGnum receives no arguments. Received {args.len()}".to_string(),
1297-
));
1298+
return Err(PecosError::ParseInvalidExpression(format!(
1299+
"RNGnum expects no arguments, got {}",
1300+
args.len()
1301+
)));
12981302
}
1303+
12991304
let rng_num = self.rng_model.rng_num();
13001305

13011306
// convert random number to bitvec
@@ -1305,9 +1310,9 @@ impl QASMEngine {
13051310
}
13061311
Ok(ExpressionValue::BitVec(bitvec))
13071312
}
1308-
_ => Err(PecosError::ParseInvalidExpression(
1309-
"Invalid RNG function '{name}'".to_string(),
1310-
)),
1313+
_ => Err(PecosError::ParseInvalidExpression(format!(
1314+
"Unknown RNG function '{name}'"
1315+
))),
13111316
}
13121317
}
13131318

python/quantum-pecos/src/pecos/engines/cvm/rng_model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(
3131

3232
def __str__(self) -> str:
3333
"""Returns the str representation of the model."""
34-
return f"RNG Model with bound {self.current_bound} with count {self.count}"
34+
return f"RNG Model bounded by {self.current_bound} with current count {self.count}"
3535

3636
def set_seed(self, seed: int) -> None:
3737
"""Setting the seed for generating random numbers."""
@@ -54,8 +54,8 @@ def set_index(self, index: int) -> None:
5454
The number after from the stream will be the idx of interest.
5555
"""
5656
if self.count > index:
57-
error_msg = "rngindex called after specified already generated"
58-
raise BufferError(error_msg)
57+
error_msg = f"RNGindex({index}) cannot move backward: current stream index is {self.count}"
58+
raise ValueError(error_msg)
5959
while self.count < index:
6060
self.rng_random()
6161

@@ -97,5 +97,5 @@ def eval_func(self, params: dict, output: dict) -> None:
9797
binary_val = BitUInt(creg.size, rng)
9898
creg.set(binary_val)
9999
else:
100-
error_msg = f"RNG function not supported {func_name}"
100+
error_msg = f"Unknown RNG Function '{func_name}'"
101101
raise ValueError(error_msg)

python/quantum-pecos/tests/pecos/unit/test_rng.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ def test_bounded_random() -> None:
3333
assert 0 <= random_number < bound
3434

3535

36+
def test_set_idx_raises_for_backwards_index() -> None:
37+
"""Verifies that an error is raised when specifying an index that was already consumed in the RNG stream."""
38+
rng = RNGModel(shot_id=0)
39+
rng.set_seed(42)
40+
41+
rng.set_index(4)
42+
43+
try:
44+
rng.set_index(3)
45+
except ValueError as exc:
46+
expected_error_msg = "RNGindex(3) cannot move backward: current stream index is 4"
47+
assert str(exc) == expected_error_msg
48+
49+
3650
def test_set_idx() -> None:
3751
"""Verifies that the idx is set properly for our model."""
3852
rng = RNGModel(shot_id=0)

0 commit comments

Comments
 (0)