wy_qcos.transpiler.cmss.compiler.openqasm3 package

Submodules

wy_qcos.transpiler.cmss.compiler.openqasm3.convertor module

class wy_qcos.transpiler.cmss.compiler.openqasm3.convertor.ConvertVisitor(annotation_handlers=None)

基类:QASMVisitor[State]

Internal visitor of converting OpenQASM 3 AST to QuantumCircuit.

The other methods on this class are internal only, and generally not part of the public interface.

参数:

annotation_handlers (dict[str, Any] | None)

convert(node, *, source=None)

Convert a program node into a QuantumCircuit.

参数:
  • node (ast.Program) -- Root node of the OpenQASM 3 AST

  • source (str | None) -- Optional source code string for error messages

返回:

Converted quantum circuit representation

返回类型:

QuantumCircuit

If given, source is a string containing the OpenQASM 3 source code that was parsed into node. This is used to generated improved error messages. A State containing information about the conversion is returned. The QuantumCircuit is stored in property thereof named circuit.

generic_visit(node, context=None)

Called if no explicit visitor function exists for a node.

visit_Program(node, context)

Process the root Program node of OpenQASM 3 AST.

参数:
  • node (ast.Program) -- Program AST node

  • context (State) -- Current conversion state

返回:

Updated state after processing all statements

返回类型:

State

Traverses all statements in the program and processes them sequentially.

visit_Include(node, context)

Process an include statement in OpenQASM 3 code.

参数:
  • node (ast.Include) -- The include statement AST node

  • context (State) -- Current conversion state

返回:

Updated state with standard gates defined from the include

返回类型:

State

抛出:

ConversionError -- If the included file is not "stdgates.inc"

This method handles the 'include "stdgates.inc"' statement by defining all standard quantum gates in the symbol table. It only defines gates that haven't been defined yet to avoid conflicts with user-defined gates.

visit_QubitDeclaration(node, context)

Process a qubit declaration.

参数:
  • node (ast.QubitDeclaration) -- Qubit declaration AST node

  • context (State) -- Current conversion state

返回:

Updated state with qubit(s) registered

返回类型:

State

Allocates qubit indices, creates quantum registers if needed, and adds symbols to the symbol table.

visit_QuantumGateDefinition(node, context)

Process a quantum gate definition.

参数:
  • node (ast.QuantumGateDefinition) -- Gate definition AST node

  • context (State) -- Current conversion state

返回:

Updated state with gate defined

返回类型:

State

Creates a GateScope for the gate body, defines standard gates within the scope, processes parameters and qubits, and registers the gate.

visit_QuantumGate(node, context)

Process a quantum gate invocation.

参数:
  • node (ast.QuantumGate) -- Gate invocation AST node

  • context (State) -- Current conversion state

返回:

Updated state with gate operation added to circuit

返回类型:

State

抛出:

ConversionError -- If gate is undefined or arguments are invalid

Resolves gate definition, parameters, and qubits, applies modifiers, creates gate operation, and appends to the circuit.

visit_QuantumPhase(node, context)

Process a quantum phase application (gphase).

参数:
  • node (ast.QuantumPhase) -- Phase application AST node

  • context (State) -- Current conversion state

返回:

Updated state with phase gate(s) added to circuit

返回类型:

State

Applies phase to all qubits if no specific qubits are provided, otherwise applies to specified qubits. Handles global phase and gate modifiers.

visit_QuantumMeasurementStatement(node, context)

Process a quantum measurement statement.

参数:
  • node (ast.QuantumMeasurementStatement) -- The measurement statement AST node

  • context (State) -- Current conversion state

返回:

Updated state with measurement operation added to circuit

返回类型:

State

抛出:
  • ConversionError -- If the measurement doesn't save its result or

  • if the target qubit specification is invalid --

This method handles OpenQASM 3 measurement statements like "measure q -> c" by creating a measurement operation and adding it to the quantum circuit.

visit_QuantumBarrier(node, context)

Process a quantum barrier (barrier) statement.

参数:
  • node (ast.QuantumBarrier) -- The barrier statement AST node

  • context (State) -- Current conversion state

返回:

Updated state with barrier operation added to circuit

返回类型:

State

This method handles the OpenQASM 3 barrier statement, which prevents optimizations from moving operations across the barrier. It creates a synchronization (Sync) operation for the specified qubits.

visit_QuantumReset(node, context)

Process a quantum reset statement.

参数:
  • node (ast.QuantumReset) -- The reset statement AST node

  • context (State) -- Current conversion state

返回:

Updated state with reset operation added to circuit

返回类型:

