def state_to_latex(state, dims=None, **args): """Return a Latex representation of a state. Wrapper function for `qiskit.visualization.array_to_latex` to add dims if necessary. Intended for use within `state_drawer`. Args: state (`Statevector` or `DensityMatrix`): State to be drawn dims (bool): Whether to display the state's `dims` *args: Arguments to be passed directly to `array_to_latex` Returns: `str`: Latex representation of the state """ if dims is None: # show dims if state is not only qubits if set(state.dims()) == {2}: dims = False else: dims = True prefix = "" suffix = "" if dims: prefix = "\\begin{align}\n" dims_str = state._op_shape.dims_l() suffix = f"\\\\\n\\text{{dims={dims_str}}}\n\\end{{align}}" latex_str = array_to_latex(state._data, source=True, **args) return prefix + latex_str + suffix
def state_to_latex(state: Union[Statevector, DensityMatrix], dims: bool = None, convention: str = "ket", **args) -> str: """Return a Latex representation of a state. Wrapper function for `qiskit.visualization.array_to_latex` for convention 'vector'. Adds dims if necessary. Intended for use within `state_drawer`. Args: state: State to be drawn dims (bool): Whether to display the state's `dims` convention (str): Either 'vector' or 'ket'. For 'ket' plot the state in the ket-notation. Otherwise plot as a vector **args: Arguments to be passed directly to `array_to_latex` for convention 'ket' Returns: Latex representation of the state """ if dims is None: # show dims if state is not only qubits if set(state.dims()) == {2}: dims = False else: dims = True prefix = "" suffix = "" if dims: prefix = "\\begin{align}\n" dims_str = state._op_shape.dims_l() suffix = f"\\\\\n\\text{{dims={dims_str}}}\n\\end{{align}}" operator_shape = state._op_shape # we only use the ket convetion for qubit statevectors # this means the operator shape should hve no input dimensions and all output dimensions equal to 2 is_qubit_statevector = len(operator_shape.dims_r()) == 0 and set( operator_shape.dims_l()) == {2} if convention == "ket" and is_qubit_statevector: latex_str = _state_to_latex_ket(state._data) else: latex_str = array_to_latex(state._data, source=True, **args) return prefix + latex_str + suffix