3 Basic Usage Example for SQUANDER Density Matrix Module 6 - Creating density matrices 7 - Applying quantum circuits 9 - Computing quantum properties 20 AmplitudeDampingChannel,
22 except ImportError
as e:
23 print(f
"Error: Could not import density matrix module: {e}")
24 print(
"Please build the module first: python setup.py build_ext")
29 """Example 1: Pure state evolution.""" 31 print(
"Example 1: Pure State Evolution")
35 rho = DensityMatrix(qbit_num=2)
36 print(f
"Initial state: |00â©â¨00|")
37 print(f
" Purity: {rho.purity():.4f} (pure state)")
38 print(f
" Entropy: {rho.entropy():.4f} bits")
41 circuit = NoisyCircuit(2)
43 circuit.add_CNOT(1, 0)
46 circuit.apply_to(np.array([]), rho)
48 print(f
"\nAfter Bell state circuit:")
49 print(f
" Purity: {rho.purity():.4f} (still pure)")
50 print(f
" Entropy: {rho.entropy():.4f} bits")
53 rho_np = rho.to_numpy()
54 print(f
"\nDensity matrix shape: {rho_np.shape}")
55 print(f
"Diagonal elements: {np.diag(rho_np).real}")
59 """Example 2: Simulation with noise.""" 61 print(
"Example 2: Noise Simulation")
65 circuit = NoisyCircuit(2)
67 circuit.add_CNOT(1, 0)
70 rho = DensityMatrix(qbit_num=2)
71 circuit.apply_to(np.array([]), rho)
73 print(f
"After circuit (no noise):")
74 print(f
" Purity: {rho.purity():.4f}")
77 noise = DepolarizingChannel(qbit_num=2, error_rate=0.05)
80 print(f
"\nAfter 5% depolarizing noise:")
81 print(f
" Purity: {rho.purity():.4f} (< 1, now mixed)")
82 print(f
" Entropy: {rho.entropy():.4f} bits (> 0)")
88 print(f
"\nAfter repeated noise application:")
89 print(f
" Purity: {rho.purity():.4f}")
90 print(f
" Approaching maximally mixed (purity â 0.25)")
94 """Example 3: T1 and T2 noise.""" 96 print(
"Example 3: T1 and T2 Noise")
100 psi = np.array([1, 1, 0, 0], dtype=complex) / np.sqrt(2)
101 rho = DensityMatrix(psi)
103 rho_np = rho.to_numpy()
104 coherence_initial = abs(rho_np[0, 1])
106 print(f
"Initial state: |+0â©")
107 print(f
" Coherence Ï(0,1): {coherence_initial:.4f}")
108 print(f
" Purity: {rho.purity():.4f}")
111 t1_noise = AmplitudeDampingChannel(target_qbit=0, gamma=0.1)
114 print(f
"\nAfter T1 noise (10% decay):")
115 print(f
" Purity: {rho.purity():.4f}")
122 """Example 4: Maximally mixed state.""" 124 print(
"Example 4: Maximally Mixed State")
128 rho = DensityMatrix.maximally_mixed(qbit_num=2)
130 print(f
"Maximally mixed state (2 qubits):")
131 print(f
" Purity: {rho.purity():.4f} (= 1/2² = 0.25)")
132 print(f
" Entropy: {rho.entropy():.4f} bits (= logâ(4) = 2)")
135 eigs = rho.eigenvalues()
136 print(f
" Eigenvalues: {eigs} (all equal for maximally mixed)")
140 """Example 5: Partial trace.""" 142 print(
"Example 5: Partial Trace")
146 circuit = NoisyCircuit(2)
148 circuit.add_CNOT(1, 0)
150 rho_full = DensityMatrix(qbit_num=2)
151 circuit.apply_to(np.array([]), rho_full)
153 print(f
"Full Bell state (2 qubits):")
154 print(f
" Purity: {rho_full.purity():.4f} (pure)")
157 rho_reduced = rho_full.partial_trace([1])
159 print(f
"\nReduced state (qubit 0 only):")
160 print(f
" Purity: {rho_reduced.purity():.4f} (maximally mixed)")
161 print(f
" Entropy: {rho_reduced.entropy():.4f} bits")
163 print(f
"\nThis demonstrates entanglement: tracing out one qubit")
164 print(f
"of an entangled pair gives a maximally mixed state.")
168 """Run all examples.""" 170 print(
"SQUANDER Density Matrix Module - Basic Usage Examples")
181 print(
"All examples completed successfully!")
184 except Exception
as e:
185 print(f
"\nError running examples: {e}")
187 traceback.print_exc()
191 if __name__ ==
"__main__":
def example_2_noise_simulation()
def example_5_partial_trace()
def example_1_pure_state_evolution()
def example_3_t1_t2_noise()
def example_4_maximally_mixed()