GCC Code Coverage Report


Directory: ./
File: Transformations/MeasurePass.cpp
Date: 2022-10-15 05:10:18
Exec Total Coverage
Lines: 45 45 100.0%
Functions: 2 2 100.0%
Branches: 65 102 63.7%
Decisions: 10 10 100.0%

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 "MeasurePass.hpp"
16
17 #include "Circuit/DAGDefs.hpp"
18 #include "Transform.hpp"
19
20 namespace tket {
21
22 namespace Transforms {
23
24 1 Transform delay_measures() {
25 11 return Transform([](Circuit &circ) {
26 11 bool success = false;
27
7/8
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 242 times.
✓ Branch 6 taken 8 times.
✓ Branch 8 taken 242 times.
✓ Branch 9 taken 8 times.
✓ Branch 11 taken 11 times.
✓ Branch 12 taken 8 times.
258 BGL_FORALL_VERTICES(v, circ.dag, DAG) {
28
3/4
✓ Branch 1 taken 242 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 230 times.
2/2
✓ Decision 'true' taken 12 times.
✓ Decision 'false' taken 230 times.
242 if (circ.get_OpType_from_Vertex(v) == OpType::Measure) {
29
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 Edge c_out_edge = circ.get_nth_out_edge(v, 1);
30
6/8
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 10 times.
2/2
✓ Decision 'true' taken 2 times.
✓ Decision 'false' taken 21 times.
23 if (!circ.detect_final_Op(circ.target(c_out_edge)) ||
31
3/4
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 10 times.
11 circ.n_out_edges_of_type(v, EdgeType::Boolean) != 0) {
32
2/4
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
2 throw CircuitInvalidity(
33 "Cannot commute Measure through classical operations to the end "
34 4 "of the circuit");
35 }
36
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 Edge out_edge = circ.get_nth_out_edge(v, 0);
37 10 Edge current_edge = out_edge;
38
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 Vertex current_vertex = circ.target(current_edge);
39
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 port_t current_port = circ.get_target_port(current_edge);
40
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 OpType current_optype = circ.get_OpType_from_Vertex(current_vertex);
41 10 while (
42
5/6
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
19 !is_final_q_type(current_optype) &&
43 4 (current_optype == OpType::SWAP ||
44
3/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 times.
4 circ.commutes_with_basis(
45
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 10 times.
19 current_vertex, Pauli::Z, PortType::Target, current_port))) {
46
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
2/2
✓ Decision 'true' taken 2 times.
✓ Decision 'false' taken 3 times.
5 if (current_optype == OpType::SWAP) {
47 // Update from SWAP
48 current_edge =
49
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 circ.get_nth_out_edge(current_vertex, 1 - current_port);
50 } else {
51 // Update to successor
52
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 current_edge = circ.get_nth_out_edge(current_vertex, current_port);
53 }
54
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 current_vertex = circ.target(current_edge);
55
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 current_port = circ.get_target_port(current_edge);
56
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 current_optype = circ.get_OpType_from_Vertex(current_vertex);
57 ;
58 }
59 // If we haven't moved it to an output, we can't continue
60
3/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 9 times.
2/2
✓ Decision 'true' taken 1 times.
✓ Decision 'false' taken 9 times.
10 if (!is_final_q_type(current_optype)) {
61
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 throw CircuitInvalidity(
62 "Cannot commute Measure through quantum gates to the end of the "
63 2 "circuit");
64 }
65 // If the measure was already at an output, nothing to do
66
3/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3 times.
2/2
✓ Decision 'true' taken 3 times.
✓ Decision 'false' taken 6 times.
9 if (current_edge == out_edge) continue;
67
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 Edge in_edge = circ.get_nth_in_edge(v, 0);
68 // Rewire measure
69
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 circ.add_edge(
70
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 {circ.source(in_edge), circ.get_source_port(in_edge)},
71
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 {circ.target(out_edge), circ.get_target_port(out_edge)},
72
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 EdgeType::Quantum);
73
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 circ.remove_edge(in_edge);
74
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 circ.remove_edge(out_edge);
75
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 circ.add_edge(
76
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 {circ.source(current_edge), circ.get_source_port(current_edge)},
77 3 {v, 0}, EdgeType::Quantum);
78
1/2
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 circ.add_edge({v, 0}, {current_vertex, 0}, EdgeType::Quantum);
79
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 circ.remove_edge(current_edge);
80 3 success = true;
81 }
82 }
83 8 return success;
84
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 });
85 }
86
87 } // namespace Transforms
88
89 } // namespace tket
90