QXel.provider.braket.openqasm.program_context module

Interpreter state for translating Braket OpenQASM into QXel circuits.

class QXel.provider.braket.openqasm.program_context.Table(title)

Bases: object

Utility class for storing and displaying items.

items()
class QXel.provider.braket.openqasm.program_context.QubitTable

Bases: Table

Map OpenQASM qubit identifiers to concrete qubit indices.

get_by_identifier(identifier)
get_by_identifier(identifier)

Convenience method to get an element with a possibly indexed identifier.

get_qubit_size(identifier)
class QXel.provider.braket.openqasm.program_context.ScopedTable(title)

Bases: Table

Scoped version of Table

push_scope()
pop_scope()
property in_global_scope
property current_scope
get_scope(key)

Get the smallest scope containing the given key

items()
class QXel.provider.braket.openqasm.program_context.SymbolTable

Bases: ScopedTable

Scoped table used to map names to types.

class Symbol(symbol_type, const=False)

Bases: object

add_symbol(name, symbol_type, const=False)

Add a symbol to the symbol table.

Parameters:
  • name (str) – Name of the symbol.

  • symbol_type (ClassicalType or LiteralType) – Descriptor for the symbol. Symbols can have a literal type when they are a numeric argument to a gate or an integer literal loop variable.

  • const (bool) – Whether the variable is immutable.

get_symbol(name)

Get a symbol from the symbol table by name.

Parameters:

name (str) – Name of the symbol.

Returns:

The symbol object.

Return type:

Symbol

get_type(name)

Get the type of a symbol by name.

Parameters:

name (str) – Name of the symbol.

Returns:

The type of the symbol.

Return type:

ClassicalType or LiteralType

get_const(name)

Get const status of a symbol by name.

Parameters:

name (str) – Name of the symbol.

Returns:

Whether the symbol is a const symbol.

Return type:

bool

class QXel.provider.braket.openqasm.program_context.VariableTable

Bases: ScopedTable

Scoped table used store values for symbols. This implements the classical memory for the Interpreter.

add_variable(name, value)
get_value(name)
get_value_by_identifier(identifier, type_width=None)
get_value_by_identifier(identifier, type_width=None)

Convenience method to get value with a possibly indexed identifier.

update_value(name, value, var_type, indices=None)

Update value of a variable, optionally providing an index

is_initalized(name)

Determine whether a declared variable is initialized

class QXel.provider.braket.openqasm.program_context.GateTable

Bases: ScopedTable

Scoped table to implement gates.

add_gate(name, definition)
get_gate_definition(name)
class QXel.provider.braket.openqasm.program_context.SubroutineTable

Bases: ScopedTable

Scoped table to implement subroutines.

add_subroutine(name, definition)
get_subroutine_definition(name)
class QXel.provider.braket.openqasm.program_context.ScopeManager(context)

Bases: object

Allows ProgramContext to manage scope with with keyword.

class QXel.provider.braket.openqasm.program_context.AbstractProgramContext

Bases: ABC

Interpreter state.

Symbol table - symbols in scope Variable table - variable values Gate table - gate definitions Subroutine table - subroutine definitions Qubit mapping - mapping from logical qubits to qubit indices

Circuit - IR build to hand off to the simulator

abstract property circuit

The circuit being built in this context.

load_inputs(inputs)

Load inputs for the circuit

Parameters:

inputs (dict[str, Any]) – A dictionary containing the inputs to be loaded

parse_pragma(pragma_body)

Parse pragma

Parameters:

pragma_body (str) – The body of the pragma statement.

declare_variable(name, symbol_type, value=None, const=False)

Declare variable in current scope

Parameters:
  • name (str) – The name of the variable

  • symbol_type – Descriptor for the variable type.

  • value (Optional[Any]) – The initial value of the variable . Defaults to None.

  • const (bool) – Flag indicating if the variable is constant. Defaults to False.

declare_qubit_alias(name, value)

Declare qubit alias in current scope

Parameters:
  • name (str) – The name of the qubit alias.

  • value (Identifier) – The identifier representing the qubit

