Source code for qrisp.circuit.pass_management.passes.cancel_zero_controls
# ********************************************************************************
# * Copyright (c) 2026 the Qrisp authors
# *
# * This program and the accompanying materials are made available under the
# * terms of the Eclipse Public License 2.0 which is available at
# * http://www.eclipse.org/legal/epl-2.0.
# *
# * This Source Code may also be made available under the following Secondary
# * Licenses when the conditions for such availability set forth in the Eclipse
# * Public License, v. 2.0 are satisfied: GNU General Public License, version 2
# * with the GNU Classpath Exception which is
# * available at https://www.gnu.org/software/classpath/license.html.
# *
# * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
# ********************************************************************************
"""Implements the cancel_zero_controls pass, removing controlled gates guaranteed to act on a zero state."""
from __future__ import annotations
from qrisp.circuit.operation import (
ControlledOperation,
Operation,
PTControlledOperation,
)
from qrisp.circuit.pass_management.circuit_pass import CircuitPass
from qrisp.circuit.quantum_circuit import QuantumCircuit
from qrisp.circuit.qubit import Qubit
# Single-qubit diagonal gates that map \|0ā© ā e^{iĻ}\|0ā©
_DIAGONAL_1Q = frozenset({"p", "rz", "z", "s", "t", "s_dg", "t_dg", "id", "gphase"})
def _is_cancelled_by_zero(op: Operation, qubits: list[Qubit], fresh: set[Qubit]) -> bool:
r"""Return True if *op* on *qubits* is a no-op given a set of \|0ā© qubits."""
# Symmetric controlled-phase gates: diag(1,1,1,e^{iĻ}).
# Identity whenever *either* qubit is \|0ā©.
if op.name in ("cp", "cz"):
return any(qb in fresh for qb in qubits)
# PTControlledOperation wrapping a phase gate on 2 qubits (ā” CP)
if isinstance(op, PTControlledOperation) and op.base_operation.name == "p" and op.num_qubits == 2:
return any(qb in fresh for qb in qubits)
# General controlled operations: cancel when a control-on-\|1ā© is still \|0ā©.
if isinstance(op, (ControlledOperation, PTControlledOperation)):
n_ctrl = len(op.controls)
ctrl_state = getattr(op, "ctrl_state", "1" * n_ctrl)
if isinstance(ctrl_state, int):
ctrl_state = bin(ctrl_state)[2:].zfill(n_ctrl)
for i in range(n_ctrl):
if qubits[i] in fresh and ctrl_state[i] == "1":
return True
return False
[docs]
@CircuitPass
def cancel_zero_controls(qc: QuantumCircuit) -> QuantumCircuit:
r"""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
----------
qc : QuantumCircuit
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)
<BLANKLINE>
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)
<BLANKLINE>
qb_60:
<BLANKLINE>
qb_61:
<BLANKLINE>
"""
fresh = set(qc.qubits) # all qubits start in \|0ā©
qc_new = qc.clearcopy()
for instr in qc.data:
op = instr.op
qubits = instr.qubits
if op.name == "qb_alloc":
fresh.add(qubits[0])
qc_new.append(instr)
continue
if op.name == "qb_dealloc":
fresh.discard(qubits[0])
qc_new.append(instr)
continue
if op.name == "barrier":
qc_new.append(instr)
continue
# Check whether the gate is a no-op given the fresh set.
if _is_cancelled_by_zero(op, qubits, fresh):
continue
# Gate is kept ā append and update freshness.
qc_new.append(instr)
# Single-qubit diagonal gates preserve \|0ā©.
if len(qubits) == 1 and op.name in _DIAGONAL_1Q:
continue
# Everything else: conservatively drop freshness.
for qb in qubits:
fresh.discard(qb)
return qc_new