braket.default_simulator.simulator module

class braket.default_simulator.simulator.OpenQASMSimulator[source]

Bases: BraketSimulator, ABC

An abstract simulator that runs an OpenQASM 3 program.

Translation of individual operations and observables from OpenQASM to the desired format is handled by implementing the AbstractProgramContext interface. This implementation is exposed by implementing the create_program_context method, which enables the parse_program method to translate an entire OpenQASM program:

>>> class MyProgramContext(AbstractProgramContext):
>>>     def __init__(self):
>>>         ...
>>>
>>>     def add_gate_instruction(self, gate_name: str, target: Tuple[int], ...):
>>>         ...
>>>
>>>     # Implement other MyProgramContext interface methods
>>>
>>> class MySimulator(OpenQASMSimulator):
>>>     def create_program_context(self) -> AbstractProgramContext:
>>>         return MyProgramContext()
>>>
>>>     # Implement other BraketSimulator interface methods
>>>
>>> parsed = MySimulator().parse_program(program)

To register a simulator so the Amazon Braket SDK recognizes its name, the name and class must be added as an entry point for “braket.simulators”. This is done by adding an entry to entry_points in the simulator package’s setup.py:

>>> entry_points = {
>>>     "braket.simulators": [
>>>         "backend_name = <backend_class>"
>>>     ]
>>> }
abstract create_program_context() AbstractProgramContext[source]

Creates a new program context to handle translation of OpenQASM into a desired format.

parse_program(program: Program) AbstractProgramContext[source]

Parses an OpenQASM program and returns a program context.

Parameters:

program (OpenQASMProgram) – The program to parse.

Returns:

AbstractProgramContext – The program context after the program has been parsed.

class braket.default_simulator.simulator.BaseLocalSimulator[source]

Bases: OpenQASMSimulator

run(circuit_ir: Program | Program, *args, **kwargs) GateModelTaskResult[source]

Simulate a circuit using either OpenQASM or Jaqcd.

Parameters:
  • circuit_ir (Union[OpenQASMProgram, JaqcdProgram]) – Circuit specification.

  • qubit_count (int, jaqcd-only) – Number of qubits.

  • shots (int, optional) – The number of shots to simulate. Default is 0, which performs a full analytical simulation.

  • batch_size (int, optional) – The size of the circuit partitions to contract, if applying multiple gates at a time is desired; see StateVectorSimulation. Must be a positive integer. Defaults to 1, which means gates are applied one at a time without any optimized contraction.

Returns:

GateModelTaskResult – object that represents the result

Raises:

ValueError – If result types are not specified in the IR or sample is specified as a result type when shots=0. Or, if StateVector and Amplitude result types are requested when shots>0.

create_program_context() AbstractProgramContext[source]

Creates a new program context to handle translation of OpenQASM into a desired format.

abstract initialize_simulation(**kwargs) Simulation[source]

Initializes simulation with keyword arguments

run_openqasm(openqasm_ir: Program, shots: int = 0, *, batch_size: int = 1) GateModelTaskResult[source]

Executes the circuit specified by the supplied circuit_ir on the simulator.

Parameters:
  • openqasm_ir (Program) – ir representation of a braket circuit specifying the instructions to execute.

  • shots (int) – The number of times to run the circuit.

  • batch_size (int) – The size of the circuit partitions to contract, if applying multiple gates at a time is desired; see StateVectorSimulation. Must be a positive integer. Defaults to 1, which means gates are applied one at a time without any optimized contraction.

Returns:

GateModelTaskResult – object that represents the result

Raises:

ValueError – If result types are not specified in the IR or sample is specified as a result type when shots=0. Or, if StateVector and Amplitude result types are requested when shots>0.

run_jaqcd(circuit_ir: Program, qubit_count: int, shots: int = 0, *, batch_size: int = 1) GateModelTaskResult[source]

Executes the circuit specified by the supplied circuit_ir on the simulator.

Parameters:
  • circuit_ir (Program) – ir representation of a braket circuit specifying the instructions to execute.

  • qubit_count (int) – The number of qubits to simulate.

  • shots (int) – The number of times to run the circuit.

  • batch_size (int) – The size of the circuit partitions to contract, if applying multiple gates at a time is desired; see StateVectorSimulation. Must be a positive integer. Defaults to 1, which means gates are applied one at a time without any optimized contraction.

Returns:

GateModelTaskResult – object that represents the result

Raises:

ValueError – If result types are not specified in the IR or sample is specified as a result type when shots=0. Or, if StateVector and Amplitude result types are requested when shots>0.