cancel_zero_controls#
- cancel_zero_controls(qc: QuantumCircuit) QuantumCircuit[source]#
Cancel controlled gates whose control qubit is guaranteed to be |0⟩.
Every qubit starts in |0⟩ (and re-enters |0⟩ after
qb_alloc). A controlled gate conditioned on |1⟩ acting on such a qubit is a no-op. For symmetric controlled-phase gates (CP, CZ) the gate is a no-op if either qubit is |0⟩.This is particularly effective on QFT-style circuits, where many controlled-phase gates act on qubits that have not yet been touched.
- Parameters:
- qcQuantumCircuit
The input circuit.
- Returns:
- QuantumCircuit
A new circuit with redundant controlled gates removed.
Examples
Cancel a CX whose control qubit is still in |0⟩:
>>> from qrisp import QuantumCircuit, PassManager >>> from qrisp import cancel_zero_controls >>> qc = QuantumCircuit(2) >>> qc.cx(0, 1) # Qubit 0 starts in \|0⟩ — the CX is a no-op >>> qc.h(1) # Now qubit 1 is marked as used >>> print(qc) qb_58: ──■─────── ┌─┴─┐┌───┐ qb_59: ┤ X ├┤ H ├ └───┘└───┘ >>> pm = PassManager() >>> pm += cancel_zero_controls >>> optimized_qc = pm.run(qc) >>> print(optimized_qc) qb_58: ───── ┌───┐ qb_59: ┤ H ├ └───┘
Symmetric controlled-phase gates (CZ, CP) cancel when either qubit is |0⟩:
>>> qc = QuantumCircuit(2) >>> qc.cz(0, 1) # both qubits start in \|0⟩ — CZ is a no-op >>> pm = PassManager() >>> pm += cancel_zero_controls >>> optimized = pm.run(qc) >>> print(optimized) qb_60: qb_61: