braket.default_simulator.simulator module
- class braket.default_simulator.simulator.OpenQASMSimulator[source]
Bases:
BraketSimulator,ABCAn 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
AbstractProgramContextinterface. This implementation is exposed by implementing thecreate_program_contextmethod, which enables theparse_programmethod 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>" >>> ] >>> }
- abstractmethod 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 | ProgramSet | Program, *args, **kwargs) GateModelTaskResult | ProgramSetTaskResult[source]
Simulate a program using either OpenQASM or Jaqcd.
- Parameters:
circuit_ir (OpenQASMProgram | ProgramSet | JaqcdProgram) – Program specification.
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 program 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 | ProgramSetTaskResult – 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.
- abstractmethod initialize_simulation(**kwargs) Simulation[source]
Initializes simulation with keyword arguments
- run_program_set(program_set: ProgramSet, shots: int = 0, *, batch_size: int = 1) ProgramSetTaskResult[source]
Executes the program set specified by the supplied
program_set_iron the simulator.- Parameters:
program_set (ProgramSet) – IR representation of the program set.
shots (int) – The number of times to run each executable in the program set.
batch_size (int) – The size of the circuit partitions to contract for each executable in the program set, 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 contraction.
- Returns:
ProgramSetTaskResult – Result of the program set simulation.
- run_openqasm(openqasm_ir: Program, shots: int = 0, *, batch_size: int = 1) GateModelTaskResult[source]
Executes the circuit specified by the supplied
circuit_iron 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: Any = None, shots: int = 0, *, batch_size: int = 1) GateModelTaskResult[source]
Executes the circuit specified by the supplied
circuit_iron the simulator.- Parameters:
circuit_ir (Program) – ir representation of a braket circuit specifying the instructions to execute.
qubit_count (Any) – Unused parameter; in signature for backwards-compatibility
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.