Skip to content

Bloch Sphere Visualization

API reference for Bloch sphere visualization functions in pyqpanda3. These functions render single-qubit states and circuits as interactive or static plots on the Bloch sphere.

plot_bloch

Unified entry point that automatically dispatches to the appropriate Bloch visualization function based on the input type.

Signature

python
plot_bloch(
    state,
    title: str = '',
    figsize: Optional[tuple] = None,
    save: Optional[str] = None
)

Parameters

ParameterTypeDescription
statearray_like or QCircuitInput to visualize. A 3-element list/tuple dispatches to plot_bloch_vector; a longer iterable dispatches to plot_bloch_multivector; a QCircuit or QProg dispatches to plot_bloch_circuit.
titlestrPlot title (default '').
figsizetuple, optionalFigure size in inches.
savestr, optionalFile path to save the output image.

Dispatch Behavior

Input TypeDispatches To
list / tuple of length 3plot_bloch_vector
Any iterable (e.g., state vector)plot_bloch_multivector
QCircuit / QProgplot_bloch_circuit

Examples

python
from pyqpanda3.visualization import plot_bloch

# Single Bloch vector (|+> state on X axis)
plot_bloch([1, 0, 0], title="X State")

# Multi-qubit state vector
plot_bloch([1, 0, 0, 0], title="Bell-like State")

# Save to file
plot_bloch([0, 0, 1], title="Z State", save="bloch.png")

plot_bloch_vector

Plot a single 3-element Bloch vector on a Bloch sphere.

Signature

python
plot_bloch_vector(
    bloch,
    title="bloch",
    axis_obj=None,
    fig_size=None
)

Parameters

ParameterTypeDescription
blocharray_likeA 3-element sequence [x, y, z] representing the Bloch vector.
titlestrPlot title (default "bloch").
axis_objmatplotlib.axes.Axes, optionalAn existing 3D axes to draw on. If None, a new figure is created.
fig_sizetuple, optionalFigure size in inches (default (5, 5)).

Returns

  • matplotlib.figure.Figure when axis_obj is None.
  • None when drawing onto an existing axis_obj.

Examples

python
from pyqpanda3.visualization import plot_bloch_vector

# |0> state (north pole)
plot_bloch_vector([0, 0, 1], title="|0> State")

# |+> state (positive X axis)
plot_bloch_vector([1, 0, 0], title="|+> State")

# |+i> state (positive Y axis)
plot_bloch_vector([0, 1, 0], title="|+i> State")

plot_bloch_multivector

Plot one Bloch sphere per qubit for a multi-qubit quantum state. Each sphere shows the reduced Bloch vector for that qubit computed via the partial trace.

Signature

python
plot_bloch_multivector(
    state,
    title='',
    fig_size=None
)

Parameters

ParameterTypeDescription
statearray_likeQuantum state vector. Length must be a power of 2 (e.g., 4 for 2 qubits, 8 for 3 qubits).
titlestrOverall figure title (default '').
fig_sizetuple, optionalFigure size in inches.

Returns

matplotlib.figure.Figure -- the figure containing all Bloch sphere subplots.

Examples

python
from pyqpanda3.visualization import plot_bloch_multivector

# Two-qubit state: both qubits in |+> (equal superposition)
plot_bloch_multivector([0.5, 0.5, 0.5, 0.5], title="Two Qubits in |+>")

# Single qubit in |0> state
plot_bloch_multivector([1, 0], title="Single Qubit |0>")

# Bell state |00> + |11> (each qubit reduced state is maximally mixed)
plot_bloch_multivector([1, 0, 0, 1] / 1.414, title="Bell State")

plot_bloch_circuit

Animate a single-qubit quantum circuit on a Bloch sphere. The state vector is updated frame-by-frame as each gate is applied, producing a smooth rotation animation.

Signature

python
plot_bloch_circuit(
    circuit,
    trace=True,
    saveas=None,
    fps=20,
    secs_per_gate=1
)

Parameters

ParameterTypeDescription
circuitQCircuit or QProgSingle-qubit quantum circuit to animate. Only supports gates: H, X, Y, Z, RX, RY, RZ, S, S†, T, T†, U1.
traceboolWhether to display the path traced by the state vector (default True).
saveasstr, optionalFile path to save the animation (e.g., 'bloch.gif'). If None, the animation is displayed interactively.
fpsintFrames per gate, controlling animation smoothness (default 20).
secs_per_gatefloatDuration in seconds for each gate's animation segment (default 1).

Supported Gates

GateNotes
HHadamard
X, Y, ZPauli gates
RX, RY, RZParameterized rotations (angle in radians)
S, SPhase and inverse phase
T, TT gate and inverse T gate
U1Single-parameter phase gate

Raises

  • ImportError -- if Matplotlib is not installed.
  • RuntimeError -- if the circuit is empty, contains unsupported gates, or acts on more than one qubit.

Examples

python
from pyqpanda3.core import QProg, H, RX, Qubit
from pyqpanda3.visualization import plot_bloch_circuit

# Simple Hadamard circuit
q = Qubit(0)
prog = QProg()
prog << H(q)

# Display interactive animation
plot_bloch_circuit(prog)

# Save animation to file
plot_bloch_circuit(prog, saveas="hadamard.gif", fps=30)

# Rotation circuit with trace
prog2 = QProg()
prog2 << RX(q, 3.14159) << H(q)
plot_bloch_circuit(prog2, trace=True, secs_per_gate=2)

See Also

Released under the MIT License.