コード例 #1
0
def display(circuit: Program,
            settings: Optional[DiagramSettings] = None,
            **image_options: Any) -> Image:
    """
    Displays a PyQuil circuit as an IPython image object.

    .. note::

       For this to work, you need two external programs, ``pdflatex`` and ``convert``,
       to be installed and accessible via your shell path.

       Further, your LaTeX installation should include class and style files for ``standalone``,
       ``geometry``, ``tikz``, and ``quantikz``. If it does not, you need to install
       these yourself.

    :param circuit: The circuit to be drawn, represented as a pyquil program.
    :param settings: An optional object of settings controlling diagram rendering and layout.
    :return: PNG image render of the circuit.
    """
    # The conversion process relies on two passes: first, 'pdflatex' is called to
    # render the tikz to a pdf. Second, Imagemagick's 'convert' is called to translate
    # this to a png image.

    pdflatex_path = "C:\\Program Files\\MiKTeX\\miktex\\bin\\x64\\pdflatex.EXE"
    convert_path = "C:\\Program Files\\ImageMagick-7.0.10-Q16-HDRI\\convert.exe"

    tmpdir = "./"
    texfile = open(os.path.join(tmpdir, "circuit.tex"), "w")
    texfile.write(to_latex(circuit))

    texfile = open(os.path.join(tmpdir, "circuit.tex"), "w")
    texfile.write(to_latex(circuit))
    result = subprocess.run(
        [
            pdflatex_path, "-halt-on-error", "-output-directory", tmpdir,
            texfile.name
        ],
        stdout=subprocess.PIPE,
    )

    png = os.path.join(tmpdir, "circuit.png")
    pdf = os.path.join(tmpdir, "circuit.pdf")
    result = subprocess.run([convert_path, "-density", "100", pdf, png],
                            stderr=subprocess.PIPE)

    return Image(filename=png, **image_options)
コード例 #2
0
ファイル: _ipython.py プロジェクト: tsatir/pyquil
def display(circuit: Program,
            settings: Optional[DiagramSettings] = None,
            **image_options) -> Image:
    """
    Displays a PyQuil circuit as an IPython image object.

    .. note::

       For this to work, you need two external programs, ``pdflatex`` and ``convert``,
       to be installed and accessible via your shell path.

       Further, your LaTeX installation should include class and style files for ``standalone``,
       ``geometry``, ``tikz``, and ``quantikz``. If it does not, you need to install
       these yourself.

    :param Program circuit: The circuit to be drawn, represented as a pyquil program.
    :param DiagramSettings settings: An optional object of settings controlling diagram rendering and layout.
    :return: PNG image render of the circuit.
    :rtype: IPython.display.Image
    """
    # The conversion process relies on two passes: first, 'pdflatex' is called to
    # render the tikz to a pdf. Second, Imagemagick's 'convert' is called to translate
    # this to a png image.
    pdflatex_path = shutil.which("pdflatex")
    convert_path = shutil.which("convert")

    if pdflatex_path is None:
        raise FileNotFoundError("Unable to locate 'pdflatex'.")
    if convert_path is None:
        raise FileNotFoundError("Unable to locate 'convert'.")

    with tempfile.TemporaryDirectory() as tmpdir:
        with open(os.path.join(tmpdir, "diagram.tex"), "w") as texfile:
            texfile.write(to_latex(circuit, settings))

        result = subprocess.run([pdflatex_path, "-halt-on-error",
                                 "-output-directory", tmpdir,
                                 texfile.name],
                                stdout=subprocess.PIPE)
        if result.returncode != 0:
            msg = "'pdflatex' terminated with return code {}.".format(result.returncode)
            # NOTE: pdflatex writes all error messages to stdout
            if result.stdout:
                msg += " Transcript:\n\n" + result.stdout.decode("utf-8")
            raise RuntimeError(msg)

        png = os.path.join(tmpdir, "diagram.png")
        pdf = os.path.join(tmpdir, "diagram.pdf")

        result = subprocess.run([convert_path, "-density", "300", pdf, png],
                                 stderr=subprocess.PIPE)
        if result.returncode != 0:
            msg = "'convert' terminated with return code {}.".format(result.returncode)
            if result.stderr:
                msg += " Transcript:\n\n" + result.stderr.decode("utf-8")
            raise RuntimeError(msg)

        return Image(filename=png, **image_options)
コード例 #3
0
ファイル: latex_generation.py プロジェクト: lupify/pyquil
def to_latex(circuit: Program,
             settings: Optional[DiagramSettings] = None) -> str:
    from pyquil.latex._main import to_latex

    warnings.warn(
        '"pyquil.latex.latex_generation.to_latex" has been moved -- please import it'
        'as "from pyquil.latex import to_latex going forward"',
        FutureWarning,
    )
    return to_latex(circuit, settings)