pyqpanda.Visualization ====================== .. py:module:: pyqpanda.Visualization .. autoapi-nested-parse:: QPanda Python Copyright (C) Origin Quantum 2017-2024 Licensed Under Apache Licence 2.0 Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/pyqpanda/Visualization/bloch/index /autoapi/pyqpanda/Visualization/bloch_plot/index /autoapi/pyqpanda/Visualization/circuit_composer/index /autoapi/pyqpanda/Visualization/circuit_draw/index /autoapi/pyqpanda/Visualization/circuit_info/index /autoapi/pyqpanda/Visualization/circuit_style/index /autoapi/pyqpanda/Visualization/draw_probability_map/index /autoapi/pyqpanda/Visualization/exceptions/index /autoapi/pyqpanda/Visualization/matplotlib_draw/index /autoapi/pyqpanda/Visualization/parameterexpression/index /autoapi/pyqpanda/Visualization/pi_check/index /autoapi/pyqpanda/Visualization/quantum_state_plot/index /autoapi/pyqpanda/Visualization/utils/index Functions --------- .. autoapisummary:: pyqpanda.Visualization.draw_qprog pyqpanda.Visualization.show_prog_info_count pyqpanda.Visualization.draw_probability pyqpanda.Visualization.draw_probability_dict pyqpanda.Visualization.plot_state_city pyqpanda.Visualization.plot_density_matrix pyqpanda.Visualization.state_to_density_matrix pyqpanda.Visualization.plot_bloch_circuit pyqpanda.Visualization.plot_bloch_vector pyqpanda.Visualization.plot_bloch_multivector Package Contents ---------------- .. py:function:: draw_qprog(prog, output=None, scale=0.7, fold=30, filename=None, with_logo=False, line_length=100, NodeIter_first=None, NodeIter_second=None, console_encode_type='utf8') Visualizes a quantum circuit in various formats based on the specified output type. Supported output formats include: - 'text': ASCII art representation suitable for console output. - 'pic': Colorful image representation rendered in Python. - 'latex': LaTeX source code for the circuit, suitable for inclusion in LaTeX documents. Args: prog (object): The quantum circuit object to be visualized. scale (float, optional): The scale factor for the image output. Defaults to 0.7. fold (int, optional): The maximum fold size for the image output. Defaults to 30. filename (str, optional): The file path to save the image output. Defaults to 'QCircuit_pic.jpg' for 'pic' output. For 'latex' output, defaults to 'QCircuit_latex.tex'. with_logo (bool, optional): Include the pyQPanda logo in the LaTeX output. Defaults to False. line_length (int, optional): The length of each line in the ASCII art output. Defaults to 100. NodeIter_first (object, optional): The starting position of the circuit segment to be visualized. NodeIter_second (object, optional): The ending position of the circuit segment to be visualized. console_encode_type (str, optional): The console encoding type, with 'utf8' and 'gbk' supported. Defaults to 'utf8'. Returns: str: The visual representation of the quantum circuit, which is returned as a string depending on the output format. .. py:function:: show_prog_info_count(prog) Visualizes the distribution of nodes and layers within a quantum circuit represented by `prog`. This function creates a pie chart to display the proportion of different types of nodes and layers in the quantum circuit. It utilizes the information obtained from the `count_prog_info` function, which provides counts for single, double, and multi-control gate nodes, as well as layer-related metrics. Args: prog (object): The quantum circuit object for which the distribution is to be visualized. Returns: None: This function does not return any value; it only displays the visualization. The visualization is composed of two pie charts: The first chart shows the distribution of node types: Single Gate, Double Gate, Multi Control Gate, and Other Nodes. The second chart illustrates the distribution of layer types: Single Gate Layer, Double Gate Layer, and Other Layers. Note: This function assumes that the `count_prog_info` function is properly implemented and that the `prog` object has the necessary attributes to compute the required counts. .. py:function:: draw_probability(list) Generate a bar plot visualizing the probabilities of a quantum state. The function takes a dictionary containing quantum state keys and their corresponding probabilities, normalizes the probabilities, and plots them using a bar chart. Args: probability_dict (dict): A dictionary where keys represent the quantum states and values represent their respective probabilities. Returns: None: This function does not return a value. It plots the probabilities directly. Notes: The bar plot uses the Chinese font 'SimHei' and ensures that negative symbols are not displayed in the axes. The x-axis represents the quantum states, while the y-axis represents the probabilities. The function includes a grid on the y-axis for better readability. .. py:function:: draw_probability_dict(prob_dict) Generate a bar plot representing the probabilities of a quantum state from a given dictionary. Args: prob_dict (dict): A dictionary where keys are quantum state labels and values are their corresponding probabilities. Returns: None: This function does not return a value; it displays a bar plot. The function configures Matplotlib to use SimHei font for better readability in Chinese characters. It filters out non-positive probabilities, calculates normalized probabilities, and then plots them as a bar chart. The plot includes a grid and labeled axes for clarity. This utility is intended for use within the pyQPanda package, which is designed for programming quantum computers. .. py:function:: plot_state_city(state, title='', figsize=None, color=None, ax_real=None, ax_imag=None) Plots the real and imaginary parts of a quantum state in a 3D bar plot. Args: state (list of complex): The quantum state represented as a list of complex numbers. title (str, optional): The title of the plot. Defaults to an empty string. figsize (tuple, optional): The size of the figure in inches. Defaults to (15, 5). color (list, optional): A list of colors to use for the real and imaginary parts. Defaults to None. ax_real (matplotlib.axes.Axes, optional): The axes object for the real part plot. Defaults to None. ax_imag (matplotlib.axes.Axes, optional): The axes object for the imaginary part plot. Defaults to None. Returns: matplotlib.figure.Figure: The figure object containing the plots. Raises: RuntimeError: If the input 'state' is not a valid quantum state. Notes: This function utilizes the pyQPanda package for quantum computing visualization. The function assumes that the input 'state' is a list of complex numbers representing the density matrix of a quantum state. The real and imaginary parts of the state are plotted side by side in a single figure. The function supports custom titles, figure sizes, and color schemes. The function also supports plotting directly onto existing axes objects for integration with other visualizations. .. py:function:: plot_density_matrix(M, xlabels=None, ylabels=None, title=None, limits=None, phase_limits=None, fig=None, axis_vals=None, threshold=None) Plots the density matrix of a quantum state as a 3D bar plot, visualizing the density of the quantum state and its phase information. Args: M (list): A list of complex numbers representing the density matrix. xlabels (list, optional): Labels for the x-axis ticks. ylabels (list, optional): Labels for the y-axis ticks. title (str, optional): Title for the plot. limits (list, optional): Limits for the z-axis. phase_limits (list, optional): Limits for the phase angle visualization (default: -π to π). fig (matplotlib.figure.Figure, optional): Existing figure object to plot on. axis_vals (matplotlib.axes.Axes, optional): Existing 3D axis object to plot on. threshold (float, optional): Threshold for highlighting density values above this value. Returns: tuple: A tuple containing the `fig` and `axis_vals` objects. Raises: RuntimeError: If the input `M` is not a valid quantum state density matrix. .. py:function:: state_to_density_matrix(quantum_state) Converts a given quantum state into its corresponding density matrix representation. Args: quantum_state (complex list): A list of complex numbers representing the quantum state vector. Returns: numpy.ndarray: The density matrix derived from the quantum state. Raises: RuntimeError: If the provided quantum state is not valid (not a 2^n state vector or not complex). .. py:function:: plot_bloch_circuit(circuit, trace=True, saveas=None, fps=20, secs_per_gate=1) Visualizes the evolution of a quantum circuit on a Bloch sphere. Args: circuit (object): The quantum circuit to be visualized. trace (bool): Flag to indicate whether to display the path of the state vector. saveas (str): The filename to save the animation. If None, the animation is displayed. fps (int): The frames per second for the animation. secs_per_gate (float): The duration in seconds for each gate operation. Returns: None: The function does not return a value; it generates a visualization. Raises: ImportError: If Matplotlib is not installed. RuntimeError: If the circuit contains no gates that can be visualized on a Bloch sphere. .. py:function:: plot_bloch_vector(bloch, title='bloch', axis_obj=None, fig_size=None) Visualizes the evolution of a quantum circuit on a Bloch sphere. Args: circuit (object): The quantum circuit to be visualized. trace (bool): Flag to indicate whether to display the path of the state vector. saveas (str): The filename to save the animation. If None, the animation is displayed. fps (int): The frames per second for the animation. secs_per_gate (float): The duration in seconds for each gate operation. Returns: None: The function does not return a value; it generates a visualization. Raises: ImportError: If Matplotlib is not installed. RuntimeError: If the circuit contains no gates that can be visualized on a Bloch sphere. .. py:function:: plot_bloch_multivector(state, title='', fig_size=None) Visualizes a quantum state on a Bloch sphere. Args: state (list): The quantum state vector. title (str): The title for the plot. fig_size (tuple): The size of the figure in inches. Returns: matplotlib.figure.Figure: The created figure with the Bloch plot. Raises: ImportError: If Matplotlib is not installed. Description: This function converts a quantum state vector into a Bloch sphere representation. It generates a figure showing the Bloch vector for each qubit in the state vector. The figure size can be customized, and the plot includes a title. If the backend is inline or notebook, the figure is automatically closed after display.