How to use the projectq.types.WeakQubitRef 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 / cengines / _linearmapper.py View on Github external
if swaps:  # first mapping requires no swaps
            # Allocate all mapped qubit ids (which are not already allocated,
            # i.e., contained in self._currently_allocated_ids)
            mapped_ids_used = set()
            for logical_id in self._currently_allocated_ids:
                mapped_ids_used.add(self.current_mapping[logical_id])
            not_allocated_ids = set(range(self.num_qubits)).difference(
                mapped_ids_used)
            for mapped_id in not_allocated_ids:
                qb = WeakQubitRef(engine=self, idx=mapped_id)
                cmd = Command(engine=self, gate=Allocate, qubits=([qb],))
                self.send([cmd])
            # Send swap operations to arrive at new_mapping:
            for qubit_id0, qubit_id1 in swaps:
                q0 = WeakQubitRef(engine=self, idx=qubit_id0)
                q1 = WeakQubitRef(engine=self, idx=qubit_id1)
                cmd = Command(engine=self, gate=Swap, qubits=([q0], [q1]))
                self.send([cmd])
            # Register statistics:
            self.num_mappings += 1
            depth = return_swap_depth(swaps)
            if depth not in self.depth_of_swaps:
                self.depth_of_swaps[depth] = 1
            else:
                self.depth_of_swaps[depth] += 1
            if len(swaps) not in self.num_of_swaps_per_mapping:
                self.num_of_swaps_per_mapping[len(swaps)] = 1
            else:
                self.num_of_swaps_per_mapping[len(swaps)] += 1
            # Deallocate all previously mapped ids which we only needed for the
            # swaps:
            mapped_ids_used = set()
github ProjectQ-Framework / ProjectQ / projectq / ops / _command.py View on Github external
def control_qubits(self, qubits):
        """
        Set control_qubits to qubits

        Args:
            control_qubits (Qureg): quantum register
        """
        self._control_qubits = ([WeakQubitRef(qubit.engine, qubit.id)
                                 for qubit in qubits])
        self._control_qubits = sorted(self._control_qubits, key=lambda x: x.id)
github ProjectQ-Framework / ProjectQ / projectq / backends / _sim / _simulator.py View on Github external
def _convert_logical_to_mapped_qureg(self, qureg):
        """
        Converts a qureg from logical to mapped qubits if there is a mapper.

        Args:
            qureg (list[Qubit],Qureg): Logical quantum bits
        """
        mapper = self.main_engine.mapper
        if mapper is not None:
            mapped_qureg = []
            for qubit in qureg:
                if qubit.id not in mapper.current_mapping:
                    raise RuntimeError("Unknown qubit id. "
                                       "Please make sure you have called "
                                       "eng.flush().")
                new_qubit = WeakQubitRef(qubit.engine,
                                         mapper.current_mapping[qubit.id])
                mapped_qureg.append(new_qubit)
            return mapped_qureg
        else:
            return qureg
github quantumlib / OpenFermion-ProjectQ / openfermionprojectq / _unitary_cc.py View on Github external
qubit_graph.nodes[pair[0]].value),
                             projectq.types.
                             WeakQubitRef(engine,
                                          qubit_graph.nodes[pair[1]].value))

    # Perform original gate - assumes use of standard ProjectQ rule set that
    # only ends with 2-qubit gates with controls. Swaps decomposed to CX
    projectq.ops.C(gate) | (projectq.types.
                            WeakQubitRef(engine,
                                         qubit_graph.
                                         nodes[graph_path[-2]].value),
                            total_qubits[1])

    # Reverse the swaps to put qubits back in place
    for pair in reversed(swap_path):
        projectq.ops.Swap | (projectq.types.
                             WeakQubitRef(engine,
                                          qubit_graph.nodes[pair[0]].value),
                             projectq.types.
                             WeakQubitRef(engine,
                                          qubit_graph.nodes[pair[1]].value))
github ProjectQ-Framework / ProjectQ / projectq / cengines / _linearmapper.py View on Github external
old_mapping=self.current_mapping, new_mapping=new_mapping)
        if swaps:  # first mapping requires no swaps
            # Allocate all mapped qubit ids (which are not already allocated,
            # i.e., contained in self._currently_allocated_ids)
            mapped_ids_used = set()
            for logical_id in self._currently_allocated_ids:
                mapped_ids_used.add(self.current_mapping[logical_id])
            not_allocated_ids = set(range(self.num_qubits)).difference(
                mapped_ids_used)
            for mapped_id in not_allocated_ids:
                qb = WeakQubitRef(engine=self, idx=mapped_id)
                cmd = Command(engine=self, gate=Allocate, qubits=([qb],))
                self.send([cmd])
            # Send swap operations to arrive at new_mapping:
            for qubit_id0, qubit_id1 in swaps:
                q0 = WeakQubitRef(engine=self, idx=qubit_id0)
                q1 = WeakQubitRef(engine=self, idx=qubit_id1)
                cmd = Command(engine=self, gate=Swap, qubits=([q0], [q1]))
                self.send([cmd])
            # Register statistics:
            self.num_mappings += 1
            depth = return_swap_depth(swaps)
            if depth not in self.depth_of_swaps:
                self.depth_of_swaps[depth] = 1
            else:
                self.depth_of_swaps[depth] += 1
            if len(swaps) not in self.num_of_swaps_per_mapping:
                self.num_of_swaps_per_mapping[len(swaps)] = 1
            else:
                self.num_of_swaps_per_mapping[len(swaps)] += 1
            # Deallocate all previously mapped ids which we only needed for the
            # swaps: