Skip to content

OriginIR Conversion ​

API reference for functions that convert between quantum programs (QProg) and the OriginIR intermediate representation format.

convert_originir_string_to_qprog ​

Parses an OriginIR instruction-set string and returns the corresponding quantum program.

Signature ​

python
convert_originir_string_to_qprog(
    originir_str: str,
) -> QProg

Parameters ​

ParameterTypeDescription
originir_strstrAn OriginIR instruction-set string to be converted.

Returns ​

The converted QProg quantum program.

Raises ​

ExceptionDescription
RuntimeErrorIf the OriginIR string contains syntax errors or references undefined gates or qubits.

Examples ​

python
from pyqpanda3.intermediate_compiler import convert_originir_string_to_qprog

ir_str = """
QINIT 2
CREG 2
H q[0]
CNOT q[0],q[1]
MEASURE q[0],c[0]
MEASURE q[1],c[1]
"""
prog = convert_originir_string_to_qprog(ir_str)

convert_originir_file_to_qprog ​

Reads an OriginIR file and returns the corresponding quantum program.

Signature ​

python
convert_originir_file_to_qprog(
    ir_filepath: str,
) -> QProg

Parameters ​

ParameterTypeDescription
ir_filepathstrPath to a file containing OriginIR instruction-set text.

Returns ​

The converted QProg quantum program.

Raises ​

ExceptionDescription
RuntimeErrorIf the file cannot be read or contains syntax errors.
FileNotFoundErrorIf the specified file path does not exist.

Examples ​

python
import tempfile
from pyqpanda3.intermediate_compiler import convert_originir_file_to_qprog

ir_content = "QINIT 2\nCREG 2\nH q[0]\nCNOT q[0],q[1]\n"
with tempfile.NamedTemporaryFile(mode="w", suffix=".originir", delete=False) as f:
    f.write(ir_content)
    tmp_path = f.name

prog = convert_originir_file_to_qprog(tmp_path)
import os
os.unlink(tmp_path)

convert_qprog_to_originir ​

Converts a quantum program to its OriginIR string representation.

Signature ​

python
convert_qprog_to_originir(
    prog: QProg,
    precision: int = 8,
) -> str

Parameters ​

ParameterTypeDescription
progQProgThe quantum program to convert.
precisionintNumber of decimal places for floating-point gate parameters. Must be a non-negative integer. Defaults to 8.

Returns ​

The OriginIR instruction-set string representing the quantum program.

Examples ​

python
from pyqpanda3.intermediate_compiler import convert_qprog_to_originir
from pyqpanda3.core import QProg, H, CNOT

prog = QProg() << H(0) << CNOT(0, 1)
ir_str = convert_qprog_to_originir(prog, precision=6)
print(ir_str)

运行结果:

QINIT 2
CREG 1
H q[0]
CNOT q[0],q[1]

See Also ​

Released under the MIT License.