GCC Code Coverage Report


Directory: ./
File: Circuit/include/Circuit/CircPool.hpp
Date: 2022-10-15 05:10:18
Exec Total Coverage
Lines: 0 2 0.0%
Functions: 0 1 0.0%
Branches: 0 0 -%
Decisions: 0 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 #pragma once
16
17 #include "Circuit.hpp"
18 #include "Gate/GatePtr.hpp"
19 #include "Utils/Expression.hpp"
20
21 namespace tket {
22
23 namespace CircPool {
24
25 /** Equivalent to BRIDGE, using four CX, first CX has control on qubit 0 */
26 const Circuit &BRIDGE_using_CX_0();
27
28 /** Equivalent to BRIDGE, using four CX, first CX has control on qubit 1 */
29 const Circuit &BRIDGE_using_CX_1();
30
31 /** Equivalent to CX, using a TK2 and single-qubit gates */
32 const Circuit &CX_using_TK2();
33
34 /** Equivalent to CX[0,1], using a CX[1,0] and four H gates */
35 const Circuit &CX_using_flipped_CX();
36
37 /** Equivalent to CX, using only ECR, Rx and U3 gates */
38 const Circuit &CX_using_ECR();
39
40 /** Equivalent to CX, using only ZZMax, Rx and Rz gates */
41 const Circuit &CX_using_ZZMax();
42
43 /** Equivalent to CX, using only XXPhase, Rx, Ry and Rz gates */
44 const Circuit &CX_using_XXPhase_0();
45
46 /** Equivalent to CX, using only XXPhase, Rx and Rz gates */
47 const Circuit &CX_using_XXPhase_1();
48
49 /**
50 * CX-reduced form of CX/V,S/CX
51 *
52 * --C--V--C-- --Z--S--V--X--S--\ /--
53 * | | => | \
54 * --X--S--X-- --X--V--S--C--V--/ \--
55 */
56 const Circuit &CX_VS_CX_reduced();
57
58 /**
59 * CX-reduced form of CX/V,-/CX
60 *
61 * --C--V--C-- --X--V--S--V--C--V--S--
62 * | | => |
63 * --X-----X-- --V-----------X--------
64 */
65 const Circuit &CX_V_CX_reduced();
66
67 /**
68 * CX-reduced form of CX/-,S/CX (= ZZMax)
69 *
70 * --C-----C-- --S-----------C--------
71 * | | => |
72 * --X--S--X-- --Z--S--V--S--X--S--V--
73 */
74 const Circuit &CX_S_CX_reduced();
75
76 /**
77 * CX-reduced form of CX/V,-/S,-/XC
78 *
79 * --C--V--S--X-- --S-----C--V--S--
80 * | | => |
81 * --X--------C-- --Z--S--X--S-----
82 */
83 const Circuit &CX_V_S_XC_reduced();
84
85 /**
86 * CX-reduced form of CX/-,S/-,V/XC
87 *
88 * --C--------X-- --X--V--C--V-----
89 * | | => |
90 * --X--S--V--C-- --V-----X--S--V--
91 */
92 const Circuit &CX_S_V_XC_reduced();
93
94 /**
95 * CX-reduced form of CX/XC
96 *
97 * --C--X-- --X--\ /--
98 * | | => | \
99 * --X--C-- --C--/ \--
100 */
101 const Circuit &CX_XC_reduced();
102
103 /** Equivalent to SWAP, using three CX, outer CX have control on qubit 0 */
104 const Circuit &SWAP_using_CX_0();
105
106 /** Equivalent to SWAP, using three CX, outer CX have control on qubit 1 */
107 const Circuit &SWAP_using_CX_1();
108
109 /** A two-qubit circuit with an Rz(1) on each qubit */
110 const Circuit &two_Rz1();
111
112 /** X[1]; CX[0,1] */
113 const Circuit &X1_CX();
114
115 /** Z[0]; CX[0,1] */
116 const Circuit &Z0_CX();
117
118 /**
119 * Equivalent to CCX up to phase shift, using three CX
120 *
121 * Warning: this is not equivalent to CCX up to global phase so cannot be used
122 * as a direct substitution except when the phase reversal can be cancelled. Its
123 * unitary representation is like CCX but with a -1 at the (5,5) position.
124 */
125 const Circuit &CCX_modulo_phase_shift();
126
127 /** Equivalent to CCX, using five CX */
128 const Circuit &CCX_normal_decomp();
129
130 /** Equivalent to CCCX, using 14 CX */
131 const Circuit &C3X_normal_decomp();
132
133 /** Equivalent to CCCCX, using 36 CX */
134 const Circuit &C4X_normal_decomp();
135
136 /** CX[0,1]; CX[2,0]; CCX[0,1,2] */
137 const Circuit &ladder_down();
138
139 /** CX[0,1]; X[0]; X[2]; CCX[0,1,2] */
140 const Circuit &ladder_down_2();
141
142 /** CCX[0,1,2]; CX[2,0]; CX[2,1] */
143 const Circuit &ladder_up();
144
145 /** Just an X gate */
146 const Circuit &X();
147
148 /** Just a CX[0,1] gate */
149 const Circuit &CX();
150
151 /** Just a CCX[0,1,2] gate */
152 const Circuit &CCX();
153
154 /** Just a BRIDGE[0,1,2] gate */
155 const Circuit &BRIDGE();
156
157 /** H[1]; CZ[0,1]; H[1] */
158 const Circuit &H_CZ_H();
159
160 /** Equivalent to CZ, using CX and single-qubit gates */
161 const Circuit &CZ_using_CX();
162
163 /** Equivalent to CY, using CX and single-qubit gates */
164 const Circuit &CY_using_CX();
165
166 /** Equivalent to CH, using CX and single-qubit gates */
167 const Circuit &CH_using_CX();
168
169 /** Equivalent to CV, using CX and single-qubit gates */
170 const Circuit &CV_using_CX();
171
172 /** Equivalent to CVdg, using CX and single-qubit gates */
173 const Circuit &CVdg_using_CX();
174
175 /** Equivalent to CSX, using CX and single-qubit gates */
176 const Circuit &CSX_using_CX();
177
178 /** Equivalent to CSXdg, using CX and single-qubit gates */
179 const Circuit &CSXdg_using_CX();
180
181 /** Equivalent to CSWAP, using CX and single-qubit gates */
182 const Circuit &CSWAP_using_CX();
183
184 /** Equivalent to ECR, using CX, Rx and U3 gates */
185 const Circuit &ECR_using_CX();
186
187 /** Equivalent to ZZMax, using CX, Rz and U3 gates */
188 const Circuit &ZZMax_using_CX();
189
190 /** Equivalent to CRz, using a TK2 and TK1 gates */
191 Circuit CRz_using_TK2(const Expr &alpha);
192
193 /** Equivalent to CRz, using CX and Rz gates */
194 Circuit CRz_using_CX(const Expr &alpha);
195
196 /** Equivalent to CRx, using a TK2 and TK1 gates */
197 Circuit CRx_using_TK2(const Expr &alpha);
198
199 /** Equivalent to CRx, using CX, H and Rx gates */
200 Circuit CRx_using_CX(const Expr &alpha);
201
202 /** Equivalent to CRy, using a TK2 and TK1 gates */
203 Circuit CRy_using_TK2(const Expr &alpha);
204
205 /** Equivalent to CRy, using CX and Ry gates */
206 Circuit CRy_using_CX(const Expr &alpha);
207
208 /** Equivalent to CU1, using a TK2 and TK1 gates */
209 Circuit CU1_using_TK2(const Expr &lambda);
210
211 /** Equivalent to CU1, using CX and U1 gates */
212 Circuit CU1_using_CX(const Expr &lambda);
213
214 /** Equivalent to CU1, using CX, U1 and U3 gates */
215 Circuit CU3_using_CX(const Expr &theta, const Expr &phi, const Expr &lambda);
216
217 /** Equivalent to ISWAP, using a TK2 gate */
218 Circuit ISWAP_using_TK2(const Expr &alpha);
219
220 /** Equivalent to ISWAP, using CX, U3 and Rz gates */
221 Circuit ISWAP_using_CX(const Expr &alpha);
222
223 /** Equivalent to XXPhase, using a TK2 gate */
224 Circuit XXPhase_using_TK2(const Expr &alpha);
225
226 /** Equivalent to XXPhase, using CX and U3 gates */
227 Circuit XXPhase_using_CX(const Expr &alpha);
228
229 /** Equivalent to YYPhase, using a TK2 gate */
230 Circuit YYPhase_using_TK2(const Expr &alpha);
231
232 /** Equivalent to YYPhase, using CX, Rz and U3 gates */
233 Circuit YYPhase_using_CX(const Expr &alpha);
234
235 /** Equivalent to ZZPhase, using a TK2 gate */
236 Circuit ZZPhase_using_TK2(const Expr &alpha);
237
238 /** Equivalent to ZZPhase, using CX and Rz gates */
239 Circuit ZZPhase_using_CX(const Expr &alpha);
240
241 /**
242 * @brief Equivalent to XXPhase, using ZZPhase and H gates.
243 *
244 * @param alpha The gate parameter to the XXPhase gate.
245 * @return Circuit Equivalent circuit using ZZPhase.
246 */
247 Circuit XXPhase_using_ZZPhase(const Expr &alpha);
248
249 /**
250 * @brief Equivalent to YYPhase, using ZZPhase and V/Vdg gates.
251 *
252 * @param alpha The gate parameter to the YYPhase gate.
253 * @return Circuit Equivalent circuit using ZZPhase.
254 */
255 Circuit YYPhase_using_ZZPhase(const Expr &alpha);
256
257 /**
258 * @brief Equivalent to TK2(0.5, 0, 0), using a single CX gate.
259
260 * Using 1 CX yields an approximate decomposition of the TK2 gate which is
261 * equivalent to a TK2(0.5, 0, 0) gate. This is always the optimal
262 * 1-CX approximation of any TK2 gate, with respect to the squared trace
263 * fidelity metric.
264 *
265 * @return Circuit Equivalent circuit to TK2(0.5, 0, 0).
266 */
267 Circuit approx_TK2_using_1xCX();
268
269 /**
270 * @brief Equivalent to TK2(α, β, 0), using 2 CX gates.
271 *
272 * Using 2 CX gates we can implement any gate of the form TK2(α, β, 0). This is
273 * the optimal 2-CX approximation for any TK2(α, β, γ), with respect to the
274 * squared trace fidelity metric.
275 *
276 * Requires 0.5 ≥ α ≥ β ≥ 0.
277 *
278 * @return Circuit Equivalent circuit to TK2(α, β, 0).
279 */
280 Circuit approx_TK2_using_2xCX(const Expr &alpha, const Expr &beta);
281
282 /**
283 * @brief Equivalent to TK2(α, β, γ), using 3 CX gates.
284 *
285 * This is an exact 3 CX decomposition of the TK2(α, β, γ) gate.
286 * Prefer using `normalised_TK2_using_CX` unless you wish to explicitly use 3 CX
287 * or if α, β and γ are not normalised to the Weyl chamber.
288 *
289 * @return Circuit Equivalent circuit to TK2(α, β, γ).
290 */
291 Circuit TK2_using_3xCX(const Expr &alpha, const Expr &beta, const Expr &gamma);
292
293 /**
294 * @brief Equivalent to TK2(α, β, γ) with minimal number of CX gates.
295 *
296 * A TK2-equivalent circuit with as few CX gates as possible (0, 1, 2 or 3 CX).
297
298 * Decomposition is exact. The parameters must be normalised to the Weyl
299 * chamber, i.e. it must hold 0.5 ≥ 𝛼 ≥ 𝛽 ≥ |𝛾|.
300 *
301 * In cases where hardware gate fidelities are known, it might be sensible to
302 * use TK2 decompositions that are inexact but less noisy. See DecomposeTK2
303 * pass and transform.
304 *
305 * @return Circuit Equivalent circuit to TK2(α, β, γ).
306 */
307 Circuit normalised_TK2_using_CX(
308 const Expr &alpha, const Expr &beta, const Expr &gamma);
309
310 /**
311 * @brief Equivalent to TK2(α, β, γ) with minimal number of CX gates.
312 *
313 * A TK2-equivalent circuit with as few CX gates as possible (0, 1, 2 or 3 CX).
314 *
315 * @return Circuit Equivalent circuit to TK2(α, β, γ).
316 */
317 Circuit TK2_using_CX(const Expr &alpha, const Expr &beta, const Expr &gamma);
318
319 /**
320 * @brief Equivalent to TK2(α, 0, 0), using 1 ZZPhase gate.
321 *
322 * Using 1 ZZPhase gate we can implement any gate of the form TK2(α, 0, 0).
323 * This is the optimal 1-ZZPhase approximation for any TK2(α, β, γ), with
324 * respect to the squared trace fidelity metric.
325 *
326 * Requires 0.5 ≥ α ≥ 0.
327 *
328 * @return Circuit Equivalent circuit to TK2(α, β, 0).
329 */
330 Circuit approx_TK2_using_1xZZPhase(const Expr &alpha);
331
332 /**
333 * @brief Equivalent to TK2(α, β, 0), using 2 ZZPhase gates.
334 *
335 * Using 2 ZZPhase gates we can implement any gate of the form TK2(α, β, 0).
336 * This is the optimal 2-ZZPhase approximation for any TK2(α, β, γ), with
337 * respect to the squared trace fidelity metric.
338 *
339 * Warning: in practice, we would not expect this decomposition to be attractive
340 * on real hardware, as the same approximation fidelity can be obtained using
341 * 2 ZZMax gates, which would typically have (same or) higher fidelity than
342 * variable angle ZZPhase gates.
343 *
344 * @return Circuit Equivalent circuit to TK2(α, β, 0).
345 */
346 Circuit approx_TK2_using_2xZZPhase(const Expr &alpha, const Expr &beta);
347
348 /**
349 * @brief Equivalent to TK2(α, β, γ), using 3 ZZPhase gates.
350 *
351 * This is an exact 3 ZZPhase decomposition of the TK2(α, β, γ) gate.
352 *
353 * Warning: in practice, we would not expect this decomposition to be attractive
354 * on real hardware, as the same approximation fidelity can be obtained using
355 * 3 ZZMax gates, which would typically have (same or) higher fidelity than
356 * variable angle ZZPhase gates.
357 *
358 * @return Circuit Equivalent circuit to TK2(α, β, γ).
359 */
360 Circuit TK2_using_ZZPhase(
361 const Expr &alpha, const Expr &beta, const Expr &gamma);
362
363 /**
364 * @brief Equivalent to TK2(α, β, γ), using up to 3 ZZMax gates.
365 *
366 * @return Circuit equivalent to TK2(α, β, γ).
367 */
368 Circuit TK2_using_ZZMax(const Expr &alpha, const Expr &beta, const Expr &gamma);
369
370 /** Equivalent to XXPhase3, using three TK2 gates */
371 Circuit XXPhase3_using_TK2(const Expr &alpha);
372
373 /** Equivalent to 3-qubit MS interaction, using CX and U3 gates */
374 Circuit XXPhase3_using_CX(const Expr &alpha);
375
376 /** Equivalent to ESWAP, using a TK2 and (Clifford) TK1 gates */
377 Circuit ESWAP_using_TK2(const Expr &alpha);
378
379 /** Equivalent to ESWAP, using CX, X, S, Ry and U1 gates */
380 Circuit ESWAP_using_CX(const Expr &alpha);
381
382 /** Equivalent to FSim, using a TK2 and TK1 gates */
383 Circuit FSim_using_TK2(const Expr &alpha, const Expr &beta);
384
385 /** Equivalent to FSim, using CX, X, S, U1 and U3 gates */
386 Circuit FSim_using_CX(const Expr &alpha, const Expr &beta);
387
388 /** Equivalent to PhasedISWAP, using a TK2 and Rz gates */
389 Circuit PhasedISWAP_using_TK2(const Expr &p, const Expr &t);
390
391 /** Equivalent to PhasedISWAP, using CX, U3 and Rz gates */
392 Circuit PhasedISWAP_using_CX(const Expr &p, const Expr &t);
393
394 /** Unwrap NPhasedX, into number_of_qubits PhasedX gates */
395 Circuit NPhasedX_using_PhasedX(
396 unsigned int number_of_qubits, const Expr &alpha, const Expr &beta);
397
398 /** TK2(a, b, c)-equivalent circuit, using normalised TK2 and single-qb gates */
399 Circuit TK2_using_normalised_TK2(
400 const Expr &alpha, const Expr &beta, const Expr &gamma);
401
402 // converts a TK1 gate to a PhasedXRz gate
403 Circuit tk1_to_PhasedXRz(
404 const Expr &alpha, const Expr &beta, const Expr &gamma);
405
406 Circuit tk1_to_rzrx(const Expr &alpha, const Expr &beta, const Expr &gamma);
407
408 Circuit tk1_to_rzh(const Expr &alpha, const Expr &beta, const Expr &gamma);
409
410 Circuit tk1_to_rzsx(const Expr &alpha, const Expr &beta, const Expr &gamma);
411
412 Circuit tk1_to_tk1(const Expr &alpha, const Expr &beta, const Expr &gamma);
413
414 class ControlDecompError : public std::logic_error {
415 public:
416 explicit ControlDecompError(const std::string &message)
417 : std::logic_error(message) {}
418 };
419
420 /**
421 * @brief Get an n-qubit incrementer circuit with linear depth and O(n^2) gate
422 * count. There exists a global phase difference
423 * https://arxiv.org/abs/2203.11882
424 *
425 * @param n number of qubits
426 * @param lsb set to false if we don't want to toggle the least significant bit
427 * @return Circuit containing CRx, X
428 */
429 Circuit incrementer_linear_depth(unsigned n, bool lsb = true);
430
431 /**
432 * @brief Implement CnU gate with linear depth and O(n^2) gate count.
433 * https://arxiv.org/abs/2203.11882
434 *
435 * @param n number of controls
436 * @param u the controlled 2x2 unitary matrix
437 * @return Circuit containing CRx, TK1, U1, and CU3
438 */
439 Circuit CnU_linear_depth_decomp(unsigned n, const Eigen::Matrix2cd &u);
440
441 Circuit incrementer_borrow_1_qubit(unsigned n);
442
443 Circuit incrementer_borrow_n_qubits(unsigned n);
444
445 Circuit CnX_normal_decomp(unsigned n);
446
447 Circuit CnX_gray_decomp(unsigned n);
448
449 Circuit CnRy_normal_decomp(const Op_ptr op, unsigned arity);
450
451 /**
452 * @brief Given a 2x2 numerical unitary matrix U and the number of control
453 * qubits n return the decomposed CnU gate
454 * @param n
455 * @param u
456 * @return Circuit containing CX, TK1, U1, and CU3
457 */
458 Circuit CnU_gray_code_decomp(unsigned n, const Eigen::Matrix2cd &u);
459
460 /**
461 * @brief Given a gate and the number of control qubits n,
462 * return the n-qubit controlled version of that gate using the gray code
463 * decomposition method. This method can handle gates with symbolic parameters
464 * @param n
465 * @param gate
466 * @return Circuit containing CX, CRx, CRy, CRz, CU1, TK1, U1, and CU3
467 */
468 Circuit CnU_gray_code_decomp(unsigned n, const Gate_ptr &gate);
469
470 /**
471 * @brief Linear decomposition method for n-qubit controlled SU(2) gate
472 * expressed as Rz(alpha)Ry(theta)Rz(beta) (multiplication order).
473 * Implements lemma 7.9 in https://arxiv.org/abs/quant-ph/9503016
474 * @param n
475 * @param alpha
476 * @param theta
477 * @param beta
478 * @return Circuit
479 */
480 Circuit CnSU2_linear_decomp(
481 unsigned n, const Expr &alpha, const Expr &theta, const Expr &beta);
482
483 } // namespace CircPool
484
485 } // namespace tket
486