-
Notifications
You must be signed in to change notification settings - Fork 364
/
statevector_simulator.py
299 lines (251 loc) · 12.1 KB
/
statevector_simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Qiskit Aer statevector simulator backend.
"""
import copy
import logging
from warnings import warn
from qiskit.utils import local_hardware_info
from qiskit.providers.options import Options
from qiskit.providers.models import QasmBackendConfiguration
from ..aererror import AerError
from ..version import __version__
from .aerbackend import AerBackend
from .backend_utils import (cpp_execute, available_devices,
MAX_QUBITS_STATEVECTOR,
LEGACY_METHOD_MAP,
add_final_save_instruction,
map_legacy_method_options)
# pylint: disable=import-error, no-name-in-module
from .controller_wrappers import aer_controller_execute
# Logger
logger = logging.getLogger(__name__)
class StatevectorSimulator(AerBackend):
"""Ideal quantum circuit statevector simulator
**Configurable Options**
The `StatevectorSimulator` supports CPU and GPU simulation methods and
additional configurable options. These may be set using the appropriate kwargs
during initialization. They can also be set of updated using the
:meth:`set_options` method.
Run-time options may also be specified as kwargs using the :meth:`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 a single-precision simulator
.. code-block:: python
backend = StatevectorSimulator(precision='single')
**Backend Options**
The following configurable backend options are supported
* ``device`` (str): Set the simulation device (Default: ``"CPU"``).
Use :meth:`available_devices` to return a list of devices supported
on the current system.
* ``method`` (str): [DEPRECATED] Set the simulation method supported
methods are ``"statevector"`` for CPU simulation, and
``"statevector_gpu"`` for GPU simulation. This option has been
deprecated, use the ``device`` option to set "CPU" or "GPU"
simulation instead.
* ``precision`` (str): Set the floating point precision for
certain simulation methods to either ``"single"`` or ``"double"``
precision (default: ``"double"``).
* ``executor`` (futures.Executor): Set a custom executor for
asynchronous running of simulation jobs (Default: None).
* ``max_job_size`` (int or None): If the number of run circuits
exceeds this value simulation will be run as a set of of sub-jobs
on the executor. If ``None`` simulation of all circuits aer submitted
to the executor as a single job (Default: None).
* ``max_shot_size`` (int or None): If the number of shots of a noisy
circuit exceeds this value simulation will be split into multi
circuits for execution and the results accumulated. If ``None``
circuits will not be split based on shots. When splitting circuits
use the ``max_job_size`` option to control how these split circuits
should be submitted to the executor (Default: None).
* ``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 the initial statevector is 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_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).
* ``statevector_parallel_threshold`` (int): Sets the threshold that
"n_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).
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]
"""
_DEFAULT_CONFIGURATION = {
'backend_name': 'statevector_simulator',
'backend_version': __version__,
'n_qubits': MAX_QUBITS_STATEVECTOR,
'url': 'https://github.com/Qiskit/qiskit-aer',
'simulator': True,
'local': True,
'conditional': True,
'open_pulse': False,
'memory': True,
'max_shots': int(1e6), # Note that this backend will only ever
# perform a single shot. This value is just
# so that the default shot value for execute
# will not raise an error when trying to run
# a simulation
'description': 'A C++ statevector circuit simulator',
'coupling_map': None,
'basis_gates': sorted([
'u1', 'u2', 'u3', 'u', 'p', 'r', 'rx', 'ry', 'rz', 'id', 'x',
'y', 'z', 'h', 's', 'sdg', 'sx', 'sxdg', 't', 'tdg', 'swap', 'cx',
'cy', 'cz', 'csx', 'cu', 'cp', 'cu1', 'cu2', 'cu3', 'rxx', 'ryy',
'rzz', 'rzx', 'ccx', 'cswap', 'mcx', 'mcy', 'mcz', 'mcsx',
'mcu', 'mcp', 'mcphase', 'mcu1', 'mcu2', 'mcu3', 'mcrx', 'mcry', 'mcrz',
'mcr', 'mcswap', 'unitary', 'diagonal', 'multiplexer',
'initialize', 'delay', 'pauli'
]),
'custom_instructions': sorted([
'kraus', 'roerror', 'quantum_channel', 'qerror_loc',
'save_expval', 'save_density_matrix', 'save_statevector',
'save_probs', 'save_probs_ket', 'save_amplitudes',
'save_amplitudes_sq', 'save_state', 'set_statevector'
]),
'gates': []
}
_SIMULATION_DEVICES = ('CPU', 'GPU', 'Thrust')
_AVAILABLE_DEVICES = None
def __init__(self,
configuration=None,
properties=None,
provider=None,
**backend_options):
warn('The `StatevectorSimulator` backend will be deprecated in the'
' future. It has been superseded by the `AerSimulator`'
' backend. To obtain legacy functionality initialize with'
' `AerSimulator(method="statevector")` and append run circuits'
' with the `save_state` instruction.', PendingDeprecationWarning)
self._controller = aer_controller_execute()
if StatevectorSimulator._AVAILABLE_DEVICES is None:
StatevectorSimulator._AVAILABLE_DEVICES = available_devices(
self._controller, StatevectorSimulator._SIMULATION_DEVICES)
if configuration is None:
configuration = QasmBackendConfiguration.from_dict(
StatevectorSimulator._DEFAULT_CONFIGURATION)
else:
configuration.open_pulse = False
super().__init__(
configuration,
properties=properties,
provider=provider,
backend_options=backend_options)
@classmethod
def _default_options(cls):
return Options(
# Global options
shots=1,
device="CPU",
precision="double",
executor=None,
max_job_size=None,
max_shot_size=None,
zero_threshold=1e-10,
validation_threshold=None,
max_parallel_threads=None,
max_parallel_experiments=None,
max_parallel_shots=None,
max_memory_mb=None,
seed_simulator=None,
fusion_enable=True,
fusion_verbose=False,
fusion_max_qubit=5,
fusion_threshold=14,
# statevector options
statevector_parallel_threshold=14)
def set_option(self, key, value):
if key == "method":
# Handle deprecation of method option for device option
warn("The method option of the `StatevectorSimulator` has been"
" deprecated as of qiskit-aer 0.9.0. To run a GPU statevector"
" simulation use the option `device='GPU'` instead",
DeprecationWarning)
if value in LEGACY_METHOD_MAP:
value, device = LEGACY_METHOD_MAP[value]
self.set_option("device", device)
if value != "statevector":
raise AerError(
"only the 'statevector' method is supported for the StatevectorSimulator")
return
super().set_option(key, value)
def available_methods(self):
"""Return the available simulation methods."""
warn("The `available_methods` method of the StatevectorSimulator"
" is deprecated as of qiskit-aer 0.9.0 as this simulator only"
" supports a single method. To check if GPU simulation is available"
" use the `available_devices` method instead.",
DeprecationWarning)
return ("statevector",)
def available_devices(self):
"""Return the available simulation methods."""
return copy.copy(self._AVAILABLE_DEVICES)
def _execute(self, qobj):
"""Execute a qobj on the backend.
Args:
qobj (QasmQobj): simulator input.
Returns:
dict: return a dictionary of results.
"""
# Make deepcopy so we don't modify the original qobj
qobj = copy.deepcopy(qobj)
qobj = add_final_save_instruction(qobj, "statevector")
qobj = map_legacy_method_options(qobj)
return cpp_execute(self._controller, qobj)
def _validate(self, qobj):
"""Semantic validations of the qobj which cannot be done via schemas.
Some of these may later move to backend schemas.
1. Set shots=1.
2. Check number of qubits will fit in local memory.
"""
name = self.name()
if getattr(qobj.config, 'noise_model', None) is not None:
raise AerError("{} does not support noise.".format(name))
n_qubits = qobj.config.n_qubits
max_qubits = self.configuration().n_qubits
if n_qubits > max_qubits:
raise AerError(
'Number of qubits ({}) is greater than max ({}) for "{}" with {} GB system memory.'
.format(n_qubits, max_qubits, name,
int(local_hardware_info()['memory'])))
if qobj.config.shots != 1:
logger.info('"%s" only supports 1 shot. Setting shots=1.', name)
qobj.config.shots = 1
for experiment in qobj.experiments:
exp_name = experiment.header.name
if getattr(experiment.config, 'shots', 1) != 1:
logger.info(
'"%s" only supports 1 shot. '
'Setting shots=1 for circuit "%s".', name, exp_name)
experiment.config.shots = 1