Coverage for src/braket/ir/jaqcd/instructions.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
14from enum import Enum
16from braket.ir.jaqcd.shared_models import (
17 Angle,
18 DoubleControl,
19 DoubleTarget,
20 MultiTarget,
21 SingleControl,
22 SingleTarget,
23 TwoDimensionalMatrix,
24)
27"""
28Instructions that can be supplied to the braket.ir.jaqcd.Program.
30To add a new instruction:
31 - Implement a class in this module.
32 - Class must contain a property, "type", that is an enum of the class implemented in the
33 next step.
34 - Implement a subclass, "Type", within this class that extends [str, enum].
35 All enum values must be unique across all instructions, otherwise de-serialization
36 will have undeterministic behaviors. These enums will be used to determine what type
37 the instruction is, i.e. what class to use for deserializing.
38 - NOTE: Due to how multiple inhertiance works in Python it is easiest to define a
39 type enum class within each instruction, instead of calling the relevant parent
40 constructors to initialize it correctly.
41 - Inherit any classes from braket.ir.jaqcd.shared_models.
42 - Write up docstrings to define the instruction, properties, and examples.
43"""
46class H(SingleTarget):
47 """
48 Hadamard gate.
50 Attributes:
51 type (str): The instruction type. default = "h". (type) is optional.
52 This should be unique among all instruction types.
53 target (int): The target qubit. This is an int >= 0.
55 Examples:
56 >>> H(target=1)
57 """
59 class Type(str, Enum):
60 h = "h"
62 type = Type.h
65class I(SingleTarget): # noqa: E742, E261
66 """
67 Identity gate.
69 Attributes:
70 type (str): The instruction type. default = "i". (type) is optional.
71 This should be unique among all instruction types.
72 target (int): The target qubit. This is an int >= 0.
74 Examples:
75 >>> I(target=1)
76 """
78 class Type(str, Enum):
79 i = "i"
81 type = Type.i
84class X(SingleTarget):
85 """
86 Pauli-X gate.
88 Attributes:
89 type (str): The instruction type. default = "x". (type) is optional.
90 This should be unique among all instruction types.
91 target (int): The target qubit. This is an int >= 0.
93 Examples:
94 >>> X(target=0)
95 """
97 class Type(str, Enum):
98 x = "x"
100 type = Type.x
103class Y(SingleTarget):
104 """
105 Pauli-Y gate.
107 Attributes:
108 type (str): The instruction type. default = "y". (type) is optional.
109 This should be unique among all instruction types.
110 target (int): The target qubit. This is an int >= 0.
112 Examples:
113 >>> Y(target=0)
114 """
116 class Type(str, Enum):
117 y = "y"
119 type = Type.y
122class Z(SingleTarget):
123 """
124 Pauli-Z gate.
126 Attributes:
127 type (str): The instruction type. default = "z". (type) is optional.
128 This should be unique among all instruction types.
129 target (int): The target qubit. This is an int >= 0.
131 Examples:
132 >>> Z(target=0)
133 """
135 class Type(str, Enum):
136 z = "z"
138 type = Type.z
141class Rx(SingleTarget, Angle):
142 """
143 X-axis rotation gate.
145 Attributes:
146 type (str): The instruction type. default = "rx". (type) is optional.
147 This should be unique among all instruction types.
148 target (int): The target qubit. This is an int >= 0.
149 angle (float): The angle in radians.
150 inf, -inf, and NaN are not allowable inputs.
152 Examples:
153 >>> Rx(target=0, angle=0.15)
154 """
156 class Type(str, Enum):
157 rx = "rx"
159 type = Type.rx
162class Ry(SingleTarget, Angle):
163 """
164 Y-axis rotation gate.
166 Attributes:
167 type (str): The instruction type. default = "ry". (type) is optional.
168 This should be unique among all instruction types.
169 target (int): The target qubit. This is an int >= 0.
170 angle (float): The angle in radians.
171 inf, -inf, and NaN are not allowable inputs.
173 Examples:
174 >>> Ry(target=0, angle=0.15)
175 """
177 class Type(str, Enum):
178 ry = "ry"
180 type = Type.ry
183class Rz(SingleTarget, Angle):
184 """
185 Z-axis rotation gate.
187 Attributes:
188 type (str): The instruction type. default = "rz". (type) is optional.
189 This should be unique among all instruction types.
190 target (int): The target qubit. This is an int >= 0.
191 angle (float): The angle in radians.
192 inf, -inf, and NaN are not allowable inputs.
194 Examples:
195 >>> Rz(target=0, angle=0.15)
196 """
198 class Type(str, Enum):
199 rz = "rz"
201 type = Type.rz
204class S(SingleTarget):
205 """
206 S gate. Applies a 90 degree rotation around the Z-axis.
208 Attributes:
209 type (str): The instruction type. default = "s". (type) is optional.
210 This should be unique among all instruction types.
211 target (int): The target qubit. This is an int >= 0.
213 Examples:
214 >>> S(target=0)
215 """
217 class Type(str, Enum):
218 s = "s"
220 type = Type.s
223class T(SingleTarget):
224 """
225 T gate. Applies a 45 degree rotation around the Z-axis.
227 Attributes:
228 type (str): The instruction type. default = "t". (type) is optional.
229 This should be unique among all instruction types.
230 target (int): The target qubit. This is an int >= 0.
232 Examples:
233 >>> T(target=0)
234 """
236 class Type(str, Enum):
237 t = "t"
239 type = Type.t
242class Si(SingleTarget):
243 """
244 Si gate. Conjugate transpose of S gate.
246 Attributes:
247 type (str): The instruction type. default = "si". (type) is optional.
248 This should be unique among all instruction types.
249 target (int): The target qubit. This is an int >= 0.
251 Examples:
252 >>> Si(target=0)
253 """
255 class Type(str, Enum):
256 si = "si"
258 type = Type.si
261class Ti(SingleTarget):
262 """
263 Ti gate. Conjugate transpose of T gate.
265 Attributes:
266 type (str): The instruction type. default = "ti". (type) is optional.
267 This should be unique among all instruction types.
268 target (int): The target qubit. This is an int >= 0.
270 Examples:
271 >>> Ti(target=0)
272 """
274 class Type(str, Enum):
275 ti = "ti"
277 type = Type.ti
280class Swap(DoubleTarget):
281 """
282 Swap gate. Swaps the state of the two qubits.
284 Attributes:
285 type (str): The instruction type. default = "swap". (type) is optional.
286 This should be unique among all instruction types.
287 targets (List[int]): The target qubits.
288 This is a list with two items and all items are int >= 0.
290 Examples:
291 >>> Swap(targets=[0, 1])
292 """
294 class Type(str, Enum):
295 swap = "swap"
297 type = Type.swap
300class CSwap(SingleControl, DoubleTarget):
301 """
302 Controlled swap gate.
304 Attributes:
305 type (str): The instruction type. default = "cswap". (type) is optional.
306 This should be unique among all instruction types.
307 control (int): The control qubit. This is an int >= 0.
308 targets (List[int]): The target qubits.
309 This is a list with two items and all items are int >= 0.
311 Examples:
312 >>> Swap(control=0, targets=[1, 2])
313 """
315 class Type(str, Enum):
316 cswap = "cswap"
318 type = Type.cswap
321class ISwap(DoubleTarget):
322 """
323 ISwap gate. Swaps the state of two qubits, applying a -i phase to q1 when it is in the 1 state
324 and a -i phase to q2 when it is in the 0 state.
326 This is equivalent to XY(pi)
328 Attributes:
329 type (str): The instruction type. default = "iswap". (type) is optional.
330 This should be unique among all instruction types.
331 targets (List[int]): The target qubits.
332 This is a list with two items and all items are int >= 0.
334 Examples:
335 >>> ISwap(targets=[0, 1])
336 """
338 class Type(str, Enum):
339 iswap = "iswap"
341 type = Type.iswap
344class PSwap(DoubleTarget, Angle):
345 """
346 Parameterized swap gate that takes in the angle of the phase to apply to the swapped gates.
348 Attributes:
349 type (str): The instruction type. default = "pswap". (type) is optional.
350 This should be unique among all instruction types.
351 angle (float): The angle in radians.
352 inf, -inf, and NaN are not allowable inputs.
353 targets (List[int]): The target qubits.
354 This is a list with two items and all items are int >= 0.
356 Examples:
357 >>> PSwap(targets=[0, 1], angle=0.15)
358 """
360 class Type(str, Enum):
361 pswap = "pswap"
363 type = Type.pswap
366class XY(DoubleTarget, Angle):
367 """
368 Rotates between \\|01> and \\|10> by the given angle.
370 Attributes:
371 type (str): The instruction type. default = "xy". (type) is optional.
372 This should be unique among all instruction types.
373 angle (float): The angle in radians.
374 inf, -inf, and NaN are not allowable inputs.
375 targets (List[int]): The target qubits.
376 This is a list with two items and all items are int >= 0.
378 Examples:
379 >>> XY(targets=[0, 1], angle=0.15)
380 """
382 class Type(str, Enum):
383 xy = "xy"
385 type = Type.xy
388class PhaseShift(SingleTarget, Angle):
389 """
390 Phase shift gate. Shifts the phase between \\|0> and \\|1> by a given angle.
392 Attributes:
393 type (str): The instruction type. default = "phaseshift". (type) is optional.
394 This should be unique among all instruction types.
395 angle (float): The angle in radians.
396 inf, -inf, and NaN are not allowable inputs.
397 target (int): The target qubit. This is an int >= 0.
399 Examples:
400 >>> PhaseShift(target=1, angle=0.15)
401 """
403 class Type(str, Enum):
404 phaseshift = "phaseshift"
406 type = Type.phaseshift
409class CPhaseShift(SingleTarget, SingleControl, Angle):
410 """
411 Controlled phase shift gate.
413 Attributes:
414 type (str): The instruction type. default = "cphaseshift". (type) is optional.
415 This should be unique among all instruction types.
416 control (int): The control qubit. This is an int >= 0.
417 angle (float): The angle in radians.
418 inf, -inf, and NaN are not allowable inputs.
419 target (int): The target qubit. This is an int >= 0.
421 Examples:
422 >>> CPhaseShift(control=0, target=1, angle=0.15)
423 """
425 class Type(str, Enum):
426 cphaseshift = "cphaseshift"
428 type = Type.cphaseshift
431class CPhaseShift00(SingleTarget, SingleControl, Angle):
432 """
433 Controlled phase shift gate that phases the \\|00> state.
435 Attributes:
436 type (str): The instruction type. default = "cphaseshift00". (type) is optional.
437 This should be unique among all instruction types.
438 control (int): The control qubit. This is an int >= 0.
439 angle (float): The angle in radians.
440 inf, -inf, and NaN are not allowable inputs.
441 target (int): The target qubit. This is an int >= 0.
443 Examples:
444 >>> CPhaseShift00(control=0, target=1, angle=0.15)
445 """
447 class Type(str, Enum):
448 cphaseshift00 = "cphaseshift00"
450 type = Type.cphaseshift00
453class CPhaseShift01(SingleTarget, SingleControl, Angle):
454 """
455 Controlled phase shift gate that phases the \\|01> state.
457 Attributes:
458 type (str): The instruction type. default = "cphaseshift01". (type) is optional.
459 This should be unique among all instruction types.
460 control (int): The control qubit. This is an int >= 0.
461 angle (float): The angle in radians.
462 inf, -inf, and NaN are not allowable inputs.
463 target (int): The target qubit. This is an int >= 0.
465 Examples:
466 >>> CPhaseShift01(control=0, target=1, angle=0.15)
467 """
469 class Type(str, Enum):
470 cphaseshift01 = "cphaseshift01"
472 type = Type.cphaseshift01
475class CPhaseShift10(SingleTarget, SingleControl, Angle):
476 """
477 Controlled phase shift gate that phases the \\|10> state.
479 Attributes:
480 type (str): The instruction type. default = "cphaseshift10". (type) is optional.
481 This should be unique among all instruction types.
482 control (int): The control qubit. This is an int >= 0.
483 angle (float): The angle in radians.
484 inf, -inf, and NaN are not allowable inputs.
485 target (int): The target qubit. This is an int >= 0.
487 Examples:
488 >>> CPhaseShift10(control=0, target=1, angle=0.15)
489 """
491 class Type(str, Enum):
492 cphaseshift10 = "cphaseshift10"
494 type = Type.cphaseshift10
497class CNot(SingleTarget, SingleControl):
498 """
499 Controlled not gate. Also known as the CX gate.
501 Attributes:
502 type (str): The instruction type. default = "cnot". (type) is optional.
503 This should be unique among all instruction types.
504 control (int): The control qubit. This is an int >= 0.
505 target (int): The target qubit. This is an int >= 0.
507 Examples:
508 >>> CNot(control=0, target=1)
509 """
511 class Type(str, Enum):
512 cnot = "cnot"
514 type = Type.cnot
517class CCNot(SingleTarget, DoubleControl):
518 """
519 Doubly-controlled NOT gate. Also known as the Toffoli gate.
521 Attributes:
522 type (str): The instruction type. default = "ccnot". (type) is optional.
523 This should be unique among all instruction types.
524 controls (int): The control qubits.
525 This is a list with two items and all items are int >= 0.
526 target (int): The target qubit. This is an int >= 0.
528 Examples:
529 >>> CCNot(control=[0,1], target=1)
530 """
532 class Type(str, Enum):
533 ccnot = "ccnot"
535 type = Type.ccnot
538class CY(SingleTarget, SingleControl):
539 """
540 Controlled Y-gate.
542 Attributes:
543 type (str): The instruction type. default = "cy". (type) is optional.
544 This should be unique among all instruction types.
545 control (int): The control qubit
546 target (int): The target qubit. This is an int >= 0.
548 Examples:
549 >>> CY(control=0, target=1)
550 """
552 class Type(str, Enum):
553 cy = "cy"
555 type = Type.cy
558class CZ(SingleTarget, SingleControl):
559 """
560 Controlled Z-gate.
562 Attributes:
563 type (str): The instruction type. default = "cz". (type) is optional.
564 This should be unique among all instruction types.
565 control (int): The control qubit. This is an int >= 0.
566 target (int): The target qubit. This is an int >= 0.
568 Examples:
569 >>> CZ(control=0, target=1)
570 """
572 class Type(str, Enum):
573 cz = "cz"
575 type = Type.cz
578class XX(DoubleTarget, Angle):
579 """
580 The Ising (XX) gate.
582 Attributes:
583 type (str): The instruction type. default = "xx". (type) is optional.
584 This should be unique among all instruction types.
585 angle (float): The angle in radians.
586 inf, -inf, and NaN are not allowable inputs.
587 targets (List[int]): The target qubits.
588 This is a list with two items and all items are int >= 0.
590 Examples:
591 >>> XX(targets=[0, 1], angle=0.15)
592 """
594 class Type(str, Enum):
595 xx = "xx"
597 type = Type.xx
600class YY(DoubleTarget, Angle):
601 """
602 The Ising (YY) gate.
604 Attributes:
605 type (str): The instruction type. default = "yy". (type) is optional.
606 This should be unique among all instruction types.
607 angle (float): The angle in radians.
608 inf, -inf, and NaN are not allowable inputs.
609 targets (List[int]): The target qubits.
610 This is a list with two items and all items are int >= 0.
612 Examples:
613 >>> YY(targets=[0, 1], angle=0.15)
614 """
616 class Type(str, Enum):
617 yy = "yy"
619 type = Type.yy
622class ZZ(DoubleTarget, Angle):
623 """
624 The Ising (ZZ) gate.
626 Attributes:
627 type (str): The instruction type. default = "zz". (type) is optional.
628 This should be unique among all instruction types.
629 angle (float): The angle in radians.
630 inf, -inf, and NaN are not allowable inputs.
631 targets (List[int]): The target qubits.
632 This is a list with two items and all items are int >= 0.
634 Examples:
635 >>> ZZ(targets=[0, 1], angle=0.15)
636 """
638 class Type(str, Enum):
639 zz = "zz"
641 type = Type.zz
644class V(SingleTarget):
645 """
646 Square root of NOT gate.
648 Attributes:
649 type (str): The instruction type. default = "v". (type) is optional.
650 This should be unique among all instruction types.
651 target (int): The target qubit. This is an int >= 0.
653 Examples:
654 >>> V(target=0)
655 """
657 class Type(str, Enum):
658 v = "v"
660 type = Type.v
663class Vi(SingleTarget):
664 """
665 Conjugate transpose of square root of NOT gate.
667 Attributes:
668 type (str): The instruction type. default = "vi". (type) is optional.
669 This should be unique among all instruction types.
670 target (int): The target qubit. This is an int >= 0.
672 Examples:
673 >>> Vi(target=0)
674 """
676 class Type(str, Enum):
677 vi = "vi"
679 type = Type.vi
682class Unitary(TwoDimensionalMatrix, MultiTarget):
683 """
684 Arbitrary unitary matrix gate
686 Attributes:
687 type (str): The instruction type. default = "unitary". (type) is optional.
688 This should be unique among all instruction types.
689 targets (List[int]): The target qubits. This is a list with ints and all ints >= 0.
690 matrix (List[List[List[float]]]): The unitary matrix specifying the behavior of the gate.
692 Examples:
693 >>> Unitary(targets=[0], matrix=[[[0, 0], [1, 0]],[[1, 0], [0, 1]]])
694 """
696 class Type(str, Enum):
697 unitary = "unitary"
699 type = Type.unitary