braket.default_simulator.openqasm.program_context module
- class braket.default_simulator.openqasm.program_context.Table(title: str)[source]
Bases:
object
Utility class for storing and displaying items.
- class braket.default_simulator.openqasm.program_context.QubitTable[source]
Bases:
Table
- get_by_identifier(identifier: Identifier | IndexedIdentifier) tuple[int] [source]
- get_by_identifier(identifier: IndexedIdentifier) tuple[int]
Convenience method to get an element with a possibly indexed identifier.
- get_qubit_size(identifier: Identifier | IndexedIdentifier) int [source]
- class braket.default_simulator.openqasm.program_context.ScopedTable(title)[source]
Bases:
Table
Scoped version of Table
- property in_global_scope
- property current_scope: dict[str, Any]
- class braket.default_simulator.openqasm.program_context.SymbolTable[source]
Bases:
ScopedTable
Scoped table used to map names to types.
- class Symbol(symbol_type: ClassicalType | BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral, const: bool = False)[source]
Bases:
object
- add_symbol(name: str, symbol_type: ClassicalType | BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral | type[Identifier], const: bool = False) None [source]
Add a symbol to the symbol table.
- Parameters:
name (str) – Name of the symbol.
symbol_type (Union[ClassicalType, LiteralType]) – Type of 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: str) Symbol [source]
Get a symbol from the symbol table by name.
- Parameters:
name (str) – Name of the symbol.
- Returns:
Symbol – The symbol object.
- get_type(name: str) ClassicalType | type[BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral] [source]
Get the type of a symbol by name.
- Parameters:
name (str) – Name of the symbol.
- Returns:
Union[ClassicalType, LiteralType] – The type of the symbol.
- class braket.default_simulator.openqasm.program_context.VariableTable[source]
Bases:
ScopedTable
Scoped table used store values for symbols. This implements the classical memory for the Interpreter.
- get_value(name: str) BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral [source]
- get_value_by_identifier(identifier: Identifier, type_width: IntegerLiteral | None = None) BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral [source]
- get_value_by_identifier(identifier: IndexedIdentifier, type_width: IntegerLiteral | None = None) BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral
Convenience method to get value with a possibly indexed identifier.
- update_value(name: str, value: Any, var_type: ClassicalType, indices: list[DiscreteSet | List[Expression | RangeDefinition]] | None = None) None [source]
Update value of a variable, optionally providing an index
- class braket.default_simulator.openqasm.program_context.GateTable[source]
Bases:
ScopedTable
Scoped table to implement gates.
- add_gate(name: str, definition: QuantumGateDefinition) None [source]
- get_gate_definition(name: str) QuantumGateDefinition [source]
- class braket.default_simulator.openqasm.program_context.SubroutineTable[source]
Bases:
ScopedTable
Scoped table to implement subroutines.
- add_subroutine(name: str, definition: SubroutineDefinition) None [source]
- get_subroutine_definition(name: str) SubroutineDefinition [source]
- class braket.default_simulator.openqasm.program_context.ScopeManager(context: ProgramContext)[source]
Bases:
object
Allows ProgramContext to manage scope with
with
keyword.
- class braket.default_simulator.openqasm.program_context.AbstractProgramContext[source]
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: dict[str, Any]) None [source]
Load inputs for the circuit
- Parameters:
inputs (dict[str, Any]) – A dictionary containing the inputs to be loaded
- parse_pragma(pragma_body: str)[source]
Parse pragma
- Parameters:
pragma_body (str) – The body of the pragma statement.
- declare_variable(name: str, symbol_type: ClassicalType | type[BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral] | type[Identifier], value: Any | None = None, const: bool = False) None [source]
Declare variable in current scope
- Parameters:
name (str) – The name of the variable
symbol_type (Union[ClassicalType, type[LiteralType], type[Identifier]]) – The type of the variable.
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: str, value: Identifier) None [source]
Declare qubit alias in current scope
- Parameters:
name (str) – The name of the qubit alias.
value (Identifier) – The identifier representing the qubit
- enter_scope() ScopeManager [source]
Allows pushing/popping scope with indentation and the
with
keyword.Usage: # inside the original scope … with program_context.enter_scope():
# inside a new scope …
# exited new scope, back in the original scope
- property in_global_scope
- get_type(name: str) ClassicalType | type[BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral] [source]
Get symbol type by name
- Parameters:
name (str) – The name of the symbol.
- Returns:
Union[ClassicalType, type[LiteralType]] – The type of the symbol.
- get_const(name: str) bool [source]
Get whether a symbol is const by name”
- Parameters:
name (str) – The name of the symbol.
- Returns:
bool – True of the symbol os const, False otherwise.
- get_value(name: str) BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral [source]
Get value of a variable by name
- Parameters:
name (str) – The name of the variable.
- Returns:
LiteralType – The value of the variable.
- Raises:
KeyError – If the variable is not found.
- get_value_by_identifier(identifier: Identifier | IndexedIdentifier) BooleanLiteral | IntegerLiteral | FloatLiteral | ArrayLiteral | BitstringLiteral [source]
Get value of a variable by identifier
- Parameters:
identifier (Union[Identifier, IndexedIdentifier]) – The identifier of the variable.
- Returns:
LiteralType – The value of the variable.
- Raises:
KeyError – If the variable is not found.
- is_initialized(name: str) bool [source]
Check whether variable is initialized by name
- Parameters:
name (str) – The name of the variable.
- Returns:
bool – True if the variable is initialized, False otherwise.
- update_value(variable: Identifier | IndexedIdentifier, value: Any) None [source]
Update value by identifier, possible only a sub-index of a variable
- Parameters:
variable (Union[Identifier, IndexedIdentifier]) – The identifier of the variable.
value (Any) – The new value of the variable.
- add_qubits(name: str, num_qubits: int | None = 1) None [source]
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: Identifier | IndexedIdentifier) tuple[int] [source]
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:
tuple[int] – The indices of the qubits.
- Raises:
KeyError – If the qubit identifier is not found.
- add_gate(name: str, definition: QuantumGateDefinition) None [source]
Add a gate definition
- Parameters:
name (str) – The name of the gate.
definition (QuantumGateDefinition) – The definition of the gate.
- get_gate_definition(name: str) QuantumGateDefinition [source]
Get a gate definition by name
- Parameters:
name (str) – The name of the gate.
- Returns:
QuantumGateDefinition – The definition of the gate.
- Raises:
ValueError – If the gate is not defined.
- is_user_defined_gate(name: str) bool [source]
Check whether the gate is user-defined gate
- Parameters:
name (str) – The name of the gate.
- Returns:
bool – True of the gate is user-defined, False otherwise.
- abstract is_builtin_gate(name: str) bool [source]
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:
bool – True if the gate is a built-in gate, False otherwise.
- add_subroutine(name: str, definition: SubroutineDefinition) None [source]
Add a subroutine definition
- Parameters:
name (str) – The name of the subroutine.
definition (SubroutineDefinition) – The definition of the subroutine.
- get_subroutine_definition(name: str) SubroutineDefinition [source]
Get a subroutine definition by name
- Parameters:
name (str) – The name of the subroutine.
- Returns:
SubroutineDefinition – The definition of the subroutine.
- Raises:
NameError – If the subroutine with the give name is not defined.
- add_result(result: Amplitude | Expectation | Probability | Sample | StateVector | DensityMatrix | Variance | AdjointGradient) None [source]
Abstract method to add result type to the circuit
- Parameters:
result (Results) – The result object representing the measurement results
- add_phase(phase: FloatLiteral, qubits: list[Identifier | IndexedIdentifier] | None = None) None [source]
Add quantum phase instruction to the circuit
- abstract add_phase_instruction(target, phase_value)[source]
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: str, parameters: list[FloatLiteral], qubits: list[Identifier | IndexedIdentifier], modifiers: list[QuantumGateModifier] | None = None) None [source]
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: float | Expr) Any [source]
Convert parameter value to required format. Default conversion is noop. :param value: Value of the parameter :type value: Union[float, Expr]
- abstract add_gate_instruction(gate_name: str, target: tuple[int, ...], params, ctrl_modifiers: list[int], power: int)[source]
Abstract method to add Braket gate to the circuit. :param gate_name: name of the built-in Braket gate. :type gate_name: str :param target: control_qubits + target_qubits. :type target: tuple[int] :param ctrl_modifiers: Quantum state on which to control the
operation. Must be a binary sequence of same length as number of qubits in
control-qubits
in target. For example “0101”, [0, 1, 0, 1], 5 all represent controlling on qubits 0 and 2 being in the |0⟩ state and qubits 1 and 3 being in the |1⟩ state.- Parameters:
power (float) – Integer or fractional power to raise the gate to.
- add_custom_unitary(unitary: ndarray, target: tuple[int, ...]) None [source]
Abstract method to add a custom Unitary instruction to the circuit :param unitary: unitary matrix :type unitary: np.ndarray :param target: control_qubits + target_qubits :type target: tuple[int, …]
- add_noise_instruction(noise_instruction: str, target: list[int], probabilities: list[float])[source]
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: list[ndarray], target: list[int])[source]
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.
- class braket.default_simulator.openqasm.program_context.ProgramContext(circuit: Circuit | None = None)[source]
Bases:
AbstractProgramContext
- 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: str) bool [source]
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:
bool – True if the gate is a built-in gate, False otherwise.
- add_phase_instruction(target: tuple[int], phase_value: int)[source]
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: str, target: tuple[int, ...], params, ctrl_modifiers: list[int], power: int)[source]
Abstract method to add Braket gate to the circuit. :param gate_name: name of the built-in Braket gate. :type gate_name: str :param target: control_qubits + target_qubits. :type target: tuple[int] :param ctrl_modifiers: Quantum state on which to control the
operation. Must be a binary sequence of same length as number of qubits in
control-qubits
in target. For example “0101”, [0, 1, 0, 1], 5 all represent controlling on qubits 0 and 2 being in the |0⟩ state and qubits 1 and 3 being in the |1⟩ state.- Parameters:
power (float) – Integer or fractional power to raise the gate to.
- add_custom_unitary(unitary: ndarray, target: tuple[int, ...]) None [source]
Abstract method to add a custom Unitary instruction to the circuit :param unitary: unitary matrix :type unitary: np.ndarray :param target: control_qubits + target_qubits :type target: tuple[int, …]
- add_noise_instruction(noise_instruction: str, target: list[int], probabilities: list[float])[source]
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: list[ndarray], target: list[int])[source]
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.