Skip to content

decompose ¤

Some common decomposition methods for two-qubit and three-qubit gates.

Classes:

Name Description
ThreeQubitGateDecompose

A transpiler pass that decomposes three-qubit gates into combinations of single- and two-qubit gates.

Functions:

Name Description
u_dot_u

Carry out u @ u and return a new u information

h2u

Convert H gate to U3 gate tuple.

x2u

Convert X gate to U3 gate tuple.

sdg2u

Convert sdg gate to U3 gate tuple.

s2u

Convert S gate to U3 gate tuple.

rx2u

Convert RX gate to U3 gate tuple.

ry2u

Convert RY gate to U3 gate tuple.

rz2u

Convert RZ gate to U3 gate tuple.

cz_decompose

Decompose CZ gate to U3 gates and CZ gates.

cx_decompose

Decompose CX gate to U3 gates and CZ gates.

cy_decompose

Decompose CY gate to U3 gates and CZ gates.

swap_decompose

Decompose SWAP gate to U3 gates and CZ gates.

iswap_decompose

Decompose iswap gate with qiskit decompose algorithm.

rxx_decompose

Decompose RXX gate to U3 gates and CZ gates.

ryy_decompose

Decompose RYY gate to U3 gates and CZ gates.

rzz_decompose

Decompose RZZ gate to U3 gates and CZ gates.

cp_decompose

Decompose CPhase gate to U3 gates and CZ gates. ref: Quantum Sci. Technol. 7 (2022) 025021

ccx_decompose

Decompose ccx gate. Reference: A biological sequence comparison algorithm using quantum computers

cswap_decompose

Decompose cswap gate. Reference: http://threeplusone.com/gates

ccz_decompose

Decompose ccz gate. Reference: http://threeplusone.com/gates

ccx_decompose_mute_phase

Decompose ccx gate. it will lose some phase.

ThreeQubitGateDecompose() ¤

Bases: TranspilerPass

A transpiler pass that decomposes three-qubit gates into combinations of single- and two-qubit gates.

Methods:

Name Description
run

Decompose three-qubit gates in the quantum circuit into sequences of single- and two-qubit gates.

Source code in quark/circuit/decompose.py
641
642
def __init__(self):
    super().__init__()

run(qc: QuantumCircuit) ¤

Decompose three-qubit gates in the quantum circuit into sequences of single- and two-qubit gates.

Parameters:

Name Type Description Default
qc QuantumCircuit

The quantum circuit to process.

required

Returns:

Name Type Description
QuantumCircuit

A new quantum circuit with all three-qubit gates decomposed into single- and two-qubit gates.

Source code in quark/circuit/decompose.py
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
def run(self,qc:QuantumCircuit):
    """Decompose three-qubit gates in the quantum circuit into sequences of single- and two-qubit gates.

    Args:
        qc (QuantumCircuit): The quantum circuit to process.

    Returns:
        QuantumCircuit: A new quantum circuit with all three-qubit gates decomposed into single- and two-qubit gates.
    """
    new = []
    for gate_info in qc.gates:
        if gate_info[0] == 'ccx':
            new += ccx_decompose(*gate_info[1:])
            #new += ccx_decompose_mute_phase(*gate_info[1:])
        elif gate_info[0] == 'ccz':
            new += ccz_decompose(*gate_info[1:])
        elif gate_info[0] == 'cswap':
            new += cswap_decompose(*gate_info[1:])
        else:
            new.append(gate_info)
    new_qc = qc.deepcopy()
    new_qc.gates = new
    return new_qc

u_dot_u(u_info1: tuple, u_info2: tuple) -> tuple ¤

Carry out u @ u and return a new u information

Parameters:

Name Type Description Default
u_info1 tuple

u gate information like ('u', 1.5707963267948966, 0.0, 3.141592653589793, 0)

required
u_info2 tuple

u gate information like ('u', 1.5707963267948966, 0.0, 3.141592653589793, 0)

