Skip to content

Commit

Permalink
Create entanglement_simulation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jan 19, 2025
1 parent 4f60e58 commit 332728a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions quantum/simulations/entanglement_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# entanglement_simulation.py
import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram, plot_bloch_multivector
from qiskit.quantum_info import Statevector

def create_entangled_state():
"""
Create a quantum circuit that generates a Bell state (entangled state).
Returns:
- QuantumCircuit: The constructed quantum circuit
"""
circuit = QuantumCircuit(2, 2) # 2 qubits and 2 classical bits

# Apply Hadamard gate to the first qubit
circuit.h(0)

# Apply CNOT gate to create entanglement
circuit.cx(0, 1)

# Measure the qubits
circuit.measure(range(2), range(2))

return circuit

def run_entanglement_simulation():
"""
Run the entanglement simulation and return the results.
Returns:
- Counts of the measurement results
- Statevector of the quantum system
"""
# Create the entangled state circuit
circuit = create_entangled_state()

# Use the Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1024)
result = job.result()

# Get measurement counts
counts = result.get_counts(circuit)

# Get the state vector for visualization
statevector = Statevector.from_dict(counts)

return counts, statevector

def visualize_results(counts, statevector):
"""
Visualize the results of the entanglement simulation.
Parameters:
- counts: Measurement results
- statevector: State vector of the quantum system
"""
print("Counts:", counts)
plot_histogram(counts).show()
plot_bloch_multivector(statevector).show()

if __name__ == "__main__":
# Run the entanglement simulation
counts, statevector = run_entanglement_simulation()

# Visualize the results
visualize_results(counts, statevector)

0 comments on commit 332728a

Please sign in to comment.