Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
1 | // Copyright 2019-2022 Cambridge Quantum Computing | |||
2 | // | |||
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |||
4 | // you may not use this file except in compliance with the License. | |||
5 | // You may obtain a copy of the License at | |||
6 | // | |||
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |||
8 | // | |||
9 | // Unless required by applicable law or agreed to in writing, software | |||
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |||
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
12 | // See the License for the specific language governing permissions and | |||
13 | // limitations under the License. | |||
14 | ||||
15 | #include "NeighboursFromArchitecture.hpp" | |||
16 | ||||
17 | #include <algorithm> | |||
18 | #include <sstream> | |||
19 | #include <stdexcept> | |||
20 | ||||
21 | namespace tket { | |||
22 | ||||
23 | 2523 | NeighboursFromArchitecture::NeighboursFromArchitecture( | ||
24 | 2523 | const ArchitectureMapping& arch_mapping) | ||
25 | 2523 | : m_arch_mapping(arch_mapping) {} | ||
26 | ||||
27 | 1442785 | const std::vector<size_t>& NeighboursFromArchitecture::operator()( | ||
28 | size_t vertex) { | |||
29 |
1/2✓ Branch 1 taken 1442785 times.
✗ Branch 2 not taken.
|
1442785 | const auto num_vertices = m_arch_mapping.number_of_vertices(); | |
30 | // GCOVR_EXCL_START | |||
31 | TKET_ASSERT( | |||
32 | vertex < num_vertices || | |||
33 | AssertMessage() << "get_neighbours: invalid vertex " << vertex | |||
34 | << " (only have " << num_vertices << " vertices)"); | |||
35 | // GCOVR_EXCL_STOP | |||
36 |
1/2✓ Branch 1 taken 1442785 times.
✗ Branch 2 not taken.
|
1442785 | auto& neighbours = m_cached_neighbours[vertex]; | |
37 |
2/2✓ Branch 1 taken 1390361 times.
✓ Branch 2 taken 52424 times.
|
2/2✓ Decision 'true' taken 1390361 times.
✓ Decision 'false' taken 52424 times.
|
1442785 | if (!neighbours.empty()) { |
38 | // Already cached. | |||
39 | 1390361 | return neighbours; | ||
40 | } | |||
41 | ||||
42 | // OK, if a vertex is isolated (has no neighbours) then this is wasteful; | |||
43 | // however this case should almost never occur in practice. | |||
44 | ||||
45 |
1/2✓ Branch 1 taken 52424 times.
✗ Branch 2 not taken.
|
52424 | const auto& source_node = m_arch_mapping.get_node(vertex); | |
46 | const auto neighbour_nodes = | |||
47 |
2/4✓ Branch 1 taken 52424 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 52424 times.
✗ Branch 5 not taken.
|
52424 | m_arch_mapping.get_architecture().get_neighbour_nodes(source_node); | |
48 | ||||
49 |
1/2✓ Branch 2 taken 52424 times.
✗ Branch 3 not taken.
|
52424 | neighbours.reserve(neighbour_nodes.size()); | |
50 | ||||
51 |
2/2✓ Branch 5 taken 140733 times.
✓ Branch 6 taken 52424 times.
|
2/2✓ Decision 'true' taken 140733 times.
✓ Decision 'false' taken 52424 times.
|
193157 | for (const Node& node : neighbour_nodes) { |
52 |
1/2✓ Branch 1 taken 140733 times.
✗ Branch 2 not taken.
|
140733 | const auto neighbour_vertex = m_arch_mapping.get_vertex(node); | |
53 | // GCOVR_EXCL_START | |||
54 | TKET_ASSERT( | |||
55 | neighbour_vertex != vertex || | |||
56 | AssertMessage() | |||
57 | << "get_neighbours: vertex " << vertex << " for node " | |||
58 | << node.repr() << " has " << neighbour_nodes.size() | |||
59 | << " neighbours, and lists itself as a neighbour (loops not " | |||
60 | "allowed)"); | |||
61 | // GCOVR_EXCL_STOP | |||
62 |
1/2✓ Branch 1 taken 140733 times.
✗ Branch 2 not taken.
|
140733 | neighbours.push_back(neighbour_vertex); | |
63 | } | |||
64 |
1/2✓ Branch 3 taken 52424 times.
✗ Branch 4 not taken.
|
52424 | std::sort(neighbours.begin(), neighbours.end()); | |
65 | 52424 | return neighbours; | ||
66 | 52424 | } | ||
67 | ||||
68 | } // namespace tket | |||
69 |