tequila-basic 1.9.8__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.
Files changed (86) hide show
  1. tequila/__init__.py +29 -14
  2. tequila/apps/__init__.py +14 -5
  3. tequila/apps/_unary_state_prep_impl.py +145 -112
  4. tequila/apps/adapt/__init__.py +9 -1
  5. tequila/apps/adapt/adapt.py +154 -113
  6. tequila/apps/krylov/__init__.py +1 -1
  7. tequila/apps/krylov/krylov.py +23 -21
  8. tequila/apps/robustness/helpers.py +10 -6
  9. tequila/apps/robustness/interval.py +238 -156
  10. tequila/apps/unary_state_prep.py +29 -23
  11. tequila/autograd_imports.py +8 -5
  12. tequila/circuit/__init__.py +2 -1
  13. tequila/circuit/_gates_impl.py +135 -67
  14. tequila/circuit/circuit.py +177 -88
  15. tequila/circuit/compiler.py +114 -105
  16. tequila/circuit/gates.py +288 -120
  17. tequila/circuit/gradient.py +35 -23
  18. tequila/circuit/noise.py +83 -74
  19. tequila/circuit/postselection.py +120 -0
  20. tequila/circuit/pyzx.py +10 -6
  21. tequila/circuit/qasm.py +201 -83
  22. tequila/circuit/qpic.py +63 -61
  23. tequila/grouping/binary_rep.py +148 -146
  24. tequila/grouping/binary_utils.py +84 -75
  25. tequila/grouping/compile_groups.py +334 -230
  26. tequila/grouping/ev_utils.py +77 -41
  27. tequila/grouping/fermionic_functions.py +383 -308
  28. tequila/grouping/fermionic_methods.py +170 -123
  29. tequila/grouping/overlapping_methods.py +69 -52
  30. tequila/hamiltonian/paulis.py +12 -13
  31. tequila/hamiltonian/paulistring.py +1 -1
  32. tequila/hamiltonian/qubit_hamiltonian.py +45 -35
  33. tequila/ml/__init__.py +1 -0
  34. tequila/ml/interface_torch.py +19 -16
  35. tequila/ml/ml_api.py +11 -10
  36. tequila/ml/utils_ml.py +12 -11
  37. tequila/objective/__init__.py +8 -3
  38. tequila/objective/braket.py +55 -47
  39. tequila/objective/objective.py +91 -56
  40. tequila/objective/qtensor.py +36 -27
  41. tequila/optimizers/__init__.py +31 -23
  42. tequila/optimizers/_containers.py +11 -7
  43. tequila/optimizers/optimizer_base.py +111 -83
  44. tequila/optimizers/optimizer_gd.py +258 -231
  45. tequila/optimizers/optimizer_gpyopt.py +56 -42
  46. tequila/optimizers/optimizer_scipy.py +157 -112
  47. tequila/quantumchemistry/__init__.py +66 -38
  48. tequila/quantumchemistry/chemistry_tools.py +394 -203
  49. tequila/quantumchemistry/encodings.py +121 -13
  50. tequila/quantumchemistry/madness_interface.py +170 -96
  51. tequila/quantumchemistry/orbital_optimizer.py +86 -40
  52. tequila/quantumchemistry/psi4_interface.py +166 -97
  53. tequila/quantumchemistry/pyscf_interface.py +70 -23
  54. tequila/quantumchemistry/qc_base.py +866 -414
  55. tequila/simulators/__init__.py +0 -3
  56. tequila/simulators/simulator_api.py +258 -106
  57. tequila/simulators/simulator_aqt.py +102 -0
  58. tequila/simulators/simulator_base.py +156 -55
  59. tequila/simulators/simulator_cirq.py +58 -42
  60. tequila/simulators/simulator_cudaq.py +600 -0
  61. tequila/simulators/simulator_ddsim.py +390 -0
  62. tequila/simulators/simulator_mqp.py +30 -0
  63. tequila/simulators/simulator_pyquil.py +190 -171
  64. tequila/simulators/simulator_qibo.py +95 -87
  65. tequila/simulators/simulator_qiskit.py +124 -114
  66. tequila/simulators/simulator_qlm.py +52 -26
  67. tequila/simulators/simulator_qulacs.py +85 -59
  68. tequila/simulators/simulator_spex.py +464 -0
  69. tequila/simulators/simulator_symbolic.py +6 -5
  70. tequila/simulators/test_spex_simulator.py +208 -0
  71. tequila/tools/convenience.py +4 -4
  72. tequila/tools/qng.py +72 -64
  73. tequila/tools/random_generators.py +38 -34
  74. tequila/utils/bitstrings.py +13 -7
  75. tequila/utils/exceptions.py +19 -5
  76. tequila/utils/joined_transformation.py +8 -10
  77. tequila/utils/keymap.py +0 -5
  78. tequila/utils/misc.py +6 -4
  79. tequila/version.py +1 -1
  80. tequila/wavefunction/qubit_wavefunction.py +52 -30
  81. {tequila_basic-1.9.8.dist-info → tequila_basic-1.9.10.dist-info}/METADATA +23 -17
  82. tequila_basic-1.9.10.dist-info/RECORD +93 -0
  83. {tequila_basic-1.9.8.dist-info → tequila_basic-1.9.10.dist-info}/WHEEL +1 -1
  84. tequila_basic-1.9.8.dist-info/RECORD +0 -86
  85. {tequila_basic-1.9.8.dist-info → tequila_basic-1.9.10.dist-info/licenses}/LICENSE +0 -0
  86. {tequila_basic-1.9.8.dist-info → tequila_basic-1.9.10.dist-info}/top_level.txt +0 -0
