Skip to content

Commit

Permalink
Throw execption for multi measurement (#2593)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdg221 authored Dec 3, 2024
1 parent a2a6818 commit da77ec0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mitiq/observable/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,26 @@ def _measure_in(

basis_rotations = set()
support = set()
qubits_with_measurements = set[cirq.Qid]()

# Find any existing measurement gates in the circuit
for _, op, _ in circuit.findall_operations_with_gate_type(
cirq.MeasurementGate
):
qubits_with_measurements.update(op.qubits)

for pauli in paulis.elements:
basis_rotations.update(pauli._basis_rotations())
support.update(pauli._qubits_to_measure())
measured = circuit + basis_rotations + cirq.measure(*sorted(support))

if support & qubits_with_measurements:
raise ValueError(
f"More than one measurement found for qubits: "
f"{support & qubits_with_measurements}. Only a single "
f"measurement is allowed per qubit."
)

# Transform circuit back to original qubits.
reverse_qubit_map = dict(zip(qubit_map.values(), qubit_map.keys()))
return measured.transform_qubits(lambda q: reverse_qubit_map[q])
Expand Down
11 changes: 11 additions & 0 deletions mitiq/observable/tests/test_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ def test_pauli_measure_in_bad_qubits_error():
pauli.measure_in(circuit)


def test_pauli_measure_in_multi__measurement_per_qubit():
n = 4
pauli = PauliString(spec="Z" * n)
circuit = cirq.Circuit(cirq.H.on_each(cirq.LineQubit.range(n)))

# add a measurement to qubit 0 and 1
circuit = circuit + cirq.measure(cirq.LineQubit(0), cirq.LineQubit(1))
with pytest.raises(ValueError, match="More than one measurement"):
pauli.measure_in(circuit)


def test_can_be_measured_with_single_qubit():
pauli = PauliString(spec="Z")

Expand Down

0 comments on commit da77ec0

Please sign in to comment.