1 from qiskit
import QuantumCircuit, transpile
2 from qiskit_aer
import AerSimulator
3 from qiskit_aer.noise
import NoiseModel, depolarizing_error, thermal_relaxation_error
4 from qiskit.visualization
import plot_histogram
5 import matplotlib.pyplot
as plt
8 qc = QuantumCircuit(3, 3)
21 qc.measure([0, 1, 2], [0, 1, 2])
28 noise_model = NoiseModel()
32 single_qubit_error_rate = 0.01
33 single_qubit_error = depolarizing_error(single_qubit_error_rate, 1)
36 two_qubit_error_rate = 0.05
37 two_qubit_error = depolarizing_error(two_qubit_error_rate, 2)
46 thermal_error_1q = thermal_relaxation_error(t1, t2, gate_time_1q)
47 thermal_error_2q = thermal_relaxation_error(t1, t2, gate_time_2q).expand(
48 thermal_relaxation_error(t1, t2, gate_time_2q)
52 noise_model.add_all_qubit_quantum_error(
53 single_qubit_error, [
'h',
'rx',
'ry',
'rz'])
54 noise_model.add_all_qubit_quantum_error(two_qubit_error, [
'cx'])
60 print(
"\nNoise Model:")
68 ideal_simulator = AerSimulator()
69 transpiled_ideal = transpile(qc, ideal_simulator)
70 ideal_result = ideal_simulator.run(transpiled_ideal, shots=shots).
result()
71 ideal_counts = ideal_result.get_counts()
74 noisy_simulator = AerSimulator(noise_model=noise_model)
75 transpiled_noisy = transpile(qc, noisy_simulator)
76 noisy_result = noisy_simulator.run(transpiled_noisy, shots=shots).
result()
77 noisy_counts = noisy_result.get_counts()
82 print(
"RESULTS COMPARISON")
85 print(
"\nIdeal (noiseless) counts:")
86 for state, count
in sorted(ideal_counts.items(), key=
lambda x: -x[1]):
87 print(f
" |{state}>: {count} ({100*count/shots:.2f}%)")
89 print(
"\nNoisy counts:")
90 for state, count
in sorted(noisy_counts.items(), key=
lambda x: -x[1]):
91 print(f
" |{state}>: {count} ({100*count/shots:.2f}%)")
94 fig, axes = plt.subplots(1, 2, figsize=(14, 5))
98 states = sorted(ideal_counts.keys())
99 ideal_values = [ideal_counts.get(s, 0)
for s
in states]
100 ax1.bar(states, ideal_values, color=
'steelblue')
101 ax1.set_title(
'Ideal (Noiseless) Simulation', fontsize=14)
102 ax1.set_xlabel(
'Quantum State', fontsize=12)
103 ax1.set_ylabel(
'Counts', fontsize=12)
104 ax1.tick_params(axis=
'x', rotation=45)
108 all_states = sorted(set(ideal_counts.keys()) | set(noisy_counts.keys()))
109 noisy_values = [noisy_counts.get(s, 0)
for s
in all_states]
110 ax2.bar(all_states, noisy_values, color=
'coral')
111 ax2.set_title(
'Noisy Simulation', fontsize=14)
112 ax2.set_xlabel(
'Quantum State', fontsize=12)
113 ax2.set_ylabel(
'Counts', fontsize=12)
114 ax2.tick_params(axis=
'x', rotation=45)
117 plt.savefig(
'noisy_simulation_results.png', dpi=150)
120 print(
"\nPlot saved to 'noisy_simulation_results.png'")