Source code for qiskit_experiments.database_service.device_component
# This code is part of Qiskit.## (C) Copyright IBM 2021.## This code is licensed under the Apache License, Version 2.0. You may# obtain a copy of this license in the LICENSE.txt file in the root directory# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.## Any modifications or derivative works of this code must retain this# copyright notice, and modified files need to carry a notice indicating# that they have been altered from the originals."""Device component classes."""fromabcimportABC,abstractmethod
[docs]classDeviceComponent(ABC):"""Abstract class representing a device component. Custom components should be subclassed from this class."""@abstractmethoddef__str__(self):passdef__repr__(self):returnf"<{self.__class__.__name__}({str(self)})>"def__eq__(self,value):returnstr(self)==str(value)
[docs]classQubit(DeviceComponent):"""Class representing a qubit device component."""def__init__(self,index:int):self.index=indexdef__str__(self):returnf"Q{self.index}"def__json_encode__(self):return{"index":self.index}
[docs]classResonator(DeviceComponent):"""Class representing a resonator device component."""def__init__(self,index:int):self.index=indexdef__str__(self):returnf"R{self.index}"def__json_encode__(self):return{"index":self.index}
[docs]classUnknownComponent(DeviceComponent):"""Class representing an unknown device component."""def__init__(self,component:str):self.component=componentdef__str__(self):returnself.componentdef__json_encode__(self):return{"component":self.component}
[docs]defto_component(string:str)->DeviceComponent:"""Convert the input string to a :class:`.DeviceComponent` instance. Args: string: String to be converted. Returns: A :class:`.DeviceComponent` instance. Raises: ValueError: If input string is not a valid device component. """ifisinstance(string,DeviceComponent):returnstringifstring.startswith("Q"):returnQubit(int(string[1:]))ifstring.startswith("R"):returnResonator(int(string[1:]))returnUnknownComponent(string)