How to use the projectq.meta.get_control_count function in projectq

To help you get started, we’ve selected a few projectq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / arb1qubit2rzandry.py View on Github external
def _recognize_arb1qubit(cmd):
    """ 
    Recognize an arbitrary one qubit gate which has a matrix property.

    It does not allow gates which have control qubits as otherwise the
    AutoReplacer might go into an infinite loop. Use
    carb1qubit2cnotrzandry instead.
    """
    try:
        m = cmd.gate.matrix
        if len(m) == 2 and get_control_count(cmd) == 0:
            return True
        else:
            return False
    except:
        return False
github QuTech-Delft / quantuminspire / src / quantuminspire / projectq / backend_qx.py View on Github external
if self.main_engine.mapper is None:
                logical_id = cmd.qubits[0][0].id  # no mapping
            assert logical_id is not None
            self._measured_ids += [logical_id]
            # do not add the measurement statement when fsp is possible
            if not self._full_state_projection:
                if self._is_simulation_backend:
                    self.qasm += f"\nmeasure q[{sim_qubit_id}]"
            return

        # when we find a gate after measurements we don't have fsp
        # add any delayed measurement statements
        if self._full_state_projection and len(self._measured_ids) != 0:
            self._switch_fsp_to_nonfsp()

        if gate == NOT and get_control_count(cmd) == 1:
            # this case also covers the CX controlled gate
            ctrl_pos = self._physical_to_simulated(cmd.control_qubits[0].id)
            qb_pos = self._physical_to_simulated(cmd.qubits[0][0].id)
            self.qasm += f"\ncnot q[{ctrl_pos}], q[{qb_pos}]"
        elif gate == Swap:
            q0 = self._physical_to_simulated(cmd.qubits[0][0].id)
            q1 = self._physical_to_simulated(cmd.qubits[1][0].id)
            self.qasm += f"\nswap q[{q0}], q[{q1}]"
        elif gate == X and get_control_count(cmd) == 2:
            ctrl_pos1 = self._physical_to_simulated(cmd.control_qubits[0].id)
            ctrl_pos2 = self._physical_to_simulated(cmd.control_qubits[1].id)
            qb_pos = self._physical_to_simulated(cmd.qubits[0][0].id)
            self.qasm += f"\ntoffoli q[{ctrl_pos1}], q[{ctrl_pos2}], q[{qb_pos}]"
        elif gate == Z and get_control_count(cmd) == 1:
            ctrl_pos = self._physical_to_simulated(cmd.control_qubits[0].id)
            qb_pos = self._physical_to_simulated(cmd.qubits[0][0].id)
github ProjectQ-Framework / ProjectQ / projectq / backends / _ibm / _ibm.py View on Github external
def is_available(self, cmd):
        """
        Return true if the command can be executed.

        The IBM quantum chip can do X, Y, Z, T, Tdag, S, Sdag,
        rotation gates, barriers, and CX / CNOT.

        Args:
            cmd (Command): Command for which to check availability
        """
        g = cmd.gate
        if g == NOT and get_control_count(cmd) <= 1:
            return True
        if get_control_count(cmd) == 0:
            if g in (T, Tdag, S, Sdag, H, Y, Z):
                return True
            if isinstance(g, (Rx, Ry, Rz)):
                return True
        if g in (Measure, Allocate, Deallocate, Barrier):
            return True
        return False
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / cnu2toffoliandcu.py View on Github external
def _recognize_CnU(cmd):
    """
    Recognize an arbitrary gate which has n>=2 control qubits, except a
    Toffoli gate.
    """
    if get_control_count(cmd) == 2:
        if not isinstance(cmd.gate, XGate):
            return True
    elif get_control_count(cmd) > 2:
        return True
    return False
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / cnu2toffoliandcu.py View on Github external
def _decompose_CnU(cmd):
    """
    Decompose a multi-controlled gate U with n control qubits into a single-
    controlled U.

    It uses (n-1) work qubits and 2 * (n-1) Toffoli gates for general U
    and (n-2) work qubits and 2n - 3 Toffoli gates if U is an X-gate.
    """
    eng = cmd.engine
    qubits = cmd.qubits
    ctrl_qureg = cmd.control_qubits
    gate = cmd.gate
    n = get_control_count(cmd)

    # specialized for X-gate
    if gate == XGate() and n > 2:
        n -= 1
    ancilla_qureg = eng.allocate_qureg(n-1)

    with Compute(eng):
        Toffoli | (ctrl_qureg[0], ctrl_qureg[1], ancilla_qureg[0])
        for ctrl_index in range(2, n):
            Toffoli | (ctrl_qureg[ctrl_index], ancilla_qureg[ctrl_index-2],
                       ancilla_qureg[ctrl_index-1])
    ctrls = [ancilla_qureg[-1]]

    # specialized for X-gate
    if gate == XGate() and get_control_count(cmd) > 2:
        ctrls += [ctrl_qureg[-1]]
github ProjectQ-Framework / ProjectQ / projectq / backends / _printer.py View on Github external
def _print_cmd(self, cmd):
        """
        Print a command or, if the command is a measurement instruction and
        the CommandPrinter is the last engine in the engine pipeline: Query
        the user for the measurement result (if accept_input = True) / Set
        the result to 0 (if it's False).

        Args:
            cmd (Command): Command to print.
        """
        if self.is_last_engine and cmd.gate == Measure:
            assert(get_control_count(cmd) == 0)
            print(cmd)
            for qureg in cmd.qubits:
                for qubit in qureg:
                    if self._accept_input:
                        m = None
                        while m != '0' and m != '1' and m != 1 and m != 0:
                            prompt = ("Input measurement result (0 or 1) for"
                                      " qubit " + str(qubit) + ": ")
                            m = input(prompt)
                    else:
                        m = self._default_measure
                    m = int(m)
                    # Check there was a mapper and redirect result
                    logical_id_tag = None
                    for tag in cmd.tags:
                        if isinstance(tag, LogicalQubitIDTag):
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / globalphase.py View on Github external
def _recognize_PhNoCtrl(cmd):
    """ Recognize global phases (no controls). """
    return get_control_count(cmd) == 0
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / qubitop2onequbit.py View on Github external
def _recognize_qubitop(cmd):
    """ For efficiency only use this if at most 1 control qubit."""
    return get_control_count(cmd) <= 1
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / crz2cxandrz.py View on Github external
def _recognize_CRz(cmd):
    """ Recognize the controlled Rz gate. """
    return get_control_count(cmd) >= 1