-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgate_operations.py
More file actions
1425 lines (1097 loc) · 36.1 KB
/
gate_operations.py
File metadata and controls
1425 lines (1097 loc) · 36.1 KB
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
993
994
995
996
997
998
999
1000
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
from __future__ import annotations
from collections.abc import Sequence
import numpy as np
import braket.ir.jaqcd as braket_instruction
from braket.default_simulator.operation import GateOperation
from braket.default_simulator.operation_helpers import (
_from_braket_instruction,
check_matrix_dimensions,
check_unitary,
ir_matrix_to_ndarray,
)
class Identity(GateOperation):
"""Identity gate"""
_matrix = np.eye(2, dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return Identity._matrix
@property
def gate_type(self) -> str:
return "identity"
@_from_braket_instruction.register(braket_instruction.I)
def _i(instruction) -> Identity:
return Identity([instruction.target])
class Hadamard(GateOperation):
"""Hadamard gate"""
_matrix = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return Hadamard._matrix
@property
def gate_type(self) -> str:
return "hadamard"
@_from_braket_instruction.register(braket_instruction.H)
def _hadamard(instruction) -> Hadamard:
return Hadamard([instruction.target])
class PauliX(GateOperation):
"""Pauli-X gate"""
_matrix = np.array([[0, 1], [1, 0]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return PauliX._matrix
@property
def gate_type(self) -> str:
return "pauli_x"
@_from_braket_instruction.register(braket_instruction.X)
def _pauli_x(instruction) -> PauliX:
return PauliX([instruction.target])
class PauliY(GateOperation):
"""Pauli-Y gate"""
_matrix = np.array([[0, -1j], [1j, 0]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return PauliY._matrix
@property
def gate_type(self) -> str:
return "pauli_y"
@_from_braket_instruction.register(braket_instruction.Y)
def _pauli_y(instruction) -> PauliY:
return PauliY([instruction.target])
class PauliZ(GateOperation):
"""Pauli-Z gate"""
_matrix = np.diag([1.0, -1.0]).astype(complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return PauliZ._matrix
@property
def gate_type(self) -> str:
return "pauli_z"
@_from_braket_instruction.register(braket_instruction.Z)
def _pauli_z(instruction) -> PauliZ:
return PauliZ([instruction.target])
class CV(GateOperation):
"""Controlled-Sqrt(NOT) gate"""
_matrix = np.array(
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0.5 + 0.5j, 0.5 - 0.5j],
[0, 0, 0.5 - 0.5j, 0.5 + 0.5j],
],
dtype=complex,
)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return CV._matrix
@property
def gate_type(self) -> str:
return "cv"
@_from_braket_instruction.register(braket_instruction.CV)
def _cv(instruction) -> CV:
return CV([instruction.control, instruction.target])
class CX(GateOperation):
"""Controlled Pauli-X gate"""
_matrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return CX._matrix
@property
def gate_type(self) -> str:
return "cx"
@_from_braket_instruction.register(braket_instruction.CNot)
def _cx(instruction) -> CX:
return CX([instruction.control, instruction.target])
class CY(GateOperation):
"""Controlled Pauli-Y gate"""
_matrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, -1j], [0, 0, 1j, 0]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self):
return CY._matrix
@property
def gate_type(self) -> str:
return "cy"
@_from_braket_instruction.register(braket_instruction.CY)
def _cy(instruction) -> CY:
return CY([instruction.control, instruction.target])
class CZ(GateOperation):
"""Controlled Pauli-Z gate"""
_matrix = np.diag([1, 1, 1, -1]).astype(complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return CZ._matrix
@property
def gate_type(self) -> str:
return "cz"
@_from_braket_instruction.register(braket_instruction.CZ)
def _cz(instruction) -> CZ:
return CZ([instruction.control, instruction.target])
class ECR(GateOperation):
"""ECR gate"""
_matrix = (
1
/ np.sqrt(2)
* np.array(
[[0, 0, 1, 1.0j], [0, 0, 1.0j, 1], [1, -1.0j, 0, 0], [-1.0j, 1, 0, 0]],
dtype=complex,
)
)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return ECR._matrix
@property
def gate_type(self) -> str:
return "ecr"
@_from_braket_instruction.register(braket_instruction.ECR)
def _ecr(instruction) -> ECR:
return ECR(instruction.targets)
class S(GateOperation):
"""S gate"""
_matrix = np.array([[1, 0], [0, 1j]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return S._matrix
@property
def gate_type(self) -> str:
return "s"
@_from_braket_instruction.register(braket_instruction.S)
def _s(instruction) -> S:
return S([instruction.target])
class Si(GateOperation):
r"""The adjoint :math:`S^{\dagger}` of the S gate"""
_matrix = np.array([[1, 0], [0, -1j]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return Si._matrix
@property
def gate_type(self) -> str:
return "si"
@_from_braket_instruction.register(braket_instruction.Si)
def _si(instruction) -> Si:
return Si([instruction.target])
class T(GateOperation):
"""T gate"""
_matrix = np.array([[1, 0], [0, np.exp(1j * np.pi / 4)]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return T._matrix
@property
def gate_type(self) -> str:
return "t"
@_from_braket_instruction.register(braket_instruction.T)
def _t(instruction) -> T:
return T([instruction.target])
class Ti(GateOperation):
r"""The adjoint :math:`T^{\dagger}` of the T gate"""
_matrix = np.array([[1, 0], [0, np.exp(-1j * np.pi / 4)]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return Ti._matrix
@property
def gate_type(self) -> str:
return "ti"
@_from_braket_instruction.register(braket_instruction.Ti)
def _ti(instruction) -> Ti:
return Ti([instruction.target])
class V(GateOperation):
"""Square root of the X (not) gate"""
_matrix = np.array([[0.5 + 0.5j, 0.5 - 0.5j], [0.5 - 0.5j, 0.5 + 0.5j]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return V._matrix
@property
def gate_type(self) -> str:
return "v"
@_from_braket_instruction.register(braket_instruction.V)
def _v(instruction) -> V:
return V([instruction.target])
class Vi(GateOperation):
r"""The adjoint :math:`V^{\dagger}` of the square root of the X (not) gate"""
_matrix = np.array(([[0.5 - 0.5j, 0.5 + 0.5j], [0.5 + 0.5j, 0.5 - 0.5j]]), dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return Vi._matrix
@property
def gate_type(self) -> str:
return "vi"
@_from_braket_instruction.register(braket_instruction.Vi)
def _vi(instruction) -> Vi:
return Vi([instruction.target])
class PhaseShift(GateOperation):
"""Phase shift gate"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
return np.array([[1, 0], [0, np.exp(1j * angle_float)]], dtype=complex)
@property
def gate_type(self) -> str:
return "phaseshift"
@_from_braket_instruction.register(braket_instruction.PhaseShift)
def _phase_shift(instruction) -> PhaseShift:
return PhaseShift([instruction.target], instruction.angle)
class CPhaseShift(GateOperation):
"""Controlled phase shift gate"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
return np.diag([1.0, 1.0, 1.0, np.exp(1j * angle_float)]).astype(complex)
@property
def gate_type(self) -> str:
return "cphaseshift"
@_from_braket_instruction.register(braket_instruction.CPhaseShift)
def _c_phase_shift(instruction) -> CPhaseShift:
return CPhaseShift([instruction.control, instruction.target], instruction.angle)
class CPhaseShift00(GateOperation):
r"""Controlled phase shift gate phasing the :math:`\ket{00}` state"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
return np.diag([np.exp(1j * angle_float), 1.0, 1.0, 1.0]).astype(complex)
@property
def gate_type(self) -> str:
return "cphaseshift00"
@_from_braket_instruction.register(braket_instruction.CPhaseShift00)
def _c_phase_shift_00(instruction) -> CPhaseShift00:
return CPhaseShift00([instruction.control, instruction.target], instruction.angle)
class CPhaseShift01(GateOperation):
r"""Controlled phase shift gate phasing the :math:`\ket{01}` state"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
return np.diag([1.0, np.exp(1j * angle_float), 1.0, 1.0]).astype(complex)
@property
def gate_type(self) -> str:
return "cphaseshift01"
@_from_braket_instruction.register(braket_instruction.CPhaseShift01)
def _c_phase_shift_01(instruction) -> CPhaseShift01:
return CPhaseShift01([instruction.control, instruction.target], instruction.angle)
class CPhaseShift10(GateOperation):
r"""Controlled phase shift gate phasing the :math:`\ket{10}` state"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
return np.diag([1.0, 1.0, np.exp(1j * angle_float), 1.0]).astype(complex)
@property
def gate_type(self) -> str:
return "cphaseshift10"
@_from_braket_instruction.register(braket_instruction.CPhaseShift10)
def _c_phase_shift_10(instruction) -> CPhaseShift10:
return CPhaseShift10([instruction.control, instruction.target], instruction.angle)
class RotX(GateOperation):
"""X-axis rotation gate"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
cos_half_angle = np.cos(angle_float / 2).astype(complex)
i_sin_half_angle = 1j * np.sin(angle_float / 2).astype(complex)
return np.array(
[[cos_half_angle, -i_sin_half_angle], [-i_sin_half_angle, cos_half_angle]],
dtype=complex,
)
@property
def gate_type(self) -> str:
return "rx"
@_from_braket_instruction.register(braket_instruction.Rx)
def _rot_x(instruction) -> RotX:
return RotX([instruction.target], instruction.angle)
class RotY(GateOperation):
"""Y-axis rotation gate"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
cos_half_angle = np.cos(angle_float / 2).astype(complex)
sin_half_angle = np.sin(angle_float / 2).astype(complex)
return np.array(
[[cos_half_angle, -sin_half_angle], [sin_half_angle, cos_half_angle]], dtype=complex
)
@property
def gate_type(self) -> str:
return "ry"
@_from_braket_instruction.register(braket_instruction.Ry)
def _rot_y(instruction) -> RotY:
return RotY([instruction.target], instruction.angle)
class RotZ(GateOperation):
"""Z-axis rotation gate"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
return np.array(
[[np.exp(-1j * angle_float / 2), 0], [0, np.exp(1j * angle_float / 2)]], dtype=complex
)
@property
def gate_type(self) -> str:
return "rz"
@_from_braket_instruction.register(braket_instruction.Rz)
def _rot_z(instruction) -> RotZ:
return RotZ([instruction.target], instruction.angle)
class Swap(GateOperation):
"""Swap gate"""
_matrix = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]], dtype=complex)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return Swap._matrix
@property
def gate_type(self) -> str:
return "swap"
@_from_braket_instruction.register(braket_instruction.Swap)
def _swap(instruction) -> Swap:
return Swap(instruction.targets)
class ISwap(GateOperation):
"""iSwap gate"""
_matrix = np.array(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0j, 0.0],
[0.0, 1.0j, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
dtype=complex,
)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return ISwap._matrix
@property
def gate_type(self) -> str:
return "iswap"
@_from_braket_instruction.register(braket_instruction.ISwap)
def _iswap(instruction) -> ISwap:
return ISwap(instruction.targets)
class PSwap(GateOperation):
"""Parametrized Swap gate"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
exp_angle = np.exp(1j * angle_float)
return np.array(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, exp_angle, 0.0],
[0.0, exp_angle, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
dtype=complex,
)
@property
def gate_type(self) -> str:
return "pswap"
@_from_braket_instruction.register(braket_instruction.PSwap)
def _pswap(instruction) -> PSwap:
return PSwap(instruction.targets, instruction.angle)
class XY(GateOperation):
"""XY gate
Reference: https://arxiv.org/abs/1912.04424v1
"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
cos = np.cos(angle_float / 2).astype(complex)
sin = np.sin(angle_float / 2).astype(complex)
return np.array(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, cos, 1.0j * sin, 0.0],
[0.0, 1.0j * sin, cos, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
dtype=complex,
)
@property
def gate_type(self) -> str:
return "xy"
@_from_braket_instruction.register(braket_instruction.XY)
def _xy(instruction) -> XY:
return XY(instruction.targets, instruction.angle)
class XX(GateOperation):
"""Ising XX gate
Reference: https://arxiv.org/abs/1707.06356
"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
cos_angle = np.cos(angle_float / 2).astype(complex)
i_sin_angle = 1j * np.sin(angle_float / 2).astype(complex)
return np.array(
[
[cos_angle, 0, 0, -i_sin_angle],
[0, cos_angle, -i_sin_angle, 0],
[0, -i_sin_angle, cos_angle, 0],
[-i_sin_angle, 0, 0, cos_angle],
],
dtype=complex,
)
@property
def gate_type(self) -> str:
return "xx"
@_from_braket_instruction.register(braket_instruction.XX)
def _xx(instruction) -> XX:
return XX(instruction.targets, instruction.angle)
class YY(GateOperation):
"""Ising YY gate
Reference: https://arxiv.org/abs/1707.06356
"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
cos_angle = np.cos(angle_float / 2).astype(complex)
i_sin_angle = 1j * np.sin(angle_float / 2).astype(complex)
return np.array(
[
[cos_angle, 0, 0, i_sin_angle],
[0, cos_angle, -i_sin_angle, 0],
[0, -i_sin_angle, cos_angle, 0],
[i_sin_angle, 0, 0, cos_angle],
],
dtype=complex,
)
@property
def gate_type(self) -> str:
return "yy"
@_from_braket_instruction.register(braket_instruction.YY)
def _yy(instruction) -> YY:
return YY(instruction.targets, instruction.angle)
class ZZ(GateOperation):
"""Ising ZZ gate
Reference: https://arxiv.org/abs/1707.06356
"""
def __init__(self, targets, angle, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
self._angle = angle
@property
def _base_matrix(self) -> np.ndarray:
angle_float = float(self._angle)
positive_phase = np.exp(1j * angle_float / 2)
negative_phase = np.exp(-1j * angle_float / 2)
return np.array(
[
[negative_phase, 0, 0, 0],
[0, positive_phase, 0, 0],
[0, 0, positive_phase, 0],
[0, 0, 0, negative_phase],
],
dtype=complex,
)
@property
def gate_type(self) -> str:
return "zz"
@_from_braket_instruction.register(braket_instruction.ZZ)
def _zz(instruction) -> ZZ:
return ZZ(instruction.targets, instruction.angle)
class CCNot(GateOperation):
"""Controlled CNot or Toffoli gate"""
_matrix = np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0],
],
dtype=complex,
)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return CCNot._matrix
@property
def gate_type(self) -> str:
return "ccnot"
@_from_braket_instruction.register(braket_instruction.CCNot)
def _ccnot(instruction) -> CCNot:
return CCNot([*instruction.controls, instruction.target])
class CSwap(GateOperation):
"""Controlled Swap gate"""
_matrix = np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
],
dtype=complex,
)
def __init__(self, targets, ctrl_modifiers=(), power=1):
super().__init__(
targets=targets,
ctrl_modifiers=ctrl_modifiers,
power=power,
)
@property
def _base_matrix(self) -> np.ndarray:
return CSwap._matrix
@property
def gate_type(self) -> str:
return "cswap"