def test_largest_sequence_search_traverses_grid(): q00 = cirq.GridQubit(0, 0) q10 = cirq.GridQubit(1, 0) q11 = cirq.GridQubit(1, 1) q20 = cirq.GridQubit(2, 0) q30 = cirq.GridQubit(3, 0) q40 = cirq.GridQubit(4, 0) q41 = cirq.GridQubit(4, 1) q42 = cirq.GridQubit(4, 2) q50 = cirq.GridQubit(5, 0) qubits = [q00, q10, q11, q20, q30, q40, q50, q41, q42] device = _create_device(qubits) search = greedy._PickLargestArea(device, q20) # + + +-+-+ + # | # + + # | # + assert search._choose_next_qubit(q20, {q20}) == q30 assert search._choose_next_qubit(q30, {q20, q30}) == q40 assert search._choose_next_qubit(q40, {q20, q30, q40}) == q41 assert search._choose_next_qubit(q41, {q20, q30, q40, q41}) == q42 assert search._choose_next_qubit(q42, {q20, q30, q40, q41, q42}) is None method = greedy.GreedySequenceSearchStrategy('largest_area') assert method.place_line(device, 7) == GridQubitLineTuple( [q00, q10, q20, q30, q40, q41, q42]) with pytest.raises(NotFoundError): _ = method.place_line(device, 8)
def test_greedy_search_method_returns_longest(): q00 = cirq.GridQubit(0, 0) q10 = cirq.GridQubit(1, 0) device = _create_device([q00, q10]) length = 1 method = greedy.GreedySequenceSearchStrategy() assert method.place_line(device, length) == GridQubitLineTuple([q00])
def test_line_placement_str(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) placement = GridQubitLineTuple([q00, q01, q02]) assert str(placement).strip() == """ (0, 0)━━(0, 1)━━(0, 2) """.strip()
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_line_placement_to_str(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) q10 = cirq.GridQubit(1, 0) q11 = cirq.GridQubit(1, 1) placement = GridQubitLineTuple([q02, q01, q00, q10, q11]) assert str(placement).strip() == """ (0, 0)━━(0, 1)━━(0, 2) ┃ (1, 0)━━(1, 1) """.strip()
def test_greedy_search_method_calls_minimal_only(minimal, largest): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) device = _create_device([q00, q01]) length = 2 sequence = [q00, q01] minimal.return_value.get_or_search.return_value = sequence method = greedy.GreedySequenceSearchStrategy('minimal_connectivity') assert method.place_line(device, length) == GridQubitLineTuple(sequence) largest.return_value.get_or_search.assert_not_called() minimal.return_value.get_or_search.assert_called_once_with()
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)
def test_greedy_search_method_returns_empty_when_empty(): device = _create_device([]) length = 0 method = greedy.GreedySequenceSearchStrategy() assert method.place_line(device, length) == GridQubitLineTuple()
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)