def process_data(rho): """ Sort rho data """ result = dict() num = int(np.log2(len(rho))) labels = list(map(lambda x: x.to_label(), pauli_group(num))) values = list(map(lambda x: np.real(np.trace(np.dot(x.to_matrix(), rho))), pauli_group(num))) for position, label in enumerate(labels): result[label] = values[position] return result
def plot_state_paulivec(rho, title="", figsize=None, color=None): """Plot the paulivec representation of a quantum state. Plot a bargraph of the mixed state rho over the pauli matrices Args: rho (ndarray): Numpy array for state vector or density matrix title (str): a string that represents the plot title figsize (tuple): Figure size in inches. color (list or str): Color of the expectation value bars. Returns: matplotlib.Figure: The matplotlib.Figure of the visualization Raises: ImportError: Requires matplotlib. """ if not HAS_MATPLOTLIB: raise ImportError('Must have Matplotlib installed.') rho = _validate_input_state(rho) if figsize is None: figsize = (7, 5) num = int(np.log2(len(rho))) labels = list(map(lambda x: x.to_label(), pauli_group(num))) values = list( map(lambda x: np.real(np.trace(np.dot(x.to_matrix(), rho))), pauli_group(num))) numelem = len(values) if color is None: color = "#648fff" ind = np.arange(numelem) # the x locations for the groups width = 0.5 # the width of the bars fig, ax = plt.subplots(figsize=figsize) ax.grid(zorder=0, linewidth=1, linestyle='--') ax.bar(ind, values, width, color=color, zorder=2) ax.axhline(linewidth=1, color='k') # add some text for labels, title, and axes ticks ax.set_ylabel('Expectation value', fontsize=14) ax.set_xticks(ind) ax.set_yticks([-1, -0.5, 0, 0.5, 1]) ax.set_xticklabels(labels, fontsize=14, rotation=70) ax.set_xlabel('Pauli', fontsize=14) ax.set_ylim([-1, 1]) ax.set_facecolor('#eeeeee') for tick in ax.xaxis.get_major_ticks() + ax.yaxis.get_major_ticks(): tick.label.set_fontsize(14) ax.set_title(title, fontsize=16) if get_backend() in ['module://ipykernel.pylab.backend_inline', 'nbAgg']: plt.close(fig) return fig
def plot_state_paulivec(rho, title="", figsize=None, color=None, ax=None): """Plot the paulivec representation of a quantum state. Plot a bargraph of the mixed state rho over the pauli matrices Args: rho (ndarray): Numpy array for state vector or density matrix title (str): a string that represents the plot title figsize (tuple): Figure size in inches. color (list or str): Color of the expectation value bars. ax (matplotlib.axes.Axes): An optional Axes object to be used for the visualization output. If none is specified a new matplotlib Figure will be created and used. Additionally, if specified there will be no returned Figure since it is redundant. Returns: matplotlib.Figure: The matplotlib.Figure of the visualization if the ``ax`` kwarg is not set Raises: ImportError: Requires matplotlib. Example: .. jupyter-execute:: from qiskit import QuantumCircuit, BasicAer, execute from qiskit.visualization import plot_state_paulivec %matplotlib inline qc = QuantumCircuit(2, 2) qc.h(0) qc.cx(0, 1) qc.measure([0, 1], [0, 1]) backend = BasicAer.get_backend('statevector_simulator') job = execute(qc, backend).result() plot_state_paulivec(job.get_statevector(qc), color='midnightblue', title="New PauliVec plot") """ if not HAS_MATPLOTLIB: raise ImportError('Must have Matplotlib installed.') rho = _validate_input_state(rho) if figsize is None: figsize = (7, 5) num = int(np.log2(len(rho))) labels = list(map(lambda x: x.to_label(), pauli_group(num))) values = list( map(lambda x: np.real(np.trace(np.dot(x.to_matrix(), rho))), pauli_group(num))) numelem = len(values) if color is None: color = "#648fff" ind = np.arange(numelem) # the x locations for the groups width = 0.5 # the width of the bars if ax is None: return_fig = True fig, ax = plt.subplots(figsize=figsize) else: return_fig = False fig = ax.get_figure() ax.grid(zorder=0, linewidth=1, linestyle='--') ax.bar(ind, values, width, color=color, zorder=2) ax.axhline(linewidth=1, color='k') # add some text for labels, title, and axes ticks ax.set_ylabel('Expectation value', fontsize=14) ax.set_xticks(ind) ax.set_yticks([-1, -0.5, 0, 0.5, 1]) ax.set_xticklabels(labels, fontsize=14, rotation=70) ax.set_xlabel('Pauli', fontsize=14) ax.set_ylim([-1, 1]) ax.set_facecolor('#eeeeee') for tick in ax.xaxis.get_major_ticks() + ax.yaxis.get_major_ticks(): tick.label.set_fontsize(14) ax.set_title(title, fontsize=16) if return_fig: if get_backend() in [ 'module://ipykernel.pylab.backend_inline', 'nbAgg' ]: plt.close(fig) return fig