-
Notifications
You must be signed in to change notification settings - Fork 558
/
test_split_non_commuting.py
457 lines (368 loc) · 15.4 KB
/
test_split_non_commuting.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# Copyright 2018-2020 Xanadu Quantum Technologies Inc.
# 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.
""" Tests for the transform ``qml.transform.split_non_commuting()`` """
# pylint: disable=no-self-use, import-outside-toplevel, no-member, import-error
import pytest
import numpy as np
import pennylane as qml
import pennylane.numpy as pnp
from pennylane.transforms import split_non_commuting
### example tape with 3 commuting groups [[0,3],[1,4],[2,5]]
with qml.queuing.AnnotatedQueue() as q3:
qml.PauliZ(0)
qml.Hadamard(0)
qml.CNOT((0, 1))
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
qml.expval(qml.PauliX(0) @ qml.PauliX(1))
qml.expval(qml.PauliY(0) @ qml.PauliY(1))
qml.expval(qml.PauliZ(0))
qml.expval(qml.PauliX(0))
qml.expval(qml.PauliY(0))
non_commuting_tape3 = qml.tape.QuantumScript.from_queue(q3)
### example tape with 2 -commuting groups [[0,2],[1,3]]
with qml.queuing.AnnotatedQueue() as q2:
qml.PauliZ(0)
qml.Hadamard(0)
qml.CNOT((0, 1))
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
qml.expval(qml.PauliX(0) @ qml.PauliX(1))
qml.expval(qml.PauliZ(0))
qml.expval(qml.PauliX(0))
non_commuting_tape2 = qml.tape.QuantumScript.from_queue(q2)
# For testing different observable types
obs_fn = [qml.expval, qml.var]
class TestUnittestSplitNonCommuting:
"""Unit tests on ``qml.transforms.split_non_commuting()``"""
def test_commuting_group_no_split(self, mocker):
"""Testing that commuting groups are not split"""
with qml.queuing.AnnotatedQueue() as q:
qml.PauliZ(0)
qml.Hadamard(0)
qml.CNOT((0, 1))
qml.expval(qml.PauliZ(0))
qml.expval(qml.PauliZ(0))
qml.expval(qml.PauliX(1))
qml.expval(qml.PauliZ(2))
qml.expval(qml.PauliZ(0) @ qml.PauliZ(3))
tape = qml.tape.QuantumScript.from_queue(q)
split, fn = split_non_commuting(tape)
spy = mocker.spy(qml.math, "concatenate")
assert all(isinstance(t, qml.tape.QuantumScript) for t in split)
assert fn([0.5]) == 0.5
qs = qml.tape.QuantumScript(tape.operations, tape.measurements)
split, fn = split_non_commuting(qs)
assert all(isinstance(i_qs, qml.tape.QuantumScript) for i_qs in split)
assert fn([0.5]) == 0.5
spy.assert_not_called()
@pytest.mark.parametrize("tape,expected", [(non_commuting_tape2, 2), (non_commuting_tape3, 3)])
def test_non_commuting_group_right_number(self, tape, expected):
"""Test that the output is of the correct size"""
split, _ = split_non_commuting(tape)
assert len(split) == expected
qs = qml.tape.QuantumScript(tape.operations, tape.measurements)
split, _ = split_non_commuting(qs)
assert len(split) == expected
@pytest.mark.parametrize(
"tape,group_coeffs",
[(non_commuting_tape2, [[0, 2], [1, 3]]), (non_commuting_tape3, [[0, 3], [1, 4], [2, 5]])],
)
def test_non_commuting_group_right_reorder(self, tape, group_coeffs):
"""Test that the output is of the correct order"""
split, fn = split_non_commuting(tape)
assert all(np.array(fn(group_coeffs)) == np.arange(len(split) * 2))
qs = qml.tape.QuantumScript(tape.operations, tape.measurements)
split, fn = split_non_commuting(qs)
assert all(np.array(fn(group_coeffs)) == np.arange(len(split) * 2))
@pytest.mark.parametrize("meas_type", obs_fn)
def test_different_measurement_types(self, meas_type):
"""Test that expval, var and sample are correctly reproduced"""
with qml.queuing.AnnotatedQueue() as q:
qml.PauliZ(0)
qml.Hadamard(0)
qml.CNOT((0, 1))
meas_type(qml.PauliZ(0) @ qml.PauliZ(1))
meas_type(qml.PauliX(0) @ qml.PauliX(1))
meas_type(qml.PauliZ(0))
meas_type(qml.PauliX(0))
tape = qml.tape.QuantumScript.from_queue(q)
the_return_type = tape.measurements[0].return_type
split, _ = split_non_commuting(tape)
for new_tape in split:
for meas in new_tape.measurements:
assert meas.return_type == the_return_type
qs = qml.tape.QuantumScript(tape.operations, tape.measurements)
split, _ = split_non_commuting(qs)
for new_tape in split:
for meas in new_tape.measurements:
assert meas.return_type == the_return_type
def test_mixed_measurement_types(self):
"""Test that mixing expval and var works correctly."""
with qml.queuing.AnnotatedQueue() as q:
qml.Hadamard(0)
qml.Hadamard(1)
qml.expval(qml.PauliX(0))
qml.expval(qml.PauliZ(1))
qml.var(qml.PauliZ(0))
tape = qml.tape.QuantumScript.from_queue(q)
split, _ = split_non_commuting(tape)
assert len(split) == 2
with qml.queuing.AnnotatedQueue() as q:
qml.Hadamard(0)
qml.Hadamard(1)
qml.expval(qml.PauliX(0))
qml.var(qml.PauliZ(0))
qml.expval(qml.PauliZ(1))
tape = qml.tape.QuantumScript.from_queue(q)
split, _ = split_non_commuting(tape)
assert len(split) == 2
assert qml.equal(split[0].measurements[0], qml.expval(qml.PauliX(0)))
assert qml.equal(split[0].measurements[1], qml.expval(qml.PauliZ(1)))
assert qml.equal(split[1].measurements[0], qml.var(qml.PauliZ(0)))
def test_raise_not_supported(self):
"""Test that NotImplementedError is raised when probabilities or samples are called"""
with qml.queuing.AnnotatedQueue() as q:
qml.expval(qml.PauliZ(0))
qml.probs(wires=0)
tape = qml.tape.QuantumScript.from_queue(q)
with pytest.raises(NotImplementedError, match="non-commuting observables are used"):
split_non_commuting(tape)
class TestIntegration:
"""Integration tests for ``qml.transforms.split_non_commuting()``"""
def test_expval_non_commuting_observables(self):
"""Test expval with multiple non-commuting operators"""
dev = qml.device("default.qubit", wires=6)
@qml.qnode(dev)
def circuit():
qml.Hadamard(1)
qml.Hadamard(0)
qml.PauliZ(0)
qml.Hadamard(3)
qml.Hadamard(5)
qml.T(5)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliX(0)),
qml.expval(qml.PauliZ(1)),
qml.expval(qml.PauliX(1) @ qml.PauliX(4)),
qml.expval(qml.PauliX(3)),
qml.expval(qml.PauliY(5)),
)
res = circuit()
assert isinstance(res, tuple)
assert len(res) == 6
assert all(isinstance(r, np.ndarray) for r in res)
assert all(r.shape == () for r in res)
res = qml.math.stack(res)
assert all(np.isclose(res, np.array([0.0, -1.0, 0.0, 0.0, 1.0, 1 / np.sqrt(2)])))
def test_expval_non_commuting_observables_qnode(self):
"""Test expval with multiple non-commuting operators as a tranform program on the qnode."""
dev = qml.device("default.qubit", wires=6)
@qml.qnode(dev)
def circuit():
qml.Hadamard(1)
qml.Hadamard(0)
qml.PauliZ(0)
qml.Hadamard(3)
qml.Hadamard(5)
qml.T(5)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliX(0)),
qml.expval(qml.PauliZ(1)),
qml.expval(qml.PauliX(1) @ qml.PauliX(4)),
qml.expval(qml.PauliX(3)),
qml.expval(qml.PauliY(5)),
)
res = split_non_commuting(circuit)()
assert isinstance(res, tuple)
assert len(res) == 6
assert all(isinstance(r, np.ndarray) for r in res)
assert all(r.shape == () for r in res)
res = qml.math.stack(res)
assert all(np.isclose(res, np.array([0.0, -1.0, 0.0, 0.0, 1.0, 1 / np.sqrt(2)])))
def test_shot_vector_support(self):
"""Test output is correct when using shot vectors"""
dev = qml.device("default.qubit", wires=6, shots=(10000, (20000, 2), 30000))
@qml.qnode(dev)
def circuit():
qml.Hadamard(1)
qml.Hadamard(0)
qml.PauliZ(0)
qml.Hadamard(3)
qml.Hadamard(5)
qml.T(5)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliX(0)),
qml.expval(qml.PauliZ(1)),
qml.expval(
qml.PauliY(0) @ qml.PauliY(1) @ qml.PauliZ(3) @ qml.PauliY(4) @ qml.PauliX(5)
),
qml.expval(qml.PauliX(1) @ qml.PauliX(4)),
qml.expval(qml.PauliX(3)),
qml.expval(qml.PauliY(5)),
)
res = circuit()
assert isinstance(res, tuple)
assert len(res) == 4
assert all(isinstance(shot_res, tuple) for shot_res in res)
assert all(len(shot_res) == 7 for shot_res in res)
# pylint:disable=not-an-iterable
assert all(
all(list(isinstance(r, np.ndarray) and r.shape == () for r in shot_res))
for shot_res in res
)
res = qml.math.stack([qml.math.stack(r) for r in res])
assert np.allclose(
res, np.array([0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 1 / np.sqrt(2)]), atol=0.05
)
# Autodiff tests
exp_res = np.array([0.77015115, -0.47942554, 0.87758256])
exp_grad = np.array(
[[-4.20735492e-01, -4.20735492e-01], [-8.77582562e-01, 0.0], [-4.79425539e-01, 0.0]]
)
class TestAutodiffSplitNonCommuting:
"""Autodiff tests for all frameworks"""
@pytest.mark.autograd
def test_split_with_autograd(self):
"""Test that results after splitting are still differentiable with autograd"""
dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
def cost(params):
res = circuit(params)
return qml.math.stack(res)
params = pnp.array([0.5, 0.5])
res = cost(params)
grad = qml.jacobian(cost)(params)
assert all(np.isclose(res, exp_res))
assert all(np.isclose(grad, exp_grad).flatten())
@pytest.mark.jax
def test_split_with_jax(self):
"""Test that results after splitting are still differentiable with jax"""
import jax
import jax.numpy as jnp
dev = qml.device("default.qubit.jax", wires=3)
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
params = jnp.array([0.5, 0.5])
res = circuit(params)
grad = jax.jacobian(circuit)(params)
assert all(np.isclose(res, exp_res))
assert all(np.isclose(grad, exp_grad, atol=1e-5).flatten())
@pytest.mark.jax
def test_split_with_jax_multi_params(self):
"""Test that results after splitting are still differentiable with jax
with multiple parameters"""
import jax
import jax.numpy as jnp
dev = qml.device("default.qubit.jax", wires=3)
@qml.qnode(dev)
def circuit(x, y):
qml.RX(x, wires=0)
qml.RY(y, wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
x = jnp.array(0.5)
y = jnp.array(0.5)
res = circuit(x, y)
grad = jax.jacobian(circuit, argnums=[0, 1])(x, y)
assert all(np.isclose(res, exp_res))
assert isinstance(grad, tuple)
assert len(grad) == 3
for i, meas_grad in enumerate(grad):
assert isinstance(meas_grad, tuple)
assert len(meas_grad) == 2
assert all(isinstance(g, jnp.ndarray) and g.shape == () for g in meas_grad)
assert np.allclose(meas_grad, exp_grad[i], atol=1e-5)
@pytest.mark.jax
def test_split_with_jax_jit(self):
"""Test that results after splitting are still differentiable with jax-jit"""
import jax
import jax.numpy as jnp
dev = qml.device("default.qubit", wires=3)
@jax.jit
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
params = jnp.array([0.5, 0.5])
res = circuit(params)
grad = jax.jacobian(circuit)(params)
assert all(np.isclose(res, exp_res))
assert all(np.isclose(grad, exp_grad, atol=1e-5).flatten())
@pytest.mark.torch
def test_split_with_torch(self):
"""Test that results after splitting are still differentiable with torch"""
import torch
from torch.autograd.functional import jacobian
dev = qml.device("default.qubit.torch", wires=3)
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
def cost(params):
res = circuit(params)
return qml.math.stack(res)
params = torch.tensor([0.5, 0.5], requires_grad=True)
res = cost(params)
grad = jacobian(cost, (params))
assert all(np.isclose(res.detach().numpy(), exp_res))
assert all(np.isclose(grad.detach().numpy(), exp_grad, atol=1e-5).flatten())
@pytest.mark.tf
def test_split_with_tf(self):
"""Test that results after splitting are still differentiable with tf"""
import tensorflow as tf
dev = qml.device("default.qubit.tf", wires=3)
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
params = tf.Variable([0.5, 0.5])
res = circuit(params)
with tf.GradientTape() as tape:
loss = circuit(params)
loss = tf.stack(loss)
grad = tape.jacobian(loss, params)
assert all(np.isclose(res, exp_res))
assert all(np.isclose(grad, exp_grad, atol=1e-5).flatten())