old_contents: # Copyright 2018 The Cirq Developers
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     https://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """An optimizer that expands composite operations via `cirq.decompose`."""
  
  from typing import Callable
  
  from cirq import ops, protocols
  from cirq.circuits.optimization_pass import (
      PointOptimizer,
      PointOptimizationSummary,
  )
  
  
  class ExpandComposite(PointOptimizer):
      """An optimizer that expands composite operations via `cirq.decompose`.
  
      For each operation in the circuit, this pass examines if the operation can
      be decomposed. If it can be, the operation is cleared out and and replaced
      with its decomposition using a fixed insertion strategy.
      """
  
      def __init__(self,
                   no_decomp: Callable[[ops.Operation], bool]=(lambda _: False)
                   ) -> None:
          """Construct the optimization pass.
  
          Args:
              no_decomp: A predicate that determines whether an operation should
                  be decomposed or not. Defaults to decomposing everything.
          """
          super().__init__()
          self.no_decomp = no_decomp
  
      def optimization_at(self, circuit, index, op):
          decomposition = self._decompose(op)
          if decomposition is op:
              return None
  
          return PointOptimizationSummary(
              clear_span=1,
              clear_qubits=op.qubits,
              new_operations=decomposition)
  
      def _decompose(self, op: ops.Operation) -> ops.OP_TREE:
          """Recursively decompose composite gates into an OP_TREE of gates."""
          skip = self.no_decomp(op)
          if skip and (skip is not NotImplemented):
              return op
  
          decomposed = protocols.decompose_once(op, None)
          if decomposed is None:
              return op
          return (self._decompose(op) for op in decomposed)
  
new_contents: # Copyright 2018 The Cirq Developers
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     https://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """An optimizer that expands composite operations via `cirq.decompose`."""
  
  from typing import Callable
  
  from cirq import ops, protocols
  from cirq.circuits.optimization_pass import (
      PointOptimizer,
      PointOptimizationSummary,
  )
  
  
  class ExpandComposite(PointOptimizer):
      """An optimizer that expands composite operations via `cirq.decompose`.
  
      For each operation in the circuit, this pass examines if the operation can
      be decomposed. If it can be, the operation is cleared out and and replaced
      with its decomposition using a fixed insertion strategy.
      """
  
      def __init__(self,
                   no_decomp: Callable[[ops.Operation], bool]=(lambda _: False)
                   ) -> None:
          """Construct the optimization pass.
  
          Args:
              no_decomp: A predicate that determines whether an operation should
                  be decomposed or not. Defaults to decomposing everything.
          """
          super().__init__()
          self.no_decomp = no_decomp
  
      def optimization_at(self, circuit, index, op):
          decomposition = protocols.decompose(op, keep=self.no_decomp,
                                              on_stuck_raise=None)
          if decomposition is op:
              return None
  
          return PointOptimizationSummary(
              clear_span=1,
              clear_qubits=op.qubits,
              new_operations=decomposition)
subject: Replace decompose_once with decompose in expand_composite (#1069)