tequila/ml/ml_api.py CHANGED
@@ -1,29 +1,30 @@
1
1
  from .utils_ml import TequilaMLException
2
- from shutil import which
3
2
  from tequila.objective import Objective
4
- SUPPORTED_PLATFORMS = ['pytorch']
3
+
4
+ SUPPORTED_PLATFORMS = ["pytorch"]
5
5
  CONVERTERS = {}
6
6
 
7
- #HAS_TORCH = which('torch') is not None or which('pytorch') is not None
7
+ # HAS_TORCH = which('torch') is not None or which('pytorch') is not None
8
8
  HAS_TORCH = True
9
9
  try:
10
10
  import torch
11
- except:
11
+ except Exception:
12
12
  HAS_TORCH = False
13
13
 
14
14
  if HAS_TORCH:
15
15
  from .interface_torch import TorchLayer
16
- CONVERTERS['pytorch'] = TorchLayer
17
16
 
18
- def to_platform(objective: Objective, platform: str,
19
- compile_args: dict = None, input_vars: list = None):
17
+ CONVERTERS["pytorch"] = TorchLayer
18
+
19
+
20
+ def to_platform(objective: Objective, platform: str, compile_args: dict = None, input_vars: list = None):
20
21
  plat = platform.lower()
21
- if plat == 'torch':
22
+ if plat == "torch":
22
23
  # common alias.
23
- plat = 'pytorch'
24
+ plat = "pytorch"
24
25
 
25
26
  try:
26
27
  f = CONVERTERS[plat]
27
28
  return f(objective, compile_args, input_vars)
28
29
  except KeyError:
29
- raise TequilaMLException('Desired ML platform {} either not supported, or not installed.'.format(plat))
30
+ raise TequilaMLException("Desired ML platform {} either not supported, or not installed.".format(plat))
tequila/ml/utils_ml.py CHANGED
@@ -1,6 +1,5 @@
1
1
  from tequila.utils.exceptions import TequilaException
2
- from tequila.objective.objective import assign_variable, Objective,\
3
- format_variable_dictionary, format_variable_list
2
+ from tequila.objective.objective import assign_variable, Objective, format_variable_dictionary, format_variable_list
4
3
  import typing
5
4
  from tequila.simulators.simulator_api import compile
6
5
  from tequila.circuit.gradient import grad
@@ -29,12 +28,12 @@ def check_compiler_args(c_args: dict) -> typing.Dict:
29
28
  tequila.simulators.simulator_api.compile
