@@ -137,27 +137,59 @@ def visualize_gate_effects(single_qubit_circuits):
137137 print ("=== GATE EFFECTS VISUALIZATION ===" )
138138 print ()
139139
140- # Create Bloch sphere plots
141- n_gates = len (single_qubit_circuits )
142- fig , axes = plt .subplots (1 , n_gates , figsize = (3 * n_gates , 3 ))
143- if n_gates == 1 :
144- axes = [axes ]
145-
140+ # Create individual Bloch sphere plots for each gate
146141 for i , (gate_name , circuit ) in enumerate (single_qubit_circuits .items ()):
147142 state = Statevector .from_instruction (circuit )
148143
149- ax = axes [i ]
150- # plot_bloch_multivector no longer accepts ax parameter in Qiskit 2.x
144+ print (f"{ gate_name } :" )
145+ print (f" State vector: { state } " )
146+ print (f" Probabilities: |0⟩: { abs (state [0 ])** 2 :.3f} , |1⟩: { abs (state [1 ])** 2 :.3f} " )
147+
148+ # Create individual Bloch sphere plots
151149 try :
152- bloch_fig = plot_bloch_multivector (state , title = "Qubit State" )
153- # Save individual figure instead of using subplot
150+ bloch_fig = plot_bloch_multivector (state , title = f"{ gate_name } - Qubit State" )
151+ plt .savefig (f'module1_02_bloch_{ i + 1 } .png' , dpi = 300 , bbox_inches = 'tight' )
152+ plt .show ()
154153 except Exception as e :
155- print (f"⚠️ Could not create Bloch sphere: { e } " )
156- ax .set_title (gate_name , fontsize = 10 , pad = 10 )
154+ print (f"⚠️ Could not create Bloch sphere for { gate_name } : { e } " )
155+ # Provide alternative visualization information
156+ print (f" Alternative: State components - α=({ state [0 ].real :.3f} +{ state [0 ].imag :.3f} i), β=({ state [1 ].real :.3f} +{ state [1 ].imag :.3f} i)" )
157+
158+ print ()
157159
158- plt .tight_layout ()
159- plt .savefig ('module1_02_gate_effects.png' , dpi = 300 , bbox_inches = 'tight' )
160- plt .show ()
160+ # Create a summary visualization with state information
161+ try :
162+ fig , ax = plt .subplots (1 , 1 , figsize = (12 , 8 ))
163+
164+ gate_names = list (single_qubit_circuits .keys ())
165+ prob_0 = []
166+ prob_1 = []
167+
168+ for gate_name , circuit in single_qubit_circuits .items ():
169+ state = Statevector .from_instruction (circuit )
170+ prob_0 .append (abs (state [0 ])** 2 )
171+ prob_1 .append (abs (state [1 ])** 2 )
172+
173+ x = range (len (gate_names ))
174+ width = 0.35
175+
176+ ax .bar ([i - width / 2 for i in x ], prob_0 , width , label = '|0⟩ probability' , alpha = 0.8 )
177+ ax .bar ([i + width / 2 for i in x ], prob_1 , width , label = '|1⟩ probability' , alpha = 0.8 )
178+
179+ ax .set_xlabel ('Quantum Gates' )
180+ ax .set_ylabel ('Probability' )
181+ ax .set_title ('Gate Effects: Measurement Probabilities' )
182+ ax .set_xticks (x )
183+ ax .set_xticklabels (gate_names , rotation = 45 , ha = 'right' )
184+ ax .legend ()
185+ ax .grid (True , alpha = 0.3 )
186+
187+ plt .tight_layout ()
188+ plt .savefig ('module1_02_gate_effects.png' , dpi = 300 , bbox_inches = 'tight' )
189+ plt .show ()
190+
191+ except Exception as e :
192+ print (f"⚠️ Could not create gate effects summary: { e } " )
161193
162194
163195def create_quantum_circuit_examples ():
@@ -193,25 +225,49 @@ def create_quantum_circuit_examples():
193225 qc3 .h (0 )
194226 circuits ['Circuit 3: GHZ preparation' ] = qc3
195227
196- # Display circuit diagrams
197- fig , axes = plt .subplots (len (circuits ), 1 , figsize = (12 , 3 * len (circuits )))
198- if len (circuits ) == 1 :
199- axes = [axes ]
200-
228+ # Display circuit information and create diagrams
201229 for i , (name , circuit ) in enumerate (circuits .items ()):
202230 print (f"{ name } :" )
203231 print (f" Depth: { circuit .depth ()} " )
204232 print (f" Gates: { circuit .count_ops ()} " )
205233
206- # Draw circuit
207- circuit_drawer (circuit , output = 'mpl' , ax = axes [i ], style = {'backgroundcolor' : '#EEEEEE' })
208- axes [i ].set_title (f'{ name } (Depth: { circuit .depth ()} )' , fontsize = 12 , pad = 20 )
234+ # Draw circuit - create individual figures to avoid ax parameter issues
235+ try :
236+ fig = circuit .draw (output = 'mpl' , style = {'backgroundcolor' : '#EEEEEE' })
237+ fig .suptitle (f'{ name } (Depth: { circuit .depth ()} )' , fontsize = 12 )
238+ # Save individual circuit diagrams
239+ plt .figure (fig .number )
240+ plt .savefig (f'module1_02_circuit_{ i + 1 } .png' , dpi = 300 , bbox_inches = 'tight' )
241+ plt .show ()
242+ except Exception as e :
243+ print (f"⚠️ Could not create circuit diagram: { e } " )
244+ print (f" Circuit structure: { circuit .data } " )
209245
210246 print ()
211247
212- plt .tight_layout ()
213- plt .savefig ('module1_02_circuit_examples.png' , dpi = 300 , bbox_inches = 'tight' )
214- plt .show ()
248+ # Create combined figure with all circuits
249+ try :
250+ fig , axes = plt .subplots (len (circuits ), 1 , figsize = (12 , 3 * len (circuits )))
251+ if len (circuits ) == 1 :
252+ axes = [axes ]
253+
254+ for i , (name , circuit ) in enumerate (circuits .items ()):
255+ # Use text representation instead of circuit_drawer with ax parameter
256+ axes [i ].text (0.5 , 0.5 , f'{ name } \n Depth: { circuit .depth ()} \n Gates: { circuit .count_ops ()} ' ,
257+ ha = 'center' , va = 'center' , transform = axes [i ].transAxes ,
258+ fontsize = 10 , bbox = dict (boxstyle = "round,pad=0.3" , facecolor = "lightgray" ))
259+ axes [i ].set_xlim (0 , 1 )
260+ axes [i ].set_ylim (0 , 1 )
261+ axes [i ].set_xticks ([])
262+ axes [i ].set_yticks ([])
263+ axes [i ].set_title (f'{ name } (Depth: { circuit .depth ()} )' , fontsize = 12 , pad = 20 )
264+
265+ plt .tight_layout ()
266+ plt .savefig ('module1_02_circuit_examples.png' , dpi = 300 , bbox_inches = 'tight' )
267+ plt .show ()
268+
269+ except Exception as e :
270+ print (f"⚠️ Could not create combined circuit diagram: { e } " )
215271
216272 return circuits
217273
0 commit comments