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 "Utils/GraphHeaders.hpp" | |||
16 | #include "ZX/ZXDiagram.hpp" | |||
17 | ||||
18 | namespace tket { | |||
19 | ||||
20 | namespace zx { | |||
21 | ||||
22 | ✗ | WireProperties::WireProperties() {} | ||
23 | ||||
24 | 1121 | WireProperties::WireProperties( | ||
25 | ZXWireType _type, QuantumType _qtype, std::optional<unsigned> _source_port, | |||
26 | 1121 | std::optional<unsigned> _target_port) | ||
27 | 1121 | : type(_type), | ||
28 | 1121 | qtype(_qtype), | ||
29 | 1121 | source_port(_source_port), | ||
30 | 1121 | target_port(_target_port) {} | ||
31 | ||||
32 |
2/4✓ Branch 3 taken 146 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 146 times.
✗ Branch 7 not taken.
|
146 | ZXDiagram::ZXDiagram() : scalar(1.) { graph = std::make_unique<ZXGraph>(); } | |
33 | ||||
34 | 58 | ZXDiagram::ZXDiagram( | ||
35 | 58 | unsigned in, unsigned out, unsigned classical_in, unsigned classical_out) | ||
36 | 58 | : ZXDiagram() { | ||
37 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 58 times.
|
2/2✓ Decision 'true' taken 58 times.
✓ Decision 'false' taken 58 times.
|
116 | for (unsigned i = 0; i < in; ++i) { |
38 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | ZXVert iv = add_vertex(ZXType::Input, QuantumType::Quantum); | |
39 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | boundary.push_back(iv); | |
40 | } | |||
41 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 58 times.
|
2/2✓ Decision 'true' taken 66 times.
✓ Decision 'false' taken 58 times.
|
124 | for (unsigned o = 0; o < out; ++o) { |
42 |
1/2✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
|
66 | ZXVert ov = add_vertex(ZXType::Output, QuantumType::Quantum); | |
43 |
1/2✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
|
66 | boundary.push_back(ov); | |
44 | } | |||
45 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 58 times.
|
2/2✓ Decision 'true' taken 6 times.
✓ Decision 'false' taken 58 times.
|
64 | for (unsigned i = 0; i < classical_in; ++i) { |
46 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | ZXVert iv = add_vertex(ZXType::Input, QuantumType::Classical); | |
47 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | boundary.push_back(iv); | |
48 | } | |||
49 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 58 times.
|
2/2✓ Decision 'true' taken 13 times.
✓ Decision 'false' taken 58 times.
|
71 | for (unsigned o = 0; o < classical_out; ++o) { |
50 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | ZXVert ov = add_vertex(ZXType::Output, QuantumType::Classical); | |
51 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | boundary.push_back(ov); | |
52 | } | |||
53 | 58 | } | ||
54 | ||||
55 | 24 | ZXDiagram::ZXDiagram(const ZXDiagram& other) : ZXDiagram() { | ||
56 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | this->copy_graph(other, true); | |
57 | 24 | } | ||
58 | ||||
59 | 30 | ZXDiagram::ZXDiagram(ZXDiagram&& other) | ||
60 | 30 | : graph(std::move(other.graph)), | ||
61 | 30 | boundary(std::move(other.boundary)), | ||
62 | 30 | scalar(std::move(other.scalar)) {} | ||
63 | ||||
64 | 1 | ZXDiagram& ZXDiagram::operator=(const ZXDiagram& other) { | ||
65 | 1 | this->graph->clear(); | ||
66 | 1 | this->boundary.clear(); | ||
67 | 1 | this->scalar = 1.; | ||
68 | ||||
69 | 1 | this->copy_graph(other, true); | ||
70 | ||||
71 | 1 | return *this; | ||
72 | } | |||
73 | ||||
74 | 30 | ZXDiagram& ZXDiagram::operator=(ZXDiagram&& other) { | ||
75 | 30 | this->graph = std::move(other.graph); | ||
76 | 30 | this->boundary = std::move(other.boundary); | ||
77 | 30 | this->scalar = std::move(other.scalar); | ||
78 | ||||
79 | 30 | return *this; | ||
80 | } | |||
81 | ||||
82 | 52 | std::pair<std::map<ZXVert, ZXVert>, std::map<Wire, Wire>> ZXDiagram::copy_graph( | ||
83 | const ZXDiagram& other, bool merge_boundaries) { | |||
84 | // The isomorphism from vertices of `other` to vertices of `this` | |||
85 | 52 | std::map<ZXVert, ZXVert> iso; | ||
86 | 52 | std::map<Wire, Wire> wiso; | ||
87 |
7/8✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 567 times.
✓ Branch 7 taken 52 times.
✓ Branch 9 taken 567 times.
✓ Branch 10 taken 52 times.
✓ Branch 12 taken 52 times.
✓ Branch 13 taken 52 times.
|
671 | BGL_FORALL_VERTICES(v, *other.graph, ZXGraph) { | |
88 |
1/2✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
|
567 | ZXGen_ptr gen = other.get_vertex_ZXGen_ptr(v); | |
89 |
1/2✓ Branch 2 taken 567 times.
✗ Branch 3 not taken.
|
567 | ZXVert new_v = this->add_vertex(gen); | |
90 |
1/2✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
|
567 | iso.emplace(v, new_v); | |
91 | 567 | } | ||
92 | ||||
93 |
12/18✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 49 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 583 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 632 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 583 times.
✓ Branch 14 taken 49 times.
✓ Branch 16 taken 583 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 583 times.
✓ Branch 19 taken 49 times.
✓ Branch 21 taken 101 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 49 times.
✓ Branch 24 taken 52 times.
|
684 | BGL_FORALL_EDGES(w, *other.graph, ZXGraph) { | |
94 |
1/2✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
|
583 | WireProperties wp = other.get_wire_info(w); | |
95 |
1/2✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
|
583 | ZXVert source = other.source(w); | |
96 |
1/2✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
|
583 | ZXVert target = other.target(w); | |
97 |
3/6✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 583 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 583 times.
✗ Branch 8 not taken.
|
583 | Wire new_w = this->add_wire(iso.at(source), iso.at(target), wp); | |
98 |
1/2✓ Branch 1 taken 583 times.
✗ Branch 2 not taken.
|
583 | wiso.emplace(w, new_w); | |
99 | } | |||
100 | ||||
101 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 27 times.
|
0/1? Decision couldn't be analyzed.
|
52 | if (merge_boundaries) { |
102 |
2/2✓ Branch 5 taken 81 times.
✓ Branch 6 taken 25 times.
|
2/2✓ Decision 'true' taken 81 times.
✓ Decision 'false' taken 25 times.
|
106 | for (const ZXVert& b : other.boundary) { |
103 |
2/4✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 81 times.
✗ Branch 5 not taken.
|
81 | this->boundary.push_back(iso.at(b)); | |
104 | } | |||
105 | } | |||
106 | ||||
107 |
2/4✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
|
52 | this->multiply_scalar(other.get_scalar()); | |
108 | ||||
109 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
104 | return {iso, wiso}; | |
110 | 52 | } | ||
111 | ||||
112 | } // namespace zx | |||
113 | ||||
114 | } // namespace tket | |||
115 |