cirq.drop_diagonal_before_measurement

Removes Z and CZ gates that appear immediately before measurements.

This transformer optimizes circuits by removing Z-type and CZ-type diagonal gates (specifically ZPowGate instances like Z, S, T, Rz, and CZPowGate instances like CZ) that appear immediately before measurement operations. Since measurements project onto the computational basis, these diagonal gates applied immediately before a measurement do not affect the measurement outcome and can be safely removed (when all their qubits are measured).

To maximize the effectiveness of this optimization, the transformer first applies the eject_z transformation, which pushes Z gates (and other diagonal phases) later in the circuit. This handles cases where diagonal gates can commute past other operations. For example:

Z(q0) - CZ(q0, q1) - measure(q0) - measure(q1)

After eject_z, the Z gate on the control qubit commutes through the CZ:

CZ(q0, q1) - Z(q1) - measure(q0) - measure(q1)

Then both the CZ and Z(q1) can be removed since all their qubits are measured:

measure(q0) - measure(q1)

circuit Input circuit to transform.
context cirq.TransformerContext storing common configurable options for transformers.

Copy of the transformed input circuit with diagonal gates before measurements removed.

>>> import cirq
>>> q0, q1 = cirq.LineQubit.range(2)
>>>
>>> # Simple case: Z before measurement
>>> circuit = cirq.Circuit(cirq.H(q0), cirq.Z(q0), cirq.measure(q0))
>>> optimized = cirq.drop_diagonal_before_measurement(circuit)
>>> print(optimized)
</td>
</tr>
<tr>
<td>
`0`<a id="0"></a>
</td>
<td>
───H───M───
# Complex case: Z-CZ commutation with both qubits measured
circuit = cirq.Circuit(
    cirq.Z(q0),
    cirq.CZ(q0, q1),
    cirq.measure(q0),
    cirq.measure(q1)
)
optimized = cirq.drop_diagonal_before_measurement(circuit)
print(optimized)
</td>
</tr><tr>
<td>
`0`<a id="0"></a>
</td>
<td>
───M───
<BLANKLINE>
</td>
</tr><tr>
<td>
`1`<a id="1"></a>
</td>
<td>
───M───