示例#1
0
 def test_compose_method(self, num_qubits):
     """Test compose method"""
     samples = 10
     num_gates = 10
     seed = 600
     gates = 'all'
     for i in range(samples):
         circ1 = random_clifford_circuit(num_qubits,
                                         num_gates,
                                         gates=gates,
                                         seed=seed + i)
         circ2 = random_clifford_circuit(num_qubits,
                                         num_gates,
                                         gates=gates,
                                         seed=seed + samples + i)
         cliff1 = Clifford(circ1)
         cliff2 = Clifford(circ2)
         value = cliff1.compose(cliff2)
         target = Clifford(circ1.extend(circ2))
         self.assertEqual(target, value)
示例#2
0
    def run(self, dag):
        """Run the OptimizeCliffords pass on `dag`.

        Args:
            dag (DAGCircuit): the DAG to be optimized.

        Returns:
            DAGCircuit: the optimized DAG.
        """

        blocks = []
        prev_node = None
        cur_block = []

        # Iterate over all nodes and collect consecutive Cliffords over the
        # same qubits. In this very first proof-of-concept implementation
        # we require the same ordering of qubits, but this restriction will
        # be shortly removed. An interesting question is whether we may also
        # want to compose Cliffords over different sets of qubits, such as
        # cliff1 over qubits [1, 2, 3] and cliff2 over [2, 3, 4].
        for node in dag.topological_op_nodes():
            if isinstance(node.op, Clifford):
                if prev_node is None:
                    blocks.append(cur_block)
                    cur_block = [node]
                else:
                    if prev_node.qargs == node.qargs:
                        cur_block.append(node)
                    else:
                        blocks.append(cur_block)
                        cur_block = [node]

                prev_node = node

            else:
                # not a clifford
                if cur_block:
                    blocks.append(cur_block)
                prev_node = None
                cur_block = []

        if cur_block:
            blocks.append(cur_block)

        # Replace every discovered block of cliffords by a single clifford
        # based on the Cliffords' compose function.
        for cur_nodes in blocks:
            # Create clifford functions only out of blocks with at least 2 gates
            if len(cur_nodes) <= 1:
                continue

            wire_pos_map = dict(
                (qb, ix) for ix, qb in enumerate(cur_nodes[0].qargs))

            # Construct a linear circuit
            cliff = cur_nodes[0].op
            for i, node in enumerate(cur_nodes):
                if i > 0:
                    cliff = Clifford.compose(node.op, cliff, front=True)

            # Replace the block by the composed clifford
            dag.replace_block_with_op(cur_nodes,
                                      cliff,
                                      wire_pos_map,
                                      cycle_check=False)

        return dag