示例#1
0
    def is_available(self, cmd):
        """
        Test whether a Command is supported by a compiler engine.

        Specialized implementation of is_available: The simulator can deal with all arbitrarily-controlled gates which
        provide a gate-matrix (via gate.matrix) and acts on 5 or less qubits (not counting the control qubits).

        Args:
            cmd (Command): Command for which to check availability (single- qubit gate, arbitrary controls)

        Returns:
            True if it can be simulated and False otherwise.
        """
        if has_negative_control(cmd):
            return False

        if (
            cmd.gate == Measure
            or cmd.gate == Allocate
            or cmd.gate == Deallocate
            or isinstance(cmd.gate, (BasicMathGate, TimeEvolution))
        ):
            return True
        try:
            matrix = cmd.gate.matrix
            # Allow up to 5-qubit gates
            if len(matrix) > 2 ** 5:
                return False
            return True
        except AttributeError:
            return False
示例#2
0
    def is_available(self, cmd):
        """
        Test if this backend is available to process the provided command.

        Args:
            cmd (Command): A command to process.

        Returns:
            bool: If this backend can process the command.
        """
        gate = cmd.gate

        # Metagates.
        if gate in (Measure, Allocate, Deallocate, Barrier):
            return True

        if has_negative_control(cmd):
            return False

        # CNOT gates.
        # NOTE: IonQ supports up to 7 control qubits
        num_ctrl_qubits = get_control_count(cmd)
        if 0 < num_ctrl_qubits <= 7:
            return isinstance(gate, (XGate, ))

        # Gates witout control bits.
        if num_ctrl_qubits == 0:
            supported = isinstance(gate, SUPPORTED_GATES)
            supported_transpose = gate in (Sdag, Tdag)
            return supported or supported_transpose
        return False
示例#3
0
    def is_available(self, cmd):
        """
        Test whether a Command is supported by a compiler engine.

        Specialized implementation of is_available: The unitary simulator can deal with all arbitrarily-controlled gates
        which provide a gate-matrix (via gate.matrix).

        Args:
            cmd (Command): Command for which to check availability (single- qubit gate, arbitrary controls)

        Returns:
            True if it can be simulated and False otherwise.
        """
        if has_negative_control(cmd):
            return False

        if isinstance(cmd.gate,
                      (AllocateQubitGate, DeallocateQubitGate, MeasureGate)):
            return True

        try:
            gate_mat = cmd.gate.matrix
            if len(gate_mat) > 2**6:
                warnings.warn(
                    "Potentially large matrix gate encountered! ({} qubits)".
                    format(math.log2(len(gate_mat))))
            return True
        except AttributeError:
            return False
示例#4
0
def test_controlstate_priority():
    saving_backend = DummyEngine(save_commands=True)
    rule_set = DecompositionRuleSet(modules=[cnot2cz, controlstate])
    eng = MainEngine(
        backend=saving_backend,
        engine_list=[AutoReplacer(rule_set),
                     InstructionFilter(filter_func)])
    qubit1 = eng.allocate_qubit()
    qubit2 = eng.allocate_qubit()
    qubit3 = eng.allocate_qubit()
    with Control(eng, qubit2, ctrl_state='0'):
        X | qubit1
    with Control(eng, qubit3, ctrl_state='1'):
        X | qubit1
    eng.flush()

    assert len(saving_backend.received_commands) == 8
    for cmd in saving_backend.received_commands:
        assert not has_negative_control(cmd)
示例#5
0
    def is_available(self, cmd):
        """
        Return true if the command can be executed.

        The IBM quantum chip can only do U1,U2,U3,barriers, and CX / CNOT.
        Conversion implemented for Rotation gates and H gates.

        Args:
            cmd (Command): Command for which to check availability
        """
        if has_negative_control(cmd):
            return False

        gate = cmd.gate

        if get_control_count(cmd) == 1:
            return gate == NOT
        if get_control_count(cmd) == 0:
            return gate == H or isinstance(
                gate, (Rx, Ry, Rz)) or gate in (Measure, Allocate, Deallocate,
                                                Barrier)
        return False
