Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Addressing feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgranade committed Apr 7, 2022
1 parent 9b05a04 commit b60d691
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Simulation/qdk_sim_rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ TODO
- Too many APIs `panic!` or `unwrap`, and need replaced with `Result` returns instead.

# Crate features
<!-- Note that this section is filled in automatically by document-features. -->
4 changes: 2 additions & 2 deletions src/Simulation/qdk_sim_rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use thiserror::Error;
/// Represents errors that can occur during linear algebra operations.
#[derive(Debug, Diagnostic, Error)]
pub enum QdkSimError {
// NB: As a design note, these should be eliminated before stabilizing the
// API for this simulator crate.
// NB: As a design note, please consider if a more specific error is better
// suited for your usecase before returning `MiscError`.
/// Raised on miscellaneous errors.
#[error("{0}")]
#[diagnostic(code(qdk_sim::other))]
Expand Down
24 changes: 20 additions & 4 deletions src/Simulation/qdk_sim_rs/src/linalg/array_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ where
D: Dimension + RemoveAxis,
{
fn swap_index_axis(&mut self, axis: Axis, idx_source: usize, idx_dest: usize) {
// TODO: Avoid copying both; this is needed currently to avoid mutably
// borrowing something which has an outstanding immutable
// borrow.
// TODO[perf]: Avoid copying both; this is needed currently to avoid mutably
// borrowing something which has an outstanding immutable
// borrow.
let source = self.index_axis(axis, idx_source).clone().to_owned();
let dest = self.index_axis(axis, idx_dest).clone().to_owned();
self.index_axis_mut(axis, idx_source).assign(&dest);
Expand All @@ -88,7 +88,7 @@ where

#[cfg(test)]
mod tests {
use super::ArrayBaseMatrixExt;
use super::{ArrayBaseMatrixExt, ArrayBaseRemoveAxisExt};
use ndarray::{array, Array2};

#[test]
Expand All @@ -110,4 +110,20 @@ mod tests {
array![[6.0, 0.0, 0.0], [2.0, 12.0, 0.0], [4.0, 15.0, 3.0]]
);
}

#[test]
fn swap_index_axis_swaps_rows_correctly() {
let mut mtx = array![[6.0, 18.0, 3.0], [2.0, 12.0, 1.0], [4.0, 15.0, 3.0]];
mtx.swap_index_axis(ndarray::Axis(0), 1, 2);
let expected = array![[6.0, 18.0, 3.0], [4.0, 15.0, 3.0], [2.0, 12.0, 1.0]];
assert_eq!(mtx, expected);
}

#[test]
fn swap_index_axis_swaps_columns_correctly() {
let mut mtx = array![[6.0, 18.0, 3.0], [2.0, 12.0, 1.0], [4.0, 15.0, 3.0]];
mtx.swap_index_axis(ndarray::Axis(1), 1, 2);
let expected = array![[6.0, 3.0, 18.0], [2.0, 1.0, 12.0], [4.0, 3.0, 15.0]];
assert_eq!(mtx, expected);
}
}

0 comments on commit b60d691

Please sign in to comment.