Skip to content

Commit ea16157

Browse files
committed
Create bellstates.py
1 parent 484bfea commit ea16157

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

doc/src/week3/bellstates.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from qiskit import QuantumCircuit, transpile, assemble
2+
from qiskit.visualization import circuit_drawer
3+
import qiskit_aer
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 the control and the second qubit as the 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)
21+
22+
# Number of shots
23+
num_shots = 10000
24+
25+
# Simulate the circuit using the Aer simulator
26+
simulator = qiskit_aer.Aer.get_backend('qasm_simulator')
27+
28+
# Transpile the circuit for the simulator
29+
transpiled_circuit = transpile(bell_circuit, simulator)
30+
31+
# Execute the transpiled circuit on the simulator with the specified number of shots
32+
result = simulator.run(transpiled_circuit, shots=num_shots).result()
33+
34+
# Get measurement counts
35+
counts = result.get_counts(bell_circuit)
36+
37+
# Get and print the measurement results
38+
counts = result.get_counts(bell_circuit)
39+
print("\nMeasurement Results:")
40+
print(counts)
41+
42+
# Plot the histogram using Matplotlib
43+
plt.bar(counts.keys(), counts.values())
44+
plt.title('Bell State Measurement Results')
45+
plt.ylabel('Counts')
46+
plt.show()
47+
48+

0 commit comments

Comments
 (0)