示例#6
0
def filter_func(eng, cmd):
    if has_negative_control(cmd):
        return False
    return True
示例#7
0
    def is_available(self, cmd):  # pylint: disable=too-many-return-statements,too-many-branches
        """
        Return true if the command can be executed.

        Depending on the device chosen, the operations available differ.

        The operations avialable for the Aspen-8 Rigetti device are:
        - "cz" = Control Z, "xy" = Not available in ProjectQ, "ccnot" = Toffoli (ie. controlled CNOT), "cnot" =
          Control X, "cphaseshift" = Control R, "cphaseshift00" "cphaseshift01" "cphaseshift10" = Not available
          in ProjectQ,
          "cswap" = Control Swap, "h" = H, "i" = Identity, not in ProjectQ, "iswap" = Not available in ProjectQ,
          "phaseshift" = R, "pswap" = Not available in ProjectQ, "rx" = Rx, "ry" = Ry, "rz" = Rz, "s" = S, "si" =
          Sdag, "swap" = Swap, "t" = T, "ti" = Tdag, "x" = X, "y" = Y, "z" = Z

        The operations available for the IonQ Device are:
        - "x" = X, "y" = Y, "z" = Z, "rx" = Rx, "ry" = Ry, "rz" = Rz, "h", H, "cnot" = Control X, "s" = S, "si" =
          Sdag, "t" = T, "ti" = Tdag, "v" = SqrtX, "vi" = Not available in ProjectQ, "xx" "yy" "zz" = Not available in
          ProjectQ, "swap" = Swap, "i" = Identity, not in ProjectQ

        The operations available for the StateVector simulator (SV1) are the union of the ones for Rigetti Aspen-8 and
        IonQ Device plus some more:
        - "cy" = Control Y, "unitary" = Arbitrary unitary gate defined as a matrix equivalent to the MatrixGate in
          ProjectQ, "xy" = Not available in ProjectQ

        Args:
            cmd (Command): Command for which to check availability
        """
        gate = cmd.gate
        if gate in (Measure, Allocate, Deallocate, Barrier):
            return True

        if has_negative_control(cmd):
            return False

        if self.device == 'Aspen-8':
            if get_control_count(cmd) == 2:
                return isinstance(gate, XGate)
            if get_control_count(cmd) == 1:
                return isinstance(gate, (R, ZGate, XGate, SwapGate))
            if get_control_count(cmd) == 0:
                return isinstance(
                    gate,
                    (
                        R,
                        Rx,
                        Ry,
                        Rz,
                        XGate,
                        YGate,
                        ZGate,
                        HGate,
                        SGate,
                        TGate,
                        SwapGate,
                    ),
                ) or gate in (Sdag, Tdag)

        if self.device == 'IonQ Device':
            if get_control_count(cmd) == 1:
                return isinstance(gate, XGate)
            if get_control_count(cmd) == 0:
                return isinstance(
                    gate,
                    (
                        Rx,
                        Ry,
                        Rz,
                        XGate,
                        YGate,
                        ZGate,
                        HGate,
                        SGate,
                        TGate,
                        SqrtXGate,
                        SwapGate,
                    ),
                ) or gate in (Sdag, Tdag)

        if self.device == 'SV1':
            if get_control_count(cmd) == 2:
                return isinstance(gate, XGate)
            if get_control_count(cmd) == 1:
                return isinstance(gate, (R, ZGate, YGate, XGate, SwapGate))
            if get_control_count(cmd) == 0:
                # TODO: add MatrixGate to cover the unitary operation
                # TODO: Missing XY gate in ProjectQ
                return isinstance(
                    gate,
                    (
                        R,
                        Rx,
                        Ry,
                        Rz,
                        XGate,
                        YGate,
                        ZGate,
                        HGate,
                        SGate,
                        TGate,
                        SqrtXGate,
                        SwapGate,
                    ),
                ) or gate in (Sdag, Tdag)
        return False