Coverage for src/braket/circuits/qubit.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 __future__ import annotations
16from typing import Union
18QubitInput = Union["Qubit", int]
21class Qubit(int):
22 """
23 A quantum bit index. The index of this qubit is locally scoped towards the contained
24 circuit. This may not be the exact qubit index on the quantum device.
25 """
27 def __new__(cls, index: int):
28 """
29 Args:
30 index (int): Index of the qubit.
32 Raises:
33 ValueError: If `index` is less than zero.
35 Examples:
36 >>> Qubit(0)
37 >>> Qubit(1)
38 """
39 if index < 0:
40 raise ValueError(f"Supplied index, {index}, cannot be less than zero.")
41 return super().__new__(cls, index)
43 def __repr__(self):
44 return f"Qubit({super().__repr__()})"
46 def __str__(self):
47 return self.__repr__()
49 @staticmethod
50 def new(qubit: QubitInput) -> Qubit:
51 """
52 Helper constructor - if input is a Qubit it returns the same value,
53 else a new Qubit is constructed.
55 Args:
56 qubit (int or Qubit): Qubit index. If type == Qubit then the `qubit` is returned.
57 """
59 if isinstance(qubit, Qubit):
60 return qubit
61 else:
62 return Qubit(qubit)