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)
26"""
27Instructions that can be supplied to the braket.ir.jaqcd.Program.
29To add a new instruction:
30 - Implement a class in this module.
31 - Class must contain a property, "type", that is an enum of the class implemented in the
32 next step.
33 - Implement a subclass, "Type", within this class that extends [str, enum].
34 All enum values must be unique across all instructions, otherwise de-serialization
35 will have undeterministic behaviors. These enums will be used to determine what type
36 the instruction is, i.e. what class to use for deserializing.
37 - NOTE: Due to how multiple inhertiance works in Python it is easiest to define a
38 type enum class within each instruction, instead of calling the relevant parent
39 constructors to initialize it correctly.
40 - Inherit any classes from braket.ir.jaqcd.shared_models.
41 - Write up docstrings to define the instruction, properties, and examples.
42"""
45class H(SingleTarget):
46 """
47 Hadamard gate.
49 Attributes:
50 type (str): The instruction type. default = "h". (type) is optional.
51 This should be unique among all instruction types.
52 target (int): The target qubit. This is an int >= 0.
54 Examples:
55 >>> H(target=1)
56 """
58 class Type(str, Enum):
59 h = "h"
61 type = Type.h
64class I(SingleTarget): # noqa: E742, E261
65 """
66 Identity gate.
68 Attributes:
69 type (str): The instruction type. default = "i". (type) is optional.
70 This should be unique among all instruction types.
71 target (int): The target qubit. This is an int >= 0.
73 Examples:
74 >>> I(target=1)
75 """
77 class Type(str, Enum):
78 i = "i"
80 type = Type.i
83class X(SingleTarget):
84 """
85 Pauli-X gate.
87 Attributes:
88 type (str): The instruction type. default = "x". (type) is optional.
89 This should be unique among all instruction types.
90 target (int): The target qubit. This is an int >= 0.
92 Examples:
93 >>> X(target=0)
94 """
96 class Type(str, Enum):
97 x = "x"
99 type = Type.x
102class Y(SingleTarget):
103 """
104 Pauli-Y gate.
106 Attributes:
107 type (str): The instruction type. default = "y". (type) is optional.
108 This should be unique among all instruction types.
109 target (int): The target qubit. This is an int >= 0.
111 Examples:
112 >>> Y(target=0)
113 """
115 class Type(str, Enum):
116 y = "y"
118 type = Type.y
121class Z(SingleTarget):
122 """
123 Pauli-Z gate.
125 Attributes:
126 type (str): The instruction type. default = "z". (type) is optional.
127 This should be unique among all instruction types.
128 target (int): The target qubit. This is an int >= 0.
130 Examples:
131 >>> Z(target=0)
132 """
134 class Type(str, Enum):
135 z = "z"
137 type = Type.z
140class Rx(SingleTarget, Angle):
141 """
142 X-axis rotation gate.
144 Attributes:
145 type (str): The instruction type. default = "rx". (type) is optional.
146 This should be unique among all instruction types.
147 target (int): The target qubit. This is an int >= 0.
148 angle (float): The angle in radians.
149 inf, -inf, and NaN are not allowable inputs.
151 Examples:
152 >>> Rx(target=0, angle=0.15)
153 """
155 class Type(str, Enum):
156 rx = "rx"
158 type = Type.rx
161class Ry(SingleTarget, Angle):
162 """
163 Y-axis rotation gate.
165 Attributes:
166 type (str): The instruction type. default = "ry". (type) is optional.
167 This should be unique among all instruction types.
168 target (int): The target qubit. This is an int >= 0.
169 angle (float): The angle in radians.
170 inf, -inf, and NaN are not allowable inputs.
172 Examples:
173 >>> Ry(target=0, angle=0.15)
174 """
176 class Type(str, Enum):
177 ry = "ry"
179 type = Type.ry
182class Rz(SingleTarget, Angle):
183 """
184 Z-axis rotation gate.
186 Attributes:
187 type (str): The instruction type. default = "rz". (type) is optional.
188 This should be unique among all instruction types.
189 target (int): The target qubit. This is an int >= 0.
190 angle (float): The angle in radians.
191 inf, -inf, and NaN are not allowable inputs.
193 Examples:
194 >>> Rz(target=0, angle=0.15)
195 """
197 class Type(str, Enum):
198 rz = "rz"
200 type = Type.rz
203class S(SingleTarget):
204 """
205 S gate. Applies a 90 degree rotation around the Z-axis.
207 Attributes:
208 type (str): The instruction type. default = "s". (type) is optional.
209 This should be unique among all instruction types.
210 target (int): The target qubit. This is an int >= 0.
212 Examples:
213 >>> S(target=0)
214 """
216 class Type(str, Enum):
217 s = "s"
219 type = Type.s
222class T(SingleTarget):
223 """
224 T gate. Applies a 45 degree rotation around the Z-axis.
226 Attributes:
227 type (str): The instruction type. default = "t". (type) is optional.
228 This should be unique among all instruction types.
229 target (int): The target qubit. This is an int >= 0.
231 Examples:
232 >>> T(target=0)
233 """
235 class Type(str, Enum):
236 t = "t"
238 type = Type.t
241class Si(SingleTarget):
242 """
243 Si gate. Conjugate transpose of S gate.
245 Attributes:
246 type (str): The instruction type. default = "si". (type) is optional.
247 This should be unique among all instruction types.
248 target (int): The target qubit. This is an int >= 0.
250 Examples:
251 >>> Si(target=0)
252 """
254 class Type(str, Enum):
255 si = "si"
257 type = Type.si
260class Ti(SingleTarget):
261 """
262 Ti gate. Conjugate transpose of T gate.
264 Attributes:
265 type (str): The instruction type. default = "ti". (type) is optional.
266 This should be unique among all instruction types.
267 target (int): The target qubit. This is an int >= 0.
269 Examples:
270 >>> Ti(target=0)
271 """
273 class Type(str, Enum):
274 ti = "ti"
276 type = Type.ti
279class Swap(DoubleTarget):
280 """
281 Swap gate. Swaps the state of the two qubits.
283 Attributes:
284 type (str): The instruction type. default = "swap". (type) is optional.
285 This should be unique among all instruction types.
286 targets (List[int]): The target qubits.
287 This is a list with two items and all items are int >= 0.
289 Examples:
290 >>> Swap(targets=[0, 1])
291 """
293 class Type(str, Enum):
294 swap = "swap"
296 type = Type.swap
299class CSwap(SingleControl, DoubleTarget):
300 """
301 Controlled swap gate.
303 Attributes:
304 type (str): The instruction type. default = "cswap". (type) is optional.
305 This should be unique among all instruction types.
306 control (int): The control qubit. This is an int >= 0.
307 targets (List[int]): The target qubits.
308 This is a list with two items and all items are int >= 0.
310 Examples:
311 >>> Swap(control=0, targets=[1, 2])
312 """
314 class Type(str, Enum):
315 cswap = "cswap"
317 type = Type.cswap
320class ISwap(DoubleTarget):
321 """
322 ISwap gate. Swaps the state of two qubits, applying a -i phase to q1 when it is in the 1 state
323 and a -i phase to q2 when it is in the 0 state.
325 This is equivalent to XY(pi)
327 Attributes:
328 type (str): The instruction type. default = "iswap". (type) is optional.
329 This should be unique among all instruction types.
330 targets (List[int]): The target qubits.
331 This is a list with two items and all items are int >= 0.
333 Examples:
334 >>> ISwap(targets=[0, 1])
335 """
337 class Type(str, Enum):
338 iswap = "iswap"
340 type = Type.iswap
343class PSwap(DoubleTarget, Angle):
344 """
345 Parameterized swap gate that takes in the angle of the phase to apply to the swapped gates.
347 Attributes:
348 type (str): The instruction type. default = "pswap". (type) is optional.
349 This should be unique among all instruction types.
350 angle (float): The angle in radians.
351 inf, -inf, and NaN are not allowable inputs.
352 targets (List[int]): The target qubits.
353 This is a list with two items and all items are int >= 0.
355 Examples:
356 >>> PSwap(targets=[0, 1], angle=0.15)
357 """
359 class Type(str, Enum):
360 pswap = "pswap"
362 type = Type.pswap
365class XY(DoubleTarget, Angle):
366 """
367 Rotates between \\|01> and \\|10> by the given angle.
369 Attributes:
370 type (str): The instruction type. default = "xy". (type) is optional.
371 This should be unique among all instruction types.
372 angle (float): The angle in radians.
373 inf, -inf, and NaN are not allowable inputs.
374 targets (List[int]): The target qubits.
375 This is a list with two items and all items are int >= 0.
377 Examples:
378 >>> XY(targets=[0, 1], angle=0.15)
379 """
381 class Type(str, Enum):
382 xy = "xy"
384 type = Type.xy
387class PhaseShift(SingleTarget, Angle):
388 """
389 Phase shift gate. Shifts the phase between \\|0> and \\|1> by a given angle.
391 Attributes:
392 type (str): The instruction type. default = "phaseshift". (type) is optional.
393 This should be unique among all instruction types.
394 angle (float): The angle in radians.
395 inf, -inf, and NaN are not allowable inputs.
396 target (int): The target qubit. This is an int >= 0.
398 Examples:
399 >>> PhaseShift(target=1, angle=0.15)
400 """
402 class Type(str, Enum):
403 phaseshift = "phaseshift"
405 type = Type.phaseshift
408class CPhaseShift(SingleTarget, SingleControl, Angle):
409 """
410 Controlled phase shift gate.
412 Attributes:
413 type (str): The instruction type. default = "cphaseshift". (type) is optional.
414 This should be unique among all instruction types.
415 control (int): The control qubit. This is an int >= 0.
416 angle (float): The angle in radians.
417 inf, -inf, and NaN are not allowable inputs.
418 target (int): The target qubit. This is an int >= 0.
420 Examples:
421 >>> CPhaseShift(control=0, target=1, angle=0.15)
422 """
424 class Type(str, Enum):
425 cphaseshift = "cphaseshift"
427 type = Type.cphaseshift
430class CPhaseShift00(SingleTarget, SingleControl, Angle):
431 """
432 Controlled phase shift gate that phases the \\|00> state.
434 Attributes:
435 type (str): The instruction type. default = "cphaseshift00". (type) is optional.
436 This should be unique among all instruction types.
437 control (int): The control qubit. This is an int >= 0.
438 angle (float): The angle in radians.
439 inf, -inf, and NaN are not allowable inputs.
440 target (int): The target qubit. This is an int >= 0.
442 Examples:
443 >>> CPhaseShift00(control=0, target=1, angle=0.15)
444 """
446 class Type(str, Enum):
447 cphaseshift00 = "cphaseshift00"
449 type = Type.cphaseshift00
452class CPhaseShift01(SingleTarget, SingleControl, Angle):
453 """
454 Controlled phase shift gate that phases the \\|01> state.
456 Attributes:
457 type (str): The instruction type. default = "cphaseshift01". (type) is optional.
458 This should be unique among all instruction types.
459 control (int): The control qubit. This is an int >= 0.
460 angle (float): The angle in radians.
461 inf, -inf, and NaN are not allowable inputs.
462 target (int): The target qubit. This is an int >= 0.
464 Examples:
465 >>> CPhaseShift01(control=0, target=1, angle=0.15)
466 """
468 class Type(str, Enum):
469 cphaseshift01 = "cphaseshift01"
471 type = Type.cphaseshift01
474class CPhaseShift10(SingleTarget, SingleControl, Angle):
475 """
476 Controlled phase shift gate that phases the \\|10> state.
478 Attributes:
479 type (str): The instruction type. default = "cphaseshift10". (type) is optional.
480 This should be unique among all instruction types.
481 control (int): The control qubit. This is an int >= 0.
482 angle (float): The angle in radians.
483 inf, -inf, and NaN are not allowable inputs.
484 target (int): The target qubit. This is an int >= 0.
486 Examples:
487 >>> CPhaseShift10(control=0, target=1, angle=0.15)
488 """
490 class Type(str, Enum):
491 cphaseshift10 = "cphaseshift10"
493 type = Type.cphaseshift10
496class CNot(SingleTarget, SingleControl):
497 """
498 Controlled not gate. Also known as the CX gate.
500 Attributes:
501 type (str): The instruction type. default = "cnot". (type) is optional.
502 This should be unique among all instruction types.
503 control (int): The control qubit. This is an int >= 0.
504 target (int): The target qubit. This is an int >= 0.
506 Examples:
507 >>> CNot(control=0, target=1)
508 """
510 class Type(str, Enum):
511 cnot = "cnot"
513 type = Type.cnot
516class CCNot(SingleTarget, DoubleControl):
517 """
518 Doubly-controlled NOT gate. Also known as the Toffoli gate.
520 Attributes:
521 type (str): The instruction type. default = "ccnot". (type) is optional.
522 This should be unique among all instruction types.
523 controls (int): The control qubits.
524 This is a list with two items and all items are int >= 0.
525 target (int): The target qubit. This is an int >= 0.
527 Examples:
528 >>> CCNot(control=[0,1], target=1)
529 """
531 class Type(str, Enum):
532 ccnot = "ccnot"
534 type = Type.ccnot
537class CY(SingleTarget, SingleControl):
538 """
539 Controlled Y-gate.
541 Attributes:
542 type (str): The instruction type. default = "cy". (type) is optional.
543 This should be unique among all instruction types.
544 control (int): The control qubit
545 target (int): The target qubit. This is an int >= 0.
547 Examples:
548 >>> CY(control=0, target=1)
549 """
551 class Type(str, Enum):
552 cy = "cy"
554 type = Type.cy
557class CZ(SingleTarget, SingleControl):
558 """
559 Controlled Z-gate.
561 Attributes:
562 type (str): The instruction type. default = "cz". (type) is optional.
563 This should be unique among all instruction types.
564 control (int): The control qubit. This is an int >= 0.
565 target (int): The target qubit. This is an int >= 0.
567 Examples:
568 >>> CZ(control=0, target=1)
569 """
571 class Type(str, Enum):
572 cz = "cz"
574 type = Type.cz
577class XX(DoubleTarget, Angle):
578 """
579 The Ising (XX) gate.
581 Attributes:
582 type (str): The instruction type. default = "xx". (type) is optional.
583 This should be unique among all instruction types.
584 angle (float): The angle in radians.
585 inf, -inf, and NaN are not allowable inputs.
586 targets (List[int]): The target qubits.
587 This is a list with two items and all items are int >= 0.
589 Examples:
590 >>> XX(targets=[0, 1], angle=0.15)
591 """
593 class Type(str, Enum):
594 xx = "xx"
596 type = Type.xx
599class YY(DoubleTarget, Angle):
600 """
601 The Ising (YY) gate.
603 Attributes:
604 type (str): The instruction type. default = "yy". (type) is optional.
605 This should be unique among all instruction types.
606 angle (float): The angle in radians.
607 inf, -inf, and NaN are not allowable inputs.
608 targets (List[int]): The target qubits.
609 This is a list with two items and all items are int >= 0.
611 Examples:
612 >>> YY(targets=[0, 1], angle=0.15)
613 """
615 class Type(str, Enum):
616 yy = "yy"
618 type = Type.yy
621class ZZ(DoubleTarget, Angle):
622 """
623 The Ising (ZZ) gate.
625 Attributes:
626 type (str): The instruction type. default = "zz". (type) is optional.
627 This should be unique among all instruction types.
628 angle (float): The angle in radians.
629 inf, -inf, and NaN are not allowable inputs.
630 targets (List[int]): The target qubits.
631 This is a list with two items and all items are int >= 0.
633 Examples:
634 >>> ZZ(targets=[0, 1], angle=0.15)
635 """
637 class Type(str, Enum):
638 zz = "zz"
640 type = Type.zz
643class V(SingleTarget):
644 """
645 Square root of NOT gate.
647 Attributes:
648 type (str): The instruction type. default = "v". (type) is optional.
649 This should be unique among all instruction types.
650 target (int): The target qubit. This is an int >= 0.
652 Examples:
653 >>> V(target=0)
654 """
656 class Type(str, Enum):
657 v = "v"
659 type = Type.v
662class Vi(SingleTarget):
663 """
664 Conjugate transpose of square root of NOT gate.
666 Attributes:
667 type (str): The instruction type. default = "vi". (type) is optional.
668 This should be unique among all instruction types.
669 target (int): The target qubit. This is an int >= 0.
671 Examples:
672 >>> Vi(target=0)
673 """
675 class Type(str, Enum):
676 vi = "vi"
678 type = Type.vi
681class Unitary(TwoDimensionalMatrix, MultiTarget):
682 """
683 Arbitrary unitary matrix gate
685 Attributes:
686 type (str): The instruction type. default = "unitary". (type) is optional.
687 This should be unique among all instruction types.
688 targets (List[int]): The target qubits. This is a list with ints and all ints >= 0.
689 matrix (List[List[List[float]]]): The unitary matrix specifying the behavior of the gate.
691 Examples:
692 >>> Unitary(targets=[0], matrix=[[[0, 0], [1, 0]],[[1, 0], [0, 1]]])
693 """
695 class Type(str, Enum):
696 unitary = "unitary"
698 type = Type.unitary