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 typing import Union 

15 

16QubitInput = Union["Qubit", int] 

17 

18 

19class Qubit(int): 

20 """ 

21 A quantum bit index. The index of this qubit is locally scoped towards the contained 

22 circuit. This may not be the exact qubit index on the quantum device. 

23 """ 

24 

25 def __new__(cls, index: int): 

26 """ 

27 Args: 

28 index (int): Index of the qubit. 

29 

30 Raises: 

31 ValueError: If `index` is less than zero. 

32 

33 Examples: 

34 >>> Qubit(0) 

35 >>> Qubit(1) 

36 """ 

37 if index < 0: 

38 raise ValueError(f"Supplied index, {index}, cannot be less than zero.") 

39 return super().__new__(cls, index) 

40 

41 def __repr__(self): 

42 return f"Qubit({super().__repr__()})" 

43 

44 def __str__(self): 

45 return self.__repr__() 

46 

47 @staticmethod 

48 def new(qubit: QubitInput) -> "Qubit": 

49 """ 

50 Helper constructor - if input is a Qubit it returns the same value, 

51 else a new Qubit is constructed. 

52 

53 Args: 

54 qubit (int or Qubit): Qubit index. If type == Qubit then the `qubit` is returned. 

55 """ 

56 

57 if isinstance(qubit, Qubit): 

58 return qubit 

59 else: 

60 return Qubit(qubit)