State

抛出:

ConversionError -- If the qubit specification is invalid

This method handles the OpenQASM 3 reset statement, which resets specified qubits to the kit 0 state. It creates a Reset operation for single qubits or qubit arrays.

visit_ClassicalDeclaration(node, context)

Process a classical bit declaration statement.

参数:
  • node (ast.ClassicalDeclaration) -- Classical declaration AST node

  • context (State) -- Current conversion state

返回:

Updated state with classical bits/registers defined

返回类型:

State

抛出:
  • ConversionError -- If declaration is not in global scope or

  • if the type is not supported --

This method handles declarations of classical bits and registers, including initialization with measurement results. It supports both single-bit declarations and bit arrays (registers).

visit_IODeclaration(node, context)
参数:
  • node (IODeclaration)

  • context (State)

返回类型:

State

visit_BreakStatement(node, context)
参数:
  • node (BreakStatement)

  • context (State)

返回类型:

State

visit_ContinueStatement(node, context)
参数:
  • node (ContinueStatement)

  • context (State)

返回类型:

State

visit_BranchingStatement(node, context)
参数:
  • node (BranchingStatement)

  • context (State)

返回类型:

State

visit_WhileLoop(node, context)
参数:
  • node (WhileLoop)

  • context (State)

返回类型:

State

visit_ForInLoop(node, context)

Process a for-in loop statement.

参数:
  • node (ast.ForInLoop) -- The for-in loop AST node

  • context (State) -- Current conversion state

返回:

Unchanged state (for-in loops are placeholders)

返回类型:

State

抛出:
  • ConversionError -- If loop variable type is not integer or

  • if the range/set cannot be resolved --

备注

Currently, for-in loops are not fully implemented and are treated as no-ops. The method validates loop syntax but doesn't execute the loop body.

visit_Box(node, context)

Process a box (timing annotation) statement.

参数:
  • node (ast.Box) -- The box statement AST node

  • context (State) -- Current conversion state

返回:

Updated state with timing information (if provided)

返回类型:

State

This method handles timing annotations for quantum operations, including duration specifications and custom annotations. Future implementations will use this timing information for circuit scheduling and optimization.

visit_DelayInstruction(node, context)

Process a delay instruction statement.

参数:
  • node (ast.DelayInstruction) -- The delay instruction AST node

  • context (State) -- Current conversion state

返回:

Updated state (delay operations are placeholders)

返回类型:

State

备注

Currently, delay instructions are not fully implemented and are treated as no-ops. Future implementations will add timing and scheduling support for delay operations.

visit_AliasStatement(node, context)

Process an alias statement (let) in OpenQASM 3 code.

参数:
  • node (ast.AliasStatement) -- The alias statement AST node

  • context (State) -- Current conversion state containing symbols and types

返回:

Updated state with alias registered in symbol table

返回类型:

State

抛出:

ConversionError -- If the aliased value is not a bit or qubit register

This method handles the OpenQASM 3 'let' statement, which creates an alias (reference) to an existing quantum or classical register. The alias is added to the symbol table with the original register's bits/type, allowing the alias to be used interchangeably with the original register in subsequent statements.

示例

let alias_name = q[0:1];

wy_qcos.transpiler.cmss.compiler.openqasm3.data module