enter_scope()

Allow pushing and popping scope with the with keyword.

Usage:

with program_context.enter_scope():
    ...
push_scope()

Enter a new scope

pop_scope()

Exit current scope

property in_global_scope
get_type(name)

Get symbol type by name

Parameters:

name (str) – The name of the symbol.

Returns:

The type of the symbol.

Return type:

ClassicalType or LiteralType

get_const(name)

Get whether a symbol is const by name”

Parameters:

name (str) – The name of the symbol.

Returns:

True of the symbol os const, False otherwise.

Return type:

bool

get_value(name)

Get value of a variable by name

Parameters:

name (str) – The name of the variable.

Returns:

The value of the variable.

Return type:

LiteralType

Raises:

KeyError – If the variable is not found.

get_value_by_identifier(identifier)

Get value of a variable by identifier

Parameters:

identifier (Union[Identifier, IndexedIdentifier]) – The identifier of the variable.

Returns:

The value of the variable.

Return type:

LiteralType

Raises:

KeyError – If the variable is not found.

is_initialized(name)

Check whether variable is initialized by name

Parameters:

name (str) – The name of the variable.

Returns:

True if the variable is initialized, False otherwise.

Return type:

bool

update_value(variable, value)

Update value by identifier, possible only a sub-index of a variable

Parameters:
add_qubits(name, num_qubits=1)

Allocate additional qubits for the circuit

Parameters:
  • name (str) – The name of the qubit register

  • num_qubits (Optional[int]) – The number of qubits to allocate. Default is 1.

get_qubits(qubits)

Get qubit indices from a qubit identifier, possibly referring to a sub-index of a qubit register

Parameters:

qubits (Union[Identifier, IndexedIdentifier]) – The identifier of the qubits.

Returns:

The indices of the qubits.

Return type:

tuple[int]

Raises:

KeyError – If the qubit identifier is not found.

add_gate(name, definition)

Add a gate definition

Parameters:
  • name (str) – The name of the gate.

  • definition (QuantumGateDefinition) – The definition of the gate.

get_gate_definition(name)

Get a gate definition by name

Parameters:

name (str) – The name of the gate.

Returns:

The definition of the gate.

Return type:

QuantumGateDefinition

Raises:

ValueError – If the gate is not defined.

is_user_defined_gate(name)

Check whether the gate is user-defined gate

Parameters:

name (str) – The name of the gate.

Returns:

True of the gate is user-defined, False otherwise.

Return type:

bool

abstractmethod is_builtin_gate(name)

Abstract method to check if the gate with the given name is currently in scope as a built-in Braket gate. :param name: name of the built-in Braket gate to be checked :type name: str

Returns:

True if the gate is a built-in gate, False otherwise.

Return type:

bool

add_subroutine(name, definition)

Add a subroutine definition

Parameters:
  • name (str) – The name of the subroutine.

  • definition (SubroutineDefinition) – The definition of the subroutine.

get_subroutine_definition(name)

Get a subroutine definition by name

Parameters:

name (str) – The name of the subroutine.

Returns:

The definition of the subroutine.

Return type:

SubroutineDefinition

Raises:

NameError – If the subroutine with the give name is not defined.

add_result(result)

Abstract method to add result type to the circuit

Parameters:

result (Results) – The result object representing the measurement results

add_phase(phase, qubits=None)

Add quantum phase instruction to the circuit

abstractmethod add_phase_instruction(target, phase_value)

Abstract method to add phase instruction to the circuit

Parameters:
  • target (int or list[int]) – The target qubit or qubits to which the phase instruction is applied

  • phase_value (float) – The phase value to be applied

add_builtin_gate(gate_name, parameters, qubits, modifiers=None)

Add a builtin gate instruction to the circuit

Parameters:
  • gate_name (str) – The name of the built-in gate.

  • parameters (list[FloatLiteral]) – The list of the gate parameters.

  • qubits (list[Union[Identifier, IndexedIdentifier]]) – The list of qubits the gate acts on.

  • modifiers (Optional[list[QuantumGateModifier]]) – The list of gate modifiers (optional).

