cirq.transformer

Decorator to verify API and append logging functionality to transformer functions & classes.

Used in the notebooks

Used in the tutorials

A transformer is a callable that takes as inputs a cirq.AbstractCircuit and cirq.TransformerContext, and returns another cirq.AbstractCircuit without modifying the input circuit. A transformer could be a function, for example:

@cirq.transformer
def convert_to_cz(
   circuit: cirq.AbstractCircuit, *, context: 'Optional[cirq.TransformerContext]' = None
) -> cirq.Circuit:
   ...

Or it could be a class that implements __call__ with the same API, for example:

@cirq.transformer
class ConvertToSqrtISwaps:
   def __init__(self):
       ...
   def __call__(
       self,
       circuit: cirq.AbstractCircuit,
       *,
       context: 'Optional[cirq.TransformerContext]' = None,
   ) -> cirq.Circuit:
       ...

Note that transformers which take additional parameters as **kwargs, with default values specified for each keyword argument, are also supported. For example:

@cirq.transformer
def convert_to_sqrt_iswap(
    circuit: cirq.AbstractCircuit,
    *,
    context: 'Optional[cirq.TransformerContext]' = None,
    atol: float = 1e-8,
    sqrt_iswap_gate: cirq.ISwapPowGate = cirq.SQRT_ISWAP_INV,
    cleanup_operations: bool = True,
) -> cirq.Circuit:
    pass

cls_or_func The callable class or function to be decorated.
add_deep_support If True, the decorator adds the logic to first apply the decorated transformer on subcircuits wrapped inside cirq.CircuitOperations before applying it on the top-level circuit, if context.deep is True.

Decorated class / function which includes additional logging boilerplate.