-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_measurements_class.py
992 lines (857 loc) · 37.2 KB
/
test_measurements_class.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
# Copyright 2018-2024 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.
import itertools
import math
from typing import Sequence
import numpy as np
import pennylane as qml
import pytest
from conftest import ( # tested device
PHI,
THETA,
LightningDevice,
LightningMeasurements,
LightningStateVector,
device_name,
)
from flaky import flaky
from pennylane.devices import DefaultQubit
from pennylane.measurements import VarianceMP
from scipy.sparse import csr_matrix, random_array
if not LightningDevice._new_API:
pytest.skip(
"Exclusive tests for new API devices. Skipping.",
allow_module_level=True,
)
if not LightningDevice._CPP_BINARY_AVAILABLE:
pytest.skip("No binary module found. Skipping.", allow_module_level=True)
def get_hermitian_matrix(n):
H = np.random.rand(n, n) + 1.0j * np.random.rand(n, n)
return H + np.conj(H).T
def get_sparse_hermitian_matrix(n):
H = random_array((n, n), density=0.15)
H = H + 1.0j * random_array((n, n), density=0.15)
return csr_matrix(H + H.conj().T)
class CustomStateMeasurement(qml.measurements.StateMeasurement):
def process_state(self, state, wire_order):
return 1
# Observables not supported in lightning.tensor
def obs_not_supported_in_ltensor(obs):
if device_name == "lightning.tensor":
if isinstance(obs, qml.Projector) or isinstance(obs, qml.SparseHamiltonian):
return True
if isinstance(obs, qml.Hamiltonian):
return any([obs_not_supported_in_ltensor(o) for o in obs])
if isinstance(obs, qml.Hermitian) and len(obs.wires) > 1:
return True
if isinstance(obs, list) and all([isinstance(o, int) for o in obs]): # out of order probs
return obs != sorted(obs)
return False
else:
return False
def get_final_state(statevector, tape):
if device_name == "lightning.tensor":
return statevector.set_tensor_network(tape)
return statevector.get_final_state(tape)
def measure_final_state(m, tape):
if device_name == "lightning.tensor":
return m.measure_tensor_network(tape)
return m.measure_final_state(tape)
def test_initialization(lightning_sv):
"""Tests for the initialization of the LightningMeasurements class."""
statevector = lightning_sv(num_wires=5)
m = LightningMeasurements(statevector)
if device_name == "lightning.tensor":
assert m.dtype == statevector.dtype
else:
assert m.qubit_state is statevector
assert m.dtype == statevector.dtype
class TestGetMeasurementFunction:
"""Tests for the get_measurement_function method."""
def test_only_support_state_measurements(self, lightning_sv):
"""Test than a NotImplementedError is raised if the measurement is not a state measurement."""
statevector = lightning_sv(num_wires=5)
m = LightningMeasurements(statevector)
mp = qml.counts(wires=(0, 1))
with pytest.raises(NotImplementedError):
m.get_measurement_function(mp)
@pytest.mark.parametrize(
"mp",
(
qml.vn_entropy(wires=0),
CustomStateMeasurement(),
qml.expval(qml.Identity(0)),
qml.expval(qml.Projector([1, 0], wires=(0, 1))),
qml.var(qml.Identity(0)),
qml.var(qml.Projector([1, 0], wires=(0, 1))),
),
)
def test_state_diagonalizing_gates_measurements(self, lightning_sv, mp):
"""Test that any non-expval measurement calls the state_diagonalizing_gates method"""
if obs_not_supported_in_ltensor(mp.obs):
pytest.skip("Observable not supported in lightning.tensor.")
statevector = lightning_sv(num_wires=5)
m = LightningMeasurements(statevector)
assert m.get_measurement_function(mp) == m.state_diagonalizing_gates
@pytest.mark.parametrize(
"obs",
(
qml.PauliX(0),
qml.PauliY(0),
qml.PauliZ(0),
qml.sum(qml.PauliX(0), qml.PauliY(0)),
qml.prod(qml.PauliX(0), qml.PauliY(1)),
qml.s_prod(2.0, qml.PauliX(0)),
qml.Hamiltonian([1.0, 2.0], [qml.PauliX(0), qml.PauliY(0)]),
qml.Hermitian(np.eye(2), wires=0),
qml.SparseHamiltonian(qml.PauliX.compute_sparse_matrix(), wires=0),
),
)
def test_expval_selected(self, lightning_sv, obs):
"""Test that expval is chosen for a variety of different expectation values."""
if obs_not_supported_in_ltensor(obs):
pytest.skip("Observable not supported in lightning.tensor.")
statevector = lightning_sv(num_wires=5)
m = LightningMeasurements(statevector)
mp = qml.expval(obs)
assert m.get_measurement_function(mp) == m.expval
@pytest.mark.parametrize("method_name", ("state_diagonalizing_gates", "measurement"))
class TestStateDiagonalizingGates:
"""Tests for various measurements that go through state_diagonalizing_gates"""
def expected_entropy_Ising_XX(self, param):
"""
Return the analytical entropy for the IsingXX.
"""
eig_1 = (1 + np.sqrt(1 - 4 * np.cos(param / 2) ** 2 * np.sin(param / 2) ** 2)) / 2
eig_2 = (1 - np.sqrt(1 - 4 * np.cos(param / 2) ** 2 * np.sin(param / 2) ** 2)) / 2
eigs = [eig_1, eig_2]
eigs = [eig for eig in eigs if eig > 0]
expected_entropy = eigs * np.log(eigs)
expected_entropy = -np.sum(expected_entropy)
return expected_entropy
def test_vn_entropy(self, lightning_sv, method_name):
"""Test that state_diagonalizing_gates can handle an arbitrary measurement process."""
phi = 0.5
statevector = lightning_sv(num_wires=5)
statevector.apply_operations([qml.IsingXX(phi, wires=(0, 1))])
m = LightningMeasurements(statevector)
measurement = qml.vn_entropy(wires=0)
result = getattr(m, method_name)(measurement)
assert qml.math.allclose(result, self.expected_entropy_Ising_XX(phi))
def test_custom_measurement(self, lightning_sv, method_name):
"""Test that LightningMeasurements can handle a custom state based measurement."""
statevector = lightning_sv(num_wires=5)
m = LightningMeasurements(statevector)
measurement = CustomStateMeasurement()
result = getattr(m, method_name)(measurement)
assert result == 1
def test_measurement_with_diagonalizing_gates(self, lightning_sv, method_name):
statevector = lightning_sv(num_wires=5)
m = LightningMeasurements(statevector)
measurement = qml.probs(op=qml.PauliX(0))
result = getattr(m, method_name)(measurement)
assert qml.math.allclose(result, [0.5, 0.5])
def test_identity_expval(self, lightning_sv, method_name):
"""Test that the expectation value of an identity is always one."""
statevector = lightning_sv(num_wires=5)
statevector.apply_operations([qml.Rot(0.5, 4.2, 6.8, wires=4)])
m = LightningMeasurements(statevector)
result = getattr(m, method_name)(qml.expval(qml.I(4)))
assert np.allclose(result, 1.0)
@pytest.mark.skipif(
device_name == "lightning.tensor",
reason="lightning.tensor does not support a single-wire circuit.",
)
def test_basis_state_projector_expval(self, lightning_sv, method_name):
"""Test expectation value for a basis state projector."""
phi = 0.8
statevector = lightning_sv(num_wires=1)
statevector.apply_operations([qml.RX(phi, 0)])
m = LightningMeasurements(statevector)
result = getattr(m, method_name)(qml.expval(qml.Projector([0], wires=0)))
assert qml.math.allclose(result, np.cos(phi / 2) ** 2)
@pytest.mark.skipif(
device_name == "lightning.tensor",
reason="lightning.tensor does not support a single-wire circuit.",
)
def test_state_vector_projector_expval(self, lightning_sv, method_name):
"""Test expectation value for a state vector projector."""
phi = -0.6
statevector = lightning_sv(num_wires=1)
statevector.apply_operations([qml.RX(phi, 0)])
m = LightningMeasurements(statevector)
result = getattr(m, method_name)(qml.expval(qml.Projector([0, 1], wires=0)))
assert qml.math.allclose(result, np.sin(phi / 2) ** 2)
@pytest.mark.parametrize("theta, phi", list(zip(THETA, PHI)))
class TestExpval:
"""Test expectation value calculations."""
def test_identity(self, theta, phi, tol, lightning_sv):
"""Tests applying identities."""
wires = 3
ops = [
qml.Identity(0),
qml.Identity((0, 1)),
qml.Identity((1, 2)),
qml.RX(theta, 0),
qml.RX(phi, 1),
]
measurements = [qml.expval(qml.PauliZ(0))]
tape = qml.tape.QuantumScript(ops, measurements)
statevector = lightning_sv(wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = np.cos(theta)
assert np.allclose(result, expected, tol)
def test_identity_expectation(self, theta, phi, tol, lightning_sv):
"""Tests identity expectations."""
wires = 2
tape = qml.tape.QuantumScript(
[qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.CNOT(wires=[0, 1])],
[qml.expval(qml.Identity(wires=[0])), qml.expval(qml.Identity(wires=[1]))],
)
statevector = lightning_sv(wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = 1.0
assert np.allclose(result, expected, tol)
def test_multi_wire_identity_expectation(self, theta, phi, tol, lightning_sv):
"""Tests multi-wire identity."""
wires = 2
tape = qml.tape.QuantumScript(
[qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.CNOT(wires=[0, 1])],
[qml.expval(qml.Identity(wires=[0, 1]))],
)
statevector = lightning_sv(wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = 1.0
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize(
"Obs, Op, expected_fn",
[
(
[qml.PauliX(wires=[0]), qml.PauliX(wires=[1])],
qml.RY,
lambda theta, phi: np.array([np.sin(theta) * np.sin(phi), np.sin(phi)]),
),
(
[qml.PauliY(wires=[0]), qml.PauliY(wires=[1])],
qml.RX,
lambda theta, phi: np.array([0, -np.cos(theta) * np.sin(phi)]),
),
(
[qml.PauliZ(wires=[0]), qml.PauliZ(wires=[1])],
qml.RX,
lambda theta, phi: np.array([np.cos(theta), np.cos(theta) * np.cos(phi)]),
),
(
[qml.Hadamard(wires=[0]), qml.Hadamard(wires=[1])],
qml.RY,
lambda theta, phi: np.array(
[
np.sin(theta) * np.sin(phi) + np.cos(theta),
np.cos(theta) * np.cos(phi) + np.sin(phi),
]
)
/ np.sqrt(2),
),
],
)
def test_single_wire_observables_expectation(
self, Obs, Op, expected_fn, theta, phi, tol, lightning_sv
):
"""Test that expectation values for single wire observables are correct"""
wires = 3
tape = qml.tape.QuantumScript(
[Op(theta, wires=[0]), Op(phi, wires=[1]), qml.CNOT(wires=[0, 1])],
[qml.expval(Obs[0]), qml.expval(Obs[1])],
)
statevector = lightning_sv(wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = expected_fn(theta, phi)
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize("method_name", ("expval", "measurement"))
class TestExpvalHamiltonian:
"""Tests expval for Hamiltonians"""
wires = 2
@pytest.mark.parametrize(
"obs, coeffs, expected",
[
([qml.PauliX(0) @ qml.PauliZ(1)], [1.0], 0.0),
([qml.PauliZ(0) @ qml.PauliZ(1)], [1.0], math.cos(0.4) * math.cos(-0.2)),
(
[
qml.PauliX(0) @ qml.PauliZ(1),
qml.Hermitian(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, 3.0, 0.0, 0.0],
[0.0, 0.0, -1.0, 1.0],
[0.0, 0.0, 1.0, -2.0],
],
wires=[0, 1],
),
],
[0.3, 1.0],
0.9319728930156066,
),
],
)
def test_expval_hamiltonian(self, obs, coeffs, expected, tol, lightning_sv, method_name):
"""Test expval with Hamiltonian"""
if any(isinstance(o, qml.Hermitian) for o in obs) and device_name == "lightning.tensor":
pytest.skip("Hermitian with 1+ wires target not supported in lightning.tensor.")
ham = qml.Hamiltonian(coeffs, obs)
statevector = lightning_sv(self.wires)
statevector.apply_operations([qml.RX(0.4, wires=[0]), qml.RY(-0.2, wires=[1])])
m = LightningMeasurements(statevector)
result = getattr(m, method_name)(qml.expval(ham))
assert np.allclose(result, expected, atol=tol, rtol=0)
@pytest.mark.skipif(
device_name == "lightning.tensor", reason="lightning.tensor does not support sparseH."
)
class TestSparseExpval:
"""Tests for the expval function"""
wires = 2
@pytest.mark.parametrize(
"ham_terms, expected",
[
[qml.PauliX(0) @ qml.Identity(1), 0.00000000000000000],
[qml.Identity(0) @ qml.PauliX(1), -0.19866933079506122],
[qml.PauliY(0) @ qml.Identity(1), -0.38941834230865050],
[qml.Identity(0) @ qml.PauliY(1), 0.00000000000000000],
[qml.PauliZ(0) @ qml.Identity(1), 0.92106099400288520],
[qml.Identity(0) @ qml.PauliZ(1), 0.98006657784124170],
],
)
def test_sparse_Pauli_words(self, ham_terms, expected, tol, lightning_sv):
"""Test expval of some simple sparse Hamiltonian"""
ops = [qml.RX(0.4, wires=[0]), qml.RY(-0.2, wires=[1])]
measurements = [
qml.expval(
qml.SparseHamiltonian(
qml.Hamiltonian([1], [ham_terms]).sparse_matrix(), wires=[0, 1]
)
)
]
tape = qml.tape.QuantumScript(ops, measurements)
statevector = lightning_sv(self.wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
assert np.allclose(result, expected, tol)
class TestMeasurements:
"""Tests all measurements"""
@staticmethod
def calculate_reference(tape, lightning_sv):
use_default = True
new_meas = []
for m in tape.measurements:
# NotImplementedError in DefaultQubit
# We therefore validate against `qml.Hermitian`
if isinstance(m, VarianceMP) and isinstance(m.obs, (qml.SparseHamiltonian)):
use_default = False
new_meas.append(m.__class__(qml.Hermitian(qml.matrix(m.obs), wires=m.obs.wires)))
continue
new_meas.append(m)
if use_default:
dev = DefaultQubit(max_workers=1)
program, _ = dev.preprocess()
tapes, transf_fn = program([tape])
results = dev.execute(tapes)
return transf_fn(results)
tape = qml.tape.QuantumScript(tape.operations, new_meas)
statevector = lightning_sv(tape.num_wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
return measure_final_state(m, tape)
@flaky(max_runs=5)
@pytest.mark.parametrize("shots", [None, 500_000, [500_000, 500_000]])
@pytest.mark.parametrize("measurement", [qml.expval, qml.probs, qml.var])
@pytest.mark.parametrize(
"observable",
(
[0],
[1, 2],
[1, 0],
qml.PauliX(0),
qml.PauliY(1),
qml.PauliZ(2),
qml.sum(qml.PauliX(0), qml.PauliY(0)),
qml.prod(qml.PauliX(0), qml.PauliY(1)),
qml.s_prod(2.0, qml.PauliX(0)),
qml.Hermitian(get_hermitian_matrix(2**2), wires=[0, 1]),
qml.Hermitian(get_hermitian_matrix(2**2), wires=[2, 3]),
qml.Hamiltonian(
[1.0, 2.0, 3.0], [qml.PauliX(0), qml.PauliY(1), qml.PauliZ(2) @ qml.PauliZ(3)]
),
qml.SparseHamiltonian(get_sparse_hermitian_matrix(2**4), wires=range(4)),
),
)
def test_single_return_value(self, shots, measurement, observable, lightning_sv, tol):
if obs_not_supported_in_ltensor(observable):
pytest.skip("Observable not supported in lightning.tensor.")
if measurement is qml.probs and isinstance(
observable,
(
qml.ops.Sum,
qml.ops.SProd,
qml.ops.Prod,
qml.SparseHamiltonian,
),
):
pytest.skip(
f"Observable of type {type(observable).__name__} is not supported for rotating probabilities."
)
if measurement is not qml.probs and isinstance(observable, list):
pytest.skip(
f"Measurement of type {type(measurement).__name__} does not have a keyword argument 'wires'."
)
if shots != None and measurement is qml.expval:
# Increase the number of shots
if isinstance(shots, int):
shots = 1_000_000
else:
shots = [1_000_000, 1_000_000]
n_qubits = 4
n_layers = 1
np.random.seed(0)
weights = np.random.rand(n_layers, n_qubits, 3)
ops = [qml.Hadamard(i) for i in range(n_qubits)]
if device_name != "lightning.tensor":
ops += [qml.StronglyEntanglingLayers(weights, wires=range(n_qubits))]
measurements = (
[measurement(wires=observable)]
if isinstance(observable, list)
else [measurement(op=observable)]
)
tape = qml.tape.QuantumScript(ops, measurements, shots=shots)
statevector = lightning_sv(n_qubits)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
skip_list = (
qml.ops.Sum,
qml.SparseHamiltonian,
)
do_skip = measurement is qml.var and isinstance(observable, skip_list)
do_skip = do_skip or (
measurement is qml.expval and isinstance(observable, qml.SparseHamiltonian)
)
do_skip = do_skip and shots is not None
if do_skip:
with pytest.raises(TypeError):
_ = measure_final_state(m, tape)
return
else:
result = measure_final_state(m, tape)
expected = self.calculate_reference(tape, lightning_sv)
# a few tests may fail in single precision, and hence we increase the tolerance
if shots is None:
assert np.allclose(
result,
expected,
max(tol, 1.0e-4),
1e-6 if statevector.dtype == np.complex64 else 1e-8,
)
else:
# TODO Set better atol and rtol
dtol = max(tol, 1.0e-2)
# allclose -> absolute(a - b) <= (atol + rtol * absolute(b))
assert np.allclose(result, expected, rtol=dtol, atol=dtol)
@flaky(max_runs=5)
@pytest.mark.parametrize("shots", [None, 400_000, (400_000, 400_000)])
@pytest.mark.parametrize("measurement", [qml.expval, qml.probs, qml.var])
@pytest.mark.parametrize(
"obs0_",
(
qml.PauliX(0),
qml.PauliY(1),
qml.PauliZ(2),
qml.sum(qml.PauliX(0), qml.PauliY(0)),
qml.prod(qml.PauliX(0), qml.PauliY(1)),
qml.s_prod(2.0, qml.PauliX(0)),
qml.Hermitian(get_hermitian_matrix(2), wires=[0]),
qml.Hermitian(get_hermitian_matrix(2**2), wires=[2, 3]),
qml.Hamiltonian(
[1.0, 2.0, 3.0], [qml.PauliX(0), qml.PauliY(1), qml.PauliZ(2) @ qml.PauliZ(3)]
),
qml.SparseHamiltonian(get_sparse_hermitian_matrix(2**4), wires=range(4)),
),
)
@pytest.mark.parametrize(
"obs1_",
(
qml.PauliX(0),
qml.PauliY(1),
qml.PauliZ(2),
qml.sum(qml.PauliX(0), qml.PauliY(0)),
qml.prod(qml.PauliX(0), qml.PauliY(1)),
qml.s_prod(2.0, qml.PauliX(0)),
qml.Hermitian(get_hermitian_matrix(2), wires=[0]),
qml.Hermitian(get_hermitian_matrix(2**2), wires=[2, 3]),
qml.Hamiltonian(
[1.0, 2.0, 3.0], [qml.PauliX(0), qml.PauliY(1), qml.PauliZ(2) @ qml.PauliZ(3)]
),
qml.SparseHamiltonian(get_sparse_hermitian_matrix(2**4), wires=range(4)),
),
)
def test_double_return_value(self, shots, measurement, obs0_, obs1_, lightning_sv, tol):
if obs_not_supported_in_ltensor(obs0_) or obs_not_supported_in_ltensor(obs1_):
pytest.skip("Observable not supported in lightning.tensor.")
skip_list = (
qml.ops.Sum,
qml.ops.SProd,
qml.ops.Prod,
qml.Hamiltonian,
qml.SparseHamiltonian,
)
if measurement is qml.probs and (
isinstance(obs0_, skip_list) or isinstance(obs1_, skip_list)
):
pytest.skip(
f"Observable of type {type(obs0_).__name__} is not supported for rotating probabilities."
)
if shots != None and measurement is qml.expval:
# Increase the number of shots
if isinstance(shots, int):
shots = 1_000_000
else:
shots = [1_000_000, 1_000_000]
n_qubits = 4
n_layers = 1
np.random.seed(0)
weights = np.random.rand(n_layers, n_qubits, 3)
ops = [qml.Hadamard(i) for i in range(n_qubits)]
if device_name != "lightning.tensor":
ops += [qml.StronglyEntanglingLayers(weights, wires=range(n_qubits))]
measurements = [measurement(op=obs0_), measurement(op=obs1_)]
tape = qml.tape.QuantumScript(ops, measurements, shots=shots)
statevector = lightning_sv(n_qubits)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
skip_list = (
qml.ops.Sum,
qml.Hamiltonian,
qml.SparseHamiltonian,
)
do_skip = measurement is qml.var and (
isinstance(obs0_, skip_list) or isinstance(obs1_, skip_list)
)
do_skip = do_skip or (
measurement is qml.expval
and (
isinstance(obs0_, qml.SparseHamiltonian) or isinstance(obs1_, qml.SparseHamiltonian)
)
)
do_skip = do_skip and shots is not None
if do_skip:
with pytest.raises(TypeError):
_ = measure_final_state(m, tape)
return
else:
result = measure_final_state(m, tape)
expected = self.calculate_reference(tape, lightning_sv)
if len(expected) == 1:
expected = expected[0]
assert isinstance(result, Sequence)
assert len(result) == len(expected)
# a few tests may fail in single precision, and hence we increase the tolerance
dtol = tol if shots is None else max(tol, 1.0e-2)
if device_name == "lightning.tensor" and statevector.dtype == np.complex64:
dtol = max(dtol, 1.0e-4)
# TODO Set better atol and rtol
for r, e in zip(result, expected):
if isinstance(shots, tuple) and isinstance(r[0], np.ndarray):
r = np.concatenate(r)
e = np.concatenate(e)
# allclose -> absolute(r - e) <= (atol + rtol * absolute(e))
assert np.allclose(r, e, atol=dtol, rtol=dtol)
@pytest.mark.skipif(
device_name in ("lightning.tensor"),
reason=f"{device_name} does not support out of order probs.",
)
@pytest.mark.parametrize(
"cases",
[
[[0, 1], [1, 0]],
[[1, 0], [0, 1]],
],
)
def test_probs_tape_unordered_wires(self, cases, tol):
"""Test probs with a circuit on wires=[0] fails for out-of-order wires passed to probs."""
x, y, z = [0.5, 0.3, -0.7]
dev = qml.device(device_name, wires=cases[1])
def circuit():
qml.RX(0.4, wires=[0])
qml.Rot(x, y, z, wires=[0])
qml.RY(-0.2, wires=[0])
return qml.probs(wires=cases[0])
expected = qml.QNode(circuit, qml.device("default.qubit", wires=cases[1]))()
results = qml.QNode(circuit, dev)()
assert np.allclose(expected, results, tol)
class TestControlledOps:
"""Tests for controlled operations"""
@staticmethod
def calculate_reference(tape):
dev = DefaultQubit(max_workers=1)
program, _ = dev.preprocess()
tapes, transf_fn = program([tape])
results = dev.execute(tapes)
return transf_fn(results)
@pytest.mark.parametrize(
"operation",
[
qml.PauliX,
qml.PauliY,
qml.PauliZ,
qml.Hadamard,
qml.S,
qml.T,
qml.PhaseShift,
qml.RX,
qml.RY,
qml.RZ,
qml.Rot,
qml.SWAP,
qml.IsingXX,
qml.IsingXY,
qml.IsingYY,
qml.IsingZZ,
qml.SingleExcitation,
qml.SingleExcitationMinus,
qml.SingleExcitationPlus,
qml.DoubleExcitation,
qml.DoubleExcitationMinus,
qml.DoubleExcitationPlus,
qml.MultiRZ,
qml.GlobalPhase,
],
)
@pytest.mark.parametrize("control_value", [False, True])
@pytest.mark.parametrize("n_qubits", list(range(2, 5)))
def test_controlled_qubit_gates(self, operation, n_qubits, control_value, tol, lightning_sv):
"""Test that multi-controlled gates are correctly applied to a state"""
threshold = 250 if device_name != "lightning.tensor" else 5
num_wires = max(operation.num_wires, 1)
np.random.seed(0)
for n_wires in range(num_wires + 1, num_wires + 4):
wire_lists = list(itertools.permutations(range(0, n_qubits), n_wires))
n_perms = len(wire_lists) * n_wires
if n_perms > threshold:
wire_lists = wire_lists[0 :: (n_perms // threshold)]
for all_wires in wire_lists:
target_wires = all_wires[0:num_wires]
control_wires = all_wires[num_wires:]
init_state = np.random.rand(2**n_qubits) + 1.0j * np.random.rand(2**n_qubits)
init_state /= np.linalg.norm(init_state)
ops = [
qml.StatePrep(init_state, wires=range(n_qubits)),
]
if operation.num_params == 0:
ops += [
qml.ctrl(
operation(target_wires),
control_wires,
control_values=(
[control_value or bool(i % 2) for i, _ in enumerate(control_wires)]
if device_name != "lightning.tensor"
else [control_value for _ in control_wires]
),
),
]
else:
ops += [
qml.ctrl(
operation(*tuple([0.1234] * operation.num_params), target_wires),
control_wires,
control_values=(
[control_value or bool(i % 2) for i, _ in enumerate(control_wires)]
if device_name != "lightning.tensor"
else [control_value for _ in control_wires]
),
),
]
measurements = [qml.state()]
tape = qml.tape.QuantumScript(ops, measurements)
statevector = lightning_sv(n_qubits)
if device_name == "lightning.tensor" and statevector.method == "tn":
pytest.skip("StatePrep not supported in lightning.tensor with the tn method.")
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = self.calculate_reference(tape)
if device_name == "lightning.tensor":
assert np.allclose(result, expected, 1e-4)
else:
assert np.allclose(result, expected, tol * 10)
def test_controlled_qubit_unitary_from_op(self, tol, lightning_sv):
n_qubits = 10
par = 0.1234
tape = qml.tape.QuantumScript(
[
qml.ControlledQubitUnitary(
qml.QubitUnitary(qml.RX.compute_matrix(par), wires=5), control_wires=range(5)
)
],
[qml.expval(qml.PauliX(0))],
)
statevector = lightning_sv(n_qubits)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = self.calculate_reference(tape)
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize("control_wires", range(4))
@pytest.mark.parametrize("target_wires", range(4))
def test_cnot_controlled_qubit_unitary(self, control_wires, target_wires, tol, lightning_sv):
"""Test that ControlledQubitUnitary is correctly applied to a state"""
if control_wires == target_wires:
return
n_qubits = 4
control_wires = [control_wires]
target_wires = [target_wires]
wires = control_wires + target_wires
U = qml.matrix(qml.PauliX(target_wires))
np.random.seed(0)
init_state = np.random.rand(2**n_qubits) + 1.0j * np.random.rand(2**n_qubits)
init_state /= np.linalg.norm(init_state)
tape = qml.tape.QuantumScript(
[
qml.StatePrep(init_state, wires=range(n_qubits)),
qml.ControlledQubitUnitary(U, control_wires=control_wires, wires=target_wires),
],
[qml.state()],
)
tape_cnot = qml.tape.QuantumScript(
[qml.StatePrep(init_state, wires=range(n_qubits)), qml.CNOT(wires=wires)], [qml.state()]
)
statevector = lightning_sv(n_qubits)
if device_name == "lightning.tensor" and statevector.method == "tn":
pytest.skip("StatePrep not supported in lightning.tensor with the tn method.")
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = self.calculate_reference(tape_cnot)
if device_name == "lightning.tensor":
assert np.allclose(result, expected, 1e-4)
else:
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize("control_value", [False, True])
@pytest.mark.parametrize("n_qubits", list(range(2, 8)))
def test_controlled_globalphase(self, n_qubits, control_value, tol, lightning_sv):
"""Test that multi-controlled gates are correctly applied to a state"""
threshold = 250 if device_name != "lightning.tensor" else 5
operation = qml.GlobalPhase
num_wires = max(operation.num_wires, 1)
for n_wires in range(num_wires + 1, num_wires + 4):
wire_lists = list(itertools.permutations(range(0, n_qubits), n_wires))
n_perms = len(wire_lists) * n_wires
if n_perms > threshold:
wire_lists = wire_lists[0 :: (n_perms // threshold)]
for all_wires in wire_lists:
target_wires = all_wires[0:num_wires]
control_wires = all_wires[num_wires:]
init_state = np.random.rand(2**n_qubits) + 1.0j * np.random.rand(2**n_qubits)
init_state /= np.linalg.norm(init_state)
tape = qml.tape.QuantumScript(
[
qml.StatePrep(init_state, wires=range(n_qubits)),
qml.ctrl(
operation(0.1234, target_wires),
control_wires,
control_values=(
[control_value or bool(i % 2) for i, _ in enumerate(control_wires)]
if device_name != "lightning.tensor"
else [control_value for _ in control_wires]
),
),
],
[qml.state()],
)
statevector = lightning_sv(n_qubits)
if device_name == "lightning.tensor" and statevector.method == "tn":
pytest.skip("StatePrep not supported in lightning.tensor with the tn method.")
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = self.calculate_reference(tape)
if device_name == "lightning.tensor" and statevector.dtype == np.complex64:
assert np.allclose(result, expected, 1e-4)
else:
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize("phi", PHI)
class TestExpOperatorArithmetic:
"""Test integration with SProd, Prod, and Sum."""
wires = 2
def test_sprod(self, phi, lightning_sv, tol):
"""Test the `SProd` class."""
tape = qml.tape.QuantumScript(
[qml.RX(phi, wires=0)],
[qml.expval(qml.s_prod(0.5, qml.PauliZ(0)))],
)
statevector = lightning_sv(self.wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = 0.5 * np.cos(phi)
assert np.allclose(result, expected, tol)
def test_prod(self, phi, lightning_sv, tol):
"""Test the `Prod` class."""
tape = qml.tape.QuantumScript(
[qml.RX(phi, wires=0), qml.Hadamard(1), qml.PauliZ(1)],
[qml.expval(qml.prod(qml.PauliZ(0), qml.PauliX(1)))],
)
statevector = lightning_sv(self.wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = -np.cos(phi)
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize("theta", THETA)
def test_sum(self, phi, theta, lightning_sv, tol):
"""Test the `Sum` class."""
tape = qml.tape.QuantumScript(
[qml.RX(phi, wires=0), qml.RY(theta, wires=1)],
[qml.expval(qml.sum(qml.PauliZ(0), qml.PauliX(1)))],
)
statevector = lightning_sv(self.wires)
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
expected = np.cos(phi) + np.sin(theta)
assert np.allclose(result, expected, tol)
@pytest.mark.parametrize(
"op,par,wires,expected",
[
(qml.StatePrep, [0, 1], [1], [1, -1]),
(qml.StatePrep, [0, 1], [0], [-1, 1]),
(qml.StatePrep, [1.0 / np.sqrt(2), 1.0 / np.sqrt(2)], [1], [1, 0]),
(qml.StatePrep, [1j / 2.0, np.sqrt(3) / 2.0], [1], [1, -0.5]),
(qml.StatePrep, [(2 - 1j) / 3.0, 2j / 3.0], [0], [1 / 9.0, 1]),
],
)
def test_state_vector_2_qubit_subset(tol, op, par, wires, expected, lightning_sv):
"""Tests qubit state vector preparation and measure on subsets of 2 qubits"""
tape = qml.tape.QuantumScript(
[op(par, wires=wires)], [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))]
)
statevector = lightning_sv(2)
if device_name == "lightning.tensor" and statevector.method == "tn":
pytest.skip("StatePrep not supported in lightning.tensor with the tn method.")
statevector = get_final_state(statevector, tape)
m = LightningMeasurements(statevector)
result = measure_final_state(m, tape)
assert np.allclose(result, expected, tol)