tequila-basic 1.9.9__py3-none-any.whl → 1.9.10__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tequila/__init__.py +29 -14
- tequila/apps/__init__.py +14 -5
- tequila/apps/_unary_state_prep_impl.py +145 -112
- tequila/apps/adapt/__init__.py +9 -1
- tequila/apps/adapt/adapt.py +154 -113
- tequila/apps/krylov/__init__.py +1 -1
- tequila/apps/krylov/krylov.py +23 -21
- tequila/apps/robustness/helpers.py +10 -6
- tequila/apps/robustness/interval.py +238 -156
- tequila/apps/unary_state_prep.py +29 -23
- tequila/autograd_imports.py +8 -5
- tequila/circuit/__init__.py +2 -1
- tequila/circuit/_gates_impl.py +135 -67
- tequila/circuit/circuit.py +163 -79
- tequila/circuit/compiler.py +114 -105
- tequila/circuit/gates.py +288 -120
- tequila/circuit/gradient.py +35 -23
- tequila/circuit/noise.py +83 -74
- tequila/circuit/postselection.py +120 -0
- tequila/circuit/pyzx.py +10 -6
- tequila/circuit/qasm.py +201 -83
- tequila/circuit/qpic.py +63 -61
- tequila/grouping/binary_rep.py +148 -146
- tequila/grouping/binary_utils.py +84 -75
- tequila/grouping/compile_groups.py +334 -230
- tequila/grouping/ev_utils.py +77 -41
- tequila/grouping/fermionic_functions.py +383 -308
- tequila/grouping/fermionic_methods.py +170 -123
- tequila/grouping/overlapping_methods.py +69 -52
- tequila/hamiltonian/paulis.py +12 -13
- tequila/hamiltonian/paulistring.py +1 -1
- tequila/hamiltonian/qubit_hamiltonian.py +45 -35
- tequila/ml/__init__.py +1 -0
- tequila/ml/interface_torch.py +19 -16
- tequila/ml/ml_api.py +11 -10
- tequila/ml/utils_ml.py +12 -11
- tequila/objective/__init__.py +8 -3
- tequila/objective/braket.py +55 -47
- tequila/objective/objective.py +87 -55
- tequila/objective/qtensor.py +36 -27
- tequila/optimizers/__init__.py +31 -23
- tequila/optimizers/_containers.py +11 -7
- tequila/optimizers/optimizer_base.py +111 -83
- tequila/optimizers/optimizer_gd.py +258 -231
- tequila/optimizers/optimizer_gpyopt.py +56 -42
- tequila/optimizers/optimizer_scipy.py +157 -112
- tequila/quantumchemistry/__init__.py +66 -38
- tequila/quantumchemistry/chemistry_tools.py +393 -209
- tequila/quantumchemistry/encodings.py +121 -13
- tequila/quantumchemistry/madness_interface.py +170 -96
- tequila/quantumchemistry/orbital_optimizer.py +86 -41
- tequila/quantumchemistry/psi4_interface.py +166 -97
- tequila/quantumchemistry/pyscf_interface.py +70 -23
- tequila/quantumchemistry/qc_base.py +866 -414
- tequila/simulators/__init__.py +0 -3
- tequila/simulators/simulator_api.py +247 -105
- tequila/simulators/simulator_aqt.py +102 -0
- tequila/simulators/simulator_base.py +147 -53
- tequila/simulators/simulator_cirq.py +58 -42
- tequila/simulators/simulator_cudaq.py +600 -0
- tequila/simulators/simulator_ddsim.py +390 -0
- tequila/simulators/simulator_mqp.py +30 -0
- tequila/simulators/simulator_pyquil.py +190 -171
- tequila/simulators/simulator_qibo.py +95 -87
- tequila/simulators/simulator_qiskit.py +119 -107
- tequila/simulators/simulator_qlm.py +52 -26
- tequila/simulators/simulator_qulacs.py +74 -52
- tequila/simulators/simulator_spex.py +95 -60
- tequila/simulators/simulator_symbolic.py +6 -5
- tequila/simulators/test_spex_simulator.py +8 -11
- tequila/tools/convenience.py +4 -4
- tequila/tools/qng.py +72 -64
- tequila/tools/random_generators.py +38 -34
- tequila/utils/bitstrings.py +7 -7
- tequila/utils/exceptions.py +19 -5
- tequila/utils/joined_transformation.py +8 -10
- tequila/utils/keymap.py +0 -5
- tequila/utils/misc.py +6 -4
- tequila/version.py +1 -1
- tequila/wavefunction/qubit_wavefunction.py +47 -28
- {tequila_basic-1.9.9.dist-info → tequila_basic-1.9.10.dist-info}/METADATA +13 -16
- tequila_basic-1.9.10.dist-info/RECORD +93 -0
- {tequila_basic-1.9.9.dist-info → tequila_basic-1.9.10.dist-info}/WHEEL +1 -1
- tequila_basic-1.9.9.dist-info/RECORD +0 -88
- {tequila_basic-1.9.9.dist-info → tequila_basic-1.9.10.dist-info}/licenses/LICENSE +0 -0
- {tequila_basic-1.9.9.dist-info → tequila_basic-1.9.10.dist-info}/top_level.txt +0 -0
tequila/simulators/__init__.py
CHANGED
@@ -1,23 +1,43 @@
|
|
1
1
|
from collections import namedtuple
|
2
|
-
import typing
|
2
|
+
import typing
|
3
|
+
import warnings
|
4
|
+
import numpy
|
3
5
|
from numbers import Real as RealNumber
|
4
6
|
from typing import Dict, Union, Hashable
|
5
7
|
import pkg_resources
|
6
8
|
from pkg_resources import DistributionNotFound
|
7
|
-
|
8
9
|
from tequila.objective import Objective, Variable, assign_variable, format_variable_dictionary, QTensor
|
9
10
|
from tequila.utils.exceptions import TequilaException, TequilaWarning
|
10
11
|
from tequila.simulators.simulator_base import BackendCircuit, BackendExpectationValue
|
11
12
|
from tequila.circuit.noise import NoiseModel
|
12
13
|
from tequila.wavefunction.qubit_wavefunction import QubitWaveFunction
|
14
|
+
from tequila.simulators.simulator_symbolic import BackendCircuitSymbolic, BackendExpectationValueSymbolic
|
15
|
+
|
16
|
+
SUPPORTED_BACKENDS = [
|
17
|
+
"qulacs",
|
18
|
+
"qulacs_gpu",
|
19
|
+
"qibo",
|
20
|
+
"qiskit",
|
21
|
+
"qiskit_gpu",
|
22
|
+
"cirq",
|
23
|
+
"pyquil",
|
24
|
+
"symbolic",
|
25
|
+
"qlm",
|
26
|
+
"spex",
|
27
|
+
"aqt",
|
28
|
+
"mqp",
|
29
|
+
"ddsim",
|
30
|
+
"cudaq",
|
31
|
+
]
|
13
32
|
|
14
|
-
SUPPORTED_BACKENDS = ["qulacs", "qulacs_gpu", "qibo", "qiskit", "qiskit_gpu", "cirq", "pyquil", "symbolic", "qlm", "spex"]
|
15
33
|
# TODO: Reenable noise for Qiskit
|
16
34
|
SUPPORTED_NOISE_BACKENDS = ["cirq", "pyquil"] # qulacs removed in v.1.9
|
17
|
-
|
35
|
+
|
36
|
+
BackendTypes = namedtuple("BackendTypes", "CircType ExpValueType")
|
18
37
|
INSTALLED_SIMULATORS = {}
|
19
38
|
INSTALLED_SAMPLERS = {}
|
20
39
|
|
40
|
+
|
21
41
|
HAS_QULACS = True
|
22
42
|
INSTALLED_NOISE_SAMPLERS = {}
|
23
43
|
if typing.TYPE_CHECKING:
|
@@ -30,6 +50,15 @@ Check which simulators are installed
|
|
30
50
|
We are distinguishing two classes of simulators: Samplers and full wavefunction simulators
|
31
51
|
"""
|
32
52
|
|
53
|
+
# a check block for cudaq
|
54
|
+
HAS_CUDAQ = True
|
55
|
+
try:
|
56
|
+
from tequila.simulators.simulator_cudaq import BackendCircuitCudaq, BackendExpectationValueCudaq
|
57
|
+
|
58
|
+
INSTALLED_SIMULATORS["cudaq"] = BackendTypes(BackendCircuitCudaq, BackendExpectationValueCudaq)
|
59
|
+
except ImportError:
|
60
|
+
HAS_CUDAQ = False
|
61
|
+
|
33
62
|
|
34
63
|
HAS_SPEX = True
|
35
64
|
try:
|
@@ -39,14 +68,33 @@ try:
|
|
39
68
|
except ImportError:
|
40
69
|
HAS_SPEX = False
|
41
70
|
|
71
|
+
# mqp and aqt are samplers, not simulators
|
72
|
+
HAS_AQT = True
|
73
|
+
try:
|
74
|
+
from tequila.simulators.simulator_aqt import BackendCircuitAQT, BackendExpectationValueAQT
|
75
|
+
|
76
|
+
INSTALLED_SAMPLERS["aqt"] = BackendTypes(BackendCircuitAQT, BackendExpectationValueAQT)
|
77
|
+
except ImportError:
|
78
|
+
HAS_AQT = False
|
79
|
+
|
80
|
+
HAS_MQP = True
|
81
|
+
try:
|
82
|
+
from tequila.simulators.simulator_mqp import BackendCircuitMQP, BackendExpectationValueMQP
|
83
|
+
|
84
|
+
INSTALLED_SAMPLERS["mqp"] = BackendTypes(BackendCircuitMQP, BackendExpectationValueMQP)
|
85
|
+
except ImportError:
|
86
|
+
HAS_MQP = False
|
87
|
+
|
42
88
|
|
43
89
|
HAS_QISKIT = True
|
44
90
|
try:
|
45
91
|
from tequila.simulators.simulator_qiskit import BackendCircuitQiskit, BackendExpectationValueQiskit
|
92
|
+
|
46
93
|
HAS_QISKIT = True
|
47
94
|
INSTALLED_SIMULATORS["qiskit"] = BackendTypes(BackendCircuitQiskit, BackendExpectationValueQiskit)
|
48
95
|
INSTALLED_SAMPLERS["qiskit"] = BackendTypes(BackendCircuitQiskit, BackendExpectationValueQiskit)
|
49
96
|
from tequila.simulators.simulator_qiskit import HAS_NOISE as HAS_QISKIT_NOISE
|
97
|
+
|
50
98
|
if HAS_QISKIT_NOISE:
|
51
99
|
INSTALLED_NOISE_SAMPLERS["qiskit"] = BackendTypes(BackendCircuitQiskit, BackendExpectationValueQiskit)
|
52
100
|
except ImportError:
|
@@ -56,10 +104,12 @@ except ImportError:
|
|
56
104
|
try:
|
57
105
|
pkg_resources.require("qiskit-aer-gpu")
|
58
106
|
from tequila.simulators.simulator_qiskit_gpu import BackendCircuitQiskitGpu, BackendExpectationValueQiskitGpu
|
107
|
+
|
59
108
|
HAS_QISKIT_GPU = True
|
60
109
|
INSTALLED_SIMULATORS["qiskit_gpu"] = BackendTypes(BackendCircuitQiskitGpu, BackendExpectationValueQiskitGpu)
|
61
110
|
INSTALLED_SAMPLERS["qiskit_gpu"] = BackendTypes(BackendCircuitQiskitGpu, BackendExpectationValueQiskitGpu)
|
62
111
|
from tequila.simulators.simulator_qiskit import HAS_NOISE as HAS_QISKIT_GPU_NOISE
|
112
|
+
|
63
113
|
if HAS_QISKIT_GPU_NOISE:
|
64
114
|
INSTALLED_NOISE_SAMPLERS["qiskit_gpu"] = BackendTypes(BackendCircuitQiskitGpu, BackendExpectationValueQiskitGpu)
|
65
115
|
except (ImportError, DistributionNotFound):
|
@@ -69,6 +119,7 @@ except (ImportError, DistributionNotFound):
|
|
69
119
|
HAS_QIBO = True
|
70
120
|
try:
|
71
121
|
from tequila.simulators.simulator_qibo import BackendCircuitQibo, BackendExpectationValueQibo
|
122
|
+
|
72
123
|
HAS_QIBO = True
|
73
124
|
INSTALLED_SIMULATORS["qibo"] = BackendTypes(BackendCircuitQibo, BackendExpectationValueQibo)
|
74
125
|
INSTALLED_SAMPLERS["qibo"] = BackendTypes(BackendCircuitQibo, BackendExpectationValueQibo)
|
@@ -83,8 +134,9 @@ try:
|
|
83
134
|
HAS_CIRQ = True
|
84
135
|
INSTALLED_SIMULATORS["cirq"] = BackendTypes(CircType=BackendCircuitCirq, ExpValueType=BackendExpectationValueCirq)
|
85
136
|
INSTALLED_SAMPLERS["cirq"] = BackendTypes(CircType=BackendCircuitCirq, ExpValueType=BackendExpectationValueCirq)
|
86
|
-
INSTALLED_NOISE_SAMPLERS["cirq"] = BackendTypes(
|
87
|
-
|
137
|
+
INSTALLED_NOISE_SAMPLERS["cirq"] = BackendTypes(
|
138
|
+
CircType=BackendCircuitCirq, ExpValueType=BackendExpectationValueCirq
|
139
|
+
)
|
88
140
|
|
89
141
|
except ImportError:
|
90
142
|
HAS_CIRQ = False
|
@@ -95,12 +147,15 @@ try:
|
|
95
147
|
from tequila.simulators.simulator_qulacs import BackendCircuitQulacs, BackendExpectationValueQulacs
|
96
148
|
|
97
149
|
HAS_QULACS = True
|
98
|
-
INSTALLED_SIMULATORS["qulacs"] = BackendTypes(
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
150
|
+
INSTALLED_SIMULATORS["qulacs"] = BackendTypes(
|
151
|
+
CircType=BackendCircuitQulacs, ExpValueType=BackendExpectationValueQulacs
|
152
|
+
)
|
153
|
+
INSTALLED_SAMPLERS["qulacs"] = BackendTypes(
|
154
|
+
CircType=BackendCircuitQulacs, ExpValueType=BackendExpectationValueQulacs
|
155
|
+
)
|
156
|
+
INSTALLED_NOISE_SAMPLERS["qulacs"] = BackendTypes(
|
157
|
+
CircType=BackendCircuitQulacs, ExpValueType=BackendExpectationValueQulacs
|
158
|
+
)
|
104
159
|
except (ImportError, DistributionNotFound):
|
105
160
|
HAS_QULACS = False
|
106
161
|
|
@@ -110,12 +165,15 @@ try:
|
|
110
165
|
from tequila.simulators.simulator_qulacs_gpu import BackendCircuitQulacsGpu, BackendExpectationValueQulacsGpu
|
111
166
|
|
112
167
|
HAS_QULACS_GPU = True
|
113
|
-
INSTALLED_SIMULATORS["qulacs_gpu"] = BackendTypes(
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
168
|
+
INSTALLED_SIMULATORS["qulacs_gpu"] = BackendTypes(
|
169
|
+
CircType=BackendCircuitQulacsGpu, ExpValueType=BackendExpectationValueQulacsGpu
|
170
|
+
)
|
171
|
+
INSTALLED_SAMPLERS["qulacs_gpu"] = BackendTypes(
|
172
|
+
CircType=BackendCircuitQulacsGpu, ExpValueType=BackendExpectationValueQulacsGpu
|
173
|
+
)
|
174
|
+
INSTALLED_NOISE_SAMPLERS["qulacs_gpu"] = BackendTypes(
|
175
|
+
CircType=BackendCircuitQulacsGpu, ExpValueType=BackendExpectationValueQulacsGpu
|
176
|
+
)
|
119
177
|
except (ImportError, DistributionNotFound):
|
120
178
|
HAS_QULACS_GPU = False
|
121
179
|
|
@@ -142,30 +200,42 @@ try:
|
|
142
200
|
except ImportError:
|
143
201
|
HAS_QLM = False
|
144
202
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
ExpValueType=BackendExpectationValueSymbolic)
|
203
|
+
INSTALLED_SIMULATORS["symbolic"] = BackendTypes(
|
204
|
+
CircType=BackendCircuitSymbolic, ExpValueType=BackendExpectationValueSymbolic
|
205
|
+
)
|
149
206
|
HAS_SYMBOLIC = True
|
150
207
|
|
208
|
+
HAS_DDSIM = True
|
209
|
+
try:
|
210
|
+
from tequila.simulators.simulator_ddsim import BackendCircuitDDSim, BackendExpectationValueDDSim
|
211
|
+
|
212
|
+
INSTALLED_SIMULATORS["ddsim"] = BackendTypes(BackendCircuitDDSim, BackendExpectationValueDDSim)
|
213
|
+
INSTALLED_SAMPLERS["ddsim"] = BackendTypes(BackendCircuitDDSim, BackendExpectationValueDDSim)
|
214
|
+
except ImportError:
|
215
|
+
HAS_DDSIM = False
|
216
|
+
|
151
217
|
|
152
218
|
def show_available_simulators():
|
153
219
|
""" """
|
154
220
|
print("{:15} | {:10} | {:10} | {:10} | {:10}".format("backend", "wfn", "sampling", "noise", "installed"))
|
155
221
|
print("--------------------------------------------------------------------")
|
156
222
|
for k in SUPPORTED_BACKENDS:
|
157
|
-
print(
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
223
|
+
print(
|
224
|
+
"{:15} | {:10} | {:10} | {:10} | {:10}".format(
|
225
|
+
k,
|
226
|
+
str(k in INSTALLED_SIMULATORS),
|
227
|
+
str(k in INSTALLED_SAMPLERS),
|
228
|
+
str(k in INSTALLED_NOISE_SAMPLERS),
|
229
|
+
str(k in INSTALLED_BACKENDS),
|
230
|
+
)
|
231
|
+
)
|
162
232
|
if HAS_QISKIT and not HAS_QISKIT_NOISE:
|
163
233
|
print("missing qiskit_aer: no noisy simulation")
|
164
234
|
|
165
235
|
|
166
|
-
def pick_backend(
|
167
|
-
|
168
|
-
|
236
|
+
def pick_backend(
|
237
|
+
backend: str = None, samples: int = None, noise: NoiseModel = None, device=None, exclude_symbolic: bool = True
|
238
|
+
) -> str:
|
169
239
|
"""
|
170
240
|
choose, or verify, a backend for the user.
|
171
241
|
Parameters
|
@@ -188,11 +258,11 @@ def pick_backend(backend: str = None, samples: int = None, noise: NoiseModel = N
|
|
188
258
|
the name of the chosen (or verified) backend.
|
189
259
|
"""
|
190
260
|
|
191
|
-
if len(INSTALLED_SIMULATORS) == 0:
|
261
|
+
if len(INSTALLED_SIMULATORS) == 0 and len(INSTALLED_SAMPLERS) == 0:
|
192
262
|
raise TequilaException("No simulators installed on your system")
|
193
263
|
|
194
264
|
if backend is None and device is not None:
|
195
|
-
raise TequilaException(
|
265
|
+
raise TequilaException("device use requires backend specification!")
|
196
266
|
|
197
267
|
if backend is None:
|
198
268
|
if noise is None:
|
@@ -205,30 +275,28 @@ def pick_backend(backend: str = None, samples: int = None, noise: NoiseModel = N
|
|
205
275
|
return f
|
206
276
|
else:
|
207
277
|
if samples is None:
|
208
|
-
raise TequilaException(
|
209
|
-
"Noise requires sampling; please provide a positive, integer value for samples")
|
278
|
+
raise TequilaException("Noise requires sampling; please provide a positive, integer value for samples")
|
210
279
|
for f in SUPPORTED_NOISE_BACKENDS:
|
211
280
|
return f
|
212
|
-
raise TequilaException(
|
213
|
-
'Could not find any installed sampler!')
|
214
|
-
|
281
|
+
raise TequilaException("Could not find any installed sampler!")
|
215
282
|
|
216
283
|
if hasattr(backend, "lower"):
|
217
284
|
backend = backend.lower()
|
218
285
|
|
219
286
|
if backend == "random":
|
220
287
|
if device is not None:
|
221
|
-
raise TequilaException(
|
288
|
+
raise TequilaException("cannot ask for a random backend and a specific device!")
|
222
289
|
from numpy import random as random
|
223
290
|
import time
|
224
|
-
|
291
|
+
|
292
|
+
state = random.RandomState(int(str(time.process_time()).split(".")[-1]) % 2**32)
|
225
293
|
if samples is None:
|
226
294
|
backend = state.choice(list(INSTALLED_SIMULATORS.keys()), 1)[0]
|
227
295
|
else:
|
228
296
|
backend = state.choice(list(INSTALLED_SAMPLERS.keys()), 1)[0]
|
229
297
|
|
230
298
|
if exclude_symbolic:
|
231
|
-
while
|
299
|
+
while backend == "symbolic":
|
232
300
|
backend = state.choice(list(INSTALLED_SIMULATORS.keys()), 1)[0]
|
233
301
|
return backend
|
234
302
|
|
@@ -241,19 +309,22 @@ def pick_backend(backend: str = None, samples: int = None, noise: NoiseModel = N
|
|
241
309
|
raise TequilaException("Backend {backend} not installed or sampling not supported".format(backend=backend))
|
242
310
|
elif noise is not None and samples is not None and backend not in INSTALLED_NOISE_SAMPLERS.keys():
|
243
311
|
raise TequilaException(
|
244
|
-
"Backend {backend} not installed or else Noise has not been implemented".format(backend=backend)
|
312
|
+
"Backend {backend} not installed or else Noise has not been implemented".format(backend=backend)
|
313
|
+
)
|
245
314
|
|
246
315
|
return backend
|
247
316
|
|
248
317
|
|
249
|
-
def compile_objective(
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
318
|
+
def compile_objective(
|
319
|
+
objective: typing.Union["Objective"],
|
320
|
+
variables: typing.Dict["Variable", "RealNumber"] = None,
|
321
|
+
backend: str = None,
|
322
|
+
samples: int = None,
|
323
|
+
device: str = None,
|
324
|
+
noise: NoiseModel = None,
|
325
|
+
*args,
|
326
|
+
**kwargs,
|
327
|
+
) -> Objective:
|
257
328
|
"""
|
258
329
|
compile an objective to render it callable and return it.
|
259
330
|
Parameters
|
@@ -285,7 +356,10 @@ def compile_objective(objective: typing.Union['Objective'],
|
|
285
356
|
if variables is None:
|
286
357
|
variables = {k: 0.0 for k in objective.extract_variables()}
|
287
358
|
|
288
|
-
|
359
|
+
if samples is None:
|
360
|
+
ExpValueType = INSTALLED_SIMULATORS[pick_backend(backend=backend)].ExpValueType
|
361
|
+
else:
|
362
|
+
ExpValueType = INSTALLED_SAMPLERS[pick_backend(backend=backend, samples=samples)].ExpValueType
|
289
363
|
all_compiled = True
|
290
364
|
# check if compiling is necessary
|
291
365
|
for arg in objective.args:
|
@@ -293,7 +367,10 @@ def compile_objective(objective: typing.Union['Objective'],
|
|
293
367
|
if not isinstance(arg, ExpValueType):
|
294
368
|
warnings.warn(
|
295
369
|
"Looks like part the objective was already compiled for another backend.\nFound ExpectationValue of type {} and {}\n... proceeding with hybrid\n".format(
|
296
|
-
type(arg), ExpValueType
|
370
|
+
type(arg), ExpValueType
|
371
|
+
),
|
372
|
+
TequilaWarning,
|
373
|
+
)
|
297
374
|
elif hasattr(arg, "U") and not isinstance(arg, BackendExpectationValue):
|
298
375
|
all_compiled = False
|
299
376
|
|
@@ -309,7 +386,9 @@ def compile_objective(objective: typing.Union['Objective'],
|
|
309
386
|
for arg in argset:
|
310
387
|
if hasattr(arg, "H") and hasattr(arg, "U") and not isinstance(arg, BackendExpectationValue):
|
311
388
|
if arg not in expectationvalues:
|
312
|
-
compiled_expval = ExpValueType(
|
389
|
+
compiled_expval = ExpValueType(
|
390
|
+
arg, variables=variables, noise=noise, device=device, *args, **kwargs
|
391
|
+
)
|
313
392
|
expectationvalues[arg] = compiled_expval
|
314
393
|
else:
|
315
394
|
compiled_expval = expectationvalues[arg]
|
@@ -321,14 +400,16 @@ def compile_objective(objective: typing.Union['Objective'],
|
|
321
400
|
return type(objective)(args=compiled_sets[0], transformation=objective.transformation)
|
322
401
|
|
323
402
|
|
324
|
-
def compile_circuit(
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
403
|
+
def compile_circuit(
|
404
|
+
abstract_circuit: "QCircuit",
|
405
|
+
variables: typing.Dict["Variable", "RealNumber"] = None,
|
406
|
+
backend: str = None,
|
407
|
+
samples: int = None,
|
408
|
+
noise: NoiseModel = None,
|
409
|
+
device: str = None,
|
410
|
+
*args,
|
411
|
+
**kwargs,
|
412
|
+
) -> BackendCircuit:
|
332
413
|
"""
|
333
414
|
compile a circuit to render it callable and return it.
|
334
415
|
Parameters
|
@@ -354,8 +435,14 @@ def compile_circuit(abstract_circuit: 'QCircuit',
|
|
354
435
|
the compiled circuit.
|
355
436
|
"""
|
356
437
|
|
357
|
-
|
358
|
-
|
438
|
+
if samples is None:
|
439
|
+
CircType = INSTALLED_SIMULATORS[
|
440
|
+
pick_backend(backend=backend, samples=samples, noise=noise, device=device)
|
441
|
+
].CircType
|
442
|
+
else:
|
443
|
+
CircType = INSTALLED_SAMPLERS[
|
444
|
+
pick_backend(backend=backend, samples=samples, noise=noise, device=device)
|
445
|
+
].CircType
|
359
446
|
|
360
447
|
# dummy variables
|
361
448
|
if variables is None:
|
@@ -366,22 +453,27 @@ def compile_circuit(abstract_circuit: 'QCircuit',
|
|
366
453
|
abstract_circuit = abstract_circuit.abstract_circuit
|
367
454
|
warnings.warn(
|
368
455
|
"Looks like the circuit was already compiled for another backend.\nChanging from {} to {}\n".format(
|
369
|
-
type(abstract_circuit), CircType
|
456
|
+
type(abstract_circuit), CircType
|
457
|
+
),
|
458
|
+
TequilaWarning,
|
459
|
+
)
|
370
460
|
else:
|
371
461
|
return abstract_circuit
|
372
462
|
|
373
463
|
return CircType(abstract_circuit=abstract_circuit, variables=variables, noise=noise, device=device, *args, **kwargs)
|
374
464
|
|
375
465
|
|
376
|
-
def simulate(
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
466
|
+
def simulate(
|
467
|
+
objective: typing.Union["Objective", "QCircuit", "QTensor"],
|
468
|
+
variables: Dict[Union[Variable, Hashable], RealNumber] = None,
|
469
|
+
samples: int = None,
|
470
|
+
backend: str = None,
|
471
|
+
noise: NoiseModel = None,
|
472
|
+
device: str = None,
|
473
|
+
initial_state: Union[int, QubitWaveFunction] = 0,
|
474
|
+
*args,
|
475
|
+
**kwargs,
|
476
|
+
) -> Union[RealNumber, QubitWaveFunction]:
|
385
477
|
"""Simulate a tequila objective or circuit
|
386
478
|
|
387
479
|
Parameters
|
@@ -417,10 +509,20 @@ def simulate(objective: typing.Union['Objective', 'QCircuit', 'QTensor'],
|
|
417
509
|
if variables is None and not (len(objective.extract_variables()) == 0):
|
418
510
|
raise TequilaException(
|
419
511
|
"You called simulate for a parametrized type but forgot to pass down the variables: {}".format(
|
420
|
-
objective.extract_variables()
|
421
|
-
|
422
|
-
|
423
|
-
|
512
|
+
objective.extract_variables()
|
513
|
+
)
|
514
|
+
)
|
515
|
+
|
516
|
+
compiled_objective = compile(
|
517
|
+
objective=objective,
|
518
|
+
samples=samples,
|
519
|
+
variables=variables,
|
520
|
+
backend=backend,
|
521
|
+
noise=noise,
|
522
|
+
device=device,
|
523
|
+
*args,
|
524
|
+
**kwargs,
|
525
|
+
)
|
424
526
|
|
425
527
|
return compiled_objective(variables=variables, samples=samples, initial_state=initial_state, *args, **kwargs)
|
426
528
|
|
@@ -448,6 +550,7 @@ def draw(objective, variables=None, backend: str = None, name=None, *args, **kwa
|
|
448
550
|
|
449
551
|
if backend is None:
|
450
552
|
from tequila.circuit.qpic import system_has_qpic
|
553
|
+
|
451
554
|
if system_has_qpic:
|
452
555
|
backend = "qpic"
|
453
556
|
elif "cirq" in INSTALLED_SIMULATORS:
|
@@ -471,7 +574,7 @@ def draw(objective, variables=None, backend: str = None, name=None, *args, **kwa
|
|
471
574
|
print("total measurements = {}".format(measurements))
|
472
575
|
variables = E.U.extract_variables()
|
473
576
|
print("variables = {}".format(len(variables)))
|
474
|
-
filename = "{}_{}.png".format(name,i)
|
577
|
+
filename = "{}_{}.png".format(name, i)
|
475
578
|
print("circuit = {}".format(filename))
|
476
579
|
draw(E.U, backend=backend, filename=filename)
|
477
580
|
drawn[E] = i
|
@@ -483,6 +586,7 @@ def draw(objective, variables=None, backend: str = None, name=None, *args, **kwa
|
|
483
586
|
import IPython
|
484
587
|
import qpic
|
485
588
|
from tequila.circuit.qpic import export_to
|
589
|
+
|
486
590
|
if "filename" not in kwargs:
|
487
591
|
kwargs["filename"] = "tmp_{}.png".format(hash(backend))
|
488
592
|
|
@@ -493,17 +597,21 @@ def draw(objective, variables=None, backend: str = None, name=None, *args, **kwa
|
|
493
597
|
circuit = objective.abstract_circuit
|
494
598
|
|
495
599
|
export_to(circuit=circuit, *args, **kwargs)
|
496
|
-
width=None
|
497
|
-
height=200
|
600
|
+
width = None # full size
|
601
|
+
height = 200
|
498
602
|
if "width" in kwargs:
|
499
|
-
width=kwargs["width"]
|
603
|
+
width = kwargs["width"]
|
500
604
|
if "height" in kwargs:
|
501
|
-
height=kwargs["height"]
|
502
|
-
image=IPython.display.Image(filename=kwargs["filename"], height=height, width=width)
|
605
|
+
height = kwargs["height"] # this is buggy in jupyter and will be ignored
|
606
|
+
image = IPython.display.Image(filename=kwargs["filename"], height=height, width=width)
|
503
607
|
IPython.display.display(image)
|
504
608
|
|
505
609
|
except ImportError as E:
|
506
|
-
raise Exception(
|
610
|
+
raise Exception(
|
611
|
+
"Original Error Message:{}\nYou are missing dependencies for drawing: You need IPython, qpic and pdfatex.\n".format(
|
612
|
+
E
|
613
|
+
)
|
614
|
+
)
|
507
615
|
else:
|
508
616
|
compiled = compile_circuit(abstract_circuit=objective, backend=backend)
|
509
617
|
if backend == "qiskit":
|
@@ -512,14 +620,17 @@ def draw(objective, variables=None, backend: str = None, name=None, *args, **kwa
|
|
512
620
|
print(compiled.circuit)
|
513
621
|
return ""
|
514
622
|
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
623
|
+
|
624
|
+
def compile(
|
625
|
+
objective: typing.Union["Objective", "QCircuit", "QTensor"],
|
626
|
+
variables: Dict[Union["Variable", Hashable], RealNumber] = None,
|
627
|
+
samples: int = None,
|
628
|
+
backend: str = None,
|
629
|
+
noise: NoiseModel = None,
|
630
|
+
device: str = None,
|
631
|
+
*args,
|
632
|
+
**kwargs,
|
633
|
+
) -> typing.Union["BackendCircuit", "Objective"]:
|
523
634
|
"""Compile a tequila objective or circuit to a backend
|
524
635
|
|
525
636
|
Parameters
|
@@ -552,21 +663,50 @@ def compile(objective: typing.Union['Objective', 'QCircuit', 'QTensor'],
|
|
552
663
|
|
553
664
|
if isinstance(objective, QTensor):
|
554
665
|
ff = numpy.vectorize(compile_objective)
|
555
|
-
return ff(
|
556
|
-
|
666
|
+
return ff(
|
667
|
+
objective=objective,
|
668
|
+
samples=samples,
|
669
|
+
variables=variables,
|
670
|
+
backend=backend,
|
671
|
+
noise=noise,
|
672
|
+
device=device,
|
673
|
+
*args,
|
674
|
+
**kwargs,
|
675
|
+
)
|
676
|
+
|
557
677
|
if isinstance(objective, Objective) or hasattr(objective, "args"):
|
558
|
-
return compile_objective(
|
678
|
+
return compile_objective(
|
679
|
+
objective=objective,
|
680
|
+
samples=samples,
|
681
|
+
variables=variables,
|
682
|
+
backend=backend,
|
683
|
+
noise=noise,
|
684
|
+
device=device,
|
685
|
+
*args,
|
686
|
+
**kwargs,
|
687
|
+
)
|
559
688
|
elif hasattr(objective, "gates") or hasattr(objective, "abstract_circuit"):
|
560
|
-
return compile_circuit(
|
561
|
-
|
689
|
+
return compile_circuit(
|
690
|
+
abstract_circuit=objective,
|
691
|
+
variables=variables,
|
692
|
+
backend=backend,
|
693
|
+
samples=samples,
|
694
|
+
noise=noise,
|
695
|
+
device=device,
|
696
|
+
*args,
|
697
|
+
**kwargs,
|
698
|
+
)
|
562
699
|
else:
|
563
700
|
raise TequilaException(
|
564
|
-
"Don't know how to compile object of type: {type}, \n{object}".format(
|
565
|
-
|
701
|
+
"Don't know how to compile object of type: {type}, \n{object}".format(
|
702
|
+
type=type(objective), object=objective
|
703
|
+
)
|
704
|
+
)
|
566
705
|
|
567
706
|
|
568
|
-
def compile_to_function(
|
569
|
-
|
707
|
+
def compile_to_function(
|
708
|
+
objective: typing.Union["Objective", "QCircuit"], *args, **kwargs
|
709
|
+
) -> typing.Union["BackendCircuit", "Objective"]:
|
570
710
|
"""
|
571
711
|
Notes
|
572
712
|
----------
|
@@ -585,16 +725,18 @@ def compile_to_function(objective: typing.Union['Objective', 'QCircuit'], *args,
|
|
585
725
|
"""
|
586
726
|
|
587
727
|
compiled_objective = compile(objective, *args, **kwargs)
|
588
|
-
if
|
589
|
-
varnames = list(kwargs[
|
728
|
+
if "variables" in kwargs:
|
729
|
+
varnames = list(kwargs["variables"].keys())
|
590
730
|
else:
|
591
731
|
varnames = objective.extract_variables()
|
592
732
|
|
593
733
|
def objective_function(*fargs, **fkwargs):
|
594
734
|
if len(fargs) != len(varnames):
|
595
|
-
raise Exception(
|
596
|
-
|
597
|
-
|
735
|
+
raise Exception(
|
736
|
+
"Compiled function takes {} variables. You passed down {} arguments."
|
737
|
+
"Use keywords for samples and other instructions\n"
|
738
|
+
"like function(a,b,c, samples=10)".format(len(varnames), len(fargs))
|
739
|
+
)
|
598
740
|
vars = {varnames[i]: fargs[i] for i, v in enumerate(fargs)}
|
599
741
|
return compiled_objective(variables=vars, **fkwargs)
|
600
742
|
|