qiskit-documentation/docs/api/qiskit/0.24/qiskit.providers.aer.QasmSi...

317 lines
20 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: QasmSimulator
description: API reference for qiskit.providers.aer.QasmSimulator
in_page_toc_min_heading_level: 1
python_api_type: class
python_api_name: qiskit.providers.aer.QasmSimulator
---
<span id="qiskit-providers-aer-qasmsimulator" />
# qiskit.providers.aer.QasmSimulator
<Class id="qiskit.providers.aer.QasmSimulator" isDedicatedPage={true} github="https://github.com/qiskit/qiskit-aer/tree/stable/0.7/qiskit/providers/aer/backends/qasm_simulator.py" signature="QasmSimulator(configuration=None, properties=None, provider=None, **backend_options)" modifiers="class">
Noisy quantum circuit simulator backend.
**Configurable Options**
The QasmSimulator supports multiple simulation methods and configurable options for each simulation method. These may be set using the appropriate kwargs during initialization. They can also be set of updated using the [`set_options()`](#qiskit.providers.aer.QasmSimulator.set_options "qiskit.providers.aer.QasmSimulator.set_options") method.
Run-time options may also be specified as kwargs using the [`run()`](#qiskit.providers.aer.QasmSimulator.run "qiskit.providers.aer.QasmSimulator.run") method. These will not be stored in the backend and will only apply to that execution. They will also override any previously set options.
For example, to configure a density matrix simulator with a custom noise model to use for every execution
```python
noise_model = NoiseModel.from_backend(backend)
backend = QasmSimulator(method='density_matrix',
noise_model=noise_model)
```
**Simulating an IBMQ Backend**
The simulator can be automatically configured to mimic an IBMQ backend using the [`from_backend()`](#qiskit.providers.aer.QasmSimulator.from_backend "qiskit.providers.aer.QasmSimulator.from_backend") method. This will configure the simulator to use the basic device `NoiseModel` for that backend, and the same basis gates and coupling map.
```python
backend = QasmSimulator.from_backend(backend)
```
**Simulation Method Option**
The simulation method is set using the `method` kwarg. Supported simulation methods are:
* `"statevector"`: A dense statevector simulation that can sample measurement outcomes from *ideal* circuits with all measurements at end of the circuit. For noisy simulations each shot samples a randomly sampled noisy circuit from the noise model. `"statevector_cpu"` is an alias of `"statevector"`.
* `"statevector_gpu"`: A dense statevector simulation that provides the same functionalities with `"statevector"`. GPU performs the computation to calculate probability amplitudes as CPU does. If no GPU is available, a runtime error is raised.
* `"density_matrix"`: A dense density matrix simulation that may sample measurement outcomes from *noisy* circuits with all measurements at end of the circuit. It can only simulate half the number of qubits as the statevector method.
* `"density_matrix_gpu"`: A dense density matrix simulation that provides the same functionalities with `"density_matrix"`. GPU performs the computation to calculate probability amplitudes as CPU does. If no GPU is available, a runtime error is raised.
* `"stabilizer"`: An efficient Clifford stabilizer state simulator that can simulate noisy Clifford circuits if all errors in the noise model are also Clifford errors.
* `"extended_stabilizer"`: An approximate simulated based on a ranked-stabilizer decomposition that decomposes circuits into stabilizer state terms. The number of terms grows with the number of non-Clifford gates.
* `"matrix_product_state"`: A tensor-network statevector simulator that uses a Matrix Product State (MPS) representation for the state.
* `"automatic"`: The default behavior where the method is chosen automatically for each circuit based on the circuit instructions, number of qubits, and noise model.
**Additional Backend Options**
The following simulator specific backend options are supported
* `method` (str): Set the simulation method (Default: `"automatic"`).
* `precision` (str): Set the floating point precision for certain simulation methods to either `"single"` or `"double"` precision (default: `"double"`).
* `zero_threshold` (double): Sets the threshold for truncating small values to zero in the result data (Default: 1e-10).
* `validation_threshold` (double): Sets the threshold for checking if initial states are valid (Default: 1e-8).
* `max_parallel_threads` (int): Sets the maximum number of CPU cores used by OpenMP for parallelization. If set to 0 the maximum will be set to the number of CPU cores (Default: 0).
* `max_parallel_experiments` (int): Sets the maximum number of qobj experiments that may be executed in parallel up to the max\_parallel\_threads value. If set to 1 parallel circuit execution will be disabled. If set to 0 the maximum will be automatically set to max\_parallel\_threads (Default: 1).
* `max_parallel_shots` (int): Sets the maximum number of shots that may be executed in parallel during each experiment execution, up to the max\_parallel\_threads value. If set to 1 parallel shot execution will be disabled. If set to 0 the maximum will be automatically set to max\_parallel\_threads. Note that this cannot be enabled at the same time as parallel experiment execution (Default: 0).
* `max_memory_mb` (int): Sets the maximum size of memory to store a state vector. If a state vector needs more, an error is thrown. In general, a state vector of n-qubits uses 2^n complex values (16 Bytes). If set to 0, the maximum will be automatically set to the system memory size (Default: 0).
* `optimize_ideal_threshold` (int): Sets the qubit threshold for applying circuit optimization passes on ideal circuits. Passes include gate fusion and truncation of unused qubits (Default: 5).
* `optimize_noise_threshold` (int): Sets the qubit threshold for applying circuit optimization passes on ideal circuits. Passes include gate fusion and truncation of unused qubits (Default: 12).
These backend options only apply when using the `"statevector"` simulation method:
* `statevector_parallel_threshold` (int): Sets the threshold that the number of qubits must be greater than to enable OpenMP parallelization for matrix multiplication during execution of an experiment. If parallel circuit or shot execution is enabled this will only use unallocated CPU cores up to max\_parallel\_threads. Note that setting this too low can reduce performance (Default: 14).
* `statevector_sample_measure_opt` (int): Sets the threshold that the number of qubits must be greater than to enable a large qubit optimized implementation of measurement sampling. Note that setting this two low can reduce performance (Default: 10)
These backend options only apply when using the `"stabilizer"` simulation method:
* `stabilizer_max_snapshot_probabilities` (int): set the maximum qubit number for the \~qiskit.providers.aer.extensions.SnapshotProbabilities instruction (Default: 32).
These backend options only apply when using the `"extended_stabilizer"` simulation method:
* `extended_stabilizer_measure_sampling` (bool): Enable measure sampling optimization on supported circuits. This prevents the simulator from re-running the measure monte-carlo step for each shot. Enabling measure sampling may reduce accuracy of the measurement counts if the output distribution is strongly peaked (Default: False).
* `extended_stabilizer_mixing_time` (int): Set how long the monte-carlo method runs before performing measurements. If the output distribution is strongly peaked, this can be decreased alongside setting extended\_stabilizer\_disable\_measurement\_opt to True (Default: 5000).
* `"extended_stabilizer_approximation_error"` (double): Set the error in the approximation for the extended\_stabilizer method. A smaller error needs more memory and computational time (Default: 0.05).
* `extended_stabilizer_norm_estimation_samples` (int): Number of samples used to compute the correct normalization for a statevector snapshot (Default: 100).
* `extended_stabilizer_parallel_threshold` (int): Set the minimum size of the extended stabilizer decomposition before we enable OpenMP parallelization. If parallel circuit or shot execution is enabled this will only use unallocated CPU cores up to max\_parallel\_threads (Default: 100).
These backend options only apply when using the `"matrix_product_state"` simulation method:
* `matrix_product_state_max_bond_dimension` (int): Sets a limit on the number of Schmidt coefficients retained at the end of the svd algorithm. Coefficients beyond this limit will be discarded. (Default: None, i.e., no limit on the bond dimension).
* `matrix_product_state_truncation_threshold` (double): Discard the smallest coefficients for which the sum of their squares is smaller than this threshold. (Default: 1e-16).
* `mps_sample_measure_algorithm` (str): Choose which algorithm to use for `"sample_measure"`. `"mps_probabilities"` means all state probabilities are computed and measurements are based on them. It is more efficient for a large number of shots, small number of qubits and low entanglement. `"mps_apply_measure"` creates a copy of the mps structure and makes a measurement on it. It is more effients for a small number of shots, high number of qubits, and low entanglement. If the user does not specify the algorithm, a heuristic algorithm is used to select between the two algorithms. (Default: “mps\_heuristic”).
These backend options apply in circuit optimization passes:
* `fusion_enable` (bool): Enable fusion optimization in circuit optimization passes \[Default: True]
* `fusion_verbose` (bool): Output gates generated in fusion optimization into metadata \[Default: False]
* `fusion_max_qubit` (int): Maximum number of qubits for a operation generated in a fusion optimization \[Default: 5]
* `fusion_threshold` (int): Threshold that number of qubits must be greater than or equal to enable fusion optimization \[Default: 14]
Aer class for backends.
This method should initialize the module and its configuration, and raise an exception if a component of the module is not available.
**Parameters**
* **configuration** ([*BackendConfiguration*](qiskit.providers.models.BackendConfiguration "qiskit.providers.models.BackendConfiguration")) backend configuration.
* **properties** ([*BackendProperties*](qiskit.providers.models.BackendProperties "qiskit.providers.models.BackendProperties") *or None*) Optional, backend properties.
* **defaults** ([*PulseDefaults*](qiskit.providers.models.PulseDefaults "qiskit.providers.models.PulseDefaults") *or None*) Optional, backend pulse defaults.
* **available\_methods** (*list or None*) Optional, the available simulation methods if backend supports multiple methods.
* **provider** ([*BaseProvider*](qiskit.providers.BaseProvider "qiskit.providers.BaseProvider")) Optional, provider responsible for this backend.
* **backend\_options** (*dict or None*) Optional set custom backend options.
**Raises**
[**AerError**](qiskit.providers.aer.AerError "qiskit.providers.aer.AerError") if there is no name in the configuration
### \_\_init\_\_
<Function id="qiskit.providers.aer.QasmSimulator.__init__" signature="__init__(configuration=None, properties=None, provider=None, **backend_options)">
Aer class for backends.
This method should initialize the module and its configuration, and raise an exception if a component of the module is not available.
**Parameters**
* **configuration** ([*BackendConfiguration*](qiskit.providers.models.BackendConfiguration "qiskit.providers.models.BackendConfiguration")) backend configuration.
* **properties** ([*BackendProperties*](qiskit.providers.models.BackendProperties "qiskit.providers.models.BackendProperties") *or None*) Optional, backend properties.
* **defaults** ([*PulseDefaults*](qiskit.providers.models.PulseDefaults "qiskit.providers.models.PulseDefaults") *or None*) Optional, backend pulse defaults.
* **available\_methods** (*list or None*) Optional, the available simulation methods if backend supports multiple methods.
* **provider** ([*BaseProvider*](qiskit.providers.BaseProvider "qiskit.providers.BaseProvider")) Optional, provider responsible for this backend.
* **backend\_options** (*dict or None*) Optional set custom backend options.
**Raises**
[**AerError**](qiskit.providers.aer.AerError "qiskit.providers.aer.AerError") if there is no name in the configuration
</Function>
## Methods
| | |
| ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------- |
| [`__init__`](#qiskit.providers.aer.QasmSimulator.__init__ "qiskit.providers.aer.QasmSimulator.__init__")(\[configuration, properties, provider]) | Aer class for backends. |
| [`available_methods`](#qiskit.providers.aer.QasmSimulator.available_methods "qiskit.providers.aer.QasmSimulator.available_methods")() | Return the available simulation methods. |
| [`clear_options`](#qiskit.providers.aer.QasmSimulator.clear_options "qiskit.providers.aer.QasmSimulator.clear_options")() | Reset the simulator options to default values. |
| [`configuration`](#qiskit.providers.aer.QasmSimulator.configuration "qiskit.providers.aer.QasmSimulator.configuration")() | Return the simulator backend configuration. |
| [`defaults`](#qiskit.providers.aer.QasmSimulator.defaults "qiskit.providers.aer.QasmSimulator.defaults")() | Return the simulator backend pulse defaults. |
| [`from_backend`](#qiskit.providers.aer.QasmSimulator.from_backend "qiskit.providers.aer.QasmSimulator.from_backend")(backend, \*\*options) | Initialize simulator from backend. |
| [`name`](#qiskit.providers.aer.QasmSimulator.name "qiskit.providers.aer.QasmSimulator.name")() | Return the backend name. |
| [`properties`](#qiskit.providers.aer.QasmSimulator.properties "qiskit.providers.aer.QasmSimulator.properties")() | Return the simulator backend properties if set. |
| [`provider`](#qiskit.providers.aer.QasmSimulator.provider "qiskit.providers.aer.QasmSimulator.provider")() | Return the backend Provider. |
| [`run`](#qiskit.providers.aer.QasmSimulator.run "qiskit.providers.aer.QasmSimulator.run")(qobj\[, backend\_options, validate]) | Run a qobj on the backend. |
| [`set_options`](#qiskit.providers.aer.QasmSimulator.set_options "qiskit.providers.aer.QasmSimulator.set_options")(\*\*backend\_options) | Set the simulator options |
| [`status`](#qiskit.providers.aer.QasmSimulator.status "qiskit.providers.aer.QasmSimulator.status")() | Return backend status. |
| [`version`](#qiskit.providers.aer.QasmSimulator.version "qiskit.providers.aer.QasmSimulator.version")() | Return the backend version. |
## Attributes
| | |
| ----------------------------------------------------------------------------------------------------- | ------------------------------------ |
| [`options`](#qiskit.providers.aer.QasmSimulator.options "qiskit.providers.aer.QasmSimulator.options") | Return the current simulator options |
### available\_methods
<Function id="qiskit.providers.aer.QasmSimulator.available_methods" signature="available_methods()">
Return the available simulation methods.
</Function>
### clear\_options
<Function id="qiskit.providers.aer.QasmSimulator.clear_options" signature="clear_options()">
Reset the simulator options to default values.
</Function>
### configuration
<Function id="qiskit.providers.aer.QasmSimulator.configuration" signature="configuration()">
Return the simulator backend configuration.
**Returns**
the configuration for the backend.
**Return type**
[BackendConfiguration](qiskit.providers.models.BackendConfiguration "qiskit.providers.models.BackendConfiguration")
</Function>
### defaults
<Function id="qiskit.providers.aer.QasmSimulator.defaults" signature="defaults()">
Return the simulator backend pulse defaults.
**Returns**
**The backend pulse defaults or `None` if the**
backend does not support pulse.
**Return type**
[PulseDefaults](qiskit.providers.models.PulseDefaults "qiskit.providers.models.PulseDefaults")
</Function>
### from\_backend
<Function id="qiskit.providers.aer.QasmSimulator.from_backend" signature="from_backend(backend, **options)" modifiers="classmethod">
Initialize simulator from backend.
</Function>
### name
<Function id="qiskit.providers.aer.QasmSimulator.name" signature="name()">
Return the backend name.
**Returns**
the name of the backend.
**Return type**
str
</Function>
### options
<Attribute id="qiskit.providers.aer.QasmSimulator.options">
Return the current simulator options
</Attribute>
### properties
<Function id="qiskit.providers.aer.QasmSimulator.properties" signature="properties()">
Return the simulator backend properties if set.
**Returns**
**The backend properties or `None` if the**
backend does not have properties set.
**Return type**
[BackendProperties](qiskit.providers.models.BackendProperties "qiskit.providers.models.BackendProperties")
</Function>
### provider
<Function id="qiskit.providers.aer.QasmSimulator.provider" signature="provider()">
Return the backend Provider.
**Returns**
the Provider responsible for the backend.
**Return type**
[BaseProvider](qiskit.providers.BaseProvider "qiskit.providers.BaseProvider")
</Function>
### run
<Function id="qiskit.providers.aer.QasmSimulator.run" signature="run(qobj, backend_options=None, validate=False, **run_options)">
Run a qobj on the backend.
**Parameters**
* **qobj** ([*QasmQobj*](qiskit.qobj.QasmQobj "qiskit.qobj.QasmQobj")) The Qobj to be executed.
* **backend\_options** (*dict or None*) DEPRECATED dictionary of backend options for the execution (default: None).
* **validate** (*bool*) validate the Qobj before running (default: False).
* **run\_options** (*kwargs*) additional run time backend options.
**Returns**
The simulation job.
**Return type**
[AerJob](qiskit.providers.aer.AerJob "qiskit.providers.aer.AerJob")
**Additional Information:**
* kwarg options specified in `run_options` will temporarily override any set options of the same name for the current run.
* The entries in the `backend_options` will be combined with the `Qobj.config` dictionary with the values of entries in `backend_options` taking precedence. This kwarg is deprecated and direct kwargs should be used for options to pass them to `run_options`.
</Function>
### set\_options
<Function id="qiskit.providers.aer.QasmSimulator.set_options" signature="set_options(**backend_options)">
Set the simulator options
</Function>
### status
<Function id="qiskit.providers.aer.QasmSimulator.status" signature="status()">
Return backend status.
**Returns**
the status of the backend.
**Return type**
[BackendStatus](qiskit.providers.models.BackendStatus "qiskit.providers.models.BackendStatus")
</Function>
### version
<Function id="qiskit.providers.aer.QasmSimulator.version" signature="version()">
Return the backend version.
**Returns**
the X.X.X version of the backend.
**Return type**
str
</Function>
</Class>