0
# Quantum Gates and Circuits
1
2
Quantum gate operations for quantum computing applications including single-qubit, two-qubit, and multi-qubit gates.
3
4
## Capabilities
5
6
### Single-Qubit Gates
7
8
Fundamental single-qubit rotations and operations.
9
10
```python { .api }
11
def rx(phi: float, N: int = None) -> Qobj:
12
"""
13
Create rotation around X-axis by angle phi.
14
15
Parameters:
16
- phi: Rotation angle in radians
17
- N: Total number of qubits (for multi-qubit systems)
18
19
Returns:
20
- Qobj: X-rotation gate exp(-i φ σₓ/2)
21
"""
22
23
def ry(phi: float, N: int = None) -> Qobj:
24
"""
25
Create rotation around Y-axis by angle phi.
26
27
Parameters:
28
- phi: Rotation angle in radians
29
- N: Total number of qubits
30
31
Returns:
32
- Qobj: Y-rotation gate exp(-i φ σᵧ/2)
33
"""
34
35
def rz(phi: float, N: int = None) -> Qobj:
36
"""
37
Create rotation around Z-axis by angle phi.
38
39
Parameters:
40
- phi: Rotation angle in radians
41
- N: Total number of qubits
42
43
Returns:
44
- Qobj: Z-rotation gate exp(-i φ σᵤ/2)
45
"""
46
47
def phasegate(theta: float, N: int = None) -> Qobj:
48
"""
49
Create phase gate with phase theta.
50
51
Parameters:
52
- theta: Phase angle in radians
53
- N: Total number of qubits
54
55
Returns:
56
- Qobj: Phase gate diag([1, exp(i θ)])
57
"""
58
59
def qrot(theta: float, phi: float) -> Qobj:
60
"""
61
Create general single-qubit rotation.
62
63
Parameters:
64
- theta: Polar angle (0 to π)
65
- phi: Azimuthal angle (0 to 2π)
66
67
Returns:
68
- Qobj: General rotation gate
69
"""
70
71
def sqrtnot(N: int = None) -> Qobj:
72
"""
73
Create square root of NOT gate.
74
75
Parameters:
76
- N: Total number of qubits
77
78
Returns:
79
- Qobj: √NOT gate
80
"""
81
```
82
83
### Pauli and Related Gates
84
85
Standard Pauli gates and their variants.
86
87
```python { .api }
88
def s_gate(N: int = None) -> Qobj:
89
"""
90
Create S gate (phase gate with π/2 phase).
91
92
Parameters:
93
- N: Total number of qubits
94
95
Returns:
96
- Qobj: S gate diag([1, i])
97
"""
98
99
def t_gate(N: int = None) -> Qobj:
100
"""
101
Create T gate (π/8 phase gate).
102
103
Parameters:
104
- N: Total number of qubits
105
106
Returns:
107
- Qobj: T gate diag([1, exp(i π/4)])
108
"""
109
110
def cs_gate(N: int = None, control: int = 0, target: int = 1) -> Qobj:
111
"""
112
Create controlled-S gate.
113
114
Parameters:
115
- N: Total number of qubits
116
- control: Control qubit index
117
- target: Target qubit index
118
119
Returns:
120
- Qobj: Controlled S gate
121
"""
122
123
def ct_gate(N: int = None, control: int = 0, target: int = 1) -> Qobj:
124
"""
125
Create controlled-T gate.
126
127
Parameters:
128
- N: Total number of qubits
129
- control: Control qubit index
130
- target: Target qubit index
131
132
Returns:
133
- Qobj: Controlled T gate
134
"""
135
```
136
137
### Two-Qubit Gates
138
139
Fundamental two-qubit entangling gates.
140
141
```python { .api }
142
def cnot(N: int = None, control: int = 0, target: int = 1) -> Qobj:
143
"""
144
Create controlled-NOT (CNOT) gate.
145
146
Parameters:
147
- N: Total number of qubits (inferred if None)
148
- control: Control qubit index
149
- target: Target qubit index
150
151
Returns:
152
- Qobj: CNOT gate
153
"""
154
155
def cy_gate(N: int = None, control: int = 0, target: int = 1) -> Qobj:
156
"""
157
Create controlled-Y gate.
158
159
Parameters:
160
- N: Total number of qubits
161
- control: Control qubit index
162
- target: Target qubit index
163
164
Returns:
165
- Qobj: Controlled Pauli-Y gate
166
"""
167
168
def cz_gate(N: int = None, control: int = 0, target: int = 1) -> Qobj:
169
"""
170
Create controlled-Z gate.
171
172
Parameters:
173
- N: Total number of qubits
174
- control: Control qubit index
175
- target: Target qubit index
176
177
Returns:
178
- Qobj: Controlled Pauli-Z gate
179
"""
180
181
def cphase(theta: float, N: int = None, control: int = 0, target: int = 1) -> Qobj:
182
"""
183
Create controlled phase gate.
184
185
Parameters:
186
- theta: Phase angle in radians
187
- N: Total number of qubits
188
- control: Control qubit index
189
- target: Target qubit index
190
191
Returns:
192
- Qobj: Controlled phase gate
193
"""
194
195
def csign(N: int = None, control: int = 0, target: int = 1) -> Qobj:
196
"""
197
Create controlled sign gate (controlled-Z).
198
199
Parameters:
200
- N: Total number of qubits
201
- control: Control qubit index
202
- target: Target qubit index
203
204
Returns:
205
- Qobj: Controlled sign gate
206
"""
207
```
208
209
### Swap Gates
210
211
Gates for exchanging qubit states.
212
213
```python { .api }
214
def swap(N: int = None, targets: list = [0, 1]) -> Qobj:
215
"""
216
Create SWAP gate for exchanging two qubits.
217
218
Parameters:
219
- N: Total number of qubits
220
- targets: List of two qubit indices to swap
221
222
Returns:
223
- Qobj: SWAP gate
224
"""
225
226
def iswap(N: int = None, targets: list = [0, 1]) -> Qobj:
227
"""
228
Create iSWAP gate (SWAP with phase).
229
230
Parameters:
231
- N: Total number of qubits
232
- targets: List of two qubit indices
233
234
Returns:
235
- Qobj: iSWAP gate
236
"""
237
238
def sqrtswap(N: int = None, targets: list = [0, 1]) -> Qobj:
239
"""
240
Create square root of SWAP gate.
241
242
Parameters:
243
- N: Total number of qubits
244
- targets: List of two qubit indices
245
246
Returns:
247
- Qobj: √SWAP gate
248
"""
249
250
def sqrtiswap(N: int = None, targets: list = [0, 1]) -> Qobj:
251
"""
252
Create square root of iSWAP gate.
253
254
Parameters:
255
- N: Total number of qubits
256
- targets: List of two qubit indices
257
258
Returns:
259
- Qobj: √iSWAP gate
260
"""
261
262
def swapalpha(alpha: float, N: int = None, targets: list = [0, 1]) -> Qobj:
263
"""
264
Create parametric SWAP gate.
265
266
Parameters:
267
- alpha: Parameter (0 = identity, 1 = SWAP)
268
- N: Total number of qubits
269
- targets: List of two qubit indices
270
271
Returns:
272
- Qobj: Parametric SWAP gate
273
"""
274
```
275
276
### Multi-Qubit Gates
277
278
Three-qubit and higher-order quantum gates.
279
280
```python { .api }
281
def toffoli(N: int = None, controls: list = [0, 1], target: int = 2) -> Qobj:
282
"""
283
Create Toffoli (CCNOT) gate.
284
285
Parameters:
286
- N: Total number of qubits
287
- controls: List of control qubit indices
288
- target: Target qubit index
289
290
Returns:
291
- Qobj: Toffoli gate (controlled-controlled-NOT)
292
"""
293
294
def fredkin(N: int = None, control: int = 0, targets: list = [1, 2]) -> Qobj:
295
"""
296
Create Fredkin (controlled-SWAP) gate.
297
298
Parameters:
299
- N: Total number of qubits
300
- control: Control qubit index
301
- targets: List of two target qubit indices to swap
302
303
Returns:
304
- Qobj: Fredkin gate
305
"""
306
307
def berkeley(N: int = None, targets: list = [0, 1, 2]) -> Qobj:
308
"""
309
Create Berkeley B gate (three-qubit gate).
310
311
Parameters:
312
- N: Total number of qubits
313
- targets: List of three target qubit indices
314
315
Returns:
316
- Qobj: Berkeley B gate
317
"""
318
```
319
320
### Quantum Transforms
321
322
Hadamard transform and quantum Fourier transform.
323
324
```python { .api }
325
def hadamard_transform(N: int = 1) -> Qobj:
326
"""
327
Create N-qubit Hadamard transform.
328
329
Parameters:
330
- N: Number of qubits
331
332
Returns:
333
- Qobj: N-qubit Hadamard gate H⊗N
334
"""
335
336
def qft(N: int, swap: bool = True) -> Qobj:
337
"""
338
Create quantum Fourier transform for N qubits.
339
340
Parameters:
341
- N: Number of qubits (N = log₂(dimension))
342
- swap: Include bit-reversal swaps
343
344
Returns:
345
- Qobj: Quantum Fourier transform operator
346
"""
347
348
def globalphase(theta: float, N: int) -> Qobj:
349
"""
350
Create global phase gate exp(i θ).
351
352
Parameters:
353
- theta: Global phase angle
354
- N: Number of qubits
355
356
Returns:
357
- Qobj: Global phase operator
358
"""
359
```
360
361
### Specialized Gate Sets
362
363
Predefined gate sets for specific quantum computing protocols.
364
365
```python { .api }
366
def qubit_clifford_group(N: int = 1) -> list:
367
"""
368
Generate N-qubit Clifford group gates.
369
370
Parameters:
371
- N: Number of qubits
372
373
Returns:
374
- list: List of Clifford group generators
375
"""
376
377
def molmer_sorensen(theta: float, targets: list = None, N: int = None) -> Qobj:
378
"""
379
Create Mølmer-Sørensen gate for trapped ions.
380
381
Parameters:
382
- theta: Gate angle parameter
383
- targets: Target qubit indices
384
- N: Total number of qubits
385
386
Returns:
387
- Qobj: Mølmer-Sørensen gate
388
"""
389
```
390
391
### Usage Examples
392
393
```python
394
import qutip as qt
395
import numpy as np
396
397
# Single-qubit rotations
398
X_gate = qt.rx(np.pi) # π rotation around X (NOT gate)
399
Y_gate = qt.ry(np.pi) # π rotation around Y
400
Z_gate = qt.rz(np.pi) # π rotation around Z
401
H_gate = qt.ry(np.pi/2) * qt.rz(np.pi) # Hadamard gate
402
403
# Verify NOT gate
404
psi0 = qt.basis(2, 0) # |0⟩
405
psi1 = X_gate * psi0 # Should be |1⟩
406
print(f"X|0⟩ = {psi1}")
407
408
# Phase gates
409
S = qt.s_gate() # S = diag([1, i])
410
T = qt.t_gate() # T = diag([1, exp(iπ/4)])
411
phase_pi_4 = qt.phasegate(np.pi/4) # Custom phase
412
413
# Two-qubit gates
414
CNOT = qt.cnot() # 2-qubit CNOT gate
415
CZ = qt.cz_gate() # Controlled-Z
416
SWAP = qt.swap() # SWAP gate
417
418
# Create Bell state using CNOT
419
psi_plus = qt.hadamard_transform(1) # H ⊗ I
420
psi_plus = qt.tensor(psi_plus, qt.qeye(2))
421
initial = qt.tensor(qt.basis(2,0), qt.basis(2,0)) # |00⟩
422
bell_state = CNOT * psi_plus * initial
423
424
# Three-qubit gates
425
CCX = qt.toffoli() # Toffoli (CCNOT)
426
CSWAP = qt.fredkin() # Fredkin (controlled-SWAP)
427
428
# Multi-qubit systems
429
N_qubits = 3
430
X_on_qubit_1 = qt.rx(np.pi, N=N_qubits) # NOT on first qubit of 3-qubit system
431
432
# Controlled gates with specified qubits
433
CNOT_01 = qt.cnot(N=3, control=0, target=1) # CNOT from qubit 0 to 1
434
CNOT_12 = qt.cnot(N=3, control=1, target=2) # CNOT from qubit 1 to 2
435
436
# Quantum Fourier Transform
437
QFT_3 = qt.qft(3) # 3-qubit QFT
438
print(f"QFT matrix shape: {QFT_3.shape}")
439
440
# Parametric gates
441
theta = np.pi/4
442
Rx_theta = qt.rx(theta) # Parametric X rotation
443
Ry_theta = qt.ry(theta) # Parametric Y rotation
444
CP_theta = qt.cphase(theta) # Controlled phase with parameter
445
446
# Gate sequences and circuits
447
# Example: Creating GHZ state |000⟩ + |111⟩
448
initial_state = qt.tensor(qt.basis(2,0), qt.basis(2,0), qt.basis(2,0)) # |000⟩
449
450
# Step 1: Hadamard on first qubit
451
H_first = qt.tensor(qt.hadamard_transform(1), qt.qeye(2), qt.qeye(2))
452
state1 = H_first * initial_state # (|000⟩ + |100⟩)/√2
453
454
# Step 2: CNOT from qubit 0 to 1
455
CNOT_01 = qt.cnot(N=3, control=0, target=1)
456
state2 = CNOT_01 * state1 # (|000⟩ + |110⟩)/√2
457
458
# Step 3: CNOT from qubit 1 to 2
459
CNOT_12 = qt.cnot(N=3, control=1, target=2)
460
ghz_state = CNOT_12 * state2 # (|000⟩ + |111⟩)/√2
461
462
print(f"Created GHZ state fidelity: {abs(qt.ghz_state(3).dag() * ghz_state)**2}")
463
464
# Gate decompositions
465
# Decompose Toffoli into CNOT and single-qubit gates
466
def toffoli_decomp():
467
"""Toffoli decomposition using CNOTs and T gates."""
468
gates = []
469
gates.append(qt.cnot(N=3, control=1, target=2))
470
gates.append(qt.tensor(qt.qeye(2), qt.qeye(2), qt.t_gate().dag()))
471
gates.append(qt.cnot(N=3, control=0, target=2))
472
gates.append(qt.tensor(qt.qeye(2), qt.qeye(2), qt.t_gate()))
473
gates.append(qt.cnot(N=3, control=1, target=2))
474
gates.append(qt.tensor(qt.qeye(2), qt.qeye(2), qt.t_gate().dag()))
475
gates.append(qt.cnot(N=3, control=0, target=2))
476
gates.append(qt.tensor(qt.qeye(2), qt.t_gate(), qt.t_gate()))
477
gates.append(qt.cnot(N=3, control=0, target=1))
478
gates.append(qt.tensor(qt.t_gate(), qt.t_gate().dag(), qt.qeye(2)))
479
gates.append(qt.cnot(N=3, control=0, target=1))
480
481
# Apply gates in reverse order (right to left multiplication)
482
result = qt.qeye([2,2,2])
483
for gate in reversed(gates):
484
result = gate * result
485
return result
486
487
# Compare with built-in Toffoli
488
toffoli_builtin = qt.toffoli(N=3)
489
toffoli_decomposed = toffoli_decomp()
490
fidelity = abs((toffoli_builtin.dag() * toffoli_decomposed).tr() / 8)
491
print(f"Toffoli decomposition fidelity: {fidelity:.6f}")
492
493
# Random quantum circuits
494
def random_circuit(n_qubits, depth):
495
"""Generate random quantum circuit."""
496
gates = [qt.rx, qt.ry, qt.rz]
497
circuit = qt.qeye([2]*n_qubits)
498
499
for _ in range(depth):
500
# Random single-qubit gate
501
gate_func = np.random.choice(gates)
502
angle = np.random.uniform(0, 2*np.pi)
503
qubit = np.random.randint(n_qubits)
504
505
# Build gate for specific qubit
506
gate_list = [qt.qeye(2) for _ in range(n_qubits)]
507
gate_list[qubit] = gate_func(angle)
508
single_gate = qt.tensor(*gate_list)
509
510
circuit = single_gate * circuit
511
512
# Random CNOT
513
if np.random.random() > 0.5 and n_qubits > 1:
514
control, target = np.random.choice(n_qubits, 2, replace=False)
515
cnot_gate = qt.cnot(N=n_qubits, control=control, target=target)
516
circuit = cnot_gate * circuit
517
518
return circuit
519
520
# Generate and test random circuit
521
random_U = random_circuit(3, 10)
522
print(f"Random circuit unitarity check: {(random_U * random_U.dag() - qt.qeye([2,2,2])).norm():.2e}")
523
```
524
525
## Types
526
527
```python { .api }
528
# All quantum gate functions return Qobj instances with type='oper'
529
# representing unitary quantum gate operations
530
```