Hide keyboard shortcuts

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. 

13 

14from enum import Enum 

15 

16from braket.ir.jaqcd.shared_models import ( 

17 Angle, 

18 DoubleControl, 

19 DoubleTarget, 

20 MultiTarget, 

21 SingleControl, 

22 SingleTarget, 

23 TwoDimensionalMatrix, 

24) 

25 

26 

27""" 

28Instructions that can be supplied to the braket.ir.jaqcd.Program. 

29 

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""" 

44 

45 

46class H(SingleTarget): 

47 """ 

48 Hadamard gate. 

49 

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. 

54 

55 Examples: 

56 >>> H(target=1) 

57 """ 

58 

59 class Type(str, Enum): 

60 h = "h" 

61 

62 type = Type.h 

63 

64 

65class I(SingleTarget): # noqa: E742, E261 

66 """ 

67 Identity gate. 

68 

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. 

73 

74 Examples: 

75 >>> I(target=1) 

76 """ 

77 

78 class Type(str, Enum): 

79 i = "i" 

80 

81 type = Type.i 

82 

83 

84class X(SingleTarget): 

85 """ 

86 Pauli-X gate. 

87 

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. 

92 

93 Examples: 

94 >>> X(target=0) 

95 """ 

96 

97 class Type(str, Enum): 

98 x = "x" 

99 

100 type = Type.x 

101 

102 

103class Y(SingleTarget): 

104 """ 

105 Pauli-Y gate. 

106 

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. 

111 

112 Examples: 

113 >>> Y(target=0) 

114 """ 

115 

116 class Type(str, Enum): 

117 y = "y" 

118 

119 type = Type.y 

120 

121 

122class Z(SingleTarget): 

123 """ 

124 Pauli-Z gate. 

125 

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. 

130 

131 Examples: 

132 >>> Z(target=0) 

133 """ 

134 

135 class Type(str, Enum): 

136 z = "z" 

137 

138 type = Type.z 

139 

140 

141class Rx(SingleTarget, Angle): 

142 """ 

143 X-axis rotation gate. 

144 

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. 

151 

152 Examples: 

153 >>> Rx(target=0, angle=0.15) 

154 """ 

155 

156 class Type(str, Enum): 

157 rx = "rx" 

158 

159 type = Type.rx 

160 

161 

162class Ry(SingleTarget, Angle): 

163 """ 

164 Y-axis rotation gate. 

165 

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. 

172 

173 Examples: 

174 >>> Ry(target=0, angle=0.15) 

175 """ 

176 

177 class Type(str, Enum): 

178 ry = "ry" 

179 

180 type = Type.ry 

181 

182 

183class Rz(SingleTarget, Angle): 

184 """ 

185 Z-axis rotation gate. 

186 

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. 

193 

194 Examples: 

195 >>> Rz(target=0, angle=0.15) 

196 """ 

197 

198 class Type(str, Enum): 

199 rz = "rz" 

200 

201 type = Type.rz 

202 

203 

204class S(SingleTarget): 

205 """ 

206 S gate. Applies a 90 degree rotation around the Z-axis. 

207 

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. 

212 

213 Examples: 

214 >>> S(target=0) 

215 """ 

216 

217 class Type(str, Enum): 

218 s = "s" 

219 

220 type = Type.s 

221 

222 

223class T(SingleTarget): 

224 """ 

225 T gate. Applies a 45 degree rotation around the Z-axis. 

226 

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. 

231 

232 Examples: 

233 >>> T(target=0) 

234 """ 

235 

236 class Type(str, Enum): 

237 t = "t" 

238 

239 type = Type.t 

240 

241 

242class Si(SingleTarget): 

243 """ 

244 Si gate. Conjugate transpose of S gate. 

245 

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. 

250 

251 Examples: 

252 >>> Si(target=0) 

253 """ 

254 

255 class Type(str, Enum): 

256 si = "si" 

257 

258 type = Type.si 

259 

260 

