def test_find_path_between_does_not_find_path(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) q10 = cirq.GridQubit(1, 0) q20 = cirq.GridQubit(2, 0) q22 = cirq.GridQubit(2, 2) q12 = cirq.GridQubit(1, 2) qubits = [q00, q01] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._find_path_between(q00, q01, {q00, q01}) is None qubits = [q00, q01, q10] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._find_path_between(q00, q01, {q00, q01}) is None # + + + # | # + # | # + + + qubits = [q00, q01, q02, q10, q20, q22, q12] path_1 = [q00, q01, q02] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._find_path_between(q00, q02, set(path_1)) is None
def test_find_path_between_finds_path(): 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) q12 = cirq.GridQubit(1, 2) q20 = cirq.GridQubit(2, 0) q21 = cirq.GridQubit(2, 1) q22 = cirq.GridQubit(2, 2) qubits = [q00, q01, q10, q11] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._find_path_between(q00, q01, {q00, q01}) == [q10, q11] # path1: + + + path2: +-+-+ # | | # + + + + # | | # + + + +-+-+ qubits = [q00, q01, q02, q10, q20, q21, q22, q12] path_1 = [q00, q01, q02] path_2 = [q00, q10, q20, q21, q22, q12, q02] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._find_path_between(q00, q02, set(path_1)) == path_2[1:-1] assert search._find_path_between(q02, q00, set(path_1)) == path_2[-2:0:-1] assert search._find_path_between(q00, q02, set(path_2)) == path_1[1:-1] assert search._find_path_between(q02, q00, set(path_2)) == path_1[-2:0:-1]
def test_search_sequence_assembles_sequence(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) qubits = [q00, q01, q02] search = greedy.GreedySequenceSearch(_create_device(qubits), q01) with mock.patch.object(search, '_choose_next_qubit') as choose_next_qubit: choose_next_qubit.side_effect = [q01, q02, None] assert search._sequence_search(q00, []) == [q00, q01, q02]
def test_get_or_search_calls_find_sequence_once(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) search = greedy.GreedySequenceSearch(_create_device([q00, q01]), q00) with mock.patch.object(search, '_find_sequence') as find_sequence: sequence = [q00, q01] find_sequence.return_value = sequence assert search.get_or_search() == sequence find_sequence.assert_called_once_with() assert search.get_or_search() == sequence find_sequence.assert_called_once_with()
def test_find_sequence_assembles_head_and_tail(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) qubits = [q00, q01, q02] start = q01 search = greedy.GreedySequenceSearch(_create_device(qubits), start) with mock.patch.object(search, '_sequence_search') as sequence_search: head = [q01, q00] tail = [q01, q02] sequence_search.side_effect = [tail, head] assert search._find_sequence() == qubits sequence_search.assert_has_calls( [mock.call(start, []), mock.call(start, tail)])
def test_search_sequence_calls_choose_next_qubit(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) qubits = [q00, q01, q02] search = greedy.GreedySequenceSearch(_create_device(qubits), q01) with mock.patch.object(search, '_choose_next_qubit') as choose_next_qubit: choose_next_qubit.return_value = None search._sequence_search(q01, []) choose_next_qubit.assert_called_once_with(q01, {q01}) with mock.patch.object(search, '_choose_next_qubit') as choose_next_qubit: choose_next_qubit.return_value = None search._sequence_search(q01, [q00]) choose_next_qubit.assert_called_once_with(q01, {q00, q01})
def test_find_sequence_calls_expand_sequence(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) qubits = [q00, q01, q02] start = q01 search = greedy.GreedySequenceSearch(_create_device(qubits), start) with mock.patch.object( search, '_sequence_search') as sequence_search, mock.patch.object( search, '_expand_sequence') as expand_sequence: head = [q01, q00] tail = [q01, q02] sequence_search.side_effect = [tail, head] search._find_sequence() expand_sequence.assert_called_once_with(qubits)
def test_greedy_sequence_search_fails_on_wrong_start_qubit(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) with pytest.raises(ValueError): greedy.GreedySequenceSearch(_create_device([q00]), q01)
def test_expand_sequence_expands_sequence(): q00 = cirq.GridQubit(0, 0) q01 = cirq.GridQubit(0, 1) q02 = cirq.GridQubit(0, 2) q03 = cirq.GridQubit(0, 3) q04 = cirq.GridQubit(0, 4) q10 = cirq.GridQubit(1, 0) q11 = cirq.GridQubit(1, 1) q12 = cirq.GridQubit(1, 2) q13 = cirq.GridQubit(1, 3) q14 = cirq.GridQubit(1, 4) # + + -> +-+ # | | # + + +-+ qubits = [q00, q01, q10, q11] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._expand_sequence([q00, q01]) == [q00, q10, q11, q01] # + + -> +-+ # | | # + + +-+ # | | # + + qubits = [q00, q01, q02, q10, q11] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._expand_sequence([q00, q01, q02]) == [q00, q10, q11, q01, q02] # + -> + # | | # + + +-+ # | | # + + +-+ qubits = [q00, q01, q02, q11, q12] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._expand_sequence([q00, q01, q02]) == [q00, q01, q11, q12, q02] # + -> + # | | # + + +-+ # | | # + + +-+ # | | # + + qubits = [q00, q01, q02, q03, q11, q12] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._expand_sequence([q00, q01, q02, q03]) == [q00, q01, q11, q12, q02, q03] # + + -> +-+ # | | # + + +-+ # | | # + + # | | # + + +-+ # | | # + + +-+ qubits = [q00, q01, q02, q03, q04, q10, q11, q13, q14] start = q00 search = greedy.GreedySequenceSearch(_create_device(qubits), start) assert search._expand_sequence( [q00, q01, q02, q03, q04]) == [q00, q10, q11, q01, q02, q03, q13, q14, q04]