uniqc.compile.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 uniqc.compile.originir.originir_line_parser.OriginIR_LineParser[source]

Bases: 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)[source]

Parse a 1-qubit gate line.

Returns:

(operation, qubit, dagger_flag, control_qubits)

Return type:

tuple

static handle_1q1p(line)[source]

Parse a 1-qubit 1-parameter gate line.

Returns:

(operation, qubit, parameter, dagger_flag, control_qubits)

Return type:

tuple

static handle_1q2p(line)[source]

Parse a 1-qubit 2-parameter gate line.

Returns:

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

Return type:

tuple

static handle_1q3p(line)[source]

Parse a 1-qubit 3-parameter gate line.

Returns:

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

Return type:

tuple

static handle_1q4p(line)[source]

Parse a 1-qubit 4-parameter gate line.

Returns:

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

Return type:

tuple

static handle_2q(line)[source]

Parse a 2-qubit gate line.

Returns:

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

Return type:

tuple

static handle_2q15p(line)[source]

Parse a 2-qubit 15-parameter gate line.

Returns:

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

Return type:

tuple

static handle_2q1p(line)[source]

Parse a 2-qubit 1-parameter gate line.

Returns:

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

Return type:

tuple

static handle_2q3p(line)[source]

Parse a 2-qubit 3-parameter gate line.

Returns:

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

Return type:

tuple

static handle_3q(line)[source]

Parse a 3-qubit gate line.

Returns:

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

Return type:

tuple

static handle_barrier(line)[source]

Parse a BARRIER statement line.

Returns:

(“BARRIER”, qubit_indices)

Return type:

tuple

static handle_control(line)[source]

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.

Parameters:

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

Returns:

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.

Return type:

tuple of (str, list)

Notes

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)[source]

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.

Parameters:

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

Returns:

Returns “DAGGER” if the line is a DAGGER command, “ENDDAGGER” if it’s an ENDDAGGER command, or None if neither command is present.

Return type:

str or None

Notes

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_def(line)[source]

Parse a DEF block header line.

Format: DEF name(reg[size], ...) (param1, param2, ...).

The qubit signature reuses the named-register declaration syntax: a comma-separated list of name[size] register declarations. The optional trailing parenthesised group is a comma-separated list of scalar parameter names.

Returns:

(operation="DEF", formal_qregs, params, name) where

formal_qregs is a list of (reg_name, size) tuples (in declaration order) and params is a list of scalar parameter-name strings.

Return type:

tuple

static handle_def_call(line)[source]

Parse a DEF subroutine call line.

Format: name(<qubit args>) [(<scalar param args>)] where the qubit args are a comma-separated list of qubit references (reg[idx] or a whole register name reg) and the optional trailing group is a comma-separated list of scalar parameter values.

Returns:

(name, qubit_args_str, param_args_str_or_None) — the two

argument groups are returned verbatim for the base parser to resolve against the active register maps.

Return type:

tuple

static handle_measure(line)[source]

Parse a MEASURE statement line.

Returns:

(qubit, cbit)

Return type:

tuple

static handle_qram_call(line, qram_name)[source]

Parse a QRAM call line: name q[..], q[..], ... [dagger] [controlled_by(...)].

The address/data qubit list uses the same variable-length syntax as BARRIER. The optional inline dagger keyword and controlled_by(...) clause (extended-syntax suffixes shared with ordinary gates) are parsed independently of the qubit list so that controlled QRAM round-trips through OriginIR-ext text. QRAM XOR-loads are self-inverse, so dagger is accepted for symmetry but does not change execution semantics.

Returns:

(qram_name, qubit_indices, dagger_flag, control_qubits)

Return type:

tuple

static handle_qramdecl(line)[source]

Parse a QRAMDECL statement.

Format: QRAMDECL name addr_size,data_size

Returns:

(“QRAMDECL”, name, addr_size, data_size)

Return type:

tuple

lbracket = '\\('
opname = '([A-Za-z][A-Za-z\\d]*)'
parameter = '((?:[-+]?\\d+(\\.\\d*)?([eE][-+]?\\d+)?)|(?:[^,()]+?))'
static parse_line(line)[source]

Parse a single OriginIR line and return operation details.

Parameters:

line – Single line of OriginIR code.

Returns:

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

Return type:

tuple

qid = 'q *\\[ *(\\d+) *\\]'
rbracket = '\\)'
reg_ident = '[A-Za-z_][A-Za-z0-9_]*'
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_)
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)
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)
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+)?)|(?:[^,()]+?)) *, *((?)
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+) *\\] *,)* )
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+)?)|(?:[^,()]+?)) *, *((?)
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_def = re.compile('^DEF *([A-Za-z_][A-Za-z0-9_]*) *\\( *([^()]*?) *\\)(?: *\\( *([^()]*?) *\\))? *$')
regexp_def_str = '^DEF *([A-Za-z_][A-Za-z0-9_]*) *\\( *([^()]*?) *\\)(?: *\\( *([^()]*?) *\\))? *$'
regexp_defcall = re.compile('^([A-Za-z_][A-Za-z0-9_]*) *\\(([^()]*)\\)(?: *\\(([^()]*)\\))? *$')
regexp_defcall_str = '^([A-Za-z_][A-Za-z0-9_]*) *\\(([^()]*)\\)(?: *\\(([^()]*)\\))? *$'
regexp_enddef = re.compile('^ENDDEF$')
regexp_enddef_str = '^ENDDEF$'
regexp_meas = re.compile('^MEASURE *q *\\[ *(\\d+) *\\] *, *c *\\[ *(\\d+) *\\]$')
regexp_measure_str = '^MEASURE *q *\\[ *(\\d+) *\\] *, *c *\\[ *(\\d+) *\\]$'
regexp_qid = re.compile('q *\\[ *(\\d+) *\\]')
regexp_qramdecl = re.compile('^QRAMDECL *([A-Za-z_][A-Za-z0-9_]*) *(\\d+)\\s*,\\s*(\\d+) *$')
regexp_qramdecl_str = '^QRAMDECL *([A-Za-z_][A-Za-z0-9_]*) *(\\d+)\\s*,\\s*(\\d+) *$'