pyqpanda3 v0.3.4 introduces a new unified visualization module with three main plotting functions: plot_state, plot_bloch, and plot_probabilities.
Visualization Features
The pyqpanda3.visualization module provides comprehensive quantum state visualization:
State visualization. plot_state() renders quantum state vectors as 3D city plots or density matrix visualizations.
Bloch sphere visualization. plot_bloch() plots quantum states on the Bloch sphere. It accepts state vectors (for multi-qubit Bloch spheres), Bloch vectors (3-element lists for single-qubit), or quantum circuits.
Probability distribution. plot_probabilities() plots probability distributions from measurement results, with support for top_k filtering.
python
from pyqpanda3.core import QProg, H, CNOT, RX, RY
from pyqpanda3.visualization import plot_state, plot_bloch, plot_probabilities
import numpy as np
# Build a Bell state circuit
prog = QProg()
prog << H(0) << CNOT(0, 1)
# Visualize the quantum circuit
prog.draw()
# Output:
# q[0] |0>──H──●────────
# │
# q[1] |0>──────⊕────────
# Plot the state vector as a 3D city plot
state = np.array([1/np.sqrt(2), 0, 0, 1/np.sqrt(2)], dtype=complex)
plot_state(state)
# Visualize on the Bloch sphere
plot_bloch(state)
# Probability distribution from measurement results
probs = {'00': 0.5, '11': 0.5}
plot_probabilities(probs)
# Show only the top 3 results
plot_probabilities(probs, top_k=3)
# Single-qubit Bloch sphere with a rotation
qprog = QProg()
qprog << RX(0, 1.5) << RY(0, 0.8)
plot_bloch(qprog)