Skip to content

Commit

Permalink
refactor: pull request review feedback round 2
Browse files Browse the repository at this point in the history
  • Loading branch information
erichulburd committed Dec 12, 2024
1 parent 35fec63 commit a84c55d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
10 changes: 5 additions & 5 deletions crates/lib/src/qpu/experimental/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ pub enum Error {
/// An error occurred while constructing an extern signature.
#[error("error constructing extern signature: {0}")]
ExternSignatureError(#[from] ExternError),
/// The destination must be a real array.
#[error("destination must be a real array, found {destination_type:?}")]
/// The destination must be a `REAL[]`.
#[error("destination must be a REAL[], found {destination_type:?}")]
InvalidDestinationType {
/// The type on the destination declaration.
destination_type: quil_rs::instruction::ScalarType,
},
/// The source must be a real array.
#[error("source must be a real array, found {source_type:?}")]
/// The source must be a `REAL[]`.
#[error("source must be a REAL[], found {source_type:?}")]
InvalidSourceType {
/// The type on the source declaration.
source_type: quil_rs::instruction::ScalarType,
Expand Down Expand Up @@ -300,7 +300,7 @@ fn prng_value_to_sub_region_index(value: u64, sub_region_count: u8) -> u8 {
/// with a seed of 639,523, you could backout the randomly chosen sub-regions with the following:
///
/// ```rust
/// use qcs::qpu::experimental::random::choose_random_real_sub_regions_indices;
/// use qcs::qpu::experimental::random::choose_random_real_sub_region_indices;
///
/// let seed = 639_523;
/// let start_index = 0;
Expand Down
13 changes: 8 additions & 5 deletions crates/lib/src/qpu/experimental/randomized_measurements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ impl RandomizedMeasurements {
pub fn append_to_program(&self, target_program: Program) -> Result<Program> {
let measured_qubits = target_program
.to_instructions()
.iter()
.into_iter()
.filter_map(|instruction| {
if let Instruction::Measurement(measurement) = instruction {
Some(measurement.qubit.clone())
Some(measurement.qubit)
} else {
None
}
Expand All @@ -213,11 +213,14 @@ impl RandomizedMeasurements {
.qubit_randomizations
.iter()
.filter(|randomization| measured_qubits.contains(&randomization.measurement.qubit))
.map(|randomization| randomization.measurement.qubit.clone())
.map(|randomization| &randomization.measurement.qubit)
.collect::<HashSet<_>>();
if !qubits_with_redundant_measurements.is_empty() {
return Err(Error::ProgramContainsPreexistingMeasurements(
qubits_with_redundant_measurements,
qubits_with_redundant_measurements
.into_iter()
.cloned()
.collect(),
));
}
let mut program = target_program.clone_without_body_instructions();
Expand Down Expand Up @@ -399,7 +402,7 @@ impl UnitarySet {
/// Return the unitaries underlying a ZXZXZ decomposition. If the
/// [`UnitarySet`] is not a ZXZXZ decomposition, return `None`.
#[must_use]
pub fn to_zxzxz(&self) -> Option<&Array2<f64>> {
pub fn as_zxzxz(&self) -> Option<&Array2<f64>> {
match &self.0 {
UnitarySetInner::Zxzxz(unitaries) => Some(unitaries),
}
Expand Down
9 changes: 6 additions & 3 deletions crates/python/src/qpu/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,17 @@ rigetti_pyo3::impl_repr!(PyUnitarySet);
#[pymethods]
impl PyUnitarySet {
#[staticmethod]
fn from_zxzxz(inner: &PyArray2<f64>) -> PyUnitarySet {
Self(UnitarySet::try_new_zxzxz(inner.to_owned_array()).unwrap())
fn from_zxzxz(inner: &PyArray2<f64>) -> PyResult<PyUnitarySet> {
UnitarySet::try_new_zxzxz(inner.to_owned_array())
.map(Self)
.map_err(RustRandomizedMeasurementsError::from)
.map_err(RustRandomizedMeasurementsError::to_py_err)
}

fn to_zxzxz<'py>(&self, py: Python<'py>) -> PyResult<&'py PyArray2<f64>> {
let matrix = self
.as_inner()
.to_zxzxz()
.as_zxzxz()
.ok_or_else(|| PyValueError::new_err("unitary set is not ZXZXZ"))?;
Ok(PyArray2::from_array(py, matrix))
}
Expand Down

0 comments on commit a84c55d

Please sign in to comment.