0
# Tensor Operations and Composite Systems
1
2
Operations for combining quantum systems and manipulating composite quantum states and operators.
3
4
## Capabilities
5
6
### Tensor Product Operations
7
8
Fundamental operations for combining quantum systems.
9
10
```python { .api }
11
def tensor(*args) -> Qobj:
12
"""
13
Calculate tensor product of quantum objects.
14
15
Parameters:
16
- *args: Variable number of Qobj instances to tensor together
17
18
Returns:
19
- Qobj: Tensor product of input objects
20
"""
21
22
def super_tensor(*args) -> Qobj:
23
"""
24
Calculate tensor product of superoperators.
25
26
Parameters:
27
- *args: Variable number of superoperator Qobj instances
28
29
Returns:
30
- Qobj: Tensor product of superoperators
31
"""
32
```
33
34
### Partial Trace Operations
35
36
Reduction of composite quantum systems.
37
38
```python { .api }
39
def ptrace(obj: Qobj, sel) -> Qobj:
40
"""
41
Partial trace over specified subsystems.
42
43
Parameters:
44
- obj: Composite quantum object (state or operator)
45
- sel: Integer, list of integers, or boolean mask specifying subsystems to keep
46
47
Returns:
48
- Qobj: Reduced quantum object after tracing out unselected subsystems
49
"""
50
```
51
52
### Expectation Values and Measurements
53
54
Calculation of expectation values and measurement statistics.
55
56
```python { .api }
57
def expect(oper: Qobj, state: Qobj) -> float:
58
"""
59
Calculate expectation value ⟨ψ|Ô|ψ⟩ or Tr(ρÔ).
60
61
Parameters:
62
- oper: Observable operator
63
- state: Quantum state (ket or density matrix)
64
65
Returns:
66
- float: Expectation value
67
"""
68
69
def variance(oper: Qobj, state: Qobj) -> float:
70
"""
71
Calculate variance ⟨Ô²⟩ - ⟨Ô⟩².
72
73
Parameters:
74
- oper: Observable operator
75
- state: Quantum state
76
77
Returns:
78
- float: Variance of observable
79
"""
80
```
81
82
### Composite System Construction
83
84
Tools for building and manipulating composite quantum systems.
85
86
```python { .api }
87
def composite(*args) -> Qobj:
88
"""
89
Create composite quantum object from components.
90
91
Parameters:
92
- *args: Quantum objects representing subsystems
93
94
Returns:
95
- Qobj: Composite quantum object
96
"""
97
98
def expand_operator(oper: Qobj, N: int, targets: list, dims: list = None) -> Qobj:
99
"""
100
Expand operator to act on larger Hilbert space.
101
102
Parameters:
103
- oper: Operator to expand
104
- N: Total number of subsystems
105
- targets: List of target subsystem indices
106
- dims: Dimensions of each subsystem
107
108
Returns:
109
- Qobj: Expanded operator
110
"""
111
```
112
113
### Tensor Manipulation
114
115
Advanced tensor operations for quantum system manipulation.
116
117
```python { .api }
118
def tensor_swap(obj: Qobj, mask) -> Qobj:
119
"""
120
Swap subsystems in tensor product structure.
121
122
Parameters:
123
- obj: Quantum object with tensor structure
124
- mask: Permutation mask for swapping subsystems
125
126
Returns:
127
- Qobj: Object with swapped subsystem order
128
"""
129
130
def tensor_contract(obj: Qobj, *pairs) -> Qobj:
131
"""
132
Contract tensor indices in quantum object.
133
134
Parameters:
135
- obj: Quantum object to contract
136
- *pairs: Pairs of indices to contract
137
138
Returns:
139
- Qobj: Contracted quantum object
140
"""
141
```
142
143
### Usage Examples
144
145
```python
146
import qutip as qt
147
import numpy as np
148
149
# Basic tensor products
150
psi0 = qt.basis(2, 0) # |0⟩
151
psi1 = qt.basis(2, 1) # |1⟩
152
psi_00 = qt.tensor(psi0, psi0) # |00⟩
153
psi_01 = qt.tensor(psi0, psi1) # |01⟩
154
psi_10 = qt.tensor(psi1, psi0) # |10⟩
155
psi_11 = qt.tensor(psi1, psi1) # |11⟩
156
157
# Bell state construction
158
bell = (psi_00 + psi_11).unit() # (|00⟩ + |11⟩)/√2
159
160
# Tensor product of operators
161
sx = qt.sigmax()
162
sy = qt.sigmay()
163
sz = qt.sigmaz()
164
I = qt.qeye(2)
165
166
# Two-qubit operators
167
sx_I = qt.tensor(sx, I) # σₓ ⊗ I (X on first qubit)
168
I_sy = qt.tensor(I, sy) # I ⊗ σᵧ (Y on second qubit)
169
sx_sz = qt.tensor(sx, sz) # σₓ ⊗ σᵤ (X-Z interaction)
170
171
# Three-qubit system
172
psi_000 = qt.tensor(psi0, psi0, psi0) # |000⟩
173
H_3qubit = qt.tensor(sz, I, I) + qt.tensor(I, sz, I) + qt.tensor(I, I, sz)
174
175
# Partial trace examples
176
rho_total = bell * bell.dag() # Bell state density matrix
177
rho_A = qt.ptrace(rho_total, 0) # Trace out second qubit, keep first
178
rho_B = qt.ptrace(rho_total, 1) # Trace out first qubit, keep second
179
180
print(f"Original state purity: {(rho_total**2).tr():.3f}")
181
print(f"Reduced state A purity: {(rho_A**2).tr():.3f}")
182
print(f"Reduced state B purity: {(rho_B**2).tr():.3f}")
183
184
# Expectation values
185
exp_x_total = qt.expect(sx_I, bell) # ⟨σₓ ⊗ I⟩
186
exp_x_reduced = qt.expect(sx, rho_A) # ⟨σₓ⟩ on reduced state
187
print(f"Expectation values: total={exp_x_total:.3f}, reduced={exp_x_reduced:.3f}")
188
189
# Variance calculation
190
var_x = qt.variance(sx, psi0) # Variance of σₓ in |0⟩ state
191
print(f"Variance of σₓ in |0⟩: {var_x}")
192
193
# Multi-qubit expectation values
194
correlator = qt.expect(sx_sz, bell) # ⟨σₓ ⊗ σᵤ⟩
195
print(f"X-Z correlation: {correlator}")
196
197
# Multiple subsystem partial trace
198
# Three-qubit system, trace out middle qubit
199
psi_3q = qt.ghz_state(3) # GHZ state |000⟩ + |111⟩
200
rho_3q = psi_3q * psi_3q.dag()
201
rho_13 = qt.ptrace(rho_3q, [0, 2]) # Keep qubits 0 and 2, trace out 1
202
203
# Expand operator to larger space
204
single_qubit_op = sx
205
three_qubit_op = qt.expand_operator(single_qubit_op, 3, [1]) # Apply to qubit 1 in 3-qubit system
206
print(f"Expanded operator shape: {three_qubit_op.shape}")
207
208
# Harmonic oscillator tensor products
209
N = 5
210
a1 = qt.tensor(qt.destroy(N), qt.qeye(N)) # Annihilation on mode 1
211
a2 = qt.tensor(qt.qeye(N), qt.destroy(N)) # Annihilation on mode 2
212
n1 = a1.dag() * a1 # Number operator for mode 1
213
n2 = a2.dag() * a2 # Number operator for mode 2
214
215
# Two-mode squeezed state
216
r = 0.5 # Squeezing parameter
217
S_two = (r * (a1.dag() * a2.dag() - a1 * a2)).expm() # Two-mode squeezing
218
vacuum = qt.tensor(qt.basis(N, 0), qt.basis(N, 0))
219
squeezed_vacuum = S_two * vacuum
220
221
# Expectation values in two-mode system
222
n1_exp = qt.expect(n1, squeezed_vacuum)
223
n2_exp = qt.expect(n2, squeezed_vacuum)
224
correlation = qt.expect(a1.dag() * a2, squeezed_vacuum)
225
print(f"Mode populations: n1={n1_exp:.3f}, n2={n2_exp:.3f}")
226
print(f"Mode correlation: {correlation:.3f}")
227
228
# Composite system with different subsystem dimensions
229
# Qubit (dim=2) + harmonic oscillator (dim=N)
230
qubit_state = qt.basis(2, 0)
231
ho_state = qt.coherent(N, 1.0)
232
composite_state = qt.tensor(qubit_state, ho_state)
233
234
# Operators on composite system
235
qubit_op = qt.tensor(sx, qt.qeye(N)) # Pauli-X on qubit
236
ho_op = qt.tensor(qt.qeye(2), qt.num(N)) # Number op on oscillator
237
interaction = qt.tensor(sz, qt.create(N) + qt.destroy(N)) # Jaynes-Cummings type
238
239
# Measurement on subsystems
240
rho_composite = composite_state * composite_state.dag()
241
rho_qubit = qt.ptrace(rho_composite, 0) # Qubit reduced state
242
rho_ho = qt.ptrace(rho_composite, 1) # Oscillator reduced state
243
244
print(f"Qubit state: {rho_qubit}")
245
print(f"Oscillator coherence: {qt.expect(qt.destroy(N), rho_ho):.3f}")
246
```
247
248
## Types
249
250
```python { .api }
251
# All tensor operations return Qobj instances with appropriate
252
# tensor structure and dimensions automatically determined
253
```