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

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

28 

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

43 

44 

45class H(SingleTarget): 

46 """ 

47 Hadamard gate. 

48 

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. 

53 

54 Examples: 

55 >>> H(target=1) 

56 """ 

57 

58 class Type(str, Enum): 

59 h = "h" 

60 

61 type = Type.h 

62 

63 

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

65 """ 

66 Identity gate. 

67 

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. 

72 

73 Examples: 

74 >>> I(target=1) 

75 """ 

76 

77 class Type(str, Enum): 

78 i = "i" 

79 

80 type = Type.i 

81 

82 

83class X(SingleTarget): 

84 """ 

85 Pauli-X gate. 

86 

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. 

91 

92 Examples: 

93 >>> X(target=0) 

94 """ 

95 

96 class Type(str, Enum): 

97 x = "x" 

98 

99 type = Type.x 

100 

101 

102class Y(SingleTarget): 

103 """ 

104 Pauli-Y gate. 

105 

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. 

110 

111 Examples: 

112 >>> Y(target=0) 

113 """ 

114 

115 class Type(str, Enum): 

116 y = "y" 

117 

118 type = Type.y 

119 

120 

121class Z(SingleTarget): 

122 """ 

123 Pauli-Z gate. 

124 

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. 

129 

130 Examples: 

131 >>> Z(target=0) 

132 """ 

133 

134 class Type(str, Enum): 

135 z = "z" 

136 

137 type = Type.z 

138 

139 

140class Rx(SingleTarget, Angle): 

141 """ 

142 X-axis rotation gate. 

143 

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. 

150 

151 Examples: 

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

153 """ 

154 

155 class Type(str, Enum): 

156 rx = "rx" 

157 

158 type = Type.rx 

159 

160 

161class Ry(SingleTarget, Angle): 

162 """ 

163 Y-axis rotation gate. 

164 

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. 

171 

172 Examples: 

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

174 """ 

175 

176 class Type(str, Enum): 

177 ry = "ry" 

178 

179 type = Type.ry 

180 

181 

182class Rz(SingleTarget, Angle): 

183 """ 

184 Z-axis rotation gate. 

185 

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. 

192 

193 Examples: 

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

195 """ 

196 

197 class Type(str, Enum): 

198 rz = "rz" 

199 

200 type = Type.rz 

201 

202 

203class S(SingleTarget): 

204 """ 

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

206 

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. 

211 

212 Examples: 

213 >>> S(target=0) 

214 """ 

215 

216 class Type(str, Enum): 

217 s = "s" 

218 

219 type = Type.s 

220 

221 

222class T(SingleTarget): 

223 """ 

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

225 

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. 

230 

231 Examples: 

232 >>> T(target=0) 

233 """ 

234 

235 class Type(str, Enum): 

236 t = "t" 

237 

238 type = Type.t 

239 

240 

241class Si(SingleTarget): 

242 """ 

243 Si gate. Conjugate transpose of S gate. 

244 

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. 

249 

250 Examples: 

251 >>> Si(target=0) 

252 """ 

253 

254 class Type(str, Enum): 

255 si = "si" 

256 

257 type = Type.si 

258 

259 

260class Ti(SingleTarget): 

261 """ 

262 Ti gate. Conjugate transpose of T gate. 

263 

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. 

268 

269 Examples: 

270 >>> Ti(target=0) 

271 """ 

272 

273 class Type(str, Enum): 

274 ti = "ti" 

275 

276 type = Type.ti 

277 

278 

279class Swap(DoubleTarget): 

280 """ 

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

282 

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. 

288 

289 Examples: 

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

291 """ 

292 

293 class Type(str, Enum): 

294 swap = "swap" 

295 

296 type = Type.swap 

297 

298 

299class CSwap(SingleControl, DoubleTarget): 

300 """ 

301 Controlled swap gate. 

302 

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. 

309 

310 Examples: 

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

312 """ 

313 

314 class Type(str, Enum): 

315 cswap = "cswap" 

316 

317 type = Type.cswap 

318 

319 

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. 

324 

325 This is equivalent to XY(pi) 

326 

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. 

332 

333 Examples: 

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

335 """ 

336 

337 class Type(str, Enum): 

338 iswap = "iswap" 

339 

340 type = Type.iswap 

341 

342 

343class PSwap(DoubleTarget, Angle): 

344 """ 

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

346 

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. 

354 

355 Examples: 

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

357 """ 

358 

359 class Type(str, Enum): 

360 pswap = "pswap" 

361 

362 type = Type.pswap 

363 

364 

365class XY(DoubleTarget, Angle): 

366 """ 

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

368 

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. 

376 

377 Examples: 

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