required

Returns:

Name Type Description
tuple tuple

A new u gate information

Source code in quark/circuit/decompose.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def u_dot_u(u_info1: tuple, u_info2: tuple) -> tuple:
    """Carry out u @ u and return a new u information

    Args:
        u_info1 (tuple): u gate information like ('u', 1.5707963267948966, 0.0, 3.141592653589793, 0)
        u_info2 (tuple): u gate information like ('u', 1.5707963267948966, 0.0, 3.141592653589793, 0)

    Returns:
        tuple: A new u gate information
    """
    assert(u_info1[-1] == u_info2[-1])
    u_mat1 = u_mat(*u_info1[1:-1])
    u_mat2 = u_mat(*u_info2[1:-1])

    new_u = u_mat2 @ u_mat1
    theta, phi, lamda, _ = u3_decompose(new_u)
    return ('u', theta, phi, lamda, u_info1[-1])

h2u(qubit: int) -> tuple ¤

Convert H gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
47
48
49
50
51
52
53
54
55
56
def h2u(qubit: int) -> tuple:
    """Convert H gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u', np.pi/2, 0.0, np.pi, qubit)

x2u(qubit: int) -> tuple ¤

Convert X gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
58
59
60
61
62
63
64
65
66
67
def x2u(qubit: int) -> tuple:
    """Convert X gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u', np.pi, np.pi/2, -np.pi/2, qubit)

sdg2u(qubit: int) -> tuple ¤

Convert sdg gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
69
70
71
72
73
74
75
76
77
78
def sdg2u(qubit:int) -> tuple:
    """Convert sdg gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u', 0.0, -0.7853981633974483, -0.7853981633974483, qubit)

s2u(qubit: int) -> tuple ¤

Convert S gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
80
81
82
83
84
85
86
87
88
89
def s2u(qubit: int) -> tuple:
    """Convert S gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u', 0.0, 0.7853981633974483, 0.7853981633974483, qubit)

rx2u(theta: float, qubit: int) -> tuple ¤

Convert RX gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def rx2u(theta:float,qubit:int) -> tuple:
    """Convert RX gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u',theta,-np.pi/2,np.pi/2,qubit)

ry2u(theta: float, qubit: int) -> tuple ¤

Convert RY gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
102
103
104
105
106
107
108
109
110
111
def ry2u(theta:float,qubit:int) -> tuple:
    """Convert RY gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u',theta,0.0,0.0,qubit)

rz2u(theta: float, qubit: int) -> tuple ¤

Convert RZ gate to U3 gate tuple.

Parameters:

Name Type Description Default
qubit int

The qubit to apply the gate to.

required

Returns:

Name Type Description
tuple tuple

u3 gate information.

Source code in quark/circuit/decompose.py
113
114
115
116
117
118
119
120
121
122
def rz2u(theta:float,qubit:int) -> tuple:
    """Convert RZ gate to U3 gate tuple.

    Args:
        qubit (int): The qubit to apply the gate to.

    Returns:
        tuple: u3 gate information.
    """
    return ('u',0.0,0.0,theta,qubit)

cz_decompose(control_qubit: int, target_qubit: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cx', 'cz', 'iswap']) -> list ¤

Decompose CZ gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
control_qubit int

The qubit used as control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
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
def cz_decompose(control_qubit: int, target_qubit: int,convert_single_qubit_gate_to_u:bool,two_qubit_gate_basis:Literal['cx','cz','iswap']) -> list:
    """ Decompose CZ gate to U3 gates and CZ gates.

    Args:
        control_qubit (int): The qubit used as control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of U3 gates and CZ gates.
    """
    gates = []
    if two_qubit_gate_basis == 'cz':
        gates.append(('cz',control_qubit,target_qubit))
    elif two_qubit_gate_basis == 'cx':
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(target_qubit))
        else:
            gates.append(('h',target_qubit))
        gates.append(('cx', control_qubit, target_qubit))
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(target_qubit))
        else:
            gates.append(('h',target_qubit))
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cz_to_iswap(control_qubit,target_qubit,convert_single_qubit_gate_to_u)

    return gates