30
29
  """
31
30
 
32
- valid_keys=['backend', 'samples', 'noise', 'device', 'initial_values']
31
+ valid_keys = ["backend", "samples", "noise", "device", "initial_values"]
33
32
  if c_args is None:
34
- return {k:None for k in valid_keys}
33
+ return {k: None for k in valid_keys}
35
34
  for k in c_args.keys():
36
35
  if k not in valid_keys:
37
- raise TequilaException('improper keyword {} found in compilation kwargs dict; please try again.'.format(k))
36
+ raise TequilaException("improper keyword {} found in compilation kwargs dict; please try again.".format(k))
38
37
  else:
39
38
  pass
40
39
  for k in valid_keys:
@@ -64,8 +63,10 @@ def preamble(objective: Objective, compile_args: dict = None, input_vars: list =
64
63
  the compiled objective, it's compile arguments, its weight variables, dicts for the weight and input gradients,
65
64
  and a dictionary that links positions in an array to each variable (parses parameters).
66
65
  """
66
+
67
67
  def var_sorter(e):
68
68
  return hash(e.name)
69
+
69
70
  all_vars = objective.extract_variables()
70
71
  all_vars.sort(key=var_sorter)
71
72
  compile_args = check_compiler_args(compile_args)
@@ -80,21 +81,22 @@ def preamble(objective: Objective, compile_args: dict = None, input_vars: list =
80
81
  if var not in input_vars:
81
82
  weight_vars.append(assign_variable(var))
82
83
 
83
- init_vals = compile_args['initial_values']
84
+ init_vals = compile_args["initial_values"]
84
85
  if init_vals is not None:
85
86
  for k in init_vals.keys():
86
87
  if assign_variable(k) in input_vars:
87
- raise TequilaMLException('initial_values contained key {},'
88
- 'which is meant to be an input variable.'.format(k))
88
+ raise TequilaMLException(
89
+ "initial_values contained key {},which is meant to be an input variable.".format(k)
90
+ )
89
91
  init_vals = format_variable_dictionary(init_vals)
90
- compile_args.pop('initial_values')
92
+ compile_args.pop("initial_values")
91
93
 
92
94
  comped = compile(objective, **compile_args)
93
95
 
94
96
  gradients = get_gradients(objective, compile_args)
95
97
  w_grad, i_grad = separate_gradients(gradients, weight_vars=weight_vars, input_vars=input_vars)
96
98
  first, second = get_variable_orders(weight_vars, input_vars)
97
- compile_args['initial_values']=init_vals
99
+ compile_args["initial_values"] = init_vals
98
100
  return comped, compile_args, input_vars, weight_vars, i_grad, w_grad, first, second
99
101
 
100
102
 
@@ -179,4 +181,3 @@ def get_variable_orders(weight_vars, input_vars):
179
181
  for j, v in enumerate(weight_vars):
180
182
  second[j] = v
181
183
  return first, second
182
-
@@ -1,5 +1,10 @@
1
- from tequila.objective.objective import Objective,\
2
- ExpectationValue, Variable, assign_variable, format_variable_list, \
3
- format_variable_dictionary
1
+ from tequila.objective.objective import (
2
+ Objective,
3
+ ExpectationValue,
4
+ Variable,
5
+ assign_variable,
6
+ format_variable_list,
7
+ format_variable_dictionary,
8
+ )
4
9
 
5
10
  from tequila.objective.qtensor import QTensor, VectorObjective, vectorize
@@ -7,7 +7,8 @@ from tequila.hamiltonian import paulis
7
7
 
8
8
  import numpy as np
9
9
 
10
- def Fidelity(bra,ket,*args,**kwargs):
10
+
11
+ def Fidelity(bra, ket, *args, **kwargs):
11
12
  """
12
13
 
13
14
  Convenience initialization of an tq.Objective that corresponds to the fidelity |<bra|ket>|^2 between two quantum states
@@ -41,9 +42,10 @@ def Fidelity(bra,ket,*args,**kwargs):
41
42
  P0 = paulis.Qp(qubits)
42
43
  return ExpectationValue(H=P0, U=U, *args, **kwargs)
43
44
 
44
- def Overlap(bra,ket,*args,**kwargs):
45
+
46
+ def Overlap(bra, ket, *args, **kwargs):
45
47
  """
46
-
48
+
47
49
  Convenience initialization of an tq.Objective that corresponds to the overlap <bra|ket>
48
50
  initialized by the circuits bra and ket
49
51
 
@@ -55,7 +57,7 @@ def Overlap(bra,ket,*args,**kwargs):
55
57
  ----------
56
58
  bra: QCircuit
57
59
  ket: QCircuit
58
-
60
+
59
61
  Returns:
60
62
  ----------
61
63
  A tuple of tq.Objective that evaluates to the real and imaginary part of the overlap between the two states
@@ -64,8 +66,9 @@ def Overlap(bra,ket,*args,**kwargs):
64
66
 
65
67
  return BraKet(ket=ket, bra=bra, operator=None, *args, **kwargs)
66
68
 
69
+
67
70
  def BraKet(ket: QCircuit, bra: QCircuit = None, operator: QubitHamiltonian = None, *args, **kwargs) -> ExpectationValue:
68
- """Function that allows to calculate different quantities
71
+ """Function that allows to calculate different quantities
69
72
  depending on the passed parameters:
70
73
  1) If only ket is passed, returns the overlap with itself (1).
