qpandalite.originir package#

Submodules#

qpandalite.originir.originir_base_parser module#

OriginIR base parser module.

This module provides the base parser for OriginIR quantum circuit representation, including parsing QINIT, CREG statements and quantum operations.

Key exports:

OriginIR_BaseParser: Base parser class for OriginIR circuits.

class qpandalite.originir.originir_base_parser.OriginIR_BaseParser[源代码]

基类:object

Parser for OriginIR quantum circuit representation.

n_qubit

Number of qubits.

n_cbit

Number of classical bits.

program_body

List of operation opcodes.

raw_originir

Raw OriginIR string.

measure_qubits

List of measurement tuples (qubit, cbit).

property originir

OriginIR string representation (alias for to_extended_originir).

返回:

Extended OriginIR string.

返回类型:

str

parse(originir_str)[源代码]

Parse an OriginIR string and populate internal state.

参数:

originir_str -- OriginIR string to parse.

返回:

A qpandalite Circuit object.

返回类型:

Circuit

to_circuit()[源代码]

The function coverts OriginIR string into qpandalite.Circuit object.

返回:

qpandalite.Circuit object.

返回类型:

Circuit

to_extended_originir()[源代码]

Convert parsed data back to extended OriginIR string.

返回:

Extended OriginIR string representation.

返回类型:

str

to_qasm()[源代码]

The function coverts OriginIR string into OpenQASM string.

返回:

OpenQASM string.

qpandalite.originir.originir_line_parser module#

OriginIR line parser module.

This module provides regex-based parsing for individual OriginIR lines, supporting 1-3 qubit gates, parameterized gates, dagger flags, and control qubits.

Key exports:

OriginIR_LineParser: Parser class for individual OriginIR lines.

class qpandalite.originir.originir_line_parser.OriginIR_LineParser[源代码]

基类:object

Parser for individual OriginIR lines.

Provides regex-based parsing for OriginIR gate statements with support for 1-3 qubit gates, parameterized gates, dagger flags, and control qubits.

blank = ' *'
cid = 'c *\\[ *(\\d+) *\\]'
comma = ','
control_qubits = ' *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?'
dagger_flag = ' *(dagger *)?'
static handle_1q(line)[源代码]

Parse a 1-qubit gate line.

返回:

(operation, qubit, dagger_flag, control_qubits)

返回类型:

tuple

static handle_1q1p(line)[源代码]

Parse a 1-qubit 1-parameter gate line.

返回:

(operation, qubit, parameter, dagger_flag, control_qubits)

返回类型:

tuple

static handle_1q2p(line)[源代码]

Parse a 1-qubit 2-parameter gate line.

返回:

(operation, qubit, [p1, p2], dagger_flag, control_qubits)

返回类型:

tuple

static handle_1q3p(line)[源代码]

Parse a 1-qubit 3-parameter gate line.

返回:

(operation, qubit, [p1, p2, p3], dagger_flag, control_qubits)

返回类型:

tuple

static handle_1q4p(line)[源代码]

Parse a 1-qubit 4-parameter gate line.

返回:

(operation, qubit, [p1, p2, p3, p4], dagger_flag, control_qubits)

返回类型:

tuple

static handle_2q(line)[源代码]

Parse a 2-qubit gate line.

返回:

(operation, [q1, q2], dagger_flag, control_qubits)

返回类型:

tuple

static handle_2q15p(line)[源代码]

Parse a 2-qubit 15-parameter gate line.

返回:

(operation, [q1, q2], parameters, dagger_flag, control_qubits)

返回类型:

tuple

static handle_2q1p(line)[源代码]

Parse a 2-qubit 1-parameter gate line.

返回:

(operation, [q1, q2], parameter, dagger_flag, control_qubits)

返回类型:

tuple

static handle_2q3p(line)[源代码]

Parse a 2-qubit 3-parameter gate line.

返回:

