Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-hash-lookup way to retrieve the default interned value #13035

Merged
merged 4 commits into from
Aug 30, 2024
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
4 changes: 2 additions & 2 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl CircuitData {
instruction_iter.size_hint().0,
global_phase,
)?;
let no_clbit_index = res.cargs_interner.insert(&[]);
let no_clbit_index = res.cargs_interner.get_default();
for (operation, params, qargs) in instruction_iter {
let qubits = res.qargs_interner.insert(&qargs);
let params = (!params.is_empty()).then(|| Box::new(params));
Expand Down Expand Up @@ -258,7 +258,7 @@ impl CircuitData {
params: &[Param],
qargs: &[Qubit],
) -> PyResult<()> {
let no_clbit_index = self.cargs_interner.insert(&[]);
let no_clbit_index = self.cargs_interner.get_default();
let params = (!params.is_empty()).then(|| Box::new(params.iter().cloned().collect()));
let qubits = self.qargs_interner.insert(qargs);
self.data.push(PackedInstruction {
Expand Down
30 changes: 29 additions & 1 deletion crates/circuit/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,23 @@ unsafe impl<T: ?Sized> Sync for Interned<T> {}
/// itself (the `Interned` type), rather than raw references; the `Interned` type is narrower than a
/// true reference.
///
/// This is only implemented for owned types that implement `Default`, so that the convenience
/// method `Interner::get_default` can work reliably and correctly; the "default" index needs to be
/// guaranteed to be reserved and present for safety.
jakelishman marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// ```rust
/// let mut interner = Interner::<[usize]>::new();
///
/// // These are of type `Interned<[usize]>`.
/// let default_empty = interner.get_default();
/// let empty = interner.insert(&[]);
/// let other_empty = interner.insert(&[]);
/// let key = interner.insert(&[0, 1, 2, 3, 4]);
///
/// assert_eq!(empty, other_empty);
/// assert_eq!(empty, default_empty);
/// assert_ne!(empty, key);
///
/// assert_eq!(interner.get(empty), &[]);
Expand Down Expand Up @@ -93,9 +99,31 @@ where
impl<T> Interner<T>
where
T: ?Sized + ToOwned,
<T as ToOwned>::Owned: Hash + Eq + Default,
{
/// Construct a new interner. The stored type must have a default value, in order for
/// `Interner::get_default` to reliably work correctly without a hash lookup (though ideally
/// we'd just use specialisation to do that).
pub fn new() -> Self {
Self(Default::default())
let mut set = IndexSet::with_capacity_and_hasher(1, Default::default());
set.insert(Default::default());
Self(set)
}

/// Retrieve the key corresponding to the default store, without any hash or equality lookup.
/// For example, if the interned type is `[Clbit]`, the default key corresponds to the empty
/// slice `&[]`. This is a common operation with the cargs interner, for things like pushing
/// gates.
///
/// In an ideal world, we wouldn't have the `Default` trait bound on `new`, but would use
/// specialisation to insert the default key only if the stored value implemented `Default`
/// (we'd still trait-bound this method).
#[inline(always)]
pub fn get_default(&self) -> Interned<T> {
Interned {
index: 0,
_type: PhantomData,
}
}
}

Expand Down
Loading