261class Ti(SingleTarget): 

262 """ 

263 Ti gate. Conjugate transpose of T gate. 

264 

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. 

269 

270 Examples: 

271 >>> Ti(target=0) 

272 """ 

273 

274 class Type(str, Enum): 

275 ti = "ti" 

276 

277 type = Type.ti 

278 

279 

280class Swap(DoubleTarget): 

281 """ 

282 Swap gate. Swaps the state of the two qubits. 

283 

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. 

289 

290 Examples: 

291 >>> Swap(targets=[0, 1]) 

292 """ 

293 

294 class Type(str, Enum): 

295 swap = "swap" 

296 

297 type = Type.swap 

298 

299 

300class CSwap(SingleControl, DoubleTarget): 

301 """ 

302 Controlled swap gate. 

303 

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. 

310 

311 Examples: 

312 >>> Swap(control=0, targets=[1, 2]) 

313 """ 

314 

315 class Type(str, Enum): 

316 cswap = "cswap" 

317 

318 type = Type.cswap 

319 

320 

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. 

325 

326 This is equivalent to XY(pi) 

327 

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. 

333 

334 Examples: 

335 >>> ISwap(targets=[0, 1]) 

336 """ 

337 

338 class Type(str, Enum): 

339 iswap = "iswap" 

340 

341 type = Type.iswap 

342 

343 

344class PSwap(DoubleTarget, Angle): 

345 """ 

346 Parameterized swap gate that takes in the angle of the phase to apply to the swapped gates. 

347 

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. 

355 

356 Examples: 

357 >>> PSwap(targets=[0, 1], angle=0.15) 

358 """ 

359 

360 class Type(str, Enum): 

361 pswap = "pswap" 

362 

363 type = Type.pswap 

364 

365 

366class XY(DoubleTarget, Angle): 

367 """ 

368 Rotates between \\|01> and \\|10> by the given angle. 

369 

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. 

377 

378 Examples: 

379 >>> XY(targets=[0, 1], angle=0.15) 

380 """ 

381 

382 class Type(str, Enum): 

383 xy = "xy" 

384 

385 type = Type.xy 

386 

387 

388class PhaseShift(SingleTarget, Angle): 

389 """ 

390 Phase shift gate. Shifts the phase between \\|0> and \\|1> by a given angle. 

391 

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. 

398 

399 Examples: 

400 >>> PhaseShift(target=1, angle=0.15) 

401 """ 

402 

403 class Type(str, Enum): 

404 phaseshift = "phaseshift" 

405 

406 type = Type.phaseshift 

407 

408 

409class CPhaseShift(SingleTarget, SingleControl, Angle): 

410 """ 

411 Controlled phase shift gate. 

412 

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. 

420 

421 Examples: 

422 >>> CPhaseShift(control=0, target=1, angle=0.15) 

423 """ 

424 

425 class Type(str, Enum): 

426 cphaseshift = "cphaseshift" 

427 

428 type = Type.cphaseshift 

429 

430 

431class CPhaseShift00(SingleTarget, SingleControl, Angle): 

432 """ 

433 Controlled phase shift gate that phases the \\|00> state. 

434 

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. 

442 

443 Examples: 

444 >>> CPhaseShift00(control=0, target=1, angle=0.15) 

445 """ 

446 

447 class Type(str, Enum): 

448 cphaseshift00 = "cphaseshift00" 

449 

450 type = Type.cphaseshift00 

451 

452 

453class CPhaseShift01(SingleTarget, SingleControl, Angle): 

454 """ 

455 Controlled phase shift gate that phases the \\|01> state. 

456 

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. 

464 

465 Examples: 

466 >>> CPhaseShift01(control=0, target=1, angle=0.15) 

467 """ 

468 

469 class Type(str, Enum): 

470 cphaseshift01 = "cphaseshift01" 

471 

472 type = Type.cphaseshift01 

473 

474 

475class CPhaseShift10(SingleTarget, SingleControl, Angle): 

476 """ 

