From bee235c2a8fce8316934220cd7302f9d77d326fd Mon Sep 17 00:00:00 2001 From: Kevin Hartman Date: Mon, 11 Sep 2023 16:52:22 -0400 Subject: [PATCH] Fix bugs with slicing impl. --- .../src/quantum_circuit/circuit_data.rs | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/accelerate/src/quantum_circuit/circuit_data.rs b/crates/accelerate/src/quantum_circuit/circuit_data.rs index e2cf1d635dd9..fc8e8bf618da 100644 --- a/crates/accelerate/src/quantum_circuit/circuit_data.rs +++ b/crates/accelerate/src/quantum_circuit/circuit_data.rs @@ -151,7 +151,7 @@ impl CircuitData { Slice(slice) => { let indices = slice.indices(self.data.len().try_into().unwrap())?; let slice = self.convert_py_slice(py, slice)?; - let values = value.iter()?.take(slice.len()).collect::>(); + let values = value.iter()?.collect::>>()?; if indices.step != 1 && slice.len() != values.len() { return Err(PyValueError::new_err(format!( "attempt to assign sequence of size {:?} to extended slice of size {:?}", @@ -160,25 +160,27 @@ impl CircuitData { ))); } - let mut slice_itr = slice.into_iter(); - let mut value_itr = values.into_iter(); - let enumerated = zip(&mut slice_itr, &mut value_itr); + let enumerated = zip(slice.iter(), values.iter()); for (i, v) in enumerated { - self.__setitem__(py, Int(i), v?)?; + let v = v; + self.__setitem__(py, Int(*i), *v)?; } // Delete any extras. - for _ in slice_itr { - let res = self.__delitem__(py, Int(indices.stop - 1)); - if res.is_err() { - // We're empty! - break; + if slice.len() >= values.len() { + for _ in 0..(slice.len() - values.len()) { + let res = self.__delitem__(py, Int(indices.stop - 1)); + if res.is_err() { + // We're empty! + break; + } + } + } else { + // Insert any extra values. + for v in values.iter().skip(slice.len()).rev() { + let v: ElementType = (*v).extract()?; + self.insert(py, indices.stop, v)?; } - } - - // Insert any extra values. - for v in value_itr.rev() { - self.insert(py, indices.stop - 1, v?.extract()?)?; } Ok(())