|
| 1 | +from qiskit_aer import Aer |
| 2 | +from qiskit import QuantumCircuit, transpile |
| 3 | +from qiskit.visualization import plot_histogram |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +# Create a quantum circuit with two qubits |
| 7 | +bell_circuit = QuantumCircuit(2, 2) |
| 8 | + |
| 9 | +# Apply Hadamard gate to the first qubit |
| 10 | +bell_circuit.h(0) |
| 11 | + |
| 12 | +# Apply a CNOT gate with the first qubit as control and second qubit as target |
| 13 | +bell_circuit.cx(0, 1) |
| 14 | + |
| 15 | +# Add measurements to the circuit |
| 16 | +bell_circuit.measure([0, 1], [0, 1]) |
| 17 | + |
| 18 | +# Visualize the circuit |
| 19 | +print("Quantum Circuit:") |
| 20 | +print(bell_circuit.draw()) |
| 21 | + |
| 22 | +# Number of shots |
| 23 | +num_shots = 10000 |
| 24 | + |
| 25 | +# Simulate the circuit using the Aer simulator |
| 26 | +simulator = Aer.get_backend('qasm_simulator') |
| 27 | + |
| 28 | +# Transpile the circuit for optimization on the simulator backend |
| 29 | +transpiled_circuit = transpile(bell_circuit, simulator) |
| 30 | + |
| 31 | +# Execute the transpiled circuit on the simulator with specified number of shots |
| 32 | +result = simulator.run(transpiled_circuit, shots=num_shots).result() |
| 33 | + |
| 34 | +# Get measurement counts from results |
| 35 | +counts = result.get_counts(bell_circuit) |
| 36 | +print("\nMeasurement Results:") |
| 37 | +print(counts) |
| 38 | + |
| 39 | +# Plot histogram using Matplotlib's built-in function for histograms in Qiskit visualization module. |
| 40 | +plot_histogram(counts) |
| 41 | +plt.title('Bell State Measurement Results') |
| 42 | +plt.ylabel('Counts') |
| 43 | +plt.show() |
0 commit comments