diff --git a/src/braket/circuits/circuit.py b/src/braket/circuits/circuit.py index 44e45d1cb..a4b48629d 100644 --- a/src/braket/circuits/circuit.py +++ b/src/braket/circuits/circuit.py @@ -763,20 +763,23 @@ def measure(self, target_qubits: QubitSetInput | None = None) -> Circuit: if target_qubits: # Check if the target_qubits are already measured - if ( - self._measure_targets - and any(target in self._measure_targets for target in target_qubits) - or len(target_qubits) != len(set(target_qubits)) + if self._measure_targets and any( + target in self._measure_targets for target in target_qubits ): - intersection = ( - set(target_qubits) & set(self._measure_targets) - if self._measure_targets - else [qubit for qubit, count in Counter(target_qubits).items() if count > 1] - ) + intersection = set(target_qubits) & set(self._measure_targets) raise ValueError( f"cannot measure the same qubit(s) {', '.join(map(str, intersection))} " "more than once." ) + # Check if there are repeated qubits in the same measurement + if len(target_qubits) != len(set(target_qubits)): + intersection = [ + qubit for qubit, count in Counter(target_qubits).items() if count > 1 + ] + raise ValueError( + f"cannot repeat qubit(s) {', '.join(map(str, intersection))} " + "in the same measurement." + ) self._add_measure(target_qubits=target_qubits) else: # Check if any qubits are already measured diff --git a/test/unit_tests/braket/circuits/test_circuit.py b/test/unit_tests/braket/circuits/test_circuit.py index a109e6f03..7680f9b4f 100644 --- a/test/unit_tests/braket/circuits/test_circuit.py +++ b/test/unit_tests/braket/circuits/test_circuit.py @@ -725,7 +725,7 @@ def test_measure_same_qubit_twice_with_list(): def test_measure_same_qubit_twice_with_one_measure(): - message = "cannot measure the same qubit\\(s\\) 0 more than once." + message = "cannot repeat qubit\\(s\\) 0 in the same measurement." with pytest.raises(ValueError, match=message): Circuit().h(0).cnot(0, 1).measure([0, 0, 0])