handle_parameter_value(value)

Convert a gate parameter into the representation expected by QXel.

Parameters:

value (Union[float, Expr]) – Value of the parameter.

abstractmethod add_gate_instruction(gate_name, target, params, ctrl_modifiers, power)

Add a built-in Braket gate to the circuit.

Parameters:
  • gate_name (str) – name of the built-in Braket gate.

  • target (tuple[int]) – control_qubits + target_qubits.

  • ctrl_modifiers (list[int]) – Quantum state on which to control the operation. The sequence length must match the number of control qubits encoded at the front of target.

  • power (float) – Integer or fractional power to raise the gate to.

add_custom_unitary(unitary, target)

Add a custom unitary instruction to the circuit.

Parameters:
  • unitary (np.ndarray) – Unitary matrix to apply.

  • target (tuple[int, ...]) – Control qubits followed by target qubits.

add_noise_instruction(noise_instruction, target, probabilities)

Abstract method to add a noise instruction to the circuit

Parameters:
  • noise_instruction (str) – The name of the noise operation

  • target (list[int]) – The target qubit or qubits to which the noise operation is applied.

  • probabilities (list[float]) – The probabilities associated with each possible outcome of the noise operation.

add_kraus_instruction(matrices, target)

Abstract method to add a Kraus instruction to the circuit

Parameters:
  • matrices (list[ndarray]) – The matrices defining the Kraus operation

  • target (list[int]) – The target qubit or qubits to which the Kraus operation is applied.

add_measure(target, classical_targets=None)

Add qubit targets to be measured

class QXel.provider.braket.openqasm.program_context.ProgramContext(circuit=None)

Bases: AbstractProgramContext

Mutable interpreter context that accumulates a translated QXel circuit.

__init__(circuit=None)
Parameters:

circuit (Optional[Circuit]) – A partially-built circuit to continue building with this context. Default: None.

property circuit

The circuit being built in this context.

is_builtin_gate(name)

Abstract method to check if the gate with the given name is currently in scope as a built-in Braket gate. :param name: name of the built-in Braket gate to be checked :type name: str

Returns:

True if the gate is a built-in gate, False otherwise.

Return type:

bool

add_phase_instruction(target, phase_value)

Abstract method to add phase instruction to the circuit

Parameters:
  • target (int or list[int]) – The target qubit or qubits to which the phase instruction is applied

  • phase_value (float) – The phase value to be applied

add_gate_instruction(gate_name, target, params, ctrl_modifiers, power)

Add a built-in Braket gate to the circuit.

Parameters:
  • gate_name (str) – name of the built-in Braket gate.

  • target (tuple[int]) – control_qubits + target_qubits.

  • ctrl_modifiers (list[int]) – Quantum state on which to control the operation. The sequence length must match the number of control qubits encoded at the front of target.

  • power (float) – Integer or fractional power to raise the gate to.

add_custom_unitary(unitary, target)

Add a custom unitary instruction to the circuit.

Parameters:
  • unitary (np.ndarray) – Unitary matrix to apply.

  • target (tuple[int, ...]) – Control qubits followed by target qubits.

add_noise_instruction(noise_instruction, target, probabilities)

Abstract method to add a noise instruction to the circuit

Parameters:
  • noise_instruction (str) – The name of the noise operation

  • target (list[int]) – The target qubit or qubits to which the noise operation is applied.

  • probabilities (list[float]) – The probabilities associated with each possible outcome of the noise operation.

add_kraus_instruction(matrices, target)

Abstract method to add a Kraus instruction to the circuit

Parameters:
  • matrices (list[ndarray]) – The matrices defining the Kraus operation

  • target (list[int]) – The target qubit or qubits to which the Kraus operation is applied.

add_result(result)

Abstract method to add result type to the circuit

Parameters:

result (Results) – The result object representing the measurement results

add_measure(target, classical_targets=None)

Add qubit targets to be measured