qpandalite.qasm package#

Submodules#

qpandalite.qasm.exceptions module#

QASM parser exceptions module.

This module defines custom exceptions for OpenQASM 2.0 parsing, including errors for unsupported gates and register-related issues.

Key exports:

NotSupportedGateError: Exception for unsupported quantum gates. RegisterNotFoundError: Exception for missing quantum/classical registers. RegisterOutOfRangeError: Exception for register index out of bounds. RegisterDefinitionError: Exception for invalid register definitions.

exception qpandalite.qasm.exceptions.NotSupportedGateError[源代码]

基类:Exception

Raised when an unsupported gate is encountered in OpenQASM 2.

exception qpandalite.qasm.exceptions.RegisterDefinitionError[源代码]

基类:Exception

Raised when a register definition is invalid (e.g., duplicate name, empty).

exception qpandalite.qasm.exceptions.RegisterNotFoundError[源代码]

基类:Exception

Raised when a quantum or classical register is not found.

exception qpandalite.qasm.exceptions.RegisterOutOfRangeError[源代码]

基类:Exception

Raised when a register index exceeds its defined size.

qpandalite.qasm.qasm_base_parser module#

OpenQASM 2.0 base parser module.

This module provides the base parser for OpenQASM 2.0 quantum circuit representation, including parsing qreg/creg definitions, quantum operations, and measurement statements.

Key exports:

OpenQASM2_BaseParser: Base parser class for OpenQASM 2.0 circuits.

class qpandalite.qasm.qasm_base_parser.OpenQASM2_BaseParser[源代码]

基类:object

Parser for OpenQASM 2.0 quantum circuit representation.

qregs

List of quantum register tuples (name, size).

cregs

List of classical register tuples (name, size).

n_qubit

Total number of qubits.

n_cbit

Total number of classical bits.

program_body

List of operation opcodes.

measure_qubits

List of measurement tuples (qubit, cbit).

parse(raw_qasm)[源代码]

Parse an OpenQASM 2.0 string and populate internal state.

参数:

raw_qasm -- OpenQASM 2.0 string to parse.

to_circuit()[源代码]

The function coverts OpenQASM string into qpandalite.Circuit object.

In the initilization phase, we need to notice that OriginIR-based quantum circuit doe not need to specify how many qregs and cregs used.

Parameters: - qasm_str: The quantum circuit of intersts in the OpenQASM format.

Returns: - origin_qcirc: The quantum circuit of intersts in the OriginIR format.

返回类型:

Circuit

to_originir()[源代码]

Convert parsed OpenQASM data to OriginIR string.

返回:

OriginIR string representation.

返回类型:

str

qpandalite.qasm.qasm_line_parser module#

OpenQASM 2.0 line parser module.

This module provides regex-based parsing for individual OpenQASM 2.0 lines, supporting qreg/creg definitions, 1-4 qubit gates, parameterized gates, and measurements.

Key exports:

OpenQASM2_LineParser: Parser class for individual OpenQASM 2.0 lines.

class qpandalite.qasm.qasm_line_parser.OpenQASM2_LineParser[源代码]

基类:object

Parser for individual OpenQASM 2.0 lines.

Provides regex-based parsing for OpenQASM 2.0 statements including qreg/creg definitions, 1-4 qubit gates, parameterized gates, and measurements.

any_parameters: str = '\\(([^()]+)\\)'
blank: str = ' *'
comma: str = ','
static handle_1q(line)[源代码]

Parse a 1-qubit gate line.

返回:

(op_name, qreg_name, qubit_index)

返回类型:

tuple

参数:

line (str)

static handle_1q1p(line)[源代码]

Parse a 1-qubit 1-parameter gate line.

返回:

(op_name, parameters, qreg_name, qubit_index)

返回类型:

tuple

参数:

line (str)

static handle_1q2p(line)[源代码]

Parse a 1-qubit 2-parameter gate line.

返回:

(op_name, parameters, qreg_name, qubit_index)

返回类型:

tuple

参数:

line (str)

static handle_1q3p(line)[源代码]

Parse a 1-qubit 3-parameter gate line.

返回:

(op_name, parameters, qreg_name, qubit_index)

返回类型:

tuple

参数:

line (str)

static handle_1q4p(line)[源代码]

Parse a 1-qubit 4-parameter gate line.

返回:

(op_name, parameters, qreg_name, qubit_index)

返回类型:

tuple

参数:

line (str)

static handle_1qnp(line, n_parameters)[源代码]

Parse a 1-qubit n-parameter gate line.

参数:
  • line (str) -- QASM line.

  • n_parameters (int) -- Expected number of parameters.

返回:

(op_name, parameters, qreg_name, qubit_index)

返回类型:

tuple

static handle_2q(line)[源代码]

Parse a 2-qubit gate line.

返回:

(op_name, qreg_name1, qubit_index1, qreg_name2, qubit_index2)

返回类型:

tuple

参数:

line (str)

static handle_2q1p(line)[源代码]

Parse a 2-qubit 1-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2)

返回类型:

tuple

参数:

line (str)

static handle_2q2p(line)[源代码]

Parse a 2-qubit 2-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2)

返回类型:

tuple

参数:

line (str)

static handle_2q3p(line)[源代码]

Parse a 2-qubit 3-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2)

返回类型:

tuple

参数:

line (str)

static handle_2q4p(line)[源代码]

Parse a 2-qubit 4-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2)

返回类型:

tuple

参数:

line (str)

static handle_2qnp(line, n_parameters)[源代码]

Parse a 2-qubit n-parameter gate line.

参数:
  • line (str) -- QASM line.

  • n_parameters (int) -- Expected number of parameters.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2)

返回类型:

tuple

static handle_3q(line)[源代码]

Parse a 3-qubit gate line.

返回:

(op_name, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3)

返回类型:

tuple

参数:

line (str)

static handle_3q1p(line)[源代码]

Parse a 3-qubit 1-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3)

返回类型:

tuple

参数:

line (str)

static handle_3q2p(line)[源代码]

Parse a 3-qubit 2-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3)

返回类型:

tuple

参数:

line (str)

static handle_3q3p(line)[源代码]

Parse a 3-qubit 3-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3)

返回类型:

tuple

参数:

line (str)

static handle_3q4p(line)[源代码]

Parse a 3-qubit 4-parameter gate line.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3)

返回类型:

tuple

参数:

line (str)

static handle_3qnp(line, n_parameters)[源代码]

Parse a 3-qubit n-parameter gate line.

参数:
  • line (str) -- QASM line.

  • n_parameters (int) -- Expected number of parameters.

返回:

(op_name, parameters, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3)

返回类型:

tuple

static handle_4q(line)[源代码]

Parse a 4-qubit gate line.

返回:

(op_name, qreg_name1, qubit_index1, qreg_name2, qubit_index2, qreg_name3, qubit_index3, qreg_name4, qubit_index4)

返回类型:

tuple

参数:

line (str)

static handle_creg(line)[源代码]

Parse a creg definition line.

参数:

line (str) -- QASM line defining a classical register.

返回:

(register_name, size)

返回类型:

tuple

static handle_measure(line)[源代码]

Parse a MEASURE statement line.

参数:

line (str) -- QASM measure statement.

返回:

(qreg_name, qubit_index, creg_name, creg_index)

返回类型:

tuple

static handle_parameters(parameters_str)[源代码]

Parse a parameter string into a list of floats.

参数:

parameters_str (str) -- Comma-separated parameter expression string.

返回:

List of parsed parameter values.

返回类型:

list[float]

static handle_qreg(line)[源代码]

Parse a qreg definition line.

参数:

line (str) -- QASM line defining a quantum register.

返回:

(register_name, size)

返回类型:

tuple

identifier: str = '([A-Za-z_][A-Za-z_\\d]*)'
index: str = '\\[ *(\\d+) *\\]'
static parse_line(line)[源代码]

Parse a single line of OpenQASM 2 code.

返回:

Gate name string or None q: Qubit spec — (qreg_name, qubit_index) for 1q, list for multi-q, or None c: Classical bit spec — (creg_name, creg_index) or None parameter: Parameter list (from handle_parameters) or None

返回类型:

operation

参数:

line (str)

qreg_str: str = '([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *'
regexp_1q: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_1q_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_1qnp: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *\\(([^()]+)\\) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_1qnp_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *\\(([^()]+)\\) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_2q: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_2q_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_2qnp: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *\\(([^()]+)\\) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_2qnp_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *\\(([^()]+)\\) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_3q: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_3q_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_3qnp: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *\\(([^()]+)\\) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_3qnp_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *\\(([^()]+)\\) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_4q: Pattern[str] = re.compile('^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *)
regexp_4q_str: str = '^([A-Za-z_][A-Za-z_\\d]*) *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *, *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_creg: Pattern[str] = re.compile('^creg *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_creg_str: str = '^creg *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_measure: Pattern[str] = re.compile('^measure *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *-> *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_measure_str: str = '^measure *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *-> *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'
regexp_qreg: Pattern[str] = re.compile('^qreg *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$')
regexp_qreg_str: str = '^qreg *([A-Za-z_][A-Za-z_\\d]*) *\\[ *(\\d+) *\\] *$'

Module contents#