cx_decompose(control_qubit: int, target_qubit: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cx', 'cz', 'iswap']) -> list ¤

Decompose CX gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
control_qubit int

The qubit used as control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
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
def cx_decompose(control_qubit: int, target_qubit: int,convert_single_qubit_gate_to_u:bool,two_qubit_gate_basis:Literal['cx','cz','iswap']) -> list:
    """ Decompose CX gate to U3 gates and CZ gates.

    Args:
        control_qubit (int): The qubit used as control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of U3 gates and CZ gates.
    """
    gates = []
    if two_qubit_gate_basis == 'cz':
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(target_qubit))
        else:
            gates.append(('h',target_qubit))
        gates.append(('cz', control_qubit, target_qubit))
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(target_qubit))
        else:
            gates.append(('h',target_qubit))
    elif two_qubit_gate_basis == 'cx':
        gates.append(('cx',control_qubit,target_qubit))
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(control_qubit,target_qubit,convert_single_qubit_gate_to_u)
    return gates

cy_decompose(control_qubit: int, target_qubit: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose CY gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
control_qubit int

The qubit used as control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
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
def cy_decompose(control_qubit: int, target_qubit: int, convert_single_qubit_gate_to_u:bool, two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """ Decompose CY gate to U3 gates and CZ gates. 

    Args:
        control_qubit (int): The qubit used as control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of U3 gates and CZ gates.
    """

    gates = []
    if convert_single_qubit_gate_to_u:
        gates.append(sdg2u(target_qubit))
    else:
        gates.append(('sdg',target_qubit))

    if two_qubit_gate_basis == 'cz':
        gates += cx_decompose(control_qubit,target_qubit,convert_single_qubit_gate_to_u,two_qubit_gate_basis)
    elif two_qubit_gate_basis == 'cx':
        gates.append(('cx',control_qubit,target_qubit))
    elif two_qubit_gate_basis =='iswap':
        gates += convert_cx_to_iswap(control_qubit,target_qubit,convert_single_qubit_gate_to_u)

    if convert_single_qubit_gate_to_u:
        gates.append(s2u(target_qubit))
    else:
        gates.append(('s',target_qubit))
    return gates

swap_decompose(qubit1: int, qubit2: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose SWAP gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
qubit1 int

The first qubit to apply the gate to.

required
qubit2 int

The second qubit to apply the gate to.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def swap_decompose(qubit1: int, qubit2: int, convert_single_qubit_gate_to_u:bool, two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """Decompose SWAP gate to U3 gates and CZ gates.

    Args:
        qubit1 (int): The first qubit to apply the gate to.
        qubit2 (int): The second qubit to apply the gate to.

    Returns:
        list: A list of U3 gates and CZ gates.
    """
    if two_qubit_gate_basis == 'cz':
        gates = []
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(qubit2))
        else:
            gates.append(('h',qubit2))
        gates.append(('cz',qubit1,qubit2))
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(qubit2))
            gates.append(h2u(qubit1))
        else:
            gates.append(('h',qubit2))
            gates.append(('h',qubit1))
        gates.append(('cz',qubit1,qubit2))
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(qubit1))
            gates.append(h2u(qubit2))
        else:
            gates.append(('h',qubit1))
            gates.append(('h',qubit2))
        gates.append(('cz',qubit1,qubit2))
        if convert_single_qubit_gate_to_u:
            gates.append(h2u(qubit2))
        else:
            gates.append(('h',qubit2))
    elif two_qubit_gate_basis == 'cx':
        gates = []
        gates.append(('cx',qubit1,qubit2))
        gates.append(('cx',qubit2,qubit1))
        gates.append(('cx',qubit1,qubit2))
    elif two_qubit_gate_basis == 'iswap':
        gates = []
        gates.append(('iswap',qubit1,qubit2))
        gates.append(('sx',qubit2))
        gates.append(('iswap',qubit1,qubit2))
        gates.append(('sx',qubit1))
        gates.append(('iswap',qubit1,qubit2))
        gates.append(('sx',qubit2))
    return gates

iswap_decompose(qubit1: int, qubit2: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose iswap gate with qiskit decompose algorithm.

Parameters:

Name Type Description Default
qubit1 int

The first qubit to apply the gate to.

required
qubit2 int

The second qubit to apply the gate to.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
def iswap_decompose(qubit1: int, qubit2: int, convert_single_qubit_gate_to_u:bool, two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """ Decompose iswap gate with qiskit decompose algorithm. 

    Args:
        qubit1 (int): The first qubit to apply the gate to.
        qubit2 (int): The second qubit to apply the gate to.

    Returns:
        list: A list of U3 gates and CZ gates.
    """
    # iswap = R_{XX+YY}(-pi/2) = e^{i*pi/4*(X\otimesX + Y\otimesY)}
    if two_qubit_gate_basis == 'iswap':
        gates = []
        gates.append(('iswap',qubit1,qubit2))
    else:
        gates = []
        if convert_single_qubit_gate_to_u:
            gates.append(x2u(qubit1))
            gates.append(rx2u(np.pi/2,qubit1))
            gates.append(rx2u(-np.pi/2,qubit2))
        else:
            gates.append(('x', qubit1))
            gates.append(('rx', np.pi/2, qubit1))
            gates.append(('rx',-np.pi/2, qubit2))
        if two_qubit_gate_basis == 'cx':
            gates.append(('cx',  qubit1, qubit2))
        elif two_qubit_gate_basis == 'cz':
            gates += cx_decompose(qubit1, qubit2, convert_single_qubit_gate_to_u,two_qubit_gate_basis)
        if convert_single_qubit_gate_to_u:
            gates.append(rx2u(-np.pi/2,qubit1))
            gates.append(rz2u(-np.pi/2,qubit2))
        else:
            gates.append(('rx',-np.pi/2, qubit1))
            gates.append(('rz',-np.pi/2, qubit2)) 
        if two_qubit_gate_basis == 'cx':
            gates.append(('cx',  qubit1, qubit2))
        elif two_qubit_gate_basis == 'cz':
            gates += cx_decompose(qubit1, qubit2, convert_single_qubit_gate_to_u,two_qubit_gate_basis)
        if convert_single_qubit_gate_to_u:
            gates.append(rx2u(-np.pi/2, qubit1))
            gates.append(rx2u(np.pi/2, qubit2))
            gates.append(x2u(qubit1))
        else:
            gates.append(('rx',-np.pi/2, qubit1))
            gates.append(('rx', np.pi/2, qubit2))
            gates.append(('x', qubit1))
    return gates

rxx_decompose(theta: float, qubit1: int, qubit2: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose RXX gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
theta float

The rotation angle of the gate.

required
qubit1 int

The first qubit to apply the gate to.

required
qubit2 int

The second qubit to apply the gate to.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
def rxx_decompose(theta:float,qubit1:int,qubit2:int,convert_single_qubit_gate_to_u:bool, two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """Decompose RXX gate to U3 gates and CZ gates.

    Args:
        theta (float): The rotation angle of the gate.
        qubit1 (int): The first qubit to apply the gate to.
        qubit2 (int): The second qubit to apply the gate to.

    Returns:
        list: A list of U3 gates and CZ gates.
    """
    gates = []
    if convert_single_qubit_gate_to_u:
        gates.append(h2u(qubit1))
        gates.append(h2u(qubit2))
    else:
        gates.append(('h', qubit1))
        gates.append(('h', qubit2))
    if two_qubit_gate_basis == 'cx':
        gates.append(('cx',qubit1,qubit2))
    elif two_qubit_gate_basis == 'cz':        
        gates += cx_decompose(qubit1,qubit2,convert_single_qubit_gate_to_u,two_qubit_gate_basis)
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(qubit1,qubit2,convert_single_qubit_gate_to_u)

    if convert_single_qubit_gate_to_u:
        gates.append(rz2u(theta,qubit2))
    else:
        gates.append(('rz', theta, qubit2))
    if two_qubit_gate_basis == 'cx':
        gates.append(('cx',qubit1,qubit2))
    elif two_qubit_gate_basis == 'cz':        
        gates += cx_decompose(qubit1,qubit2,convert_single_qubit_gate_to_u,two_qubit_gate_basis)
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(qubit1,qubit2,convert_single_qubit_gate_to_u)

    if convert_single_qubit_gate_to_u:  
        gates.append(h2u(qubit1))
        gates.append(h2u(qubit2))
    else:
        gates.append(('h', qubit1))
        gates.append(('h', qubit2))      
    return gates

ryy_decompose(theta: float, qubit1: int, qubit2: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose RYY gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
theta float

The rotation angle of the gate.

required
qubit1 int

The first qubit to apply the gate to.

required
qubit2 int

The second qubit to apply the gate to.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
def ryy_decompose(theta:float, qubit1:int, qubit2:int, convert_single_qubit_gate_to_u:bool, two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """Decompose RYY gate to U3 gates and CZ gates.

    Args:
        theta (float): The rotation angle of the gate.
        qubit1 (int): The first qubit to apply the gate to.
        qubit2 (int): The second qubit to apply the gate to.

    Returns:
        list: A list of U3 gates and CZ gates.
    """
    gates = []
    if convert_single_qubit_gate_to_u:
        gates.append(rx2u(np.pi/2,qubit1))
        gates.append(rx2u(np.pi/2,qubit2))
    else:
        gates.append(('rx',np.pi/2,qubit1))
        gates.append(('rx',np.pi/2,qubit2))     
    if two_qubit_gate_basis == 'cx':
        gates.append(('cx', qubit1, qubit2))
    elif two_qubit_gate_basis == 'cz':
        gates += cx_decompose(qubit1,qubit2,convert_single_qubit_gate_to_u,two_qubit_gate_basis)
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(qubit1,qubit2,convert_single_qubit_gate_to_u)
    if convert_single_qubit_gate_to_u:
        gates.append(rz2u(theta,qubit2))
    else:
        gates.append(('rz', theta, qubit2))
    if two_qubit_gate_basis == 'cx':
        gates.append(('cx', qubit1, qubit2))
    elif two_qubit_gate_basis == 'cz':
        gates += cx_decompose(qubit1,qubit2,convert_single_qubit_gate_to_u,two_qubit_gate_basis)
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(qubit1,qubit2,convert_single_qubit_gate_to_u)
    if convert_single_qubit_gate_to_u:
        gates.append(rx2u(-np.pi/2,qubit1))
        gates.append(rx2u(-np.pi/2,qubit2))
    else:
        gates.append(('rx', -np.pi/2, qubit1))
        gates.append(('rx', -np.pi/2, qubit2))    
    return gates

rzz_decompose(theta: float, qubit1: int, qubit2: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose RZZ gate to U3 gates and CZ gates.

Parameters:

Name Type Description Default
theta float

The rotation angle of the gate.

required
qubit1 int

The first qubit to apply the gate to.

required
qubit2 int

The second qubit to apply the gate to.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
def rzz_decompose(theta:float, qubit1:int, qubit2:int, convert_single_qubit_gate_to_u:bool, two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """Decompose RZZ gate to U3 gates and CZ gates.

    Args:
        theta (float): The rotation angle of the gate.
        qubit1 (int): The first qubit to apply the gate to.
        qubit2 (int): The second qubit to apply the gate to.

    Returns:
        list: A list of U3 gates and CZ gates.
    """ 
    gates = []
    if two_qubit_gate_basis == 'cx':
        gates.append(('cx', qubit1, qubit2))
    elif two_qubit_gate_basis == 'cz':
        gates += cx_decompose(qubit1,qubit2,convert_single_qubit_gate_to_u,two_qubit_gate_basis)
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(qubit1,qubit2,convert_single_qubit_gate_to_u)
    if convert_single_qubit_gate_to_u:
        gates.append(rz2u(theta,qubit2))
    else:
        gates.append(('rz', theta, qubit2))
    if two_qubit_gate_basis == 'cx':
        gates.append(('cx', qubit1, qubit2))
    elif two_qubit_gate_basis == 'cz':
        gates += cx_decompose(qubit1,qubit2,convert_single_qubit_gate_to_u,two_qubit_gate_basis)  
    elif two_qubit_gate_basis == 'iswap':
        gates += convert_cx_to_iswap(qubit1,qubit2,convert_single_qubit_gate_to_u)
    return gates

cp_decompose(theta: float, control_qubit: int, target_qubit: int, convert_single_qubit_gate_to_u: bool, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap']) -> list ¤

Decompose CPhase gate to U3 gates and CZ gates. ref: Quantum Sci. Technol. 7 (2022) 025021

Parameters:

Name Type Description Default
theta float

The rotation angle of the gate.

required
control_qubit int

The qubit used as control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list list

A list of U3 gates and CZ gates.

Source code in quark/circuit/decompose.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
def cp_decompose(theta:float, control_qubit:int, target_qubit:int, convert_single_qubit_gate_to_u:bool,two_qubit_gate_basis:Literal['cz','cx','iswap']) -> list:
    """Decompose CPhase gate to U3 gates and CZ gates. ref: Quantum Sci. Technol. 7 (2022) 025021

    Args:
        theta (float): The rotation angle of the gate.
        control_qubit (int): The qubit used as control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of U3 gates and CZ gates.
    """ 
    gates = []
    if convert_single_qubit_gate_to_u:
        gates.append(h2u(target_qubit))
    else:
        gates.append(('h',target_qubit))

    #gates.append(('cz', control_qubit, target_qubit))
    gates += cz_decompose(control_qubit,target_qubit,convert_single_qubit_gate_to_u,two_qubit_gate_basis)

    if convert_single_qubit_gate_to_u:
        gates.append(rx2u(theta/2,target_qubit))
    else:
        gates.append(('rx',theta/2,target_qubit))

    #gates.append(('cz', control_qubit, target_qubit))
    gates += cz_decompose(control_qubit,target_qubit,convert_single_qubit_gate_to_u,two_qubit_gate_basis)

    if convert_single_qubit_gate_to_u:
        gates.append(rz2u(-1*theta/2, control_qubit))
        gates.append(h2u(target_qubit))
        gates.append(rz2u(-1*theta/2,target_qubit))
    else:
        gates.append(('rz',-1*theta/2,control_qubit))
        gates.append(('h',target_qubit))
        gates.append(('rz',-1*theta/2,target_qubit))
    return gates

ccx_decompose(control_qubit1: int, control_qubit2: int, target_qubit: int) ¤

Decompose ccx gate. Reference: A biological sequence comparison algorithm using quantum computers

Parameters:

Name Type Description Default
control_qubit1 int

The qubit used as the first control.

required
control_qubit2 int

The qubit used as the second control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list

A list of single- and two-qubit gates.

Source code in quark/circuit/decompose.py
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
def ccx_decompose(control_qubit1: int, control_qubit2: int, target_qubit: int,):
    """Decompose ccx gate. Reference: A biological sequence comparison algorithm using quantum computers

    Args:
        control_qubit1 (int): The qubit used as the first control.
        control_qubit2 (int): The qubit used as the second control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of single- and two-qubit gates.
    """
    gates = [
        ('h',target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('tdg',target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('t',target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('t',control_qubit2),
        ('tdg',target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('cx',control_qubit1,control_qubit2),
        ('t',target_qubit),
        ('t',control_qubit1),
        ('tdg',control_qubit2),
        ('h',target_qubit),
        ('cx',control_qubit1,control_qubit2),
    ]
    return gates

cswap_decompose(control_qubit1: int, control_qubit2: int, target_qubit: int) ¤

Decompose cswap gate. Reference: http://threeplusone.com/gates

Parameters:

Name Type Description Default
control_qubit1 int

The qubit used as the first control.

required
control_qubit2 int

The qubit used as the second control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list

A list of single- and two-qubit gates.

Source code in quark/circuit/decompose.py
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def cswap_decompose(control_qubit1: int, control_qubit2: int, target_qubit: int,):
    """Decompose cswap gate. Reference: http://threeplusone.com/gates

    Args:
        control_qubit1 (int): The qubit used as the first control.
        control_qubit2 (int): The qubit used as the second control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of single- and two-qubit gates.
    """
    gates = [
        ('cx',target_qubit,control_qubit2),
        ('h',target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('tdg',target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('t',target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('t',control_qubit2),
        ('tdg',target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('cx',control_qubit1,control_qubit2),
        ('t',target_qubit),
        ('t',control_qubit1),
        ('tdg',control_qubit2),
        ('h',target_qubit),
        ('cx',control_qubit1,control_qubit2),
        ('cx',target_qubit,control_qubit2),
    ]
    return gates

ccz_decompose(control_qubit1: int, control_qubit2: int, target_qubit: int) ¤

Decompose ccz gate. Reference: http://threeplusone.com/gates

Parameters:

Name Type Description Default
control_qubit1 int

The qubit used as the first control.

required
control_qubit2 int

The qubit used as the second control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list

A list of single- and two-qubit gates.

Source code in quark/circuit/decompose.py
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
def ccz_decompose(control_qubit1: int, control_qubit2: int, target_qubit: int,):
    """Decompose ccz gate. Reference: http://threeplusone.com/gates

    Args:
        control_qubit1 (int): The qubit used as the first control.
        control_qubit2 (int): The qubit used as the second control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of single- and two-qubit gates.
    """
    gates = [
        ('cx',control_qubit2,target_qubit),
        ('tdg',target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('t',target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('t',control_qubit2),
        ('tdg',target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('cx',control_qubit1,control_qubit2),
        ('t',target_qubit),
        ('t',control_qubit1),
        ('tdg',control_qubit2),
        ('h',target_qubit),
        ('cx',control_qubit1,control_qubit2),
        ('h',target_qubit)
    ]
    return gates

ccx_decompose_mute_phase(control_qubit1: int, control_qubit2: int, target_qubit: int) ¤

Decompose ccx gate. it will lose some phase.

Parameters:

Name Type Description Default
control_qubit1 int

The qubit used as the first control.

required
control_qubit2 int

The qubit used as the second control.

required
target_qubit int

The qubit targeted by the gate.

required

Returns:

Name Type Description
list

A list of single- and two-qubit gates.

Source code in quark/circuit/decompose.py
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
def ccx_decompose_mute_phase(control_qubit1: int, control_qubit2: int, target_qubit: int,):
    """Decompose ccx gate. it will lose some phase.

    Args:
        control_qubit1 (int): The qubit used as the first control.
        control_qubit2 (int): The qubit used as the second control.
        target_qubit (int): The qubit targeted by the gate.

    Returns:
        list: A list of single- and two-qubit gates.
    """
    gates = [
        ('u',np.pi/4,0,0,target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('u',np.pi/4,0,0,target_qubit),
        ('cx',control_qubit1,target_qubit),
        ('u',np.pi/4,-np.pi,-np.pi,target_qubit),
        ('cx',control_qubit2,target_qubit),
        ('u',np.pi/4,-np.pi,-np.pi,target_qubit),
    ]
    return gates[::-1]