477 Controlled phase shift gate that phases the \\|10> state. 

478 

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. 

486 

487 Examples: 

488 >>> CPhaseShift10(control=0, target=1, angle=0.15) 

489 """ 

490 

491 class Type(str, Enum): 

492 cphaseshift10 = "cphaseshift10" 

493 

494 type = Type.cphaseshift10 

495 

496 

497class CNot(SingleTarget, SingleControl): 

498 """ 

499 Controlled not gate. Also known as the CX gate. 

500 

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. 

506 

507 Examples: 

508 >>> CNot(control=0, target=1) 

509 """ 

510 

511 class Type(str, Enum): 

512 cnot = "cnot" 

513 

514 type = Type.cnot 

515 

516 

517class CCNot(SingleTarget, DoubleControl): 

518 """ 

519 Doubly-controlled NOT gate. Also known as the Toffoli gate. 

520 

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. 

527 

528 Examples: 

529 >>> CCNot(control=[0,1], target=1) 

530 """ 

531 

532 class Type(str, Enum): 

533 ccnot = "ccnot" 

534 

535 type = Type.ccnot 

536 

537 

538class CY(SingleTarget, SingleControl): 

539 """ 

540 Controlled Y-gate. 

541 

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. 

547 

548 Examples: 

549 >>> CY(control=0, target=1) 

550 """ 

551 

552 class Type(str, Enum): 

553 cy = "cy" 

554 

555 type = Type.cy 

556 

557 

558class CZ(SingleTarget, SingleControl): 

559 """ 

560 Controlled Z-gate. 

561 

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. 

567 

568 Examples: 

569 >>> CZ(control=0, target=1) 

570 """ 

571 

572 class Type(str, Enum): 

573 cz = "cz" 

574 

575 type = Type.cz 

576 

577 

578class XX(DoubleTarget, Angle): 

579 """ 

580 The Ising (XX) gate. 

581 

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. 

589 

590 Examples: 

591 >>> XX(targets=[0, 1], angle=0.15) 

592 """ 

593 

594 class Type(str, Enum): 

595 xx = "xx" 

596 

597 type = Type.xx 

598 

599 

600class YY(DoubleTarget, Angle): 

601 """ 

602 The Ising (YY) gate. 

603 

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. 

611 

612 Examples: 

613 >>> YY(targets=[0, 1], angle=0.15) 

614 """ 

615 

616 class Type(str, Enum): 

617 yy = "yy" 

618 

619 type = Type.yy 

620 

621 

622class ZZ(DoubleTarget, Angle): 

623 """ 

624 The Ising (ZZ) gate. 

625 

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. 

633 

634 Examples: 

635 >>> ZZ(targets=[0, 1], angle=0.15) 

636 """ 

637 

638 class Type(str, Enum): 

639 zz = "zz" 

640 

641 type = Type.zz 

642 

643 

644class V(SingleTarget): 

645 """ 

646 Square root of NOT gate. 

647 

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. 

652 

653 Examples: 

654 >>> V(target=0) 

655 """ 

656 

657 class Type(str, Enum): 

658 v = "v" 

659 

660 type = Type.v 

661 

662 

663class Vi(SingleTarget): 

664 """ 

665 Conjugate transpose of square root of NOT gate. 

666 

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. 

671 

672 Examples: 

673 >>> Vi(target=0) 

674 """ 

675 

676 class Type(str, Enum): 

677 vi = "vi" 

678 

679 type = Type.vi 

680 

681 

682class Unitary(TwoDimensionalMatrix, MultiTarget): 

683 """ 

684 Arbitrary unitary matrix gate 

685 

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. 

691 

692 Examples: 

693 >>> Unitary(targets=[0], matrix=[[[0, 0], [1, 0]],[[1, 0], [0, 1]]]) 

694 """ 

695 

696 class Type(str, Enum): 

697 unitary = "unitary" 

698 

699 type = Type.unitary