Esempio n. 1
0
    def place_line(self, device: 'cirq.google.XmonDevice', length: int) -> GridQubitLineTuple:
        """Runs line sequence search.

        Args:
            device: Chip description.
            length: Required line length.

        Returns:
            List of linear sequences on the chip found by simulated annealing
            method.
        """
        seqs = AnnealSequenceSearch(device, self.seed).search(self.trace_func)
        return GridQubitLineTuple.best_of(seqs, length)
Esempio n. 2
0
    def place_line(self,
                   device: 'cirq.google.XmonDevice',
                   length: int) -> GridQubitLineTuple:
        """Runs line sequence search.

        Args:
            device: Chip description.
            length: Required line length.

        Returns:
            Linear sequences found on the chip.

        Raises:
            ValueError: If search algorithm passed on initialization is not
                        recognized.
        """

        if not device.qubits:
            return GridQubitLineTuple()

        start = min(device.qubits)  # type: GridQubit
        sequences = []  # type: List[LineSequence]
        greedy_search = {
            'minimal_connectivity': [
                _PickFewestNeighbors(device, start),
            ],
            'largest_area': [
                _PickLargestArea(device, start),
            ],
            'best': [
                _PickFewestNeighbors(device, start),
                _PickLargestArea(device, start),
            ]
        }  # type: Dict[str, List[GreedySequenceSearch]]

        algos = greedy_search.get(self.algorithm)
        if algos is None:
            raise ValueError(
                "Unknown greedy search algorithm %s" % self.algorithm)

        for algorithm in algos:
            sequences.append(algorithm.get_or_search())

        return GridQubitLineTuple.best_of(sequences, length)
def test_best_of_gets_longest_needs_minimum():
    q00 = cirq.GridQubit(0, 0)
    q01 = cirq.GridQubit(0, 1)

    assert GridQubitLineTuple.best_of([[]], 0) == ()
    assert GridQubitLineTuple.best_of([[], [q00]], 0) == ()
    assert GridQubitLineTuple.best_of([[q00], []], 0) == ()
    assert GridQubitLineTuple.best_of([[], [q00]], 1) == (q00,)
    assert GridQubitLineTuple.best_of([[q00], []], 1) == (q00,)
    assert GridQubitLineTuple.best_of([[q00, q01], [q00]], 1) == (q00,)
    assert GridQubitLineTuple.best_of([[q00, q01], [q00]], 2) == (q00, q01)
    assert GridQubitLineTuple.best_of([[q00, q01]], 2) == (q00, q01)

    assert GridQubitLineTuple.best_of([], 0) == ()
    with pytest.raises(NotFoundError):
        _ = GridQubitLineTuple.best_of([[]], 1)
    with pytest.raises(NotFoundError):
        _ = GridQubitLineTuple.best_of([[q00]], 2)