-
Notifications
You must be signed in to change notification settings - Fork 369
/
testing_utils.py
355 lines (282 loc) · 12.6 KB
/
testing_utils.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions useful for tests."""
import collections
import itertools
import numpy
from scipy.linalg import qr
from openfermion.ops.operators import QubitOperator
from openfermion.ops.representations import (
DiagonalCoulombHamiltonian,
QuadraticHamiltonian,
InteractionOperator,
)
def haar_random_vector(n, seed=None):
"""Generate an n dimensional Haar randomd vector."""
if seed is not None:
numpy.random.seed(seed)
vector = numpy.random.randn(n).astype(complex)
vector += 1.0j * numpy.random.randn(n).astype(complex)
normalization = numpy.sqrt(vector.dot(numpy.conjugate(vector)))
return vector / normalization
def random_antisymmetric_matrix(n, real=False, seed=None):
"""Generate a random n x n antisymmetric matrix."""
if seed is not None:
numpy.random.seed(seed)
if real:
rand_mat = numpy.random.randn(n, n)
else:
rand_mat = numpy.random.randn(n, n) + 1.0j * numpy.random.randn(n, n)
antisymmetric_mat = rand_mat - rand_mat.T
return antisymmetric_mat
def random_hermitian_matrix(n, real=False, seed=None):
"""Generate a random n x n Hermitian matrix."""
if seed is not None:
numpy.random.seed(seed)
if real:
rand_mat = numpy.random.randn(n, n)
else:
rand_mat = numpy.random.randn(n, n) + 1.0j * numpy.random.randn(n, n)
hermitian_mat = rand_mat + rand_mat.T.conj()
return hermitian_mat
def random_unitary_matrix(n, real=False, seed=None):
"""Obtain a random n x n unitary matrix."""
if seed is not None:
numpy.random.seed(seed)
if real:
rand_mat = numpy.random.randn(n, n)
else:
rand_mat = numpy.random.randn(n, n) + 1.0j * numpy.random.randn(n, n)
Q, _ = qr(rand_mat)
return Q
def random_qubit_operator(n_qubits=16, max_num_terms=16, max_many_body_order=16, seed=None):
prng = numpy.random.RandomState(seed)
op = QubitOperator()
num_terms = prng.randint(1, max_num_terms + 1)
for _ in range(num_terms):
many_body_order = prng.randint(max_many_body_order + 1)
term = []
for _ in range(many_body_order):
index = prng.randint(n_qubits)
action = prng.choice(('X', 'Y', 'Z'))
term.append((index, action))
coefficient = prng.randn()
op += QubitOperator(term, coefficient)
return op
def random_diagonal_coulomb_hamiltonian(n_qubits, real=False, seed=None):
"""Generate a random instance of DiagonalCoulombHamiltonian.
Args:
n_qubits: The number of qubits
real: Whether to use only real numbers in the one-body term
"""
if seed is not None:
numpy.random.seed(seed)
one_body = random_hermitian_matrix(n_qubits, real=real)
two_body = random_hermitian_matrix(n_qubits, real=True)
constant = numpy.random.randn()
return DiagonalCoulombHamiltonian(one_body, two_body, constant)
def random_interaction_operator(n_orbitals, expand_spin=False, real=True, seed=None):
"""Generate a random instance of InteractionOperator.
Args:
n_orbitals: The number of orbitals.
expand_spin: Whether to expand each orbital symmetrically into two
spin orbitals. Note that if this option is set to True, then
the total number of orbitals will be doubled.
real: Whether to use only real numbers.
seed: A random number generator seed.
"""
if seed is not None:
numpy.random.seed(seed)
if real:
dtype = float
else:
dtype = complex
# The constant has to be real.
constant = numpy.random.randn()
# The one-body tensor is a random Hermitian matrix.
one_body_coefficients = random_hermitian_matrix(n_orbitals, real)
# Generate random two-body coefficients.
two_body_coefficients = numpy.zeros((n_orbitals, n_orbitals, n_orbitals, n_orbitals), dtype)
for p, q, r, s in itertools.product(range(n_orbitals), repeat=4):
coeff = numpy.random.randn()
if not real and len(set([p, q, r, s])) >= 3:
coeff += 1.0j * numpy.random.randn()
# Four point symmetry.
two_body_coefficients[p, q, r, s] = coeff
two_body_coefficients[q, p, s, r] = coeff
two_body_coefficients[s, r, q, p] = coeff.conjugate()
two_body_coefficients[r, s, p, q] = coeff.conjugate()
# Eight point symmetry.
if real:
two_body_coefficients[r, q, p, s] = coeff
two_body_coefficients[p, s, r, q] = coeff
two_body_coefficients[s, p, q, r] = coeff
two_body_coefficients[q, r, s, p] = coeff
# If requested, expand to spin orbitals.
if expand_spin:
n_spin_orbitals = 2 * n_orbitals
# Expand one-body tensor.
one_body_coefficients = numpy.kron(one_body_coefficients, numpy.eye(2))
# Expand two-body tensor.
new_two_body_coefficients = numpy.zeros(
(n_spin_orbitals, n_spin_orbitals, n_spin_orbitals, n_spin_orbitals), dtype=complex
)
for p, q, r, s in itertools.product(range(n_orbitals), repeat=4):
coefficient = two_body_coefficients[p, q, r, s]
# Mixed spin.
new_two_body_coefficients[2 * p, 2 * q + 1, 2 * r + 1, 2 * s] = coefficient
new_two_body_coefficients[2 * p + 1, 2 * q, 2 * r, 2 * s + 1] = coefficient
# Same spin.
new_two_body_coefficients[2 * p, 2 * q, 2 * r, 2 * s] = coefficient
new_two_body_coefficients[2 * p + 1, 2 * q + 1, 2 * r + 1, 2 * s + 1] = coefficient
two_body_coefficients = new_two_body_coefficients
# Create the InteractionOperator.
interaction_operator = InteractionOperator(
constant, one_body_coefficients, two_body_coefficients
)
return interaction_operator
def random_quadratic_hamiltonian(
n_orbitals, conserves_particle_number=False, real=False, expand_spin=False, seed=None
):
"""Generate a random instance of QuadraticHamiltonian.
Args:
n_orbitals(int): the number of orbitals
conserves_particle_number(bool): whether the returned Hamiltonian
should conserve particle number
real(bool): whether to use only real numbers
expand_spin: Whether to expand each orbital symmetrically into two
spin orbitals. Note that if this option is set to True, then
the total number of orbitals will be doubled.
Returns:
QuadraticHamiltonian
"""
if seed is not None:
numpy.random.seed(seed)
constant = numpy.random.randn()
chemical_potential = numpy.random.randn()
hermitian_mat = random_hermitian_matrix(n_orbitals, real)
if conserves_particle_number:
antisymmetric_mat = None
else:
antisymmetric_mat = random_antisymmetric_matrix(n_orbitals, real)
if expand_spin:
hermitian_mat = numpy.kron(hermitian_mat, numpy.eye(2))
if antisymmetric_mat is not None:
antisymmetric_mat = numpy.kron(antisymmetric_mat, numpy.eye(2))
return QuadraticHamiltonian(hermitian_mat, antisymmetric_mat, constant, chemical_potential)
class EqualsTester(object):
"""Tests equality against user-provided disjoint equivalence groups."""
def __init__(self, test_case):
self.groups = [(_ClassUnknownToSubjects(),)]
self.test_case = test_case
def add_equality_group(self, *group_items):
"""Tries to add a disjoint equivalence group to the equality tester.
This methods asserts that items within the group must all be equal to
each other, but not equal to any items in other groups that have been
or will be added.
Args:
*group_items: The items making up the equivalence group.
Raises:
AssertError: Items within the group are not equal to each other, or
items in another group are equal to items within the new group,
or the items violate the equals-implies-same-hash rule.
"""
self.test_case.assertIsNotNone(group_items)
# Check that group items are equivalent to each other.
for v1, v2 in itertools.product(group_items, repeat=2):
# Binary operators should always work.
self.test_case.assertTrue(v1 == v2)
self.test_case.assertTrue(not v1 != v2)
# __eq__ and __ne__ should both be correct or not implemented.
self.test_case.assertTrue(hasattr(v1, '__eq__') == hasattr(v1, '__ne__'))
# Careful: python2 int doesn't have __eq__ or __ne__.
if hasattr(v1, '__eq__'):
eq = v1.__eq__(v2)
ne = v1.__ne__(v2)
self.test_case.assertIn(
(eq, ne),
[(True, False), (NotImplemented, False), (NotImplemented, NotImplemented)],
)
# Check that this group's items don't overlap with other groups.
for other_group in self.groups:
for v1, v2 in itertools.product(group_items, other_group):
# Binary operators should always work.
self.test_case.assertTrue(not v1 == v2)
self.test_case.assertTrue(v1 != v2)
# __eq__ and __ne__ should both be correct or not implemented.
self.test_case.assertTrue(hasattr(v1, '__eq__') == hasattr(v1, '__ne__'))
# Careful: python2 int doesn't have __eq__ or __ne__.
if hasattr(v1, '__eq__'):
eq = v1.__eq__(v2)
ne = v1.__ne__(v2)
self.test_case.assertIn(
(eq, ne),
[(False, True), (NotImplemented, True), (NotImplemented, NotImplemented)],
)
# Check that group items hash to the same thing, or are all unhashable.
hashes = [hash(v) if isinstance(v, collections.abc.Hashable) else None for v in group_items]
if len(set(hashes)) > 1:
examples = (
(v1, h1, v2, h2)
for v1, h1 in zip(group_items, hashes)
for v2, h2 in zip(group_items, hashes)
if h1 != h2
)
example = next(examples)
raise AssertionError(
'Items in the same group produced different hashes. '
'Example: hash({}) is {} but hash({}) is {}.'.format(*example)
)
# Remember this group, to enable disjoint checks vs later groups.
self.groups.append(group_items)
def make_equality_pair(self, factory):
"""Tries to add a disjoint (item, item) group to the equality tester.
Uses the factory method to produce two different objects containing
equal items. Asserts that the two object are equal, but not equal to
any items in other groups that have been or will be added. Adds the
pair as a group.
Args:
factory (Callable[[], Any]): A method for producing independent
copies of an item.
Raises:
AssertError: The factory produces items not equal to each other, or
items in another group are equal to items from the factory, or
the items violate the equal-implies-same-hash rule.
"""
self.add_equality_group(factory(), factory())
class _ClassUnknownToSubjects(object):
"""Equality methods should be able to deal with the unexpected."""
def __eq__(self, other):
return isinstance(other, _ClassUnknownToSubjects)
def __ne__(self, other):
return not self == other
def __hash__(self):
return hash(_ClassUnknownToSubjects)
def module_importable(module):
"""Without importing it, returns whether python module is importable.
Args:
module (string): Name of module.
Returns:
bool
"""
import sys
if sys.version_info >= (3, 4):
from importlib import util
plug_spec = util.find_spec(module)
else:
# Won't enter unless Python<3.4, so ignore for testing
import pkgutil # pragma: ignore
plug_spec = pkgutil.find_loader(module) # pragma: no cover
if plug_spec is None:
return False
else:
return True