Skip to content

Commit

Permalink
Fix Target.instruction_supported to correctly evaluate targets with n…
Browse files Browse the repository at this point in the history
…um_qubits==None (restore pre-Rust-migration behavior)
  • Loading branch information
ElePT committed Jan 13, 2025
1 parent 8d8da66 commit 4e9194f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/accelerate/src/target_transpiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ impl Target {
match obj {
TargetOperation::Variadic(_) => {
return qargs.is_none()
|| self.num_qubits == None
|| _qargs.iter().all(|qarg| {
qarg.index() <= self.num_qubits.unwrap_or_default()
}) && qarg_set.len() == _qargs.len();
Expand All @@ -1171,7 +1172,8 @@ impl Target {
let qubit_comparison = obj.operation.num_qubits();
return qubit_comparison == _qargs.len() as u32
&& _qargs.iter().all(|qarg| {
qarg.index() < self.num_qubits.unwrap_or_default()
(qarg.index() < self.num_qubits.unwrap_or_default())
|| self.num_qubits == None
});
}
}
Expand All @@ -1182,6 +1184,7 @@ impl Target {
match obj {
TargetOperation::Variadic(_) => {
return qargs.is_none()
|| self.num_qubits == None
|| _qargs.iter().all(|qarg| {
qarg.index() <= self.num_qubits.unwrap_or_default()
}) && qarg_set.len() == _qargs.len();
Expand All @@ -1190,7 +1193,8 @@ impl Target {
let qubit_comparison = obj.operation.num_qubits();
return qubit_comparison == _qargs.len() as u32
&& _qargs.iter().all(|qarg| {
qarg.index() < self.num_qubits.unwrap_or_default()
(qarg.index() < self.num_qubits.unwrap_or_default())
|| self.num_qubits == None
});
}
}
Expand Down
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 4e9194f

Please sign in to comment.