-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathchannel.h
More file actions
149 lines (120 loc) · 3.46 KB
/
channel.h
File metadata and controls
149 lines (120 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CHANNEL_H_
#define CHANNEL_H_
#include <set>
#include <vector>
#include "gate.h"
#include "matrix.h"
namespace qsim {
/**
* Kraus operator.
*/
template <typename Gate>
struct KrausOperator {
using fp_type = typename Gate::fp_type;
enum Kind {
kNormal = 0,
kMeasurement = gate::kMeasurement,
};
/**
* Kraus operator type;
*/
Kind kind;
/**
* If true, the Kraus operator is a unitary operator times a constant.
*/
bool unitary;
/**
* Lower bound on Kraus operator probability.
*/
double prob;
/**
* Sequence of operations that represent the Kraus operator. This can be just
* one operation.
*/
std::vector<Gate> ops;
/**
* Product of K^\dagger and K. This can be empty if unitary = true.
*/
Matrix<fp_type> kd_k;
/**
* Qubits kd_k acts on. This can be empty if unitary = true.
*/
std::vector<unsigned> qubits;
/**
* Calculates the product of "K^\dagger K". Sets qubits "K^\dagger K" acts on.
*/
void CalculateKdKMatrix() {
if (ops.size() == 1) {
kd_k = ops[0].matrix;
MatrixDaggerMultiply(ops[0].qubits.size(), ops[0].matrix, kd_k);
qubits = ops[0].qubits;
} else if (ops.size() > 1) {
std::set<unsigned> qubit_map;
for (const auto& op : ops) {
for (unsigned q : op.qubits) {
qubit_map.insert(q);
}
}
unsigned num_qubits = qubit_map.size();
qubits.resize(0);
qubits.reserve(num_qubits);
for (auto it = qubit_map.begin(); it != qubit_map.end(); ++it) {
qubits.push_back(*it);
}
MatrixIdentity(unsigned{1} << num_qubits, kd_k);
for (const auto& op : ops) {
if (op.qubits.size() == num_qubits) {
MatrixMultiply(num_qubits, op.matrix, kd_k);
} else {
unsigned mask = 0;
for (auto q : op.qubits) {
for (unsigned i = 0; i < num_qubits; ++i) {
if (q == qubits[i]) {
mask |= unsigned{1} << i;
break;
}
}
}
MatrixMultiply(mask, op.qubits.size(), op.matrix, num_qubits, kd_k);
}
}
auto m = kd_k;
MatrixDaggerMultiply(num_qubits, m, kd_k);
}
}
};
/**
* Quantum channel.
*/
template <typename Gate>
using Channel = std::vector<KrausOperator<Gate>>;
/**
* Makes a channel from the gate.
* @param time The time to place the channel at.
* @param gate The input gate.
* @return The output channel.
*/
template <typename Gate>
Channel<Gate> MakeChannelFromGate(unsigned time, const Gate& gate) {
auto normal = KrausOperator<Gate>::kNormal;
auto measurement = KrausOperator<Gate>::kMeasurement;
auto kind = gate.kind == gate::kMeasurement ? measurement : normal;
Channel<Gate> channel = {{kind, true, 1, {gate}}};
channel[0].ops[0].time = time;
return channel;
}
} // namespace qsim
#endif // CHANNEL_H_