Skip to content

Commit

Permalink
Improvements to SymbolTable::gates function (#217)
Browse files Browse the repository at this point in the history
* Filter U gate from output of SymbolTable::gates

The Qiskit importer otherwise needs to filter this gate out.
Other consumers probably would do so as well. If it is useful in
antother context, we can add an option to include the U gate

Review comment: Qiskit/qiskit#12087 (comment)

* Use impl trait for SymbolTable::gate return type
So we return an iterator, not a `Vec`.
  • Loading branch information
jlapeyre authored May 2, 2024
1 parent 5162cf8 commit 0b3739c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/oq3_semantics/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,22 @@ impl SymbolTable {
.collect::<Vec<_>>()
}

/// Return a Vec of information about all gate declarations. Each element
/// Return an iterator giving information about all gate declarations. Each element
/// is a tuple of (gate name, symbol id, num classical params, num quantum params).
pub fn gates(&self) -> Vec<(&str, SymbolId, usize, usize)> {
self.all_symbols
.iter()
.enumerate()
.filter_map(|(n, sym)| {
if let Type::Gate(num_cl, num_qu) = &sym.symbol_type() {
Some((sym.name(), SymbolId(n), *num_cl, *num_qu))
} else {
/// `gphase` is not included here. It is treated specially.
/// `U` is also filtered out, as it is builtin.
pub fn gates(&self) -> impl Iterator<Item = (&str, SymbolId, usize, usize)> {
self.all_symbols.iter().enumerate().filter_map(|(n, sym)| {
if let Type::Gate(num_cl, num_qu) = &sym.symbol_type() {
if sym.name() == "U" {
None
} else {
Some((sym.name(), SymbolId(n), *num_cl, *num_qu))
}
})
.collect()
} else {
None
}
})
}

/// Return a list of hardware qubits referenced in the program as a
Expand Down

0 comments on commit 0b3739c

Please sign in to comment.