Skip to content

Commit

Permalink
Fix Target.instruction_supported when target.num_qubits == None (#…
Browse files Browse the repository at this point in the history
…13655)

* Fix Target.instruction_supported to correctly evaluate targets with num_qubits==None (restore pre-Rust-migration behavior)

* Apply Ray's suggestion

* Add reno
  • Loading branch information
ElePT authored Jan 13, 2025
1 parent d041d5b commit 79f0e72
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/accelerate/src/target_transpiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,12 @@ impl Target {

/// Checks whether an instruction is supported by the Target based on instruction name and qargs.
pub fn instruction_supported(&self, operation_name: &str, qargs: Option<&Qargs>) -> bool {
// Handle case where num_qubits is None by checking globally supported operations
let qargs: Option<&Qargs> = if self.num_qubits.is_none() {
None
} else {
qargs
};
if self.gate_map.contains_key(operation_name) {
if let Some(_qargs) = qargs {
let qarg_set: HashSet<&PhysicalQubit> = _qargs.iter().collect();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug in the :meth:`.Target.instruction_supported` method where
targets with ``self.num_qubits==None`` would always return ``false``
independently of the supported basis set.
8 changes: 8 additions & 0 deletions test/python/transpiler/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,14 @@ def test_instruction_supported_no_args(self):
def test_instruction_supported_no_operation(self):
self.assertFalse(self.ibm_target.instruction_supported(qargs=(0,), parameters=[math.pi]))

def test_instruction_supported_no_qubits(self):
"""Checks that instruction supported works when target.num_qubits is None."""
target = Target.from_configuration(["u", "cx", "rxx"])
self.assertTrue(target.instruction_supported("u", (0,)))
self.assertTrue(target.instruction_supported("cx", (0, 1)))
self.assertTrue(target.instruction_supported("cx", None))
self.assertTrue(target.instruction_supported("rxx", (2, 3)))

def test_target_serialization_preserve_variadic(self):
"""Checks that variadics are still seen as variadic after serialization"""

Expand Down

0 comments on commit 79f0e72

Please sign in to comment.