(operation, [q1, q2], [p1, p2, p3], dagger_flag, control_qubits)

返回类型:

tuple

static handle_3q(line)[源代码]

Parse a 3-qubit gate line.

返回:

(operation, [q1, q2, q3], dagger_flag, control_qubits)

返回类型:

tuple

static handle_barrier(line)[源代码]

Parse a BARRIER statement line.

返回:

("BARRIER", qubit_indices)

返回类型:

tuple

static handle_control(line)[源代码]

Parse a line to extract control qubits information and the type of control operation.

This function analyzes a given line of text to identify and extract information about control qubits and determine whether the line represents the beginning of a control operation (CONTROL) or the end of a control operation (ENDCONTROL) in OriginIR language.

参数:

line (str) -- The line of text to be parsed for control qubit information.

返回:

A tuple where the first element is a string indicating the control operation type ("CONTROL" or "ENDCONTROL") and the second element is a list of integers representing the parsed control qubits.

返回类型:

tuple of (str, list)

备注

The function relies on the regexp_control regular expression to match the CONTROL or ENDCONTROL patterns in OriginIR language. This regular expression should be predefined and properly constructed to capture the necessary information from the line.

static handle_dagger(line)[源代码]

Parse a line to identify DAGGER or ENDDAGGER commands in OriginIR.

This function checks a line of text to determine if it contains a command related to the start or end of a DAGGER operation block in the OriginIR language.

参数:

line (str) -- The line of text to be parsed.

返回:

Returns "DAGGER" if the line is a DAGGER command, "ENDDAGGER" if it's an ENDDAGGER command, or None if neither command is present.

返回类型:

str or None

备注

The DAGGER command in OriginIR denotes the start of a block where the operations are to be applied in reverse order with conjugate transposition (dagger operation). The ENDDAGGER command signifies the end of such a block.

static handle_measure(line)[源代码]

Parse a MEASURE statement line.

返回:

(qubit, cbit)

返回类型:

tuple

lbracket = '\\('
opname = '([A-Za-z][A-Za-z\\d]*)'
parameter = '([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?)'
static parse_line(line)[源代码]

Parse a single OriginIR line and return operation details.

参数:

line -- Single line of OriginIR code.

返回:

(operation, qubits, cbit, parameter, dagger_flag, control_qubits)

返回类型:

tuple

qid = 'q *\\[ *(\\d+) *\\]'
rbracket = '\\)'
regexp_1q = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$')
regexp_1q1p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$')
regexp_1q1p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_1q2p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)*)
regexp_1q2p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_1q3p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(con)
regexp_1q3p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_1q4p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*))
regexp_1q4p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_1q_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_2q = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$')
regexp_2q15p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?))
regexp_2q15p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_2q1p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *)
regexp_2q1p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_2q3p = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?))
regexp_2q3p_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *\\( *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *, *([-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?) *\\) *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_2q_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_3q = re.compile('^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$')
regexp_3q_str = '^([A-Za-z][A-Za-z\\d]*) *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *, *q *\\[ *(\\d+) *\\] *(dagger *)? *(controlled_by *\\(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *\\) *)?$'
regexp_barrier = re.compile('^BARRIER(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *)$')
regexp_barrier_str = '^BARRIER(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *)$'
regexp_control = re.compile('^(CONTROL|ENDCONTROL)(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *)$')
regexp_control_str = '^(CONTROL|ENDCONTROL)(( *q *\\[ *(\\d+) *\\] *,)* *q *\\[ *(\\d+) *\\] *)$'
regexp_meas = re.compile('^MEASURE *q *\\[ *(\\d+) *\\] *, *c *\\[ *(\\d+) *\\]$')
regexp_measure_str = '^MEASURE *q *\\[ *(\\d+) *\\] *, *c *\\[ *(\\d+) *\\]$'
regexp_qid = re.compile('q *\\[ *(\\d+) *\\]')

Module contents#