def compile_controlled_rotation(gate: RotationGateImpl, angles: list = None) -> QCircuit: """ Recompilation of a controlled-rotation gate Basis change into Rz then recompilation of controled Rz, then change basis back :param gate: The rotational gate :param angles: new angles to set, given as a list of two. If None the angle in the gate is used (default) :return: set of gates wrapped in QCircuit class """ if not gate.is_controlled(): return QCircuit.wrap_gate(gate) if not isinstance(gate, RotationGateImpl): return QCircuit.wrap_gate(gate) if angles is None: angles = [gate.parameter / 2, -gate.parameter / 2] if len(gate.target) > 1: return compile_controlled_rotation(gate=compile_multitarget(gate=gate), angles=angles) target = gate.target control = gate.control result = QCircuit() result += change_basis(target=target, axis=gate._axis) result += RotationGateImpl(axis="z", target=target, angle=angles[0]) result += QGateImpl(name="X", target=target, control=control) result += RotationGateImpl(axis="Z", target=target, angle=angles[1]) result += QGateImpl(name="X", target=target, control=control) result += change_basis(target=target, axis=gate._axis, daggered=True) result.n_qubits = result.max_qubit() + 1 return result
def compile_ch(gate: QGateImpl) -> QCircuit: """ Compile CH gates into its equivalent: CH = Ry(0.25pi) CZ Ry(-0.25pi) Parameters ---------- gate: the gate. Returns ------- QCircuit, the result of compilation. """ if gate.name.lower() == "h" and gate.is_controlled(): return Ry(target=gate.target, control=None, angle=-numpy.pi / 4) \ + Z(target=gate.target, control=gate.control, power=gate.power if gate.is_parametrized() else None) \ + Ry(target=gate.target, control=None, angle=numpy.pi / 4) else: return QCircuit.wrap_gate(gate)
def _initialize_power_gate(name: str, target: typing.Union[list, int], control: typing.Union[list, int] = None, power=None) -> QCircuit: if power is None or power in [1, 1.0]: return QCircuit.wrap_gate( QGateImpl(name=name, target=target, control=control)) else: return QCircuit.wrap_gate( PowerGateImpl(name=name, power=power, target=target, control=control))
def QGate(name, target: typing.Union[list, int], control: typing.Union[list, int] = None): return QGateImpl(name=name, target=target, control=control)