Skip to content

Commit

Permalink
break repeated qubits into two error messages for repeat in measured_…
Browse files Browse the repository at this point in the history
…targets or repeat in target_qubits
  • Loading branch information
ashlhans committed Apr 4, 2024
1 parent 6abf1ae commit 3444e18
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down

0 comments on commit 3444e18

Please sign in to comment.