71
74
  2) If ket and bra are passed, returns the overlap between the two states.
@@ -75,23 +78,25 @@ def BraKet(ket: QCircuit, bra: QCircuit = None, operator: QubitHamiltonian = Non
75
78
  returns an instance of tq.Objective
76
79
 
77
80
  Args:
78
- ket (QCircuit): QCircuit corresponding to a state.
81
+ ket (QCircuit): QCircuit corresponding to a state.
79
82
  bra (QCircuit, optional): QCircuit corresponding to a second state.
80
83
  Defaults to None.
81
- operator (QubitHamiltonian, optional): Operator of which we want to
82
- calculate the transition element.
84
+ operator (QubitHamiltonian, optional): Operator of which we want to
85
+ calculate the transition element.
83
86
  Defaults to None.
84
87
 
85
88
  Returns:
86
89
  a tuple of tq.Objective representing the real and imaginary part of the BraKet
87
90
  """
88
-
91
+
89
92
  # allow for some convenience
90
93
  if "H" in kwargs:
91
94
  if operator is None:
92
- operator=kwargs["H"]
95
+ operator = kwargs["H"]
93
96
  else:
94
- raise TequilaException("BraKet inconsistency between operator and kwargs[\"H\"] do not given H= ... and operator= ... in the same call")
97
+ raise TequilaException(
98
+ 'BraKet inconsistency between operator and kwargs["H"] do not given H= ... and operator= ... in the same call'
99
+ )
95
100
  kwargs.pop("H")
96
101
 
97
102
  if bra is None:
@@ -99,86 +104,89 @@ def BraKet(ket: QCircuit, bra: QCircuit = None, operator: QubitHamiltonian = Non
99
104
 
100
105
  if id(ket) == id(bra):
101
106
  if operator is None:
102
- return Objective()+1.0 , Objective()
103
- return ExpectationValue(H=operator, U=ket, *args, **kwargs) , Objective()
107
+ return Objective() + 1.0, Objective()
108
+ return ExpectationValue(H=operator, U=ket, *args, **kwargs), Objective()
104
109
  else:
105
110
  if operator is None:
106
- return make_overlap(U0 = bra, U1 = ket, *args, **kwargs)
107
-
108
- return make_transition(U0 = bra, U1 = ket, H = operator, *args, **kwargs)
111
+ return make_overlap(U0=bra, U1=ket, *args, **kwargs)
112
+
113
+ return make_transition(U0=bra, U1=ket, H=operator, *args, **kwargs)
109
114
 
110
- def make_overlap(U0:QCircuit = None, U1:QCircuit = None, *args, **kwargs) -> ExpectationValue:
111
- '''
115
+
116
+ def make_overlap(U0: QCircuit = None, U1: QCircuit = None, *args, **kwargs) -> ExpectationValue:
117
+ """
112
118
  Function that calculates the overlap between two quantum states.
113
119
 
114
120
  Parameters
115
121
  ----------
116
122
  U0 : QCircuit tequila object, corresponding to the first state (will be the bra).
117
-
123
+
118
124
  U1 : QCircuit tequila object, corresponding to the second state (will be the ket).
119
125
 
120
126
  Returns
121
127
  -------
122
128
  Real and imaginary Tequila objectives to be simulated or compiled.
123
129
 
124
- '''
125
-
130
+ """
131
+
126
132
  ctrl = find_unused_qubit(U0=U0, U1=U1)
127
-
128
- #print('Control qubit:',ctrl)
129
-
130
- U_a = U0.add_controls([ctrl]) #this add the control by modifying the previous circuit
131
- U_b = U1.add_controls([ctrl]) #NonType object
132
-
133
- #bulding the circuit for the overlap evaluation
133
+
134
+ # print('Control qubit:',ctrl)
135
+
136
+ U_a = U0.add_controls([ctrl]) # this add the control by modifying the previous circuit
137
+ U_b = U1.add_controls([ctrl]) # NonType object
138
+
139
+ # bulding the circuit for the overlap evaluation
134
140
  circuit = H(target=ctrl)
135
141
  circuit += X(target=ctrl)
136
142
  circuit += U_a
137
143
  circuit += X(target=ctrl)
138
144
  circuit += U_b
139
-
145
+
140
146
  x = paulis.X(ctrl)
141
147
  y = paulis.Y(ctrl)
142
148
  Ex = ExpectationValue(H=x, U=circuit, *args, **kwargs)
143
149
  Ey = ExpectationValue(H=y, U=circuit, *args, **kwargs)
144
-
150
+
145
151
  return Ex, Ey
146
152
 
147
153
 
148
- def make_transition(U0:QCircuit = None, U1:QCircuit = None, H: QubitHamiltonian = None, *args, **kwargs) -> ExpectationValue:
149
- '''
154
+ def make_transition(
155
+ U0: QCircuit = None, U1: QCircuit = None, H: QubitHamiltonian = None, *args, **kwargs
156
+ ) -> ExpectationValue:
157
+ """
150
158
  Function that calculates the transition elements of an Hamiltonian operator
151
159
  between two different quantum states.
152
160
 
153
161
  Parameters
154
162
  ----------
155
163
  U0 : QCircuit tequila object, corresponding to the first state (will be the bra).
156
-
164
+
157
165
  U1 : QCircuit tequila object, corresponding to the second state (will be the ket).
158
-
166
+
159
167
  H : QubitHamiltonian tequila object
160
-
168
+
161
169
  Returns
162
170
  -------
163
171
  Real and imaginary Tequila objectives to be simulated or compiled.
164
172
 
165
- '''
166
-
173
+ """
174
+
167
175
  # want to measure: <U1|H|U0> -> \sum_k c_k <U1|U_k|U0>
168
-
176
+
169
177
  trans_real = 0
170
178
  trans_im = 0
171
-
179
+
172
180
  for ps in H.paulistrings:
173
- #print('string',ps)
181
+ # print('string',ps)
174
182
  c_k = ps.coeff
175
- #print('coeff', c_k)
183
+ # print('coeff', c_k)
176
184
  U_k = PauliGate(ps)
177
- objective_real, objective_im = make_overlap(U0=U0, U1=U1+U_k, *args, **kwargs)
178
-
179
- trans_real += c_k*objective_real
180
- trans_im += c_k*objective_im
185
+ objective_real, objective_im = make_overlap(U0=U0, U1=U1 + U_k, *args, **kwargs)
186
+
187
+ trans_real += c_k * objective_real
188
+ trans_im += c_k * objective_im
189
+
190
+ # print('contribution', trans_real+trans_im)
181
191
 
182
- #print('contribution', trans_real+trans_im)
183
-
184
192
  return trans_real, trans_im