Skip to content

Commit

Permalink
Update to Rust 1.75, fix new clippy lints (#147)
Browse files Browse the repository at this point in the history
* Update Rust toolchain version

* Fix new clippy lints

* Fix formatting
  • Loading branch information
swernli authored Feb 9, 2024
1 parent efbe1e7 commit 6b5ccb3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
14 changes: 14 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ pub extern "C" fn __quantum__qis__reset__body(qubit: *mut c_void) {

/// QIR API for measuring the given qubit and storing the measured value with the given result identifier,
/// then resetting it in the computational basis.
#[allow(clippy::missing_panics_doc)]
// reason="Panics can only occur if the result that was just collected is not found in the BitVec, which should not happen."
#[no_mangle]
pub extern "C" fn __quantum__qis__mresetz__body(qubit: *mut c_void, result: *mut c_void) {
SIM_STATE.with(|sim_state| {
Expand All @@ -637,6 +639,8 @@ pub extern "C" fn __quantum__qis__mresetz__body(qubit: *mut c_void, result: *mut
}

/// QIR API for measuring the given qubit in the computation basis and storing the measured value with the given result identifier.
#[allow(clippy::missing_panics_doc)]
// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen."
#[no_mangle]
pub extern "C" fn __quantum__qis__mz__body(qubit: *mut c_void, result: *mut c_void) {
SIM_STATE.with(|sim_state| {
Expand All @@ -658,6 +662,8 @@ pub extern "C" fn __quantum__qis__mz__body(qubit: *mut c_void, result: *mut c_vo

/// QIR API that reads the Boolean value corresponding to the given result identifier, where true
/// indicates a |1⟩ state and false indicates a |0⟩ state.
#[allow(clippy::missing_panics_doc)]
// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen."
#[no_mangle]
pub extern "C" fn __quantum__qis__read_result__body(result: *mut c_void) -> bool {
SIM_STATE.with(|sim_state| {
Expand Down Expand Up @@ -813,6 +819,8 @@ pub mod legacy_output {
SIM_STATE,
};

#[allow(clippy::missing_panics_doc)]
// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen."
#[allow(non_snake_case)]
pub extern "C" fn __quantum__rt__result_record_output(result: *mut c_void) {
SIM_STATE.with(|sim_state| {
Expand All @@ -835,6 +843,8 @@ pub mod legacy_output {
}

/// QIR API for recording the given result into the program output.
#[allow(clippy::missing_panics_doc)]
// reason="Panics can only occur if the result index is not found in the BitVec after resizing, which should not happen."
#[no_mangle]
pub extern "C" fn __quantum__rt__result_record_output(result: *mut c_void, tag: *mut c_char) {
SIM_STATE.with(|sim_state| {
Expand Down Expand Up @@ -894,6 +904,8 @@ pub extern "C" fn __quantum__rt__qubit_allocate() -> *mut c_void {
}

/// QIR API for allocating the given number of qubits in the simulation, returning them as a runtime managed array.
/// # Panics
/// This function will panic if the requested array size is too large to be described with the system pointer size.
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn __quantum__rt__qubit_allocate_array(size: u64) -> *const QirArray {
Expand Down Expand Up @@ -936,6 +948,8 @@ pub extern "C" fn __quantum__rt__qubit_release(qubit: *mut c_void) {
}

/// QIR API for getting the string interpretation of a qubit identifier.
/// # Panics
/// This function will panic if memory cannot be allocated for the underyling string.
#[no_mangle]
pub extern "C" fn __quantum__rt__qubit_to_string(qubit: *mut c_void) -> *const CString {
unsafe {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/result_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub extern "C" fn __quantum__rt__result_update_reference_count(_res: *mut c_void
// no-op
}

/// # Panics
/// This function panics if the memory cannot be allocated for the result string.
#[no_mangle]
pub extern "C" fn __quantum__rt__result_to_string(res: *mut c_void) -> *const CString {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.70"
channel = "1.75"
components = [ "rustfmt", "clippy", "llvm-tools-preview" ]
5 changes: 4 additions & 1 deletion sparsesim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl QuantumSim {

/// Returns a sorted copy of the current sparse state as a vector of pairs of indices and complex numbers, along with
/// the total number of currently allocated qubits to help in interpreting the sparse state.
#[allow(clippy::missing_panics_doc)] // reason="Panics can only occur if the keys are not present in the map, which should not happen."
#[must_use]
pub fn get_state(&mut self) -> (Vec<(BigUint, Complex64)>, usize) {
// Swap all the entries in the state to be ordered by qubit identifier. This makes
Expand Down Expand Up @@ -188,6 +189,7 @@ impl QuantumSim {

/// Prints the current state vector to standard output with integer labels for the states, skipping any
/// states with zero amplitude.
#[allow(clippy::missing_panics_doc)] // reason="Panics can only occur if the keys are not present in the map, which should not happen."
pub fn dump(&mut self) {
// Swap all the entries in the state to be ordered by qubit identifier. This makes
// interpreting the state easier for external consumers that don't have access to the id map.
Expand Down Expand Up @@ -446,7 +448,7 @@ impl QuantumSim {

pub(crate) fn check_for_duplicates(ids: &[usize]) {
let mut unique = FxHashSet::default();
for id in ids.iter() {
for id in ids {
assert!(
unique.insert(id),
"Duplicate qubit id '{id}' found in application."
Expand Down Expand Up @@ -617,6 +619,7 @@ impl QuantumSim {
}

/// Multi-controlled Y gate.
#[allow(clippy::missing_panics_doc)] // reason="Panics can only occur if ctrls are empty, which is handled at the top of the function."
pub fn mcy(&mut self, ctls: &[usize], target: usize) {
if ctls.is_empty() {
self.y(target);
Expand Down
3 changes: 1 addition & 2 deletions stdlib/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ pub unsafe extern "C" fn __quantum__rt__array_concatenate(
new_array.data.resize(array1.data.len(), 0_u8);
new_array.data.copy_from_slice(array1.data.as_slice());

let mut copy = Vec::new();
copy.resize(array2.data.len(), 0_u8);
let mut copy = vec![0; array2.data.len()];
copy.copy_from_slice(array2.data.as_slice());

new_array.data.append(&mut copy);
Expand Down
33 changes: 15 additions & 18 deletions stdlib/src/bigints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {
fn test_bigint_create_from_int() {
let bigint_0 = __quantum__rt__bigint_create_i64(42);
unsafe {
assert_eq!(*bigint_0, (42).try_into().unwrap());
assert_eq!(*bigint_0, (42).into());
// Note that the test must decrement the reference count on any created items to ensure
// they are cleaned up and tests can pass with address sanitizer.
__quantum__rt__bigint_update_reference_count(bigint_0, -1);
Expand All @@ -185,10 +185,7 @@ mod tests {
unsafe {
let bigint_1 =
__quantum__rt__bigint_create_array(bytes.len().try_into().unwrap(), bytes.as_ptr());
assert_eq!(
*bigint_1,
(9_223_372_036_854_775_807_i64).try_into().unwrap()
);
assert_eq!(*bigint_1, (9_223_372_036_854_775_807_i64).into());
__quantum__rt__bigint_update_reference_count(bigint_1, -1);
}
}
Expand All @@ -199,19 +196,19 @@ mod tests {
let bigint_1 = __quantum__rt__bigint_create_i64(3);
unsafe {
let bigint_2 = __quantum__rt__bigint_add(bigint_0, bigint_1);
assert_eq!(*bigint_2, (45).try_into().unwrap());
assert_eq!(*bigint_2, (45).into());
let bigint_3 = __quantum__rt__bigint_subtract(bigint_2, bigint_1);
assert_eq!(*bigint_3, (42).try_into().unwrap());
assert_eq!(*bigint_3, (42).into());
let bigint_4 = __quantum__rt__bigint_divide(bigint_3, bigint_1);
assert_eq!(*bigint_4, (14).try_into().unwrap());
assert_eq!(*bigint_4, (14).into());
let bigint_5 = __quantum__rt__bigint_multiply(bigint_4, bigint_1);
assert_eq!(*bigint_5, (42).try_into().unwrap());
assert_eq!(*bigint_5, (42).into());
let bigint_6 = __quantum__rt__bigint_modulus(bigint_5, bigint_1);
assert_eq!(*bigint_6, (0).try_into().unwrap());
assert_eq!(*bigint_6, (0).into());
let bigint_7 = __quantum__rt__bigint_negate(bigint_5);
assert_eq!(*bigint_7, (-42).try_into().unwrap());
assert_eq!(*bigint_7, (-42).into());
let bigint_8 = __quantum__rt__bigint_power(bigint_7, 3);
assert_eq!(*bigint_8, (-74088).try_into().unwrap());
assert_eq!(*bigint_8, (-74088).into());
__quantum__rt__bigint_update_reference_count(bigint_8, -1);
__quantum__rt__bigint_update_reference_count(bigint_7, -1);
__quantum__rt__bigint_update_reference_count(bigint_6, -1);
Expand All @@ -230,17 +227,17 @@ mod tests {
let bigint_1 = __quantum__rt__bigint_create_i64(3);
unsafe {
let bigint_2 = __quantum__rt__bigint_bitand(bigint_0, bigint_1);
assert_eq!(*bigint_2, (2).try_into().unwrap());
assert_eq!(*bigint_2, (2).into());
let bigint_3 = __quantum__rt__bigint_bitor(bigint_0, bigint_1);
assert_eq!(*bigint_3, (43).try_into().unwrap());
assert_eq!(*bigint_3, (43).into());
let bigint_4 = __quantum__rt__bigint_bitxor(bigint_0, bigint_3);
assert_eq!(*bigint_4, (1).try_into().unwrap());
assert_eq!(*bigint_4, (1).into());
let bigint_5 = __quantum__rt__bigint_bitnot(bigint_4);
assert_eq!(*bigint_5, (-2).try_into().unwrap());
assert_eq!(*bigint_5, (-2).into());
let bigint_6 = __quantum__rt__bigint_shiftleft(bigint_0, 2);
assert_eq!(*bigint_6, (168).try_into().unwrap());
assert_eq!(*bigint_6, (168).into());
let bigint_7 = __quantum__rt__bigint_shiftright(bigint_6, 3);
assert_eq!(*bigint_7, (21).try_into().unwrap());
assert_eq!(*bigint_7, (21).into());
__quantum__rt__bigint_update_reference_count(bigint_7, -1);
__quantum__rt__bigint_update_reference_count(bigint_6, -1);
__quantum__rt__bigint_update_reference_count(bigint_5, -1);
Expand Down
3 changes: 1 addition & 2 deletions stdlib/src/range_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ pub unsafe extern "C" fn quantum__rt__array_slice_1d(
let index = i
.try_into()
.expect("Item index too large for `usize` type on this platform.");
let mut copy = Vec::new();
copy.resize(array.elem_size, 0_u8);
let mut copy = vec![0; array.elem_size];
copy.copy_from_slice(&array.data[index..index + array.elem_size]);
slice.data.append(&mut copy);
}
Expand Down

0 comments on commit 6b5ccb3

Please sign in to comment.