diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py index f23f833f466..3653a03d01d 100644 --- a/qiskit/circuit/instruction.py +++ b/qiskit/circuit/instruction.py @@ -240,8 +240,8 @@ def soft_compare(self, other: "Instruction") -> bool: """ if ( self.name != other.name - or other.num_qubits != other.num_qubits - or other.num_clbits != other.num_clbits + or self.num_qubits != other.num_qubits + or self.num_clbits != other.num_clbits or len(self.params) != len(other.params) ): return False diff --git a/releasenotes/notes/fix_soft_compare-3f4148aab3a4606b.yaml b/releasenotes/notes/fix_soft_compare-3f4148aab3a4606b.yaml new file mode 100644 index 00000000000..498576a7292 --- /dev/null +++ b/releasenotes/notes/fix_soft_compare-3f4148aab3a4606b.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + The method :meth:`qiskit.instruction.Instruction.soft_compare` is meant to compare whether two gates match in their name, number of qubits, number of clbits, and the number of parameters. However, there was a typo where it would not check the number of qubits and number of clbits for a match. This resolves the apparent typo. \ No newline at end of file diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index 35ff001c6c5..edd01c5cc1c 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -118,6 +118,12 @@ def test_instructions_soft_compare(self): Instruction("u", 1, 0, [0.4, phi]).soft_compare(Instruction("v", 1, 0, [theta, phi])) ) + # Test that when names are the same but number of qubits differ we get False + self.assertFalse(Instruction("u", 1, 0, []).soft_compare(Instruction("u", 2, 0, []))) + + # Test that when names are the same but number of clbits differ we get False + self.assertFalse(Instruction("u", 1, 0, []).soft_compare(Instruction("u", 1, 1, []))) + # Test cutoff precision. self.assertFalse( Instruction("v", 1, 0, [0.401, phi]).soft_compare(Instruction("v", 1, 0, [0.4, phi]))