379 """ 

380 

381 class Type(str, Enum): 

382 xy = "xy" 

383 

384 type = Type.xy 

385 

386 

387class PhaseShift(SingleTarget, Angle): 

388 """ 

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

390 

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. 

397 

398 Examples: 

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

400 """ 

401 

402 class Type(str, Enum): 

403 phaseshift = "phaseshift" 

404 

405 type = Type.phaseshift 

406 

407 

408class CPhaseShift(SingleTarget, SingleControl, Angle): 

409 """ 

410 Controlled phase shift gate. 

411 

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. 

419 

420 Examples: 

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

422 """ 

423 

424 class Type(str, Enum): 

425 cphaseshift = "cphaseshift" 

426 

427 type = Type.cphaseshift 

428 

429 

430class CPhaseShift00(SingleTarget, SingleControl, Angle): 

431 """ 

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

433 

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. 

441 

442 Examples: 

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

444 """ 

445 

446 class Type(str, Enum): 

447 cphaseshift00 = "cphaseshift00" 

448 

449 type = Type.cphaseshift00 

450 

451 

452class CPhaseShift01(SingleTarget, SingleControl, Angle): 

453 """ 

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

455 

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. 

463 

464 Examples: 

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

466 """ 

467 

468 class Type(str, Enum): 

469 cphaseshift01 = "cphaseshift01" 

470 

471 type = Type.cphaseshift01 

472 

473 

474class CPhaseShift10(SingleTarget, SingleControl, Angle): 

475 """ 

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

477 

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. 

485 

486 Examples: 

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

488 """ 

489 

490 class Type(str, Enum): 

491 cphaseshift10 = "cphaseshift10" 

492 

493 type = Type.cphaseshift10 

494 

495 

496class CNot(SingleTarget, SingleControl): 

497 """ 

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

499 

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. 

505 

506 Examples: 

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

508 """ 

509 

510 class Type(str, Enum): 

511 cnot = "cnot" 

512 

513 type = Type.cnot 

514 

515 

516class CCNot(SingleTarget, DoubleControl): 

517 """ 

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

519 

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. 

526 

527 Examples: 

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

529 """ 

530 

531 class Type(str, Enum): 

532 ccnot = "ccnot" 

533 

534 type = Type.ccnot 

535 

536 

537class CY(SingleTarget, SingleControl): 

538 """ 

539 Controlled Y-gate. 

540 

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. 

546 

547 Examples: 

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

549 """ 

550 

551 class Type(str, Enum): 

552 cy = "cy" 

553 

554 type = Type.cy 

555 

556 

557class CZ(SingleTarget, SingleControl): 

558 """ 

559 Controlled Z-gate. 

560 

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. 

566 

567 Examples: 

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

569 """ 

570 

571 class Type(str, Enum): 

572 cz = "cz" 

573 

574 type = Type.cz 

575 

576 

577class XX(DoubleTarget, Angle): 

578 """ 

579 The Ising (XX) gate. 

580 

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. 

588 

589 Examples: 

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

591 """ 

592 

593 class Type(str, Enum): 

594 xx = "xx" 

595 

596 type = Type.xx 

597 

598 

599class YY(DoubleTarget, Angle): 

600 """ 

601 The Ising (YY) gate. 

602 

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. 

610 

611 Examples: 

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

613 """ 

614 

615 class Type(str, Enum): 

616 yy = "yy" 

617 

618 type = Type.yy 

619 

620 

621class ZZ(DoubleTarget, Angle): 

622 """ 

623 The Ising (ZZ) gate. 

624 

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. 

632 

633 Examples: 

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

635 """ 

636 

637 class Type(str, Enum): 

638 zz = "zz" 

639 

640 type = Type.zz 

641 

642 

643class V(SingleTarget): 

644 """ 

645 Square root of NOT gate. 

646 

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. 

651 

652 Examples: 

653 >>> V(target=0) 

654 """ 

655 

656 class Type(str, Enum): 

657 v = "v" 

658 

659 type = Type.v 

660 

661 

662class Vi(SingleTarget): 

663 """ 

664 Conjugate transpose of square root of NOT gate. 

665 

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. 

670 

671 Examples: 

672 >>> Vi(target=0) 

673 """ 

674 

675 class Type(str, Enum): 

676 vi = "vi" 

677 

678 type = Type.vi 

679 

680 

681class Unitary(TwoDimensionalMatrix, MultiTarget): 

682 """ 

683 Arbitrary unitary matrix gate 

684 

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. 

690 

691 Examples: 

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

693 """ 

694 

695 class Type(str, Enum): 

696 unitary = "unitary" 

697 

698 type = Type.unitary