class wy_qcos.transpiler.cmss.compiler.openqasm3.data.Scope(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

基类:Enum

Types of scope in OpenQASM 3 programs.

GLOBAL = 1
GATE = 2
FUNCTION = 3
LOCAL = 4
CALIBRATION = 5
BUILTIN = 6
NONE = 7
class wy_qcos.transpiler.cmss.compiler.openqasm3.data.Symbol(name, data, type, scope, definer=None)

基类:object

An internal symbol used during parsing.

参数:
  • name (str)

  • data (Any)

  • type (Type)

  • scope (Scope)

  • definer (QASMNode | None)

name
data
type
scope
definer

wy_qcos.transpiler.cmss.compiler.openqasm3.exceptions module

exception wy_qcos.transpiler.cmss.compiler.openqasm3.exceptions.ConversionError(message, node=None)

基类:Exception

Raised when an error occurs converting the AST representation.

参数:

node (QASMNode | None)

wy_qcos.transpiler.cmss.compiler.openqasm3.exceptions.raise_from_node(node, message)

Raise a ConversionError caused by the given node.

参数:
  • node (QASMNode)

  • message (str)

返回类型:

NoReturn

wy_qcos.transpiler.cmss.compiler.openqasm3.expression module

class wy_qcos.transpiler.cmss.compiler.openqasm3.expression.ValueResolver(context, strict=True)

基类:QASMVisitor

A resolver for value-like objects that have exact representations.

参数:
  • context (State)

  • strict (bool)

resolve(node)

The entry point to the resolver.

参数:

node (Expression)

返回类型:

tuple[Any, Type]

visit(node, context=None)

Visit a node.

参数:
  • node (QASMNode)

  • context (None)

返回类型:

tuple[Any, Type]

visit_Identifier(node)
参数:

node (Identifier)

generic_visit(node, context=None)

Called if no explicit visitor function exists for a node.

参数:
  • node (QASMNode)

  • context (None)

visit_IntegerLiteral(node)
参数:

node (IntegerLiteral)

visit_FloatLiteral(node)
参数:

node (FloatLiteral)

visit_BooleanLiteral(node)
参数:

node (BooleanLiteral)

visit_BitstringLiteral(node)
参数:

node (BitstringLiteral)

visit_DurationLiteral(node)
参数:

node (DurationLiteral)

visit_DiscreteSet(node)
参数:

node (DiscreteSet)

visit_RangeDefinition(node)
参数:

node (RangeDefinition)

visit_Concatenation(node)
参数:

node (Concatenation)

visit_UnaryExpression(node)
参数:

node (UnaryExpression)

visit_BinaryExpression(node)
参数:

node (BinaryExpression)

visit_IndexExpression(node)
参数:

node (IndexExpression)

visit_IndexedIdentifier(node)
参数:

node (IndexedIdentifier)

wy_qcos.transpiler.cmss.compiler.openqasm3.expression.resolve_condition(node, context)

A resolver for conditions that can be converted into equality form.

A very basic equality form of either bit == const bool or bitarray == const int. This effectively just handles very special outer cases, then delegates the rest of the work to a ValueResolver.

参数:
  • node (Expression)

  • context (State)

返回类型:

tuple[int, bool] | tuple[tuple[int, ...], int]

wy_qcos.transpiler.cmss.compiler.openqasm3.parser module

wy_qcos.transpiler.cmss.compiler.openqasm3.parser.convert(node)

Convert a parsed OpenQASM 3 program in AST form, into a QuantumCircuit.

参数:

node (Program) -- The root node of the AST.

返回:

The converted circuit.

返回类型:

QuantumCircuit

wy_qcos.transpiler.cmss.compiler.openqasm3.parser.parse(input_)

Parses the OpenQASM 3 program into AST form.

And then converts the output to QuantumCircuit form.

参数:
  • input -- The OpenQASM 3 program to parse and convert.

  • input_ (str)

返回:

The converted circuit.

返回类型:

QuantumCircuit

wy_qcos.transpiler.cmss.compiler.openqasm3.state module

class wy_qcos.transpiler.cmss.compiler.openqasm3.state.Parameter(name)

基类:object

Parameter class.

参数:

name (str)

class wy_qcos.transpiler.cmss.compiler.openqasm3.state.ParameterExpression(expression, parameters=set())

基类:object

Parameter expression class.

参数:
  • expression (str)

  • parameters (set)

wy_qcos.transpiler.cmss.compiler.openqasm3.state.physical_qubit_index(name)

If this name is a physical qubit, return its integer index.

参数:

name (str | Symbol)

返回类型:

int | None

class wy_qcos.transpiler.cmss.compiler.openqasm3.state.AddressingMode

基类:object

Addressing mode for qubits in OpenQASM 3 programs.

This class is useful as long as we allow only physical or virtual addressing modes, but not mixed. If the latter is supported in the future, this class will be modified or removed.

set_physical_mode(node)

Set the addressing mode to physical.

On success return True, otherwise raise an exception.

set_virtual_mode(node)

Set the addressing mode to virtual.

On success return True, otherwise raise an exception.

is_physical()
class wy_qcos.transpiler.cmss.compiler.openqasm3.state.SymbolTable(scope, symbols=None)

基类:object

scope
symbols
class wy_qcos.transpiler.cmss.compiler.openqasm3.state.SymbolTables

基类:object

get(name, node=None)
参数:

name (str)

push(symbol_table)
参数:

symbol_table (SymbolTable)

pop()
insert(symbol)
globals()

Return an iterator over the global symbols.

class wy_qcos.transpiler.cmss.compiler.openqasm3.state.State(source=None)

基类:object

Mutable state during translation of OpenQASM code to QuantumCircuit.

This class holds the mutable state used during the conversion of OpenQASM 3 code to a QuantumCircuit object.

Slots:
scope (Scope)

The current lexical scope for the statements being translated.

_source (str)

The entire OpenQASM program being translated. This is not directly translated, but rather an AST derived from the source is the input. Instead, this source is used for diagnostics.

circuit (QuantumCircuit)

The output of the translation.

symbol_table (SymbolTables)

A structure that tracks the symbols (e.g. identifiers) that have been encountered along with some information about them.

_unique (function)

A function that returns unique symbol names.

addressing_mode

A structure that tracks the state of the addressing mode; either unknown, virtual, or hardware.

all_parameters

A set that collects all parameters used in the circuit.

qubit_mapping

A mapping from qubit names to indices.

next_qubit_index

The next available qubit index.

参数:

source (str | None)

scope
symbol_table
addressing_mode
all_parameters
qubit_mapping
next_qubit_index
circuit
allocate_qubit(name)

Allocate a qubit index to the given name.

参数:

name (str)

返回类型:

int

get_qubit_index(name)
参数:

name (str)

返回类型:

int | None

classmethod new_with_local_scope(context)

Return a copy of context with new local scope added to the stack.

classmethod new_with_gate_scope(context)

Return a copy of context with new gate scope added to the stack.

unique_name(prefix=None)

Get a name that is not defined in the current scope.

class wy_qcos.transpiler.cmss.compiler.openqasm3.state.LocalScope(context)

基类:object

参数:

context (State)

class wy_qcos.transpiler.cmss.compiler.openqasm3.state.GateScope(context)

基类:object

参数:

context (State)

wy_qcos.transpiler.cmss.compiler.openqasm3.types module

class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Type

基类:ABC

An internal representation of the OpenQASM 3 type system values.

Or at least that parts of it that Terra has some sort of support for. The reference AST does not have a nice unified object to use here; there is ClassicalType but no quantum equivalent (since it's implicit).

This class is just a typing base, and should never be instantiated. Its subclasses will be, though.

abstract pretty()

A pretty string representation of the type, useful for debugging.

返回类型:

str

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Error

基类:Type

A zero type that represents an error during type checking.

pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Never

基类:Type

The bottom type.

There are no valid values of this type, as it can never be instantiated. This is used during inference in cases where multiple types must combined into their join, but some of the elements have missing values, such as a range that has a start but no stop value.

pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.BitArray(size)

基类:Type

An array of bits.

This roughly corresponds to Terra's class:ClassicalRegister.

参数:

size (int)

size
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.QubitArray(size)

基类:Type

An array of qubits.

This roughly corresponds to Terra's class:QuantumRegister.

参数:

size (int)

size
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Bit

基类:Type

A single bit.

pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Bool(const)

基类:Type

A Boolean value.

参数:

const (bool)

const
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Qubit

基类:Type

A single qubit.

pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.HardwareQubit

基类:Type

A hardware qubit.

pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Int(const=False, size=None)

基类:Type

An integer value.

This is generally only encountered as a constant and so is represented by a Python integer, but can also be the type of the class:Parameter used to represent for-loop variables.

参数:
  • const (bool)

  • size (int | None)

const
size
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Uint(const=False, size=None)

基类:Type

An unsigned integer value.

参数:
  • const (bool)

  • size (int | None)

const
size
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Float(const=False, size=None)

基类:Type

A floating-point type.

Terra can use this either in a constant form as a Python float, or as a class:Parameter.

参数:
  • const (bool)

  • size (int | None)

const
size
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Angle(const=False, size=None)

基类:Type

An angle type.

OpenQASM 3 makes a large distinction between angle and float (the OpenQASM angle is integer-like), but Terra currently treats them as interchangeable. This might change in the future.

参数:
  • const (bool)

  • size (int | None)

const
size
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Duration(const)

基类:Type

A duration.

Right now, this is only recognised in constant form, which is represented within class:.ValueResolver as a 2-tuple of a float and its unit.

参数:

const (bool)

const
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Range(base)

基类:Type

A range selector.

The inner type is the join of the types of the start and end values.

参数:

base (Type)

base
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Sequence(base)

基类:Type

A general sequence of values.

This is represented internally as a list or tuple of the contained type.

参数:

base (Type)

base
pretty()

A pretty string representation of the type, useful for debugging.

final class wy_qcos.transpiler.cmss.compiler.openqasm3.types.Gate(n_classical, n_quantum)

基类:Type

The type of a gate.

Since the classical parameters of gates have a fixed type in OpenQASM 3, this just stores the counts of the classical and quantum arguments.

参数:
  • n_classical (int)

  • n_quantum (int)

n_classical
n_quantum
pretty()

A pretty string representation of the type, useful for debugging.

Module contents