text: # -*- coding: utf-8 -*-
  
  # This code is part of Qiskit.
  #
  # (C) Copyright IBM 2017.
  #
  # This code is licensed under the Apache License, Version 2.0. You may
  # obtain a copy of this license in the LICENSE.txt file in the root directory
  # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
  #
  # Any modifications or derivative works of this code must retain this
  # copyright notice, and modified files need to carry a notice indicating
  # that they have been altered from the originals.
  
  """
  controlled-rz gate.
  """
  from qiskit.circuit import CompositeGate
  from qiskit.circuit import Gate
  from qiskit.circuit import QuantumCircuit
  from qiskit.circuit import QuantumRegister
  from qiskit.extensions.standard.u1 import U1Gate
  from qiskit.extensions.standard.cx import CnotGate
  
  
  class CrzGate(Gate):
      """controlled-rz gate."""
  
      def __init__(self, theta):
          """Create new crz gate."""
          super().__init__("crz", 2, [theta])
  
      def _define(self):
          """
          gate crz(lambda) a,b
          { u1(lambda/2) b; cx a,b;
            u1(-lambda/2) b; cx a,b;
          }
          """
          definition = []
          q = QuantumRegister(2, "q")
          rule = [
              (U1Gate(self.params[0] / 2), [q[1]], []),
              (CnotGate(), [q[0], q[1]], []),
              (U1Gate(-self.params[0] / 2), [q[1]], []),
              (CnotGate(), [q[0], q[1]], [])
          ]
          for inst in rule:
              definition.append(inst)
          self.definition = definition
  
      def inverse(self):
          """Invert this gate."""
          return CrzGate(-self.params[0])
  
  
  def crz(self, theta, ctl, tgt):
      """Apply crz from ctl to tgt with angle theta."""
      return self.append(CrzGate(theta), [ctl, tgt], [])
  
  
  QuantumCircuit.crz = crz
  CompositeGate.crz = crz
  