Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions crates/pecos-qasm/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}
};
Expand All @@ -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(),
));
}
};
Expand All @@ -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(),
));
}
};
Expand All @@ -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
Expand All @@ -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}'"
))),
}
}

Expand Down
8 changes: 4 additions & 4 deletions python/quantum-pecos/src/pecos/engines/cvm/rng_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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()

Expand Down Expand Up @@ -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)
14 changes: 14 additions & 0 deletions python/quantum-pecos/tests/pecos/unit/test_rng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading