Skip to content

Commit 1a9f246

Browse files
authoredJan 19, 2025··
Create quantum_state_visualization.py
1 parent 3a8ba97 commit 1a9f246

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# quantum_state_visualization.py
2+
import numpy as np
3+
from qiskit import QuantumCircuit, Aer, execute
4+
from qiskit.visualization import plot_bloch_multivector, plot_state_qsphere, plot_histogram
5+
from qiskit.quantum_info import Statevector
6+
7+
def create_sample_circuit():
8+
"""
9+
Create a sample quantum circuit to generate a quantum state.
10+
11+
Returns:
12+
- QuantumCircuit: The constructed quantum circuit
13+
"""
14+
circuit = QuantumCircuit(1) # 1 qubit
15+
16+
# Prepare a superposition state
17+
circuit.h(0) # Apply Hadamard gate to create |+> state
18+
19+
return circuit
20+
21+
def run_circuit_and_get_statevector(circuit):
22+
"""
23+
Run the quantum circuit and return the state vector.
24+
25+
Parameters:
26+
- circuit: QuantumCircuit object
27+
28+
Returns:
29+
- Statevector: The state vector of the quantum system
30+
"""
31+
# Use the Aer's statevector simulator
32+
simulator = Aer.get_backend('statevector_simulator')
33+
34+
# Execute the circuit
35+
job = execute(circuit, simulator)
36+
result = job.result()
37+
38+
# Get the state vector
39+
statevector = result.get_statevector(circuit)
40+
41+
return statevector
42+
43+
def visualize_state(statevector):
44+
"""
45+
Visualize the quantum state using Bloch sphere and Q-sphere representations.
46+
47+
Parameters:
48+
- statevector: State vector of the quantum system
49+
"""
50+
print("State Vector:", statevector)
51+
52+
# Plot Bloch sphere representation
53+
plot_bloch_multivector(statevector).show()
54+
55+
# Plot Q-sphere representation
56+
plot_state_qsphere(statevector).show()
57+
58+
if __name__ == "__main__":
59+
# Create a sample quantum circuit
60+
circuit = create_sample_circuit()
61+
62+
# Run the circuit and get the state vector
63+
statevector = run_circuit_and_get_statevector(circuit)
64+
65+
# Visualize the quantum state
66+
visualize_state(statevector)

0 commit comments

Comments
 (0)
Please sign in to comment.