forked from Quantinuum/tket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableau.cpp
More file actions
277 lines (271 loc) · 12.1 KB
/
Copy pathtableau.cpp
File metadata and controls
277 lines (271 loc) · 12.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2019-2024 Cambridge Quantum Computing
//
// 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
//
// http://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.
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <sstream>
#include "tket/Converters/UnitaryTableauBox.hpp"
#include "typecast.hpp"
namespace py = pybind11;
namespace tket {
typedef py::tket_custom::SequenceVec<Qubit> py_qubit_vector_t;
PYBIND11_MODULE(tableau, m) {
py::class_<UnitaryTableau>(
m, "UnitaryTableau",
"Stabilizer tableau for a unitary in the style of Aaronson&Gottesman "
"\"Improved Simulation of Stabilizer Circuits\": rows indicate the "
"action at the output corresponding to either an X or a Z on a single "
"input.")
.def(
py::init<unsigned>(),
"Constructs a :py:class:`UnitaryTableau` representing the identity "
"operation over some number of qubits. Qubits will be indexed "
"sequentially in the default register."
"\n\n:param nqb: The number of qubits in the unitary.",
py::arg("nqb"))
.def(
py::init<
const MatrixXb&, const MatrixXb&, const VectorXb&,
const MatrixXb&, const MatrixXb&, const VectorXb&>(),
"Constructs a :py:class:`UnitaryTableau` from the binary tables of "
"its components."
"\n\n:param xx: The X component of the X rows."
"\n:param xz: The Z component of the X rows."
"\n:param xph: The phases of the X rows."
"\n:param zx: The X component of the Z rows."
"\n:param zz: The Z component of the Z rows."
"\n:param zph: The phases of the Z rows.",
py::arg("xx"), py::arg("xz"), py::arg("xph"), py::arg("zx"),
py::arg("zz"), py::arg("zph"))
.def(
py::init<>([](const Circuit& circ) {
return circuit_to_unitary_tableau(circ);
}),
"Constructs a :py:class:`UnitaryTableau` from a unitary "
":py:class:`Circuit`. Throws an exception if the input contains "
"non-unitary operations."
"\n\n:param circ: The unitary circuit to convert to a tableau.")
.def(
"__repr__",
[](const UnitaryTableau& tab) {
std::stringstream str;
str << tab;
return str.str();
})
.def(
"get_xrow",
[](const UnitaryTableau& tab, const Qubit& qb) {
return SpCxPauliTensor(tab.get_xrow(qb));
},
"Read off an X row as a Pauli string."
"\n\n:param qb: The qubits whose X row to read off."
"\n:return: The Pauli string :math:`P` such that :math:`PU=UX_{qb}`.",
py::arg("qb"))
.def(
"get_zrow",
[](const UnitaryTableau& tab, const Qubit& qb) {
return SpCxPauliTensor(tab.get_zrow(qb));
},
"Read off an Z row as a Pauli string."
"\n\n:param qb: The qubits whose Z row to read off."
"\n:return: The Pauli string :math:`P` such that :math:`PU=UZ_{qb}`.",
py::arg("qb"))
.def(
"get_row_product",
[](const UnitaryTableau& tab, const SpCxPauliTensor& paulis) {
SpCxPauliTensor res =
tab.get_row_product(SpPauliStabiliser(paulis.string));
res.coeff *= paulis.coeff;
return res;
},
"Combine rows to yield the effect of a given Pauli string."
"\n\n:param paulis: The Pauli string :math:`P` to consider at the "
"input."
"\n:return: The Pauli string :math:`Q` such that :math:`QU=UP`.",
py::arg("paulis"))
.def(
"apply_gate_at_front",
[](UnitaryTableau& self, const OpType& type,
const py_qubit_vector_t& qbs) {
return self.apply_gate_at_front(type, qbs);
},
"Update the tableau according to adding a Clifford gate before the "
"current unitary, i.e. updates :math:`U` to :math:`UG` for a gate "
":math:`G`."
"\n\n:param type: The :py:class:`OpType` of the gate to add. Must be "
"an unparameterised Clifford gate type."
"\n:param qbs: The qubits to apply the gate to. Length must match "
"the arity of the given gate type.",
py::arg("type"), py::arg("qbs"))
.def(
"apply_gate_at_end",
[](UnitaryTableau& self, const OpType& type,
const py_qubit_vector_t& qbs) {
return self.apply_gate_at_end(type, qbs);
},
"Update the tableau according to adding a Clifford gate after the "
"current unitary, i.e. updates :math:`U` to :math:`GU` for a gate "
":math:`G`."
"\n\n:param type: The :py:class:`OpType` of the gate to add. Must be "
"an unparameterised Clifford gate type."
"\n:param qbs: The qubits to apply the gate to. Length must match "
"the arity of the given gate type.",
py::arg("type"), py::arg("qbs"))
.def(
"to_circuit", &unitary_tableau_to_circuit,
"Synthesises a unitary :py:class:`Circuit` realising the same "
"unitary as the tableau. Uses the method from Aaronson & Gottesman: "
"\"Improved Simulation of Stabilizer Circuits\", Theorem 8. This is "
"not optimised for gate count, so is not recommended for "
"performance-sensitive usage.");
py::class_<UnitaryRevTableau>(
m, "UnitaryRevTableau",
"Equivalent to the UnitaryTableau, except that the rows indicate the "
"action at the input corresponding to either an X or a Z on a single "
"output.")
.def(
py::init<unsigned>(),
"Constructs a :py:class:`UnitaryRevTableau` representing the "
"identity "
"operation over some number of qubits. Qubits will be indexed "
"sequentially in the default register."
"\n\n:param nqb: The number of qubits in the unitary.",
py::arg("nqb"))
.def(
py::init<
const MatrixXb&, const MatrixXb&, const VectorXb&,
const MatrixXb&, const MatrixXb&, const VectorXb&>(),
"Constructs a :py:class:`UnitaryRevTableau` from the binary tables "
"of "
"its components."
"\n\n:param xx: The X component of the X rows."
"\n:param xz: The Z component of the X rows."
"\n:param xph: The phases of the X rows."
"\n:param zx: The X component of the Z rows."
"\n:param zz: The Z component of the Z rows."
"\n:param zph: The phases of the Z rows.",
py::arg("xx"), py::arg("xz"), py::arg("xph"), py::arg("zx"),
py::arg("zz"), py::arg("zph"))
.def(
py::init<>([](const Circuit& circ) {
return circuit_to_unitary_rev_tableau(circ);
}),
"Constructs a :py:class:`UnitaryRevTableau` from a unitary "
":py:class:`Circuit`. Throws an exception if the input contains "
"non-unitary operations."
"\n\n:param circ: The unitary circuit to convert to a tableau.")
.def(
"__repr__",
[](const UnitaryRevTableau& tab) {
std::stringstream str;
str << tab;
return str.str();
})
.def(
"get_xrow",
[](const UnitaryRevTableau& tab, const Qubit& qb) {
return SpCxPauliTensor(tab.get_xrow(qb));
},
"Read off an X row as a Pauli string."
"\n\n:param qb: The qubits whose X row to read off."
"\n:return: The Pauli string :math:`P` such that :math:`UP=X_{qb}U`.",
py::arg("qb"))
.def(
"get_zrow",
[](const UnitaryRevTableau& tab, const Qubit& qb) {
return SpCxPauliTensor(tab.get_zrow(qb));
},
"Read off an Z row as a Pauli string."
"\n\n:param qb: The qubits whose Z row to read off."
"\n:return: The Pauli string :math:`P` such that :math:`UP=Z_{qb}U`.",
py::arg("qb"))
.def(
"get_row_product",
[](const UnitaryRevTableau& tab, const SpCxPauliTensor& paulis) {
SpCxPauliTensor res =
tab.get_row_product(SpPauliStabiliser(paulis.string));
res.coeff *= paulis.coeff;
return res;
},
"Combine rows to yield the effect of a given Pauli string."
"\n\n:param paulis: The Pauli string :math:`P` to consider at the "
"output."
"\n:return: The Pauli string :math:`Q` such that :math:`UQ=PU`.",
py::arg("paulis"))
.def(
"apply_gate_at_front",
[](UnitaryRevTableau& self, const OpType& type,
const py_qubit_vector_t& qbs) {
return self.apply_gate_at_front(type, qbs);
},
"Update the tableau according to adding a Clifford gate before the "
"current unitary, i.e. updates :math:`U` to :math:`UG` for a gate "
":math:`G`."
"\n\n:param type: The :py:class:`OpType` of the gate to add. Must be "
"an unparameterised Clifford gate type."
"\n:param qbs: The qubits to apply the gate to. Length must match "
"the arity of the given gate type.",
py::arg("type"), py::arg("qbs"))
.def(
"apply_gate_at_end",
[](UnitaryRevTableau& self, const OpType& type,
const py_qubit_vector_t& qbs) {
return self.apply_gate_at_end(type, qbs);
},
"Update the tableau according to adding a Clifford gate after the "
"current unitary, i.e. updates :math:`U` to :math:`GU` for a gate "
":math:`G`."
"\n\n:param type: The :py:class:`OpType` of the gate to add. Must be "
"an unparameterised Clifford gate type."
"\n:param qbs: The qubits to apply the gate to. Length must match "
"the arity of the given gate type.",
py::arg("type"), py::arg("qbs"))
.def(
"to_circuit", &unitary_rev_tableau_to_circuit,
"Synthesises a unitary :py:class:`Circuit` realising the same "
"unitary as the tableau. Uses the method from Aaronson & Gottesman: "
"\"Improved Simulation of Stabilizer Circuits\", Theorem 8. This is "
"not optimised for gate count, so is not recommended for "
"performance-sensitive usage.");
py::class_<UnitaryTableauBox, std::shared_ptr<UnitaryTableauBox>, Op>(
m, "UnitaryTableauBox",
"A Clifford unitary specified by its actions on Paulis.")
.def(
py::init<const UnitaryTableau&>(),
"Construct from a given tableau.\n\n"
":param tab: The :py:class:`UnitaryTableau` representing the desired "
"unitary.",
py::arg("tab"))
.def(
py::init<
const MatrixXb&, const MatrixXb&, const VectorXb&,
const MatrixXb&, const MatrixXb&, const VectorXb&>(),
"Construct the tableau from the binary tables of its components."
"\n\n:param xx: The X component of the X rows."
"\n:param xz: The Z component of the X rows."
"\n:param xph: The phases of the X rows."
"\n:param zx: The X component of the Z rows."
"\n:param zz: The Z component of the Z rows."
"\n:param zph: The phases of the Z rows.",
py::arg("xx"), py::arg("xz"), py::arg("xph"), py::arg("zx"),
py::arg("zz"), py::arg("zph"))
.def(
"get_circuit",
[](UnitaryTableauBox& ubox) { return *ubox.to_circuit(); },
":return: The :py:class:`Circuit` described by the box.")
.def(
"get_tableau", &UnitaryTableauBox::get_tableau,
":return: The tableau representing the unitary operation.